Skip to content

Commit 2e234e3

Browse files
authored
Merge pull request #18880 from PrototypeNM1/extra-includes
Add config setting which allows adding additional include paths to the VFS.
2 parents 3fe50ef + 1f96869 commit 2e234e3

File tree

7 files changed

+67
-3
lines changed

7 files changed

+67
-3
lines changed

crates/project-model/src/cargo_workspace.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ pub struct CargoConfig {
9292
pub sysroot_src: Option<AbsPathBuf>,
9393
/// rustc private crate source
9494
pub rustc_source: Option<RustLibSource>,
95+
/// Extra includes to add to the VFS.
96+
pub extra_includes: Vec<AbsPathBuf>,
9597
pub cfg_overrides: CfgOverrides,
9698
/// Invoke `cargo check` through the RUSTC_WRAPPER.
9799
pub wrap_rustc_in_build_scripts: bool,

crates/project-model/src/tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ fn load_workspace_from_metadata(file: &str) -> ProjectWorkspace {
4949
rustc_cfg: Vec::new(),
5050
toolchain: None,
5151
target_layout: Err("target_data_layout not loaded".into()),
52+
extra_includes: Vec::new(),
5253
}
5354
}
5455

@@ -63,6 +64,7 @@ fn load_rust_project(file: &str) -> (CrateGraph, ProcMacroPaths) {
6364
toolchain: None,
6465
target_layout: Err(Arc::from("test has no data layout")),
6566
cfg_overrides: Default::default(),
67+
extra_includes: Vec::new(),
6668
};
6769
to_crate_graph(project_workspace, &mut Default::default())
6870
}
@@ -284,6 +286,7 @@ fn smoke_test_real_sysroot_cargo() {
284286
cfg_overrides: Default::default(),
285287
toolchain: None,
286288
target_layout: Err("target_data_layout not loaded".into()),
289+
extra_includes: Vec::new(),
287290
};
288291
project_workspace.to_crate_graph(
289292
&mut {

crates/project-model/src/workspace.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ pub struct ProjectWorkspace {
6363
pub target_layout: TargetLayoutLoadResult,
6464
/// A set of cfg overrides for this workspace.
6565
pub cfg_overrides: CfgOverrides,
66+
/// Additional includes to add for the VFS.
67+
pub extra_includes: Vec<AbsPathBuf>,
6668
}
6769

6870
#[derive(Clone)]
@@ -104,7 +106,15 @@ pub enum ProjectWorkspaceKind {
104106
impl fmt::Debug for ProjectWorkspace {
105107
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106108
// Make sure this isn't too verbose.
107-
let Self { kind, sysroot, rustc_cfg, toolchain, target_layout, cfg_overrides } = self;
109+
let Self {
110+
kind,
111+
sysroot,
112+
rustc_cfg,
113+
toolchain,
114+
target_layout,
115+
cfg_overrides,
116+
extra_includes,
117+
} = self;
108118
match kind {
109119
ProjectWorkspaceKind::Cargo { cargo, error: _, build_scripts, rustc, set_test } => f
110120
.debug_struct("Cargo")
@@ -117,6 +127,7 @@ impl fmt::Debug for ProjectWorkspace {
117127
)
118128
.field("n_rustc_cfg", &rustc_cfg.len())
119129
.field("n_cfg_overrides", &cfg_overrides.len())
130+
.field("n_extra_includes", &extra_includes.len())
120131
.field("toolchain", &toolchain)
121132
.field("data_layout", &target_layout)
122133
.field("set_test", set_test)
@@ -130,7 +141,8 @@ impl fmt::Debug for ProjectWorkspace {
130141
.field("n_rustc_cfg", &rustc_cfg.len())
131142
.field("toolchain", &toolchain)
132143
.field("data_layout", &target_layout)
133-
.field("n_cfg_overrides", &cfg_overrides.len());
144+
.field("n_cfg_overrides", &cfg_overrides.len())
145+
.field("n_extra_includes", &extra_includes.len());
134146

135147
debug_struct.finish()
136148
}
@@ -144,6 +156,7 @@ impl fmt::Debug for ProjectWorkspace {
144156
.field("toolchain", &toolchain)
145157
.field("data_layout", &target_layout)
146158
.field("n_cfg_overrides", &cfg_overrides.len())
159+
.field("n_extra_includes", &extra_includes.len())
147160
.field("set_test", set_test)
148161
.finish(),
149162
}
@@ -320,6 +333,7 @@ impl ProjectWorkspace {
320333
cfg_overrides,
321334
toolchain,
322335
target_layout: data_layout.map(Arc::from).map_err(|it| Arc::from(it.to_string())),
336+
extra_includes: config.extra_includes.clone(),
323337
})
324338
}
325339

@@ -340,6 +354,7 @@ impl ProjectWorkspace {
340354
toolchain,
341355
target_layout: data_layout.map(Arc::from).map_err(|it| Arc::from(it.to_string())),
342356
cfg_overrides: config.cfg_overrides.clone(),
357+
extra_includes: config.extra_includes.clone(),
343358
}
344359
}
345360

@@ -399,6 +414,7 @@ impl ProjectWorkspace {
399414
toolchain,
400415
target_layout: data_layout.map(Arc::from).map_err(|it| Arc::from(it.to_string())),
401416
cfg_overrides: config.cfg_overrides.clone(),
417+
extra_includes: config.extra_includes.clone(),
402418
})
403419
}
404420

@@ -565,7 +581,13 @@ impl ProjectWorkspace {
565581

566582
PackageRoot {
567583
is_local: krate.is_workspace_member,
568-
include: krate.include.iter().cloned().chain(build_file).collect(),
584+
include: krate
585+
.include
586+
.iter()
587+
.cloned()
588+
.chain(build_file)
589+
.chain(self.extra_includes.iter().cloned())
590+
.collect(),
569591
exclude: krate.exclude.clone(),
570592
}
571593
})
@@ -603,6 +625,8 @@ impl ProjectWorkspace {
603625

604626
let mut exclude = vec![pkg_root.join(".git")];
605627
if is_local {
628+
include.extend(self.extra_includes.iter().cloned());
629+
606630
exclude.push(pkg_root.join("target"));
607631
} else {
608632
exclude.push(pkg_root.join("tests"));
@@ -661,6 +685,8 @@ impl ProjectWorkspace {
661685

662686
let mut exclude = vec![pkg_root.join(".git")];
663687
if is_local {
688+
include.extend(self.extra_includes.iter().cloned());
689+
664690
exclude.push(pkg_root.join("target"));
665691
} else {
666692
exclude.push(pkg_root.join("tests"));

crates/rust-analyzer/src/cli/rustc_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ impl Tester {
9393
toolchain: None,
9494
target_layout: data_layout.map(Arc::from).map_err(|it| Arc::from(it.to_string())),
9595
cfg_overrides: Default::default(),
96+
extra_includes: vec![],
9697
};
9798
let load_cargo_config = LoadCargoConfig {
9899
load_out_dirs_from_check: false,

crates/rust-analyzer/src/config.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,10 @@ config_data! {
725725
/// available on a nightly build.
726726
rustfmt_rangeFormatting_enable: bool = false,
727727

728+
/// Additional paths to include in the VFS. Generally for code that is
729+
/// generated or otherwise managed by a build system outside of Cargo,
730+
/// though Cargo might be the eventual consumer.
731+
vfs_extraIncludes: Vec<String> = vec![],
728732

729733
/// Workspace symbol search kind.
730734
workspace_symbol_search_kind: WorkspaceSymbolSearchKindDef = WorkspaceSymbolSearchKindDef::OnlyTypes,
@@ -1923,6 +1927,13 @@ impl Config {
19231927
});
19241928
let sysroot_src =
19251929
self.cargo_sysrootSrc(source_root).as_ref().map(|sysroot| self.root_path.join(sysroot));
1930+
let extra_includes = self
1931+
.vfs_extraIncludes(source_root)
1932+
.iter()
1933+
.map(String::as_str)
1934+
.map(AbsPathBuf::try_from)
1935+
.filter_map(Result::ok)
1936+
.collect();
19261937

19271938
CargoConfig {
19281939
all_targets: *self.cargo_allTargets(source_root),
@@ -1937,6 +1948,7 @@ impl Config {
19371948
sysroot,
19381949
sysroot_src,
19391950
rustc_source,
1951+
extra_includes,
19401952
cfg_overrides: project_model::CfgOverrides {
19411953
global: CfgDiff::new(
19421954
self.cargo_cfgs(source_root)

docs/user/generated_config.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,13 @@ Show documentation.
10511051
--
10521052
Specify the characters to exclude from triggering typing assists. The default trigger characters are `.`, `=`, `<`, `>`, `{`, and `(`.
10531053
--
1054+
[[rust-analyzer.vfs.extraIncludes]]rust-analyzer.vfs.extraIncludes (default: `[]`)::
1055+
+
1056+
--
1057+
Additional paths to include in the VFS. Generally for code that is
1058+
generated or otherwise managed by a build system outside of Cargo,
1059+
though Cargo might be the eventual consumer.
1060+
--
10541061
[[rust-analyzer.workspace.discoverConfig]]rust-analyzer.workspace.discoverConfig (default: `null`)::
10551062
+
10561063
--

editors/code/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2721,6 +2721,19 @@
27212721
}
27222722
}
27232723
},
2724+
{
2725+
"title": "vfs",
2726+
"properties": {
2727+
"rust-analyzer.vfs.extraIncludes": {
2728+
"markdownDescription": "Additional paths to include in the VFS. Generally for code that is\ngenerated or otherwise managed by a build system outside of Cargo,\nthough Cargo might be the eventual consumer.",
2729+
"default": [],
2730+
"type": "array",
2731+
"items": {
2732+
"type": "string"
2733+
}
2734+
}
2735+
}
2736+
},
27242737
{
27252738
"title": "workspace",
27262739
"properties": {

0 commit comments

Comments
 (0)