gen_rust_project: external crate paths wrong under bzlmod + local_path_override; workspace members not detected
Maintainer thường phản hồi trong vòng 3 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 58/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Ít trao đổi
- Công nghệ
- rust
- Lĩnh vực
- build-system, tooling
Hướng nghiên cứu
Bắt đầu trong tools/rust_analyzer/lib.rs tại deserialize_file_content, sau đó kiểm tra assemble_rust_project trong tools/rust_analyzer/rust_project.rs. Chạy bản tái hiện bzlmod và local_path_override với gen_rust_project, rồi kiểm tra rust-project.json được tạo. Hoàn tất khi các đường dẫn root_module của crate bên ngoài được phân giải bên ngoài execroot và các crate override cục bộ được đánh dấu là thành viên workspace với diagnostics được bật.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Description
Two bugs in gen_rust_project that together make rust-analyzer non-functional when using bzlmod with local_path_override.
Bug 1: External crate paths point into the ephemeral execroot
gen_rust_project generates root_module paths like:
{output_base}/execroot/_main/external/rules_rust+crate+anyhow-1.0.0/src/lib.rs
These paths do not exist after the build completes. The execroot/ directory is a build-time sandbox; its external/ symlinks are only valid during an action. The real cached sources live at:
{output_base}/external/rules_rust+crate+anyhow-1.0.0/src/lib.rs
Because rust-analyzer cannot read these paths, it has no type information for any external crate — no completions, no go-to-definition, no inline documentation for dependencies like clap, anyhow, serde, etc.
The root cause is in tools/rust_analyzer/lib.rs, deserialize_file_content: __EXEC_ROOT__ is replaced with execution_root ({output_base}/execroot/_main), which is correct for bazel-out/ paths but wrong for external/ paths.
Bug 2: local_path_override modules not detected as workspace members
In bzlmod, a module declared with local_path_override has its sources in the real workspace tree, accessed via a symlink at {output_base}/external/<module>+/. The aspect marks these crates is_workspace_member: false because the build system has no way to communicate this. rust-analyzer therefore shows no diagnostics (errors, warnings) for these crates.
After Bug 1 is fixed, the root_module path for such crates ({output_base}/external/<module>+/src/main.rs) resolves through the symlink back into the workspace tree. The fix is to canonicalize the path and check if it falls under workspace.
Reproduction steps
Minimal bzlmod workspace with a local_path_override module:
# MODULE.bazel (root)
bazel_dep(name = "rules_rust", version = "0.70.0")
bazel_dep(name = "mypkg", version = "0.1.0")
local_path_override(module_name = "mypkg", path = "mypkg")
# mypkg/MODULE.bazel
module(name = "mypkg", version = "0.1.0")
bazel_dep(name = "rules_rust", version = "0.70.0")
crate = use_extension("@rules_rust//crate_universe:extension.bzl", "crate")
crate.from_cargo(name = "crates", manifests = ["//tools/mybin:Cargo.toml"])
use_repo(crate, "crates")
# mypkg/BUILD.bazel
rust_binary(name = "mybin", srcs = ["src/main.rs"], deps = ["@crates//:anyhow"])
// mypkg/src/main.rs
fn main() { let _e = anyhow::anyhow!("test"); }
Run gen_rust_project and open src/main.rs in any editor with rust-analyzer:
anyhow::produces no completions (Bug 1)- Errors in
src/main.rsare not shown (Bug 2)
Proposed fixes:
tools/rust_analyzer/lib.rs:
let content = fs::read_to_string(path)?
...
.replace("__EXEC_ROOT__", execution_root.as_str())
.replace("__OUTPUT_BASE__", output_base.as_str())
// External crate sources live at output_base/external/, not inside execroot.
// execroot symlinks are ephemeral (only valid during a build action).
.replace(
&format!("{}/external/", execution_root),
&format!("{}/external/", output_base),
);
tools/rust_analyzer/rust_project.rs, inside assemble_rust_project:
// Crates from local_path_override modules resolve through an external/ symlink
// back into the real workspace tree. Detect this so rust-analyzer shows diagnostics.
let is_workspace_member = c.is_workspace_member
|| std::path::Path::new(&c.root_module)
.canonicalize()
.map(|p| p.starts_with(workspace.as_std_path()))
.unwrap_or(false);
Additional context
Workaround (used in our project): post-process the generated rust-project.json with a Python script that replaces execroot/_main/external/ with external/ in all paths and sets is_workspace_member: true for crates whose root_module resolves to a path under the workspace.
Bug 1 likely affects all bzlmod projects, not just those using local_path_override, since execroot/external/ symlinks are never stable across builds.
Impact
rust-analyzer is effectively broken for any Rust code in a bzlmod local_path_override module. Workaround exists (post-processing script) but it is fragile. Does not block updating to a newer rules_rust version, but makes IDE support unusable without the workaround.
Bazel and rules_rust version
- Bazel: 9.x (bzlmod)
- rules_rust: 0.70.0
- Ngôn ngữ chính
- Starlark
- Star
- 857
- Fork
- 701
- Merge trung bình
- 4 ngày 7 giờ
- Pull request đã merge (30 ngày)
- 46
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của bazelbuild/rules_rust
-
`rust_test(crate = ...)` does not stage the crate's transitive dynamic libraries into runfilesCó thể đã có người làm @Wito-1 đã nhận 65 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
bazelbuild/rules_rust#4309 ·
Maintainer thường phản hồi trong vòng 3 ngày
-
Windows (no-symlink path): drain_runfiles_dir_windows deletes retained runfiles — inverted filename_suffixes_to_retain conditionCó thể đã có người làm @Mattbusel đã nhận 17 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
bazelbuild/rules_rust#4266 ·
Maintainer thường phản hồi trong vòng 3 ngày
-
rust.toolchain(strip_level = ...) is silently ignoredCó thể đã có người làm @Mattbusel đã nhận 17 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
bazelbuild/rules_rust#4223 ·
Maintainer thường phản hồi trong vòng 3 ngày
-
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 90/100
bazelbuild/rules_rust#4152 · 1 reaction ·
Maintainer thường phản hồi trong vòng 3 ngày
-
awaiting-response
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
bazelbuild/rules_rust#3599 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 3 ngày
Tất cả issue của bazelbuild/rules_rust
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
bazelbuild/apple_support#707 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
-
blocky: new version 0.38.0Đang mởout-of-date
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
CachyOS/CachyOS-PKGBUILDS#2232 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
blast/large refactor risk/low spend-dem
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
chriskelly/MusicWall#87 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
neilotoole/sq#1503 ·
Maintainer thường phản hồi trong vòng 1 ngày