`rust_analyzer_aspect` emits the wrong crate root for mixed source/generated crates

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

维护者通常 3 天内回复

@UebelAndre 已经在做这个了。

开始于 2026年8月19日。

评估

这个 Issue 还没有评估数据。

描述

needs-triage rust-analyzer

Description

For a Rust target whose srcs contains both a source crate root and a generated Rust file, rust_analyzer_aspect copies the sources into Bazel's output tree but sets root_module back to the workspace source file.

Relative include! and mod paths are resolved from the crate root's directory. The generated sibling exists beside the output-tree copy of the crate root, not beside the workspace source file, so rust-analyzer cannot resolve it. The generated local crate also has no build metadata.

Reproduction steps

MODULE.bazel:

module(name = "rust_analyzer_generated_srcs_repro")

bazel_dep(name = "rules_rust", version = "0.73.0")

BUILD.bazel:

load("@rules_rust//rust:defs.bzl", "rust_library")

genrule(
    name = "generated",
    outs = ["generated.rs"],
    cmd = "echo 'pub fn generated() {}' > $@",
)

rust_library(
    name = "mixed_sources",
    srcs = [
        "lib.rs",
        ":generated",
    ],
    edition = "2021",
)

lib.rs:

include!("generated.rs");

Generate the rust-analyzer project:

bazel run @rules_rust//tools/rust_analyzer:gen_rust_project -- //:mixed_sources

The command succeeds and generates rust-project.json. Both files needed by rust-analyzer exist together in the output tree:

bazel-bin/lib.rs
bazel-bin/generated.rs

However, the mixed_sources entry in rust-project.json points the crate root at the workspace copy. Paths below are shortened for clarity:

{
  "root_module": "/path/to/repro/lib.rs",
  "source": {
    "include_dirs": [
      "/path/to/repro/.",
      "/path/to/execroot/_main/bazel-out/k8-fastbuild/bin"
    ]
  }
}

There is no generated.rs beside /path/to/repro/lib.rs, and the crate entry has no build field.

Impact

rust-analyzer cannot resolve generated files referenced through relative include! or mod declarations in mixed source/generated crates. This causes missing or incorrect diagnostics, completion, navigation, and code-lens behavior even though the crate builds successfully with Bazel.

Bazel and rules_rust version

  • Bazel: 9.2.0
  • rules_rust: 0.73.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 发到你的邮箱

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