rust-analyzer sysroot enrichment fails: no cargo in rust_analyzer_toolchain
メンテナーはふだん 3 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 45/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 静か
- 技術スタック
- rust
調査の方向性
bazel run @rules_rust//tools/rust_analyzer:setup -- helix で生成された統合から始め、discover_bazel_rust_project、rust-project.json、および参照されている sysroot を調査します。rust-analyzer analysis-stats と、提供されている src/lib.rs の例を使って再現します。生成された統合で cargo/sysroot の拡充が失敗しなくなり、標準ライブラリのジェネリックコンテナに対して hover と go-to-definition が機能すれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Description
When using rules_rust's rust-analyzer integration (rust_analyzer_toolchain +
the rust-project.json produced by discover_bazel_rust_project), hover and
go-to-definition are broken for any local variable/expression whose type is a
generic container from std/alloc (e.g. Option<T>, Arc<T>, RefCell<T>)
instantiated with a workspace-local T.
- Hovering such a variable shows
{unknown}instead of the real type. - Go-to-definition into the corresponding
std/coresource (e.g. jumping
intoRefCell::new) silently does nothing — no navigation, no error.
Plain, non-generic types (local newtypes, etc.) and types coming straight from
an already-fully-inferred function return (e.g. anyhow::Result<...>) are
unaffected — only generic std/alloc containers wrapping a workspace-local type
are impacted.
Expected behavior: hover and go-to-definition should work the same way they do
for identical code loaded via a normal cargo-based project. We verified this
directly — the same source file, same rust-analyzer version, loaded via a
plain Cargo project instead of the Bazel-generated rust-project.json, and
both hover and go-to-definition work correctly there.
Reproduction steps
This reproduces in a minimal, standalone workspace — no other configuration
involved:
-
Create a workspace with just these three files:
MODULE.bazel:module(name = "rules_rust_ra_repro") bazel_dep(name = "rules_rust", version = "0.73.0") rust = use_extension("@rules_rust//rust:extensions.bzl", "rust") rust.toolchain( edition = "2024", versions = ["1.95.0"], ) use_repo(rust, "rust_toolchains") register_toolchains("@rust_toolchains//:all")BUILD.bazel:load("@rules_rust//rust:defs.bzl", "rust_library") rust_library( name = "repro", srcs = ["src/lib.rs"], edition = "2024", )src/lib.rs:use std::cell::RefCell; pub struct Foo; pub fn example() -> Option<Foo> { let y: Option<Foo> = None; let _z = RefCell::new(Foo); y } -
bazel build //:repro— builds cleanly. -
bazel run @rules_rust//tools/rust_analyzer:setup -- helix— generates the
rust-analyzer integration and arust-project.json-producing discover
binary under.helix/.rules_rust_analyzer/. -
Inspect the sysroot directory referenced by the generated
rust-project.json'ssysrootfield
(<output_base>/external/rules_rust++rust+rust_analyzer_1.95.0_tools):
it containsrust-analyzer,rustc,rustdoc,rust-gdb,rust-gdbgui,
rust-lldb— nocargobinary. -
Run the generated
rust-project.jsonthrough rust-analyzer directly
(no editor needed):<sysroot>/bin/rust-analyzer analysis-stats <dir containing rust-project.json>This logs:
WARN `cargo metadata` failed and returning succeeded result with `--no-deps` error=`cargo metadata` exited with an error: error: unknown `-Z` flag specified: lockfile-path -
Opening the same project in an editor via the generated
workspace.discoverConfigintegration (we used Helix 25.07.1) shows the
downstream symptom onsrc/lib.rs: hoveringy(typeOption<Foo>)
displays{unknown}instead of the real type, and go-to-definition on
RefCelldoes nothing. Earlier in the session, rust-analyzer also logs:WARN Workspace `<crate path>` has sysroot errors: sysroot at `<sysroot>/lib/rustlib/src/library` is missing a `core` library, try running `rustup component add rust-src` to possibly fix thiseven though that sysroot directory contains a structurally valid
core
crate (Cargo.toml+src/lib.rspresent).
Additional context
- The
-Zlockfile-pathfailure is not simply a missing-RUSTC_BOOTSTRAP/
nightly-channel issue: even withRUSTC_BOOTSTRAP=1forced, thecargo
that ends up being invoked for this call reportsunknown -Z flag specified: lockfile-pathoutright — the flag isn't recognized by that
cargo build at all. - The
rust_analyzer_toolchain-provided sysroot directory (the one referenced
by thesysroot/sysroot_srcfields of the generatedrust-project.json)
containsrust-analyzer,rustc,rustdoc,rust-gdb,rust-gdbgui,
rust-lldb— but nocargobinary. - Reproduces identically on rules_rust 0.71.3 and 0.73.0.
- Editor used for testing: Helix 25.07.1, via the standard
workspace.discoverConfigintegration generated by
bazel run @rules_rust//tools/rust_analyzer:setup. - Confirmed as a regression relative to a plain Cargo-based project setup:
identical source and rust-analyzer version, loaded without going through
rules_rust's discovery/rust-project.jsongeneration, does not exhibit
either symptom.
Impact
We are not aware of a workaround that stays purely within the Bazel/
rules_rust toolchain — avoiding the issue currently seems to require making
a cargo binary available somewhere rust-analyzer will pick it up from. This
does not block using rules_rust/rust_analyzer_toolchain outright, but it
meaningfully degrades the IDE experience for any code using standard generic
containers over local types — a very common pattern — for any team that
doesn't otherwise have a separate Cargo/rustup toolchain installed.
Workaround
rust-analyzer resolves the cargo for this call purely via PATH — we
confirmed it does not look for one relative to the sysroot it was given
(pointing sysroot at a directory that does contain a matching cargo had
no effect). So the only workaround we found is to make a cargo available on
PATH for the rust-analyzer process itself, matched to the same version as
the rust_analyzer_toolchain's rustc/rust-analyzer (a mismatched, e.g.
system/rustup-provided, cargo either rejects the -Z flag outright or
requires nightly). Bazel does build such a matching cargo as part of the
regular (non-rust-analyzer) rust_toolchain, so we pointed rust-analyzer at
that one via a small wrapper script that Helix's command setting launches
instead of the rust-analyzer binary directly:
#!/bin/sh
output_base=$(bazel info output_base 2>/dev/null)
cargo_dir="$output_base/external/<rust_toolchain_repo>/bin"
export PATH="$cargo_dir:$PATH"
export RUSTC_BOOTSTRAP=1
exec "<path to the rust-analyzer binary>" "$@"
This is entirely external to rules_rust's own configuration surface (there
is no rust-analyzer.cargo.* setting that redirects or disables this call),
so it has to be layered on top of the generated editor integration.
In principle discover_bazel_rust_project/setup could generate a wrapper
like this itself — it already emits small executables into
.helix/.rules_rust_analyzer/ (discover_bazel_rust_project.exe,
flycheck.exe) and has access to both the rust_analyzer_toolchain's
rust-analyzer binary and the regular rust_toolchain's matching cargo
during that step. We're not asking for that specifically, though — a few
reasons it may not be the right fix to bake in as default behavior:
RUSTC_BOOTSTRAP=1unlocks all unstable features for that process,
silently. Shipping it unconditionally in official tooling is a broader
escape hatch than "fix this one call."- It's inherently version-fragile: it only works because this particular
cargo/rustcpairing happens to support-Zlockfile-path. A future
rust-analyzer release could start requiring a different unstable flag the
matchedcargodoesn't have either. - It doesn't fix the actual gap (rust-analyzer having no way to redirect or
disable this call) — it papers over it. A real fix likely belongs upstream
in rust-analyzer.
Bazel and rules_rust version
- Bazel: 9.1.1
- rules_rust: 0.73.0 (also reproduced on 0.71.3)
- rust-analyzer version from
rust_analyzer_toolchain: 1.95.0
- 主要言語
- Starlark
- スター
- 857
- フォーク
- 701
- 平均マージ
- 4日 7時間
- マージ済み PR(30日)
- 46
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
bazelbuild/rules_rust のほかの issue
-
`rust_test(crate = ...)` does not stage the crate's transitive dynamic libraries into runfiles対応中かも @Wito-1 が 65 日前に担当しました。 オープン
難易度 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 件 ·
メンテナーはふだん 3 日以内に返信
-
awaiting-response
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
bazelbuild/rules_rust#3599 · コメント 1 件 ·
メンテナーはふだん 3 日以内に返信
bazelbuild/rules_rust の issue をすべて見る
似ている issue
-
XML grammar: only the closing --> of a comment gets the scope comment.block.xml対応中かも @alexeyfala が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
danipen/TextMateSharp#145 · コメント 2 件 ·
-
accepted
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
future-agi/future-agi#6368 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
omacom/omarchy#21787 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Windows 内置更新报“更新进程启动确认超时”:Explorer ShellExecute 冗余的 -WindowStyle Hidden + -EncodedCommand 触发卡巴斯基挂起オープンbug status/needs-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
WhiteBr1ck/agy-powertools#397 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
leanprover/vscode-lean4#841 ·
メンテナーはふだん 2 日以内に返信