From fb7dd13c208bf618797916c71956188a119a8cd1 Mon Sep 17 00:00:00 2001 From: Fabrice Date: Thu, 3 Sep 2026 15:06:49 +0200 Subject: [PATCH] Prevent re-anchoring rust-analyzer root_module when siblings are generated (#4233) --- rust/private/rust_analyzer.bzl | 12 ++++++++++-- .../generated_srcs_test/rust_project_json_test.rs | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/rust/private/rust_analyzer.bzl b/rust/private/rust_analyzer.bzl index 7fa8686caf..76519f5d05 100644 --- a/rust/private/rust_analyzer.bzl +++ b/rust/private/rust_analyzer.bzl @@ -266,7 +266,7 @@ def _create_single_crate(ctx, attrs, info): # We're only interested in the build info for local crates as these are the # only ones for which we want build file watching and code lens runnables support. - if not is_external and not is_generated: + if not is_external: crate["build"] = { "build_file": _WORKSPACE_TEMPLATE + ctx.build_file_path, # Emit canonical `//pkg:name` form. Bazel's BEP reports action @@ -280,8 +280,16 @@ def _create_single_crate(ctx, attrs, info): if is_generated: srcs = getattr(ctx.rule.files, "srcs", []) src_map = {src.short_path: src for src in srcs if src.is_source} + + # Only re-anchor into the workspace when every sibling src lives there too. + has_generated_srcs = any([ + not src.is_source + for src in srcs + if src.short_path != info.crate.root.short_path + ]) if info.crate.root.short_path in src_map: - crate["root_module"] = _WORKSPACE_TEMPLATE + src_map[info.crate.root.short_path].path + if not has_generated_srcs: + crate["root_module"] = _WORKSPACE_TEMPLATE + src_map[info.crate.root.short_path].path crate["source"]["include_dirs"].extend([ _WORKSPACE_TEMPLATE + src_map[info.crate.root.short_path].dirname, path_prefix + info.crate.root.dirname, diff --git a/test/rust_analyzer/generated_srcs_test/rust_project_json_test.rs b/test/rust_analyzer/generated_srcs_test/rust_project_json_test.rs index 2c6e351aa1..24b65f9959 100644 --- a/test/rust_analyzer/generated_srcs_test/rust_project_json_test.rs +++ b/test/rust_analyzer/generated_srcs_test/rust_project_json_test.rs @@ -49,6 +49,9 @@ mod tests { assert!(with_gen.root_module.starts_with("/")); assert!(with_gen.root_module.ends_with("/lib.rs")); + // root_module isn't re-anchored to the workspace since it has a generated sibling. + assert!(with_gen.root_module.starts_with(output_base)); + let include_dirs = &with_gen.source.as_ref().unwrap().include_dirs; assert_eq!(include_dirs.len(), 2);