Skip to content

Fix rust_analyzer flycheck label for sources in subdirectories - #4240

Open
brunal wants to merge 1 commit into
bazelbuild:mainfrom
qube-rt:flycheck-subdir-label
Open

Fix rust_analyzer flycheck label for sources in subdirectories#4240
brunal wants to merge 1 commit into
bazelbuild:mainfrom
qube-rt:flycheck-subdir-label

Conversation

@brunal

@brunal brunal commented Aug 25, 2026

Copy link
Copy Markdown

query_label_for built the saved file's label from its basename rather than its path relative to the package, so for a file at foo/bar/src/publisher.rs it queried

attr(srcs, "//foo/bar:publisher.rs", //foo/bar:*)

which matches nothing. Flycheck then failed with "bazel query returned no targets" and rust-analyzer reported "Cargo watcher failed, the command produced no valid metadata".

This problem can easily arise in cargo-compatible codebases, as the "src/" subdir is standard with cargo.

Note that crate root modules are exempt because build_file_label_map populates the sidecar's file_labels from each spec's root_module, so saving lib.rs or main.rs takes the fast path and never runs the query. Every other saved file in a package with a src/ layout hit this.

Pull the label construction into source_file_label/package_label so it can be unit tested, and normalize \ to / there -- both halves are derived from the saved file, so on Windows they arrive backslash-separated and //foo\bar:... is not a valid label. Implementation-wise, I simply always normalize, when there could be a platform-specific implementation.

`query_label_for` built the saved file's label from its basename rather
than its path relative to the package, so for a file at
`foo/bar/src/publisher.rs` it queried

    attr(srcs, "//foo/bar:publisher.rs", //foo/bar:*)

which matches nothing. Flycheck then failed with "bazel query returned
no targets" and rust-analyzer reported "Cargo watcher failed, the
command produced no valid metadata".

This problem can easily arise in cargo-compatible codebases, as the
"src/" subdir is standard with cargo.

Note that crate root modules are exempt because `build_file_label_map`
populates the sidecar's `file_labels` from each spec's `root_module`, so
saving `lib.rs` or `main.rs` takes the fast path and never runs the
query. Every other saved file in a package with a `src/` layout hit
this.

Pull the label construction into `source_file_label`/`package_label` so
it can be unit tested, and normalize `\` to `/` there -- both halves are
derived from the saved file, so on Windows they arrive
backslash-separated and `//foo\bar:...` is not a valid label.
Implementation-wise, I simply always normalize, when there could be a
platform-specific implementation.
@google-cla

google-cla Bot commented Aug 25, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant