Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go.library_to_source should only accept Targets for deps #1784

Closed
jayconrod opened this issue Oct 23, 2018 · 5 comments · Fixed by #2665
Closed

go.library_to_source should only accept Targets for deps #1784

jayconrod opened this issue Oct 23, 2018 · 5 comments · Fixed by #2665

Comments

@jayconrod
Copy link
Contributor

go.library_to_source accepts an attr parameter, which is usually but not always ctx.attr. When retrieving the list of dependencies, we call get_archive on each element of attr["deps"], which is normally a list of Targets.

Callers that generate packages (go_test, nogo) call library_to_source with a struct instead of ctx.attr. These callers pass in a list of GoArchives instead of a list of Targets for attr["deps"]. This makes it hard to do anything with attr["deps"] not related to archives.

The documentation says attr is just a set of attributes. We should clarify what to put here for clients that are generating packages.

@hugelgupf
Copy link
Contributor

I just happened upon this issue - another case of what if one rule generates more than one library/archive?

In particular, our rule generates several libraries that a binary then depends on. I saw this comment in the code base before https://github.com/bazelbuild/rules_go/blob/3b5f0c950e0deec63cd77cdcec7765b5f41fefbd/go/private/context.bzl#L199-L200
and it made me decide to have a macro that generates several rules, so that the binary can depend on the generated library targets. (Looks like this at the moment.)

Unfortunately, that means that users cannot specify target patterns in the cmds list and every cmd has to be listed individually (and with an absolute label, but that's my fault).

Because of this, I'd like it if library_to_source's attr["deps"] could officially accept a list of Targets and GoArchives (I didn't want to depend on that behavior today just to have to migrate away some day). Or do you have an alternative that would solve this issue for me?

@jayconrod
Copy link
Contributor Author

@hugelgupf

Seems reasonable to change to drop that TODO and change the documentation to reflact that library_to_source takes Targets or GoArchives. I think I just wanted the API to have stricter types, but the reality is that GoArchives are needed in a couple cases, and that probably isn't going away soon.

In particular, our rule generates several libraries that a binary then depends on.

Just to double check, does the rule generate multiple packages that could be imported by the binary, or one importable package with others that need to be passed to the linker? If it's the first case, you would just need one GoArchive or Target. Sounds like the second case though.

Unfortunately, that means that users cannot specify target patterns in the cmds list and every cmd has to be listed individually (and with an absolute label, but that's my fault).

Hopefully you won't need this, but you can set tags = ["manual"] on these targets. That will prevent them from being matched by ... wildcards.

@hugelgupf
Copy link
Contributor

Just to double check, does the rule generate multiple packages that could be imported by the binary, or one importable package with others that need to be passed to the linker?

It generates both multiple packages, and the binary depending on all of them. Hm. How can I put many different libraries into on GoArchive/Target? Could split the process into 2 steps (n packages rewritten in one rule + binary depending on all those packages), rather than n+1; that'd keep it a Target.

@jayconrod
Copy link
Contributor Author

How can I put many different libraries into on GoArchive/Target?

You can't. I was just thinking that if you only had one importable package, no change would be needed.

I think this ought to be possible though. I'll add a note to #2578. If there's a substantial change to the API in the future, that ought to be included.

For the short term, documenting that in go.library_to_source, attr["deps"] could contain Targets or GoArchives seems like the best way to resolve this.

@hugelgupf
Copy link
Contributor

Got it, SGTM. Thank you!

jayconrod pushed a commit to jayconrod/rules_go that referenced this issue Sep 30, 2020
attr["deps"] may be a list of either Targets or GoArchive.

Fixes bazel-contrib#1784
jayconrod pushed a commit that referenced this issue Sep 30, 2020
attr["deps"] may be a list of either Targets or GoArchive.

Fixes #1784
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants