From 4dfda64b39cc47ac75280461f46e121958990fca Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 13 Nov 2020 17:38:26 +0100 Subject: [PATCH] Cleanup workspace loading a tiny bit --- crates/project_model/src/cargo_workspace.rs | 20 ++++++----- crates/project_model/src/workspace.rs | 34 +++++++++---------- crates/rust-analyzer/src/cli/load_cargo.rs | 1 - crates/rust-analyzer/src/config.rs | 6 ++-- crates/rust-analyzer/src/reload.rs | 7 +--- .../tests/rust-analyzer/support.rs | 8 ++--- editors/code/package.json | 10 +++--- 7 files changed, 40 insertions(+), 46 deletions(-) diff --git a/crates/project_model/src/cargo_workspace.rs b/crates/project_model/src/cargo_workspace.rs index 608a031d49..540b57ae4c 100644 --- a/crates/project_model/src/cargo_workspace.rs +++ b/crates/project_model/src/cargo_workspace.rs @@ -65,6 +65,10 @@ pub struct CargoConfig { /// rustc target pub target: Option, + /// Don't load sysroot crates (`std`, `core` & friends). Might be useful + /// when debugging isolated issues. + pub no_sysroot: bool, + /// rustc private crate source pub rustc_source: Option, } @@ -140,27 +144,27 @@ impl PackageData { impl CargoWorkspace { pub fn from_cargo_metadata( cargo_toml: &AbsPath, - cargo_features: &CargoConfig, + config: &CargoConfig, ) -> Result { let mut meta = MetadataCommand::new(); meta.cargo_path(toolchain::cargo()); meta.manifest_path(cargo_toml.to_path_buf()); - if cargo_features.all_features { + if config.all_features { meta.features(CargoOpt::AllFeatures); } else { - if cargo_features.no_default_features { + if config.no_default_features { // FIXME: `NoDefaultFeatures` is mutual exclusive with `SomeFeatures` // https://github.com/oli-obk/cargo_metadata/issues/79 meta.features(CargoOpt::NoDefaultFeatures); } - if !cargo_features.features.is_empty() { - meta.features(CargoOpt::SomeFeatures(cargo_features.features.clone())); + if !config.features.is_empty() { + meta.features(CargoOpt::SomeFeatures(config.features.clone())); } } if let Some(parent) = cargo_toml.parent() { meta.current_dir(parent.to_path_buf()); } - if let Some(target) = cargo_features.target.as_ref() { + if let Some(target) = config.target.as_ref() { meta.other_options(vec![String::from("--filter-platform"), target.clone()]); } let mut meta = meta.exec().with_context(|| { @@ -170,8 +174,8 @@ impl CargoWorkspace { let mut out_dir_by_id = FxHashMap::default(); let mut cfgs = FxHashMap::default(); let mut proc_macro_dylib_paths = FxHashMap::default(); - if cargo_features.load_out_dirs_from_check { - let resources = load_extern_resources(cargo_toml, cargo_features)?; + if config.load_out_dirs_from_check { + let resources = load_extern_resources(cargo_toml, config)?; out_dir_by_id = resources.out_dirs; cfgs = resources.cfgs; proc_macro_dylib_paths = resources.proc_dylib_paths; diff --git a/crates/project_model/src/workspace.rs b/crates/project_model/src/workspace.rs index 43ea351d18..8a1a60e0ee 100644 --- a/crates/project_model/src/workspace.rs +++ b/crates/project_model/src/workspace.rs @@ -1,3 +1,7 @@ +//! Handles lowering of build-system specific workspace information (`cargo +//! metadata` or `rust-project.json`) into representation stored in the salsa +//! database -- `CrateGraph`. + use std::{fmt, fs, path::Component, process::Command}; use anyhow::{Context, Result}; @@ -56,11 +60,7 @@ impl fmt::Debug for ProjectWorkspace { } impl ProjectWorkspace { - pub fn load( - manifest: ProjectManifest, - cargo_config: &CargoConfig, - with_sysroot: bool, - ) -> Result { + pub fn load(manifest: ProjectManifest, config: &CargoConfig) -> Result { let res = match manifest { ProjectManifest::ProjectJson(project_json) => { let file = fs::read_to_string(&project_json).with_context(|| { @@ -84,32 +84,30 @@ impl ProjectWorkspace { cmd })?; - let cargo = CargoWorkspace::from_cargo_metadata(&cargo_toml, cargo_config) - .with_context(|| { + let cargo = CargoWorkspace::from_cargo_metadata(&cargo_toml, config).with_context( + || { format!( "Failed to read Cargo metadata from Cargo.toml file {}, {}", cargo_toml.display(), cargo_version ) - })?; - let sysroot = if with_sysroot { + }, + )?; + let sysroot = if config.no_sysroot { + Sysroot::default() + } else { Sysroot::discover(&cargo_toml).with_context(|| { format!( "Failed to find sysroot for Cargo.toml file {}. Is rust-src installed?", cargo_toml.display() ) })? - } else { - Sysroot::default() }; - let rustc = if let Some(rustc_dir) = &cargo_config.rustc_source { - Some( - CargoWorkspace::from_cargo_metadata(&rustc_dir, cargo_config) - .with_context(|| { - format!("Failed to read Cargo metadata for Rust sources") - })?, - ) + let rustc = if let Some(rustc_dir) = &config.rustc_source { + Some(CargoWorkspace::from_cargo_metadata(&rustc_dir, config).with_context( + || format!("Failed to read Cargo metadata for Rust sources"), + )?) } else { None }; diff --git a/crates/rust-analyzer/src/cli/load_cargo.rs b/crates/rust-analyzer/src/cli/load_cargo.rs index ab1e2ab92d..76526c66c0 100644 --- a/crates/rust-analyzer/src/cli/load_cargo.rs +++ b/crates/rust-analyzer/src/cli/load_cargo.rs @@ -21,7 +21,6 @@ pub fn load_cargo( let ws = ProjectWorkspace::load( root, &CargoConfig { load_out_dirs_from_check, ..Default::default() }, - true, )?; let (sender, receiver) = unbounded(); diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 372180ab58..d167965900 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -49,7 +49,6 @@ pub struct Config { pub hover: HoverConfig, pub semantic_tokens_refresh: bool, - pub with_sysroot: bool, pub linked_projects: Vec, pub root_path: AbsPathBuf, } @@ -155,7 +154,6 @@ impl Config { Config { client_caps: ClientCapsConfig::default(), - with_sysroot: true, publish_diagnostics: true, diagnostics: DiagnosticsConfig::default(), diagnostics_map: DiagnosticsMapConfig::default(), @@ -209,7 +207,6 @@ impl Config { let data = ConfigData::from_json(json); - self.with_sysroot = data.withSysroot; self.publish_diagnostics = data.diagnostics_enable; self.diagnostics = DiagnosticsConfig { disable_experimental: !data.diagnostics_enableExperimental, @@ -246,6 +243,7 @@ impl Config { load_out_dirs_from_check: data.cargo_loadOutDirsFromCheck, target: data.cargo_target.clone(), rustc_source: rustc_source, + no_sysroot: data.cargo_noSysroot, }; self.runnables = RunnablesConfig { override_cargo: data.runnables_overrideCargo, @@ -492,6 +490,7 @@ config_data! { cargo_loadOutDirsFromCheck: bool = false, cargo_noDefaultFeatures: bool = false, cargo_target: Option = None, + cargo_noSysroot: bool = false, checkOnSave_enable: bool = true, checkOnSave_allFeatures: Option = None, @@ -544,7 +543,6 @@ config_data! { rustfmt_extraArgs: Vec = Vec::new(), rustfmt_overrideCommand: Option> = None, - withSysroot: bool = true, rustcSource : Option = None, } } diff --git a/crates/rust-analyzer/src/reload.rs b/crates/rust-analyzer/src/reload.rs index 11c8d0e5f8..fa6e09f42b 100644 --- a/crates/rust-analyzer/src/reload.rs +++ b/crates/rust-analyzer/src/reload.rs @@ -96,17 +96,12 @@ impl GlobalState { self.task_pool.handle.spawn({ let linked_projects = self.config.linked_projects.clone(); let cargo_config = self.config.cargo.clone(); - let with_sysroot = self.config.with_sysroot.clone(); move || { let workspaces = linked_projects .iter() .map(|project| match project { LinkedProject::ProjectManifest(manifest) => { - project_model::ProjectWorkspace::load( - manifest.clone(), - &cargo_config, - with_sysroot, - ) + project_model::ProjectWorkspace::load(manifest.clone(), &cargo_config) } LinkedProject::InlineJsonProject(it) => { project_model::ProjectWorkspace::load_inline(it.clone()) diff --git a/crates/rust-analyzer/tests/rust-analyzer/support.rs b/crates/rust-analyzer/tests/rust-analyzer/support.rs index fe9362bc00..b210b98f0d 100644 --- a/crates/rust-analyzer/tests/rust-analyzer/support.rs +++ b/crates/rust-analyzer/tests/rust-analyzer/support.rs @@ -12,7 +12,7 @@ use lsp_types::{ notification::Exit, request::Shutdown, TextDocumentIdentifier, Url, WorkDoneProgress, }; use lsp_types::{ProgressParams, ProgressParamsValue}; -use project_model::ProjectManifest; +use project_model::{CargoConfig, ProjectManifest}; use rust_analyzer::{ config::{ClientCapsConfig, Config, FilesConfig, FilesWatcher, LinkedProject}, main_loop, @@ -47,8 +47,8 @@ impl<'a> Project<'a> { self } - pub(crate) fn with_sysroot(mut self, sysroot: bool) -> Project<'a> { - self.with_sysroot = sysroot; + pub(crate) fn with_sysroot(mut self, yes: bool) -> Project<'a> { + self.with_sysroot = yes; self } @@ -90,7 +90,7 @@ impl<'a> Project<'a> { work_done_progress: true, ..Default::default() }, - with_sysroot: self.with_sysroot, + cargo: CargoConfig { no_sysroot: !self.with_sysroot, ..Default::default() }, linked_projects, files: FilesConfig { watcher: FilesWatcher::Client, exclude: Vec::new() }, ..Config::new(tmp_dir_path) diff --git a/editors/code/package.json b/editors/code/package.json index 3768679fe9..220d44abc8 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -283,6 +283,11 @@ "default": null, "description": "Specify the compilation target" }, + "rust-analyzer.noSysroot": { + "markdownDescription": "Internal config for debugging, disables loading of sysroot crates", + "type": "boolean", + "default": false + }, "rust-analyzer.rustfmt.extraArgs": { "type": "array", "items": { @@ -605,11 +610,6 @@ }, "default": null }, - "rust-analyzer.withSysroot": { - "markdownDescription": "Internal config for debugging, disables loading of sysroot crates", - "type": "boolean", - "default": true - }, "rust-analyzer.diagnostics.enable": { "type": "boolean", "default": true,