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

Đang mở
#4,234 1 bình luận 0 reaction 1 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 3 ngày

@UebelAndre đang làm issue này rồi.

Từ ngày 19/8/2026.

Đánh giá

Issue này chưa được đánh giá.

Mô tả

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
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

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của bazelbuild/rules_rust

Tất cả issue của bazelbuild/rules_rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.