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
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
pub rustc_source: Option<AbsPathBuf>,
}
@ -140,27 +144,27 @@ impl PackageData {
impl CargoWorkspace {
pub fn from_cargo_metadata(
cargo_toml: &AbsPath,
cargo_features: &CargoConfig,
config: &CargoConfig,
) -> Result<CargoWorkspace> {
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;

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 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<ProjectWorkspace> {
pub fn load(manifest: ProjectManifest, config: &CargoConfig) -> Result<ProjectWorkspace> {
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
};

View File

@ -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();

View File

@ -49,7 +49,6 @@ pub struct Config {
pub hover: HoverConfig,
pub semantic_tokens_refresh: bool,
pub with_sysroot: bool,
pub linked_projects: Vec<LinkedProject>,
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<String> = None,
cargo_noSysroot: bool = false,
checkOnSave_enable: bool = true,
checkOnSave_allFeatures: Option<bool> = None,
@ -544,7 +543,6 @@ config_data! {
rustfmt_extraArgs: Vec<String> = Vec::new(),
rustfmt_overrideCommand: Option<Vec<String>> = None,
withSysroot: bool = true,
rustcSource : Option<String> = None,
}
}

View File

@ -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())

View File

@ -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)

View File

@ -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,