gen_rust_project: external crate paths wrong under bzlmod + local_path_override; workspace members not detected

未关闭
#4,057 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
58/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
冷清
技术栈
rust

调研方向

从 tools/rust_analyzer/lib.rs 中的 deserialize_file_content 开始,然后检查 tools/rust_analyzer/rust_project.rs 中的 assemble_rust_project。使用 gen_rust_project 运行 bzlmod 和 local_path_override 的复现,并检查生成的 rust-project.json。当外部 crate 的 root_module 路径解析到 execroot 之外,并且本地 override crate 被标记为启用 diagnostics 的 workspace 成员时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

awaiting-response rust-analyzer

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.rs are 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
主要语言
Starlark
星标
857
派生
701
平均合并
4 天 7 小时
30 天内合并 PR
46

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

bazelbuild/rules_rust 的其他 Issue

查看 bazelbuild/rules_rust 的全部 Issue

相似的 Issue

更多 Build System Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。