Add root_path attribute for directory crate roots#4142
Open
krasimirgg wants to merge 5 commits into
Open
Conversation
Allows specifying `root_path` when `root` (`crate_root` or `srcs` entrypoint) is a directory artifact (`TreeArtifact`). This enables rules_rust targets (`rust_library`, `rust_binary`, `rust_test`, etc.) to consume directory artifacts produced by upstream rules and correctly construct the path to the crate root (`lib.rs` / `main.rs`). * Adds `root_path` attribute to `_COMMON_ATTRS` and `CrateInfo` provider. * Updates `rustc_compile_action` argument construction (`rustc.bzl`) to join `crate_info.root.path` with `root_path` when `crate_info.root.is_directory` is true. * Adds a unit and build test case under `test/root_path/` using a custom rule that packages its `srcs` as a directory artifact (`TreeArtifact`) and compiles a `rust_library` and `rust_test` from it.
…_rust into jul26-root-path
krasimirgg
marked this pull request as ready for review
July 9, 2026 12:49
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Allows specifying
root_pathwhen the crate root (srcsentrypoint) is a directory artifact (TreeArtifact). This enables rules_rust targets (rust_library,rust_binary,rust_test, etc.) to consume directory artifacts produced by upstream rules and correctly construct the path to the crate root (lib.rs/main.rs).We use something like this internally to build standard libraries from sources distributed as .zip source archives: a custom rule extracts the contents into a generated directory artifact, which then the rust rules can consume as sources.
This may be helpful to achieve the request from #3757, where the user is trying to consume a directory of generated sources.
root_pathattribute to_COMMON_ATTRSandCrateInfoprovider.rustc_compile_actionargument construction (rustc.bzl) to joincrate_info.root.pathwithroot_pathwhencrate_info.root.is_directoryis true.test/root_path/using a custom rule that packages itssrcsas a directory artifact (TreeArtifact) and compiles arust_libraryandrust_testfrom it.I have taken special care in the rustc argument construction to ensure it is compatible with the new bazel path remapping guidelines.
While here, added a few checks in the rules for combinations of
srcs,crateandcrate_rootattributes that don't make sense and would cause compilation to fail later anyways.