aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-06-08 17:20:45 +0100
committerGitHub <[email protected]>2020-06-08 17:20:45 +0100
commit38ac331f7dea94581ec687167c92c65b10daf38d (patch)
tree7f9cc7d2d7b6dfa363f5a0d310108bad22621291
parent5ed9818a7c855bf914e91324e305f24e4e743057 (diff)
parentdbceaf522b717bd0dda89f6af1684ebd8e033aee (diff)
Merge #4784
4784: Change management of test cfg to better support json projects r=Nashenas88 a=Nashenas88 This helps support json projects where they can decide whether to add the `test` cfg or not. One alternative is to add support for marking json project crates as a sysroot crate, and adding logic to remove the `test` cfg in those cases. In my opinion, that option gives less flexibility to json projects and leads to more functionality that needs to be maintained. Fixes #4508 cc @woody77 Co-authored-by: Paul Daniel Faria <[email protected]> Co-authored-by: Paul Daniel Faria <[email protected]>
-rw-r--r--crates/ra_project_model/src/lib.rs27
-rw-r--r--crates/rust-analyzer/src/cli/load_cargo.rs21
-rw-r--r--crates/rust-analyzer/src/global_state.rs12
3 files changed, 20 insertions, 40 deletions
diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs
index 7ad941279..fe03b509e 100644
--- a/crates/ra_project_model/src/lib.rs
+++ b/crates/ra_project_model/src/lib.rs
@@ -250,7 +250,7 @@ impl ProjectWorkspace {
250 250
251 pub fn to_crate_graph( 251 pub fn to_crate_graph(
252 &self, 252 &self,
253 default_cfg_options: &CfgOptions, 253 target: Option<&str>,
254 extern_source_roots: &FxHashMap<PathBuf, ExternSourceId>, 254 extern_source_roots: &FxHashMap<PathBuf, ExternSourceId>,
255 proc_macro_client: &ProcMacroClient, 255 proc_macro_client: &ProcMacroClient,
256 load: &mut dyn FnMut(&Path) -> Option<FileId>, 256 load: &mut dyn FnMut(&Path) -> Option<FileId>,
@@ -269,7 +269,7 @@ impl ProjectWorkspace {
269 json_project::Edition::Edition2018 => Edition::Edition2018, 269 json_project::Edition::Edition2018 => Edition::Edition2018,
270 }; 270 };
271 let cfg_options = { 271 let cfg_options = {
272 let mut opts = default_cfg_options.clone(); 272 let mut opts = CfgOptions::default();
273 for cfg in &krate.cfg { 273 for cfg in &krate.cfg {
274 match cfg.find('=') { 274 match cfg.find('=') {
275 None => opts.insert_atom(cfg.into()), 275 None => opts.insert_atom(cfg.into()),
@@ -343,18 +343,13 @@ impl ProjectWorkspace {
343 } 343 }
344 } 344 }
345 ProjectWorkspace::Cargo { cargo, sysroot } => { 345 ProjectWorkspace::Cargo { cargo, sysroot } => {
346 let mut cfg_options = get_rustc_cfg_options(target);
347
346 let sysroot_crates: FxHashMap<_, _> = sysroot 348 let sysroot_crates: FxHashMap<_, _> = sysroot
347 .crates() 349 .crates()
348 .filter_map(|krate| { 350 .filter_map(|krate| {
349 let file_id = load(&sysroot[krate].root)?; 351 let file_id = load(&sysroot[krate].root)?;
350 352
351 // Crates from sysroot have `cfg(test)` disabled
352 let cfg_options = {
353 let mut opts = default_cfg_options.clone();
354 opts.remove_atom("test");
355 opts
356 };
357
358 let env = Env::default(); 353 let env = Env::default();
359 let extern_source = ExternSource::default(); 354 let extern_source = ExternSource::default();
360 let proc_macro = vec![]; 355 let proc_macro = vec![];
@@ -365,7 +360,7 @@ impl ProjectWorkspace {
365 file_id, 360 file_id,
366 Edition::Edition2018, 361 Edition::Edition2018,
367 Some(crate_name), 362 Some(crate_name),
368 cfg_options, 363 cfg_options.clone(),
369 env, 364 env,
370 extern_source, 365 extern_source,
371 proc_macro, 366 proc_macro,
@@ -396,6 +391,10 @@ impl ProjectWorkspace {
396 391
397 let mut pkg_to_lib_crate = FxHashMap::default(); 392 let mut pkg_to_lib_crate = FxHashMap::default();
398 let mut pkg_crates = FxHashMap::default(); 393 let mut pkg_crates = FxHashMap::default();
394
395 // Add test cfg for non-sysroot crates
396 cfg_options.insert_atom("test".into());
397
399 // Next, create crates for each package, target pair 398 // Next, create crates for each package, target pair
400 for pkg in cargo.packages() { 399 for pkg in cargo.packages() {
401 let mut lib_tgt = None; 400 let mut lib_tgt = None;
@@ -404,7 +403,7 @@ impl ProjectWorkspace {
404 if let Some(file_id) = load(root) { 403 if let Some(file_id) = load(root) {
405 let edition = cargo[pkg].edition; 404 let edition = cargo[pkg].edition;
406 let cfg_options = { 405 let cfg_options = {
407 let mut opts = default_cfg_options.clone(); 406 let mut opts = cfg_options.clone();
408 for feature in cargo[pkg].features.iter() { 407 for feature in cargo[pkg].features.iter() {
409 opts.insert_key_value("feature".into(), feature.into()); 408 opts.insert_key_value("feature".into(), feature.into());
410 } 409 }
@@ -561,7 +560,7 @@ impl ProjectWorkspace {
561 } 560 }
562} 561}
563 562
564pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions { 563fn get_rustc_cfg_options(target: Option<&str>) -> CfgOptions {
565 let mut cfg_options = CfgOptions::default(); 564 let mut cfg_options = CfgOptions::default();
566 565
567 // Some nightly-only cfgs, which are required for stdlib 566 // Some nightly-only cfgs, which are required for stdlib
@@ -579,7 +578,7 @@ pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions {
579 let mut cmd = Command::new(ra_toolchain::rustc()); 578 let mut cmd = Command::new(ra_toolchain::rustc());
580 cmd.args(&["--print", "cfg", "-O"]); 579 cmd.args(&["--print", "cfg", "-O"]);
581 if let Some(target) = target { 580 if let Some(target) = target {
582 cmd.args(&["--target", target.as_str()]); 581 cmd.args(&["--target", target]);
583 } 582 }
584 let output = output(cmd)?; 583 let output = output(cmd)?;
585 Ok(String::from_utf8(output.stdout)?) 584 Ok(String::from_utf8(output.stdout)?)
@@ -601,6 +600,8 @@ pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions {
601 Err(e) => log::error!("failed to get rustc cfgs: {:#}", e), 600 Err(e) => log::error!("failed to get rustc cfgs: {:#}", e),
602 } 601 }
603 602
603 cfg_options.insert_atom("debug_assertion".into());
604
604 cfg_options 605 cfg_options
605} 606}
606 607
diff --git a/crates/rust-analyzer/src/cli/load_cargo.rs b/crates/rust-analyzer/src/cli/load_cargo.rs
index c7e86fe0c..8f2aeac77 100644
--- a/crates/rust-analyzer/src/cli/load_cargo.rs
+++ b/crates/rust-analyzer/src/cli/load_cargo.rs
@@ -8,8 +8,7 @@ use crossbeam_channel::{unbounded, Receiver};
8use ra_db::{ExternSourceId, FileId, SourceRootId}; 8use ra_db::{ExternSourceId, FileId, SourceRootId};
9use ra_ide::{AnalysisChange, AnalysisHost}; 9use ra_ide::{AnalysisChange, AnalysisHost};
10use ra_project_model::{ 10use ra_project_model::{
11 get_rustc_cfg_options, CargoConfig, PackageRoot, ProcMacroClient, ProjectManifest, 11 CargoConfig, PackageRoot, ProcMacroClient, ProjectManifest, ProjectWorkspace,
12 ProjectWorkspace,
13}; 12};
14use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch}; 13use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch};
15use rustc_hash::{FxHashMap, FxHashSet}; 14use rustc_hash::{FxHashMap, FxHashSet};
@@ -148,26 +147,14 @@ pub(crate) fn load(
148 } 147 }
149 } 148 }
150 149
151 // FIXME: cfg options? 150 let crate_graph =
152 let default_cfg_options = { 151 ws.to_crate_graph(None, &extern_source_roots, proc_macro_client, &mut |path: &Path| {
153 let mut opts = get_rustc_cfg_options(None);
154 opts.insert_atom("test".into());
155 opts.insert_atom("debug_assertion".into());
156 opts
157 };
158
159 let crate_graph = ws.to_crate_graph(
160 &default_cfg_options,
161 &extern_source_roots,
162 proc_macro_client,
163 &mut |path: &Path| {
164 // Some path from metadata will be non canonicalized, e.g. /foo/../bar/lib.rs 152 // Some path from metadata will be non canonicalized, e.g. /foo/../bar/lib.rs
165 let path = path.canonicalize().ok()?; 153 let path = path.canonicalize().ok()?;
166 let vfs_file = vfs.load(&path); 154 let vfs_file = vfs.load(&path);
167 log::debug!("vfs file {:?} -> {:?}", path, vfs_file); 155 log::debug!("vfs file {:?} -> {:?}", path, vfs_file);
168 vfs_file.map(vfs_file_to_id) 156 vfs_file.map(vfs_file_to_id)
169 }, 157 });
170 );
171 log::debug!("crate graph: {:?}", crate_graph); 158 log::debug!("crate graph: {:?}", crate_graph);
172 analysis_change.set_crate_graph(crate_graph); 159 analysis_change.set_crate_graph(crate_graph);
173 160
diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs
index 0bebb5bf6..73b0f881d 100644
--- a/crates/rust-analyzer/src/global_state.rs
+++ b/crates/rust-analyzer/src/global_state.rs
@@ -15,7 +15,7 @@ use ra_flycheck::{Flycheck, FlycheckConfig};
15use ra_ide::{ 15use ra_ide::{
16 Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId, 16 Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId,
17}; 17};
18use ra_project_model::{get_rustc_cfg_options, ProcMacroClient, ProjectWorkspace}; 18use ra_project_model::{ProcMacroClient, ProjectWorkspace};
19use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsRoot, VfsTask, Watch}; 19use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsRoot, VfsTask, Watch};
20use relative_path::RelativePathBuf; 20use relative_path::RelativePathBuf;
21use stdx::format_to; 21use stdx::format_to;
@@ -135,14 +135,6 @@ impl GlobalState {
135 } 135 }
136 } 136 }
137 137
138 // FIXME: Read default cfgs from config
139 let default_cfg_options = {
140 let mut opts = get_rustc_cfg_options(config.cargo.target.as_ref());
141 opts.insert_atom("test".into());
142 opts.insert_atom("debug_assertion".into());
143 opts
144 };
145
146 let proc_macro_client = match &config.proc_macro_srv { 138 let proc_macro_client = match &config.proc_macro_srv {
147 None => ProcMacroClient::dummy(), 139 None => ProcMacroClient::dummy(),
148 Some((path, args)) => match ProcMacroClient::extern_process(path.into(), args) { 140 Some((path, args)) => match ProcMacroClient::extern_process(path.into(), args) {
@@ -168,7 +160,7 @@ impl GlobalState {
168 }; 160 };
169 for ws in workspaces.iter() { 161 for ws in workspaces.iter() {
170 crate_graph.extend(ws.to_crate_graph( 162 crate_graph.extend(ws.to_crate_graph(
171 &default_cfg_options, 163 config.cargo.target.as_deref(),
172 &extern_source_roots, 164 &extern_source_roots,
173 &proc_macro_client, 165 &proc_macro_client,
174 &mut load, 166 &mut load,