Skip to content

Commit 4dfda64

Browse files
committed
Cleanup workspace loading a tiny bit
1 parent aeda30e commit 4dfda64

File tree

7 files changed

+40
-46
lines changed

7 files changed

+40
-46
lines changed

crates/project_model/src/cargo_workspace.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ pub struct CargoConfig {
6565
/// rustc target
6666
pub target: Option<String>,
6767

68+
/// Don't load sysroot crates (`std`, `core` & friends). Might be useful
69+
/// when debugging isolated issues.
70+
pub no_sysroot: bool,
71+
6872
/// rustc private crate source
6973
pub rustc_source: Option<AbsPathBuf>,
7074
}
@@ -140,27 +144,27 @@ impl PackageData {
140144
impl CargoWorkspace {
141145
pub fn from_cargo_metadata(
142146
cargo_toml: &AbsPath,
143-
cargo_features: &CargoConfig,
147+
config: &CargoConfig,
144148
) -> Result<CargoWorkspace> {
145149
let mut meta = MetadataCommand::new();
146150
meta.cargo_path(toolchain::cargo());
147151
meta.manifest_path(cargo_toml.to_path_buf());
148-
if cargo_features.all_features {
152+
if config.all_features {
149153
meta.features(CargoOpt::AllFeatures);
150154
} else {
151-
if cargo_features.no_default_features {
155+
if config.no_default_features {
152156
// FIXME: `NoDefaultFeatures` is mutual exclusive with `SomeFeatures`
153157
// https://github.com/oli-obk/cargo_metadata/issues/79
154158
meta.features(CargoOpt::NoDefaultFeatures);
155159
}
156-
if !cargo_features.features.is_empty() {
157-
meta.features(CargoOpt::SomeFeatures(cargo_features.features.clone()));
160+
if !config.features.is_empty() {
161+
meta.features(CargoOpt::SomeFeatures(config.features.clone()));
158162
}
159163
}
160164
if let Some(parent) = cargo_toml.parent() {
161165
meta.current_dir(parent.to_path_buf());
162166
}
163-
if let Some(target) = cargo_features.target.as_ref() {
167+
if let Some(target) = config.target.as_ref() {
164168
meta.other_options(vec![String::from("--filter-platform"), target.clone()]);
165169
}
166170
let mut meta = meta.exec().with_context(|| {
@@ -170,8 +174,8 @@ impl CargoWorkspace {
170174
let mut out_dir_by_id = FxHashMap::default();
171175
let mut cfgs = FxHashMap::default();
172176
let mut proc_macro_dylib_paths = FxHashMap::default();
173-
if cargo_features.load_out_dirs_from_check {
174-
let resources = load_extern_resources(cargo_toml, cargo_features)?;
177+
if config.load_out_dirs_from_check {
178+
let resources = load_extern_resources(cargo_toml, config)?;
175179
out_dir_by_id = resources.out_dirs;
176180
cfgs = resources.cfgs;
177181
proc_macro_dylib_paths = resources.proc_dylib_paths;

crates/project_model/src/workspace.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! Handles lowering of build-system specific workspace information (`cargo
2+
//! metadata` or `rust-project.json`) into representation stored in the salsa
3+
//! database -- `CrateGraph`.
4+
15
use std::{fmt, fs, path::Component, process::Command};
26

37
use anyhow::{Context, Result};
@@ -56,11 +60,7 @@ impl fmt::Debug for ProjectWorkspace {
5660
}
5761

5862
impl ProjectWorkspace {
59-
pub fn load(
60-
manifest: ProjectManifest,
61-
cargo_config: &CargoConfig,
62-
with_sysroot: bool,
63-
) -> Result<ProjectWorkspace> {
63+
pub fn load(manifest: ProjectManifest, config: &CargoConfig) -> Result<ProjectWorkspace> {
6464
let res = match manifest {
6565
ProjectManifest::ProjectJson(project_json) => {
6666
let file = fs::read_to_string(&project_json).with_context(|| {
@@ -84,32 +84,30 @@ impl ProjectWorkspace {
8484
cmd
8585
})?;
8686

87-
let cargo = CargoWorkspace::from_cargo_metadata(&cargo_toml, cargo_config)
88-
.with_context(|| {
87+
let cargo = CargoWorkspace::from_cargo_metadata(&cargo_toml, config).with_context(
88+
|| {
8989
format!(
9090
"Failed to read Cargo metadata from Cargo.toml file {}, {}",
9191
cargo_toml.display(),
9292
cargo_version
9393
)
94-
})?;
95-
let sysroot = if with_sysroot {
94+
},
95+
)?;
96+
let sysroot = if config.no_sysroot {
97+
Sysroot::default()
98+
} else {
9699
Sysroot::discover(&cargo_toml).with_context(|| {
97100
format!(
98101
"Failed to find sysroot for Cargo.toml file {}. Is rust-src installed?",
99102
cargo_toml.display()
100103
)
101104
})?
102-
} else {
103-
Sysroot::default()
104105
};
105106

106-
let rustc = if let Some(rustc_dir) = &cargo_config.rustc_source {
107-
Some(
108-
CargoWorkspace::from_cargo_metadata(&rustc_dir, cargo_config)
109-
.with_context(|| {
110-
format!("Failed to read Cargo metadata for Rust sources")
111-
})?,
112-
)
107+
let rustc = if let Some(rustc_dir) = &config.rustc_source {
108+
Some(CargoWorkspace::from_cargo_metadata(&rustc_dir, config).with_context(
109+
|| format!("Failed to read Cargo metadata for Rust sources"),
110+
)?)
113111
} else {
114112
None
115113
};

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ pub fn load_cargo(
2121
let ws = ProjectWorkspace::load(
2222
root,
2323
&CargoConfig { load_out_dirs_from_check, ..Default::default() },
24-
true,
2524
)?;
2625

2726
let (sender, receiver) = unbounded();

crates/rust-analyzer/src/config.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ pub struct Config {
4949
pub hover: HoverConfig,
5050
pub semantic_tokens_refresh: bool,
5151

52-
pub with_sysroot: bool,
5352
pub linked_projects: Vec<LinkedProject>,
5453
pub root_path: AbsPathBuf,
5554
}
@@ -155,7 +154,6 @@ impl Config {
155154
Config {
156155
client_caps: ClientCapsConfig::default(),
157156

158-
with_sysroot: true,
159157
publish_diagnostics: true,
160158
diagnostics: DiagnosticsConfig::default(),
161159
diagnostics_map: DiagnosticsMapConfig::default(),
@@ -209,7 +207,6 @@ impl Config {
209207

210208
let data = ConfigData::from_json(json);
211209

212-
self.with_sysroot = data.withSysroot;
213210
self.publish_diagnostics = data.diagnostics_enable;
214211
self.diagnostics = DiagnosticsConfig {
215212
disable_experimental: !data.diagnostics_enableExperimental,
@@ -246,6 +243,7 @@ impl Config {
246243
load_out_dirs_from_check: data.cargo_loadOutDirsFromCheck,
247244
target: data.cargo_target.clone(),
248245
rustc_source: rustc_source,
246+
no_sysroot: data.cargo_noSysroot,
249247
};
250248
self.runnables = RunnablesConfig {
251249
override_cargo: data.runnables_overrideCargo,
@@ -492,6 +490,7 @@ config_data! {
492490
cargo_loadOutDirsFromCheck: bool = false,
493491
cargo_noDefaultFeatures: bool = false,
494492
cargo_target: Option<String> = None,
493+
cargo_noSysroot: bool = false,
495494

496495
checkOnSave_enable: bool = true,
497496
checkOnSave_allFeatures: Option<bool> = None,
@@ -544,7 +543,6 @@ config_data! {
544543
rustfmt_extraArgs: Vec<String> = Vec::new(),
545544
rustfmt_overrideCommand: Option<Vec<String>> = None,
546545

547-
withSysroot: bool = true,
548546
rustcSource : Option<String> = None,
549547
}
550548
}

crates/rust-analyzer/src/reload.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,12 @@ impl GlobalState {
9696
self.task_pool.handle.spawn({
9797
let linked_projects = self.config.linked_projects.clone();
9898
let cargo_config = self.config.cargo.clone();
99-
let with_sysroot = self.config.with_sysroot.clone();
10099
move || {
101100
let workspaces = linked_projects
102101
.iter()
103102
.map(|project| match project {
104103
LinkedProject::ProjectManifest(manifest) => {
105-
project_model::ProjectWorkspace::load(
106-
manifest.clone(),
107-
&cargo_config,
108-
with_sysroot,
109-
)
104+
project_model::ProjectWorkspace::load(manifest.clone(), &cargo_config)
110105
}
111106
LinkedProject::InlineJsonProject(it) => {
112107
project_model::ProjectWorkspace::load_inline(it.clone())

crates/rust-analyzer/tests/rust-analyzer/support.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use lsp_types::{
1212
notification::Exit, request::Shutdown, TextDocumentIdentifier, Url, WorkDoneProgress,
1313
};
1414
use lsp_types::{ProgressParams, ProgressParamsValue};
15-
use project_model::ProjectManifest;
15+
use project_model::{CargoConfig, ProjectManifest};
1616
use rust_analyzer::{
1717
config::{ClientCapsConfig, Config, FilesConfig, FilesWatcher, LinkedProject},
1818
main_loop,
@@ -47,8 +47,8 @@ impl<'a> Project<'a> {
4747
self
4848
}
4949

50-
pub(crate) fn with_sysroot(mut self, sysroot: bool) -> Project<'a> {
51-
self.with_sysroot = sysroot;
50+
pub(crate) fn with_sysroot(mut self, yes: bool) -> Project<'a> {
51+
self.with_sysroot = yes;
5252
self
5353
}
5454

@@ -90,7 +90,7 @@ impl<'a> Project<'a> {
9090
work_done_progress: true,
9191
..Default::default()
9292
},
93-
with_sysroot: self.with_sysroot,
93+
cargo: CargoConfig { no_sysroot: !self.with_sysroot, ..Default::default() },
9494
linked_projects,
9595
files: FilesConfig { watcher: FilesWatcher::Client, exclude: Vec::new() },
9696
..Config::new(tmp_dir_path)

editors/code/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@
283283
"default": null,
284284
"description": "Specify the compilation target"
285285
},
286+
"rust-analyzer.noSysroot": {
287+
"markdownDescription": "Internal config for debugging, disables loading of sysroot crates",
288+
"type": "boolean",
289+
"default": false
290+
},
286291
"rust-analyzer.rustfmt.extraArgs": {
287292
"type": "array",
288293
"items": {
@@ -605,11 +610,6 @@
605610
},
606611
"default": null
607612
},
608-
"rust-analyzer.withSysroot": {
609-
"markdownDescription": "Internal config for debugging, disables loading of sysroot crates",
610-
"type": "boolean",
611-
"default": true
612-
},
613613
"rust-analyzer.diagnostics.enable": {
614614
"type": "boolean",
615615
"default": true,

0 commit comments

Comments
 (0)