-
Notifications
You must be signed in to change notification settings - Fork 440
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
Rust library group #1848
Rust library group #1848
Conversation
This PR introduces a rule `rust_crate_group` which groups together multiple dependencies, but does not itself have any sources or provide a crate. In other words, it is an alias for a set of other rust_library targets. For example, the following two build files are equivalent: No `rust_crate_group`: ```py rust_library( name = "foobar", deps = [ ":crate1", ":crate2", ], ... ) ``` With `rust_crate_group`: ```py rust_crate_group( name = "crate_group", deps = [ ":crate1", ":crate2", ], ) rust_library( name = "foobar", deps = [":crate_group"], ... ) ``` In some situations, especially when heavy macros are involved, this is the only to achieve certain things. We have a number of use cases where this is necessary, but they are complicated. I can try to come up with a simple one if you are interested in more motivation. It is possible to create an "alias group" with the C++ and Python rules by just having a `cc_library` or `py_library` with no sources, only deps. But this is not possible with Rust since `rust_library` does not transparently re-export its dependencies.
b8b0982
to
54a1d50
Compare
I'd very much like to see a use case for this rule, especially a use case where it's the only way to achieve certain things. One should always be able to create a list |
Fair enough. Apologies for the delay, just needed to confirm that it was OK to share these examples:
We have some other internal usages and planned usages that are harder to distill down. I might have come on too strong with “only”, as one can likely envision other ways of achieving these examples. But broadly, |
We discovered that this change breaks rust-analyzer support. I'd like to add a fix for that to this review before merging. |
Instead of having a separate rule for this, why not try to match the behavior of rust_proto_library(
name = "a_proto_rust",
protos = [":a_proto"],
)
rust_proto_library(
name = "b_proto_rust",
protos = [":b_proto"],
)
rust_library(
name = "all_protos",
deps = [
":a_proto_rust",
":b_proto_rust",
],
) This seems fairly reasonable, no? |
One additional use case I've found is on #1902 where there's a desire to have patched crates live in the repo but still be "patched". The approach there requires users to expose certain filegroups so a valid BUILD file can be rendered for the patched dependency. I instead think users should be responsible for the BUILD file but I think it'd be unreasonable for them to try and keep dependencies up to date. So instead, my thought was there could be a target that represented the dependencies so they only need to track 1 target. rust_library(
name = "patched_foo",
srcs = glob(["*.rs"]),
deps = ["@crate_index//:foo.deps"],
) The use of a target avoids a greedy load on the rust dependencies. Though in practice this won't matter for |
Personally, I find the behavior of rules_cc problematic because it makes it easy to violate the "include what you use" principle. It means if A -> B -> C, if B removes the dependency on C, this can be a breaking change since A might have a phantom, undeclared dependency on C but still compile. For this reason, we use implementation_deps extensively and have a gazelle plugin that automatically puts dependencies into So I don't like the idea of I chose to make a separate rule because IMHO the situations in which you want to expose dependencies are largely disjoint from the ones in which you want to compile rust code. But this isn't a strongly-held opinion, and I mostly just want a way to do this. |
I'm running into the same situation referenced in #1848 (comment) I would love to have this renamed to @william-smith-skydio what do you think of the rename? |
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 looks great to me! Can you update the PR title and comments?
I'll give other maintainers a chance to jump in but given this has been open since February and I feel open questions have been answered, I'm suspecting folks are accepting of this change 😄
Thank you for opening the PR!
@UebelAndre updated. Thanks for the review! |
* Add rust_crate_group rule for grouping dependencies together. This PR introduces a rule `rust_crate_group` which groups together multiple dependencies, but does not itself have any sources or provide a crate. In other words, it is an alias for a set of other rust_library targets. For example, the following two build files are equivalent: No `rust_crate_group`: ```py rust_library( name = "foobar", deps = [ ":crate1", ":crate2", ], ... ) ``` With `rust_crate_group`: ```py rust_crate_group( name = "crate_group", deps = [ ":crate1", ":crate2", ], ) rust_library( name = "foobar", deps = [":crate_group"], ... ) ``` In some situations, especially when heavy macros are involved, this is the only to achieve certain things. We have a number of use cases where this is necessary, but they are complicated. I can try to come up with a simple one if you are interested in more motivation. It is possible to create an "alias group" with the C++ and Python rules by just having a `cc_library` or `py_library` with no sources, only deps. But this is not possible with Rust since `rust_library` does not transparently re-export its dependencies. * add rust_crate_group test * add runfiles to rust_crate_group * add rust-analyzer support * rename rust_crate_group -> rust_library_group * add test verifying that rust_library_group can be consumed by rust_test * add coverage support to rust_library_group --------- Co-authored-by: UebelAndre <[email protected]>
* Add rust_crate_group rule for grouping dependencies together. This PR introduces a rule `rust_crate_group` which groups together multiple dependencies, but does not itself have any sources or provide a crate. In other words, it is an alias for a set of other rust_library targets. For example, the following two build files are equivalent: No `rust_crate_group`: ```py rust_library( name = "foobar", deps = [ ":crate1", ":crate2", ], ... ) ``` With `rust_crate_group`: ```py rust_crate_group( name = "crate_group", deps = [ ":crate1", ":crate2", ], ) rust_library( name = "foobar", deps = [":crate_group"], ... ) ``` In some situations, especially when heavy macros are involved, this is the only to achieve certain things. We have a number of use cases where this is necessary, but they are complicated. I can try to come up with a simple one if you are interested in more motivation. It is possible to create an "alias group" with the C++ and Python rules by just having a `cc_library` or `py_library` with no sources, only deps. But this is not possible with Rust since `rust_library` does not transparently re-export its dependencies. * add rust_crate_group test * add runfiles to rust_crate_group * add rust-analyzer support * rename rust_crate_group -> rust_library_group * add test verifying that rust_library_group can be consumed by rust_test * add coverage support to rust_library_group --------- Co-authored-by: UebelAndre <[email protected]>
This PR introduces a rule
rust_library_group
which groups together multiple dependencies, but does not itself have any sources or provide a crate. In other words, it is an alias for a set of other rust_library targets. For example, the following two build files are equivalent:No
rust_library_group
:With
rust_library_group
:In some situations, especially when heavy macros are involved, this is the only to achieve certain things. We have a number of use cases where this is necessary, but they are complicated. I can try to come up with a simple one if you are interested in more motivation.
It is possible to create an "alias group" with the C++ and Python rules by just having a
cc_library
orpy_library
with no sources, only deps. But this is not possible with Rust sincerust_library
does not transparently re-export its dependencies.