aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_project_model/src/lib.rs
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 /crates/ra_project_model/src/lib.rs
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]>
Diffstat (limited to 'crates/ra_project_model/src/lib.rs')
-rw-r--r--crates/ra_project_model/src/lib.rs27
1 files changed, 14 insertions, 13 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