crate_universe: local_crate_mirror does not accept relative paths despite docs implying it might
Maintainers usually reply within 3 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 68/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Quiet
- Tech stack
- rust
- Domain
- build-system
Research direction
Start with crate_universe/private/local_crate_mirror.bzl and compare its path handling with the workspace-relative path emitted by crate_universe/src/metadata/metadata_annotation.rs:284–310. Reproduce the failure with the Cargo patch and bazel run //third_party/rust:update-repos; done means the generated local_crate_mirror can consume the relative path without the external-repository readdir error.
Written by the indexing model from the issue text.
Description
Summary
crate_universe's update-repos (cargo-bazel renderer) emits local_crate_mirror(path = "<workspace-relative path>") calls when consumers use a standard Cargo [patch.crates-io] entry with a relative path. But the local_crate_mirror repository rule cannot consume the path it was given: repository_ctx.path(string) resolves relative paths against the external repo's own directory, not the workspace root, so the readdir() call inside the rule fails immediately.
So the bug is entirely internal to rules_rust — the generator produces a value the consumer can't use.
Reproducer
In a Bazel workspace, vendor a crate hand-tweak it, then patch it into Cargo:
# Cargo.toml
[patch.crates-io]
mycrate = { path = "third_party/rust/vendor/mycrate-1.0.0" }
bazel run //third_party/rust:update-repos
cargo-bazel renders this into third_party/rust/crates/defs.bzl:
local_crate_mirror(
name = "crate_index__mycrate-1.0.0",
options_json = "{...,\"local_path\":\"third_party/rust/vendor/mycrate-1.0.0\",...}",
path = "third_party/rust/vendor/mycrate-1.0.0",
)
crate_universe/src/metadata/metadata_annotation.rs:284–310 explicitly produces this workspace-relative form, and the in-code comment says:
// This replacement allows in-repo patches sections to work as intended using local_crate_mirror.
So upstream's intent is clearly to support relative [patch.crates-io] paths. But crate_universe/private/local_crate_mirror.bzl:9:
def _local_crate_mirror_impl(repository_ctx):
path = repository_ctx.path(repository_ctx.attr.path)
...
for child in repository_ctx.path(path).readdir():
...
repository_ctx.path(relative_str) resolves under the external repo's own directory. The external repo @@_main~rust_crates_extension~crate_index__mycrate-1.0.0 doesn't contain third_party/rust/vendor/mycrate-1.0.0, so:
Error in readdir: can't readdir(), not a directory:
.../external/_main~rust_crates_extension~crate_index__mycrate-1.0.0/third_party/rust/vendor/mycrate-1.0.0
The path attribute docstring even has a TODO flagging this:
"path": attr.string(
# TODO: Verify what happens if this is not an absolute path.
doc = "Absolute path to the BUILD.bazel file to generate.",
),
The docstring says "absolute" but the generator emits relative — that contradiction is the bug.
Proposed fix
Make local_crate_mirror resolve path relative to the workspace root when relative:
def _local_crate_mirror_impl(repository_ctx):
path = repository_ctx.workspace_root.get_child(repository_ctx.attr.path)
...
repository_ctx.workspace_root is well-defined and always points at the main workspace root from inside an external repo's fetch.
Either that, or update the renderer in metadata/metadata_annotation.rs to emit an absolute path (and update the path attr docs/checks accordingly). The first option seems cleaner because Cargo [patch.crates-io] paths are always relative-to-the-manifest by Cargo's design.
We carry the first option as a local patch against 0.70.0. Happy to send a PR if maintainers prefer.
Versions
- rules_rust: 0.70.0
- bazel: 7.7.1
- Dominant language
- Starlark
- Stars
- 857
- Forks
- 701
- Avg merge
- 4d 7h
- Merged PRs (30d)
- 46
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from bazelbuild/rules_rust
-
`rust_test(crate = ...)` does not stage the crate's transitive dynamic libraries into runfilesPossibly taken @Wito-1 claimed this 65 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
bazelbuild/rules_rust#4309 ·
Maintainers usually reply within 3 days
-
Windows (no-symlink path): drain_runfiles_dir_windows deletes retained runfiles — inverted filename_suffixes_to_retain conditionPossibly taken @Mattbusel claimed this 17 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
bazelbuild/rules_rust#4266 ·
Maintainers usually reply within 3 days
-
rust.toolchain(strip_level = ...) is silently ignoredPossibly taken @Mattbusel claimed this 17 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
bazelbuild/rules_rust#4223 ·
Maintainers usually reply within 3 days
-
Difficulty 1/5 1-3 hours Newbie friendliness 90/100
bazelbuild/rules_rust#4152 · 1 reaction ·
Maintainers usually reply within 3 days
-
awaiting-response
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
bazelbuild/rules_rust#3599 · 1 comment ·
Maintainers usually reply within 3 days
All issues in bazelbuild/rules_rust
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
bazelbuild/apple_support#707 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
out-of-date
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
CachyOS/CachyOS-PKGBUILDS#2232 ·
Maintainers usually reply within 1 day
-
blast/large refactor risk/low spend-dem
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
chriskelly/MusicWall#87 · 2 comments ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
neilotoole/sq#1503 ·
Maintainers usually reply within 1 day