]> git.lizzy.rs Git - rust.git/commitdiff
Only apply `cfg(test)` for local crates
authorFlorian Diebold <flodiebold@gmail.com>
Mon, 20 Jun 2022 18:10:25 +0000 (20:10 +0200)
committerFlorian Diebold <flodiebold@gmail.com>
Mon, 20 Jun 2022 18:10:25 +0000 (20:10 +0200)
Don't analyze dependencies with `test`; this should fix various cases
where crates use `cfg(not(test))` and so we didn't find things.

"Local" here currently means anything that's not from the registry, so
anything inside the workspace, but also path dependencies. So this isn't
perfect, and users might still need to use
`rust-analyzer.cargo.unsetTest` for these in some cases.

crates/project-model/src/workspace.rs

index 8982a9904ec1754d299631335efe176d681f28c0..052d8fee255306b88a72fe0a7a5804954054fbdb 100644 (file)
@@ -541,8 +541,6 @@ fn cargo_to_crate_graph(
 
     let mut pkg_to_lib_crate = FxHashMap::default();
 
-    // Add test cfg for non-sysroot crates
-    cfg_options.insert_atom("test".into());
     cfg_options.insert_atom("debug_assertions".into());
 
     let mut pkg_crates = FxHashMap::default();
@@ -558,6 +556,13 @@ fn cargo_to_crate_graph(
             CfgOverrides::Selective(cfg_overrides) => cfg_overrides.get(&cargo[pkg].name),
         };
 
+        // Add test cfg for local crates
+        if cargo[pkg].is_local {
+            replaced_cfg_options = cfg_options.clone();
+            replaced_cfg_options.insert_atom("test".into());
+            cfg_options = &replaced_cfg_options;
+        }
+
         if let Some(overrides) = overrides {
             // FIXME: this is sort of a hack to deal with #![cfg(not(test))] vanishing such as seen
             // in ed25519_dalek (#7243), and libcore (#9203) (although you only hit that one while