Cleanup workspace loading a tiny bit

This commit is contained in:
Aleksey Kladov 2020-11-13 17:38:26 +01:00
parent aeda30e301
commit 4dfda64b39
7 changed files with 40 additions and 46 deletions

View file

@ -65,6 +65,10 @@ pub struct CargoConfig {
/// rustc target /// rustc target
pub target: Option<String>, pub target: Option<String>,
/// Don't load sysroot crates (`std`, `core` & friends). Might be useful
/// when debugging isolated issues.
pub no_sysroot: bool,
/// rustc private crate source /// rustc private crate source
pub rustc_source: Option<AbsPathBuf>, pub rustc_source: Option<AbsPathBuf>,
} }
@ -140,27 +144,27 @@ impl PackageData {
impl CargoWorkspace { impl CargoWorkspace {
pub fn from_cargo_metadata( pub fn from_cargo_metadata(
cargo_toml: &AbsPath, cargo_toml: &AbsPath,
cargo_features: &CargoConfig, config: &CargoConfig,
) -> Result<CargoWorkspace> { ) -> Result<CargoWorkspace> {
let mut meta = MetadataCommand::new(); let mut meta = MetadataCommand::new();
meta.cargo_path(toolchain::cargo()); meta.cargo_path(toolchain::cargo());
meta.manifest_path(cargo_toml.to_path_buf()); meta.manifest_path(cargo_toml.to_path_buf());
if cargo_features.all_features { if config.all_features {
meta.features(CargoOpt::AllFeatures); meta.features(CargoOpt::AllFeatures);
} else { } else {
if cargo_features.no_default_features { if config.no_default_features {
// FIXME: `NoDefaultFeatures` is mutual exclusive with `SomeFeatures` // FIXME: `NoDefaultFeatures` is mutual exclusive with `SomeFeatures`
// https://github.com/oli-obk/cargo_metadata/issues/79 // https://github.com/oli-obk/cargo_metadata/issues/79
meta.features(CargoOpt::NoDefaultFeatures); meta.features(CargoOpt::NoDefaultFeatures);
} }
if !cargo_features.features.is_empty() { if !config.features.is_empty() {
meta.features(CargoOpt::SomeFeatures(cargo_features.features.clone())); meta.features(CargoOpt::SomeFeatures(config.features.clone()));
} }
} }
if let Some(parent) = cargo_toml.parent() { if let Some(parent) = cargo_toml.parent() {
meta.current_dir(parent.to_path_buf()); 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()]); meta.other_options(vec![String::from("--filter-platform"), target.clone()]);
} }
let mut meta = meta.exec().with_context(|| { let mut meta = meta.exec().with_context(|| {
@ -170,8 +174,8 @@ impl CargoWorkspace {
let mut out_dir_by_id = FxHashMap::default(); let mut out_dir_by_id = FxHashMap::default();
let mut cfgs = FxHashMap::default(); let mut cfgs = FxHashMap::default();
let mut proc_macro_dylib_paths = FxHashMap::default(); let mut proc_macro_dylib_paths = FxHashMap::default();
if cargo_features.load_out_dirs_from_check { if config.load_out_dirs_from_check {
let resources = load_extern_resources(cargo_toml, cargo_features)?; let resources = load_extern_resources(cargo_toml, config)?;
out_dir_by_id = resources.out_dirs; out_dir_by_id = resources.out_dirs;
cfgs = resources.cfgs; cfgs = resources.cfgs;
proc_macro_dylib_paths = resources.proc_dylib_paths; proc_macro_dylib_paths = resources.proc_dylib_paths;

View file

@ -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 std::{fmt, fs, path::Component, process::Command};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
@ -56,11 +60,7 @@ impl fmt::Debug for ProjectWorkspace {
} }
impl ProjectWorkspace { impl ProjectWorkspace {
pub fn load( pub fn load(manifest: ProjectManifest, config: &CargoConfig) -> Result<ProjectWorkspace> {
manifest: ProjectManifest,
cargo_config: &CargoConfig,
with_sysroot: bool,
) -> Result<ProjectWorkspace> {
let res = match manifest { let res = match manifest {
ProjectManifest::ProjectJson(project_json) => { ProjectManifest::ProjectJson(project_json) => {
let file = fs::read_to_string(&project_json).with_context(|| { let file = fs::read_to_string(&project_json).with_context(|| {
@ -84,32 +84,30 @@ impl ProjectWorkspace {
cmd cmd
})?; })?;
let cargo = CargoWorkspace::from_cargo_metadata(&cargo_toml, cargo_config) let cargo = CargoWorkspace::from_cargo_metadata(&cargo_toml, config).with_context(
.with_context(|| { || {
format!( format!(
"Failed to read Cargo metadata from Cargo.toml file {}, {}", "Failed to read Cargo metadata from Cargo.toml file {}, {}",
cargo_toml.display(), cargo_toml.display(),
cargo_version cargo_version
) )
})?; },
let sysroot = if with_sysroot { )?;
let sysroot = if config.no_sysroot {
Sysroot::default()
} else {
Sysroot::discover(&cargo_toml).with_context(|| { Sysroot::discover(&cargo_toml).with_context(|| {
format!( format!(
"Failed to find sysroot for Cargo.toml file {}. Is rust-src installed?", "Failed to find sysroot for Cargo.toml file {}. Is rust-src installed?",
cargo_toml.display() cargo_toml.display()
) )
})? })?
} else {
Sysroot::default()
}; };
let rustc = if let Some(rustc_dir) = &cargo_config.rustc_source { let rustc = if let Some(rustc_dir) = &config.rustc_source {
Some( Some(CargoWorkspace::from_cargo_metadata(&rustc_dir, config).with_context(
CargoWorkspace::from_cargo_metadata(&rustc_dir, cargo_config) || format!("Failed to read Cargo metadata for Rust sources"),
.with_context(|| { )?)
format!("Failed to read Cargo metadata for Rust sources")
})?,
)
} else { } else {
None None
}; };

View file

@ -21,7 +21,6 @@ pub fn load_cargo(
let ws = ProjectWorkspace::load( let ws = ProjectWorkspace::load(
root, root,
&CargoConfig { load_out_dirs_from_check, ..Default::default() }, &CargoConfig { load_out_dirs_from_check, ..Default::default() },
true,
)?; )?;
let (sender, receiver) = unbounded(); let (sender, receiver) = unbounded();

View file

@ -49,7 +49,6 @@ pub struct Config {
pub hover: HoverConfig, pub hover: HoverConfig,
pub semantic_tokens_refresh: bool, pub semantic_tokens_refresh: bool,
pub with_sysroot: bool,
pub linked_projects: Vec<LinkedProject>, pub linked_projects: Vec<LinkedProject>,
pub root_path: AbsPathBuf, pub root_path: AbsPathBuf,
} }
@ -155,7 +154,6 @@ impl Config {
Config { Config {
client_caps: ClientCapsConfig::default(), client_caps: ClientCapsConfig::default(),
with_sysroot: true,
publish_diagnostics: true, publish_diagnostics: true,
diagnostics: DiagnosticsConfig::default(), diagnostics: DiagnosticsConfig::default(),
diagnostics_map: DiagnosticsMapConfig::default(), diagnostics_map: DiagnosticsMapConfig::default(),
@ -209,7 +207,6 @@ impl Config {
let data = ConfigData::from_json(json); let data = ConfigData::from_json(json);
self.with_sysroot = data.withSysroot;
self.publish_diagnostics = data.diagnostics_enable; self.publish_diagnostics = data.diagnostics_enable;
self.diagnostics = DiagnosticsConfig { self.diagnostics = DiagnosticsConfig {
disable_experimental: !data.diagnostics_enableExperimental, disable_experimental: !data.diagnostics_enableExperimental,
@ -246,6 +243,7 @@ impl Config {
load_out_dirs_from_check: data.cargo_loadOutDirsFromCheck, load_out_dirs_from_check: data.cargo_loadOutDirsFromCheck,
target: data.cargo_target.clone(), target: data.cargo_target.clone(),
rustc_source: rustc_source, rustc_source: rustc_source,
no_sysroot: data.cargo_noSysroot,
}; };
self.runnables = RunnablesConfig { self.runnables = RunnablesConfig {
override_cargo: data.runnables_overrideCargo, override_cargo: data.runnables_overrideCargo,
@ -492,6 +490,7 @@ config_data! {
cargo_loadOutDirsFromCheck: bool = false, cargo_loadOutDirsFromCheck: bool = false,
cargo_noDefaultFeatures: bool = false, cargo_noDefaultFeatures: bool = false,
cargo_target: Option<String> = None, cargo_target: Option<String> = None,
cargo_noSysroot: bool = false,
checkOnSave_enable: bool = true, checkOnSave_enable: bool = true,
checkOnSave_allFeatures: Option<bool> = None, checkOnSave_allFeatures: Option<bool> = None,
@ -544,7 +543,6 @@ config_data! {
rustfmt_extraArgs: Vec<String> = Vec::new(), rustfmt_extraArgs: Vec<String> = Vec::new(),
rustfmt_overrideCommand: Option<Vec<String>> = None, rustfmt_overrideCommand: Option<Vec<String>> = None,
withSysroot: bool = true,
rustcSource : Option<String> = None, rustcSource : Option<String> = None,
} }
} }

View file

@ -96,17 +96,12 @@ impl GlobalState {
self.task_pool.handle.spawn({ self.task_pool.handle.spawn({
let linked_projects = self.config.linked_projects.clone(); let linked_projects = self.config.linked_projects.clone();
let cargo_config = self.config.cargo.clone(); let cargo_config = self.config.cargo.clone();
let with_sysroot = self.config.with_sysroot.clone();
move || { move || {
let workspaces = linked_projects let workspaces = linked_projects
.iter() .iter()
.map(|project| match project { .map(|project| match project {
LinkedProject::ProjectManifest(manifest) => { LinkedProject::ProjectManifest(manifest) => {
project_model::ProjectWorkspace::load( project_model::ProjectWorkspace::load(manifest.clone(), &cargo_config)
manifest.clone(),
&cargo_config,
with_sysroot,
)
} }
LinkedProject::InlineJsonProject(it) => { LinkedProject::InlineJsonProject(it) => {
project_model::ProjectWorkspace::load_inline(it.clone()) project_model::ProjectWorkspace::load_inline(it.clone())

View file

@ -12,7 +12,7 @@ use lsp_types::{
notification::Exit, request::Shutdown, TextDocumentIdentifier, Url, WorkDoneProgress, notification::Exit, request::Shutdown, TextDocumentIdentifier, Url, WorkDoneProgress,
}; };
use lsp_types::{ProgressParams, ProgressParamsValue}; use lsp_types::{ProgressParams, ProgressParamsValue};
use project_model::ProjectManifest; use project_model::{CargoConfig, ProjectManifest};
use rust_analyzer::{ use rust_analyzer::{
config::{ClientCapsConfig, Config, FilesConfig, FilesWatcher, LinkedProject}, config::{ClientCapsConfig, Config, FilesConfig, FilesWatcher, LinkedProject},
main_loop, main_loop,
@ -47,8 +47,8 @@ impl<'a> Project<'a> {
self self
} }
pub(crate) fn with_sysroot(mut self, sysroot: bool) -> Project<'a> { pub(crate) fn with_sysroot(mut self, yes: bool) -> Project<'a> {
self.with_sysroot = sysroot; self.with_sysroot = yes;
self self
} }
@ -90,7 +90,7 @@ impl<'a> Project<'a> {
work_done_progress: true, work_done_progress: true,
..Default::default() ..Default::default()
}, },
with_sysroot: self.with_sysroot, cargo: CargoConfig { no_sysroot: !self.with_sysroot, ..Default::default() },
linked_projects, linked_projects,
files: FilesConfig { watcher: FilesWatcher::Client, exclude: Vec::new() }, files: FilesConfig { watcher: FilesWatcher::Client, exclude: Vec::new() },
..Config::new(tmp_dir_path) ..Config::new(tmp_dir_path)

View file

@ -283,6 +283,11 @@
"default": null, "default": null,
"description": "Specify the compilation target" "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": { "rust-analyzer.rustfmt.extraArgs": {
"type": "array", "type": "array",
"items": { "items": {
@ -605,11 +610,6 @@
}, },
"default": null "default": null
}, },
"rust-analyzer.withSysroot": {
"markdownDescription": "Internal config for debugging, disables loading of sysroot crates",
"type": "boolean",
"default": true
},
"rust-analyzer.diagnostics.enable": { "rust-analyzer.diagnostics.enable": {
"type": "boolean", "type": "boolean",
"default": true, "default": true,