Skip to content

Commit 301d425

Browse files
authored
Fixes rust-analyzer include_dirs for crates with generated sources (#3840)
When a crate contains generated sources, `include_dirs` needs to include BOTH the root_module directory, and the generated sources directory. When `include_dirs` is empty, the inclusion of the root_module directory is done automatically, but when setting `include_dirs`, this needs to be done manually. See: https://github.com/rust-lang/rust-analyzer/blob/a84d92ff213e30fb00d7b812e07c4f67e99dcd29/crates/project-model/src/project_json.rs#L123-L131
1 parent 247703a commit 301d425

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

rust/private/rust_analyzer.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,10 @@ def _create_single_crate(ctx, attrs, info):
263263
src_map = {src.short_path: src for src in srcs if src.is_source}
264264
if info.crate.root.short_path in src_map:
265265
crate["root_module"] = _WORKSPACE_TEMPLATE + src_map[info.crate.root.short_path].path
266-
crate["source"]["include_dirs"].append(path_prefix + info.crate.root.dirname)
266+
crate["source"]["include_dirs"].extend([
267+
_WORKSPACE_TEMPLATE + src_map[info.crate.root.short_path].dirname,
268+
path_prefix + info.crate.root.dirname,
269+
])
267270

268271
if info.build_info != None and info.build_info.out_dir != None:
269272
out_dir_path = info.build_info.out_dir.path

test/rust_analyzer/generated_srcs_test/rust_project_json_test.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ mod tests {
5050
assert!(with_gen.root_module.ends_with("/lib.rs"));
5151

5252
let include_dirs = &with_gen.source.as_ref().unwrap().include_dirs;
53-
assert!(include_dirs.len() == 1);
54-
assert!(include_dirs[0].starts_with(output_base));
53+
assert_eq!(include_dirs.len(), 2);
54+
55+
// The first entry is the workspace directory.
56+
assert!(!include_dirs[0].starts_with(output_base));
57+
58+
// The second entry is the output base, where the generated files are located.
59+
assert!(include_dirs[1].starts_with(output_base));
5560
}
5661
}

0 commit comments

Comments
 (0)