-
Notifications
You must be signed in to change notification settings - Fork 399
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
Add gen_binaries annotation to control which bins to make target for #1718
Conversation
204057f
to
fd1b5d1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a very reasonable option to add, but I'm not sure about the default... Could you describe the particular problems these targets existing is causing you?
I chose the default to be the setting that I would want to use. We also don't generate targets for an imported library's In regard to crates that are intended to be binaries (my I think if |
I think this is pretty compelling - the fact that it's hit-or-miss whether we generate bin targets based on arbitrary properties of the crate you're depending on means opting in to trying to make something work is probably reasonable (and hopefully means people can look at some documentation when they try to enable this, to see some caveats we may need to publish).
I guess my intuition here leans towards "lots of crates expose a useful binary I may want to run, and there are some which are testonly which I can ignore", whereas yours leans towards "most binary targets are testonly, and create noise" - I don't think it super matters which way we lean, though, either default I think is reasonable. That said, /cc @UebelAndre in case they have a strong argument either way :) if not, I'll merge this as-is.
FWIW I think the way we'd end up supporting this is via Cargo's artifact dependencies rather than via a custom resolver, but who knows what the future will hold! |
Neat, I hadn't seen that feature. You're right it seems very relevant. My expectations from skimming the following docs would be:
[dependencies]
# Export rust_binary for all bins, do not export the rust_library from BUILD.bazel.
# Presumably a rust_library still needs to be generated in BUILD.example-1.0.0.bazel
# because the binary depends on it.
example = { version = "1", artifact = "bin" }
# Export rust_binary for those specific bins, do not export rust_library.
example = { version = "1", artifact = ["bin:faketty", "bin:cmake"] }
# Export rust_library as well as rust_binary.
example = { version = "1", artifact = "bin", lib = true }
# Same as this PR: export rust_library, do not generate rust_binary.
example = { version = "1" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I think this seems fine. I had one request though 😄
@@ -114,6 +115,8 @@ def _annotation( | |||
data (list, optional): A list of labels to add to a crate's `rust_library::data` attribute. | |||
data_glob (list, optional): A list of glob patterns to add to a crate's `rust_library::data` attribute. | |||
deps (list, optional): A list of labels to add to a crate's `rust_library::deps` attribute. | |||
gen_binaries (list or bool, optional): As a list, the subset of the crate's bins that should get `rust_binary` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also make this configurable globally like gen_build_script
is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also add a note about this not affecting binary only crates? Maybe a section could be added to the top level crate_universe
docs in //crate_universe:docs.bzl
? Not sure where the best place is for this info but I can see that being a point of confusion for folks (already was but I want to try and avoid a bug report being incorrectly filed for this new feature)
f7a1b9a
to
9610d47
Compare
|
@@ -114,6 +115,8 @@ def _annotation( | |||
data (list, optional): A list of labels to add to a crate's `rust_library::data` attribute. | |||
data_glob (list, optional): A list of glob patterns to add to a crate's `rust_library::data` attribute. | |||
deps (list, optional): A list of labels to add to a crate's `rust_library::deps` attribute. | |||
gen_binaries (list or bool, optional): As a list, the subset of the crate's bins that should get `rust_binary` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also add a note about this not affecting binary only crates? Maybe a section could be added to the top level crate_universe
docs in //crate_universe:docs.bzl
? Not sure where the best place is for this info but I can see that being a point of confusion for folks (already was but I want to try and avoid a bug report being incorrectly filed for this new feature)
Added a documentation section on depending on binaries: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! Looks good to me 😄
I'll leave the rest up to @illicitonion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Example: crates_vendor( name = "vendor", annotations = { "aaa": [crate.annotation( # Do not generate `rust_binary` for any of the package's bins. # This is the new default behavior. gen_binaries = False, )], "bbb": [crate.annotation( # Generate `rust_binary` for all bins. This is the previous # default behavior in rules_rust 0.15.0. gen_binaries = True, )], "ccc": [crate.annotation( # Generate `rust_binary` for a specific subset of the bins. gen_binaries = ["gcc-shim"], )], }, cargo_lockfile = ":Cargo.lock", manifests = [":Cargo.toml"], mode = "remote", tags = ["manual"], vendor_path = "bazel", )
|
In Cargo, when you depend on a library, it does not make Cargo build all the bin crates that might be included in that same package; your crate only depends on the other package's lib crate. This means packages in the Cargo ecosystem, even quite foundational ones like
cc
andclap
, mindlessly include various small bins that are useful for their test suite or useful for local development.Some examples:
cc__gcc-shim
https://github.com/rust-lang/cc-rs/blob/1.0.77/src/bin/gcc-shim.rsclap__stdio-fixture
https://github.com/clap-rs/clap/blob/v4.0.29/src/bin/stdio-fixture.rsphf_generator__gen_hash_test
https://github.com/rust-phf/rust-phf/blob/v0.11.0/phf_generator/src/bin/gen_hash_test.rsTaking into account the state of the ecosystem, I don't think it makes sense for rules_rust's default behavior to expose all these, since they were never meant to be accessed downstream.
This PR changes generation of
rust_binary
targets incrates_vendor
to opt-in by default. You can setgen_binaries = True
for a specific package to generate Bazel targets for all of that package's bins, orgen_binaries = ["array", "of", "names"]
to select specific bins you need a target for.The naming of the new annotation mirrors the existing
gen_build_script
annotation.Example