From 4dfda64b39cc47ac75280461f46e121958990fca Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 13 Nov 2020 17:38:26 +0100 Subject: Cleanup workspace loading a tiny bit --- crates/project_model/src/cargo_workspace.rs | 20 ++++++++++------- crates/project_model/src/workspace.rs | 34 ++++++++++++++--------------- 2 files changed, 28 insertions(+), 26 deletions(-) (limited to 'crates/project_model') diff --git a/crates/project_model/src/cargo_workspace.rs b/crates/project_model/src/cargo_workspace.rs index 608a031d4..540b57ae4 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 43ea351d1..8a1a60e0e 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 }; -- cgit v1.2.3