rust_analyzer: rust-project.json consolidation merges distinct crates with the same crate_id
维护者通常 3 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 52/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 冷清
- 技术栈
- rust
- 领域
- build-system, tooling
调研方向
阅读 tools/rust_analyzer/aquery.rs,重点关注 consolidate_crate_specs 以及 CrateSpec.crate_id 如何被用作合并键。使用提供的 MODULE.bazel、BUILD.bazel 和 main.rs 重现该问题,然后运行 Bazel rust-project generator 并检查生成的条目。当独立的 //:app 和 //:app_test 仍保持不同,同时现有的 rust_test(crate = ":lib") 合并仍能正常工作时,即视为完成。
由索引模型根据 Issue 内容生成。
描述
This issue was created with AI assistance and has been human reviewed for accuracy before filing.
Description
rust-project.json generation can merge distinct Rust crates that happen to share the same crate_id.
The current consolidation logic in tools/rust_analyzer/aquery.rs::consolidate_crate_specs uses only CrateSpec.crate_id as the map key. crate_id is derived from the crate root module path, so sharing it is intentional for some cases, such as rust_test(crate = ":lib"). However, the same crate_id can also occur for crates that should remain distinct.
Current wrong behavior:
A standalone rust_binary and rust_test using the same root file are consolidated into one rust-analyzer crate. The resulting crate can contain mixed metadata from both targets. In the repro below, the generated crate has display_name = "app", but env.KIND = "test", env.CARGO_CRATE_NAME = "app_test", and build.label = "//:app_test".
Expected behavior:
rust-project.json should only merge crate specs when they represent the same logical rust-analyzer crate, such as the existing rust_test(crate = ":lib") case. Distinct same-root standalone binary/test crates should remain separate entries.
Reproduction steps
Create a minimal workspace using upstream, unpatched rules_rust.
MODULE.bazel:
module(name = "ra_crate_id_repro")
bazel_dep(name = "rules_rust", version = "0.71.3")
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(edition = "2021")
use_repo(rust, "rust_toolchains")
register_toolchains("@rust_toolchains//:all")
BUILD.bazel:
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_test")
rust_binary(
name = "app",
srcs = ["main.rs"],
rustc_env = {"KIND": "binary"},
)
rust_test(
name = "app_test",
srcs = ["main.rs"],
rustc_env = {"KIND": "test"},
)
main.rs:
pub fn value() -> u8 {
1
}
fn main() {
let _ = value();
}
#[cfg(test)]
mod tests {
#[test]
fn value_is_one() {
assert_eq!(super::value(), 1);
}
}
First verify the targets themselves build/test successfully:
bazel test //:app_test
bazel build //:app
Generate rust-project.json:
bazel run @rules_rust//tools/rust_analyzer:gen_rust_project -- //:app //:app_test
Inspect the crates generated for main.rs:
jq '[.crates[] | select(.root_module | endswith("/main.rs"))] | {matching_crate_count: length, crates: [.[] | {display_name, env_KIND: .env.KIND, cargo_crate_name: .env.CARGO_CRATE_NAME, build_label: .build.label, build_target_kind: .build.target_kind}]}' rust-project.json
Actual output from the repro:
{
"matching_crate_count": 1,
"crates": [
{
"display_name": "app",
"env_KIND": "test",
"cargo_crate_name": "app_test",
"build_label": "//:app_test",
"build_target_kind": "bin"
}
]
}
Expected output:
There should be two distinct crate entries for main.rs: one for //:app with KIND = "binary", and one for //:app_test with KIND = "test".
Additional context
This appears to be caused by consolidate_crate_specs using spec.crate_id alone as the identity key for consolidation.
The existing merge behavior is useful for rust_test(crate = ":lib"), where the test and library share a root module and should be merged so rust-analyzer test code lenses work. The problem is that same crate_id does not always mean same logical rust-analyzer crate.
The same identity issue can also affect target-specific crate variants when the same crate root appears under multiple target triples/configurations. A fix likely needs to distinguish intentional library/test consolidation from unrelated same-root crates and avoid collapsing target-specific variants into one crate.
Related but distinct issues:
- #1589 discusses consolidation creating dependency cycles.
- #3126 discusses generated-source consolidation behavior.
Impact
This can make rust-analyzer use incorrect crate metadata. In practice this can cause wrong runnables/code lenses, wrong build labels, wrong environment variables, wrong dependency/alias metadata, or incorrect target-specific analysis.
The workaround is to carry a local patch that qualifies crate IDs before consolidation for distinct same-root binary/test crates and for non-proc-macro crates appearing under multiple target triples.
Bazel and rules_rust version
Reproduced with:
- Bazel:
9.1.1 - rules_rust:
0.71.3from Bzlmod - Also reproduced against upstream
bazelbuild/rules_rustmainat46d67bc1fda4592dab7465c8152d94ae6f969cfc
- 主要语言
- Starlark
- 星标
- 857
- 派生
- 701
- 平均合并
- 4 天 7 小时
- 30 天内合并 PR
- 46
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
bazelbuild/rules_rust 的其他 Issue
-
`rust_test(crate = ...)` does not stage the crate's transitive dynamic libraries into runfiles可能已有人在做 @Wito-1 于 66 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 88/100
bazelbuild/rules_rust#4309 ·
维护者通常 3 天内回复
-
Windows (no-symlink path): drain_runfiles_dir_windows deletes retained runfiles — inverted filename_suffixes_to_retain condition可能已有人在做 @Mattbusel 于 17 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 82/100
bazelbuild/rules_rust#4266 ·
维护者通常 3 天内回复
-
rust.toolchain(strip_level = ...) is silently ignored可能已有人在做 @Mattbusel 于 17 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 82/100
bazelbuild/rules_rust#4223 ·
维护者通常 3 天内回复
-
难度 1/5 1-3 小时 新手友好度 90/100
bazelbuild/rules_rust#4152 · 1 个 reaction ·
维护者通常 3 天内回复
-
awaiting-response
难度 2/5 1-3 小时 新手友好度 76/100
bazelbuild/rules_rust#3599 · 1 条评论 ·
维护者通常 3 天内回复
查看 bazelbuild/rules_rust 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 65/100
bazelbuild/apple_support#707 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 92/100
-
out-of-date
难度 2/5 1-3 小时 新手友好度 68/100
CachyOS/CachyOS-PKGBUILDS#2232 ·
维护者通常 1 天内回复
-
blast/large refactor risk/low spend-dem
难度 2/5 1-3 小时 新手友好度 68/100
chriskelly/MusicWall#87 · 2 条评论 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 78/100
neilotoole/sq#1503 ·
维护者通常 1 天内回复