Skip to content

Commit

Permalink
Auto merge of rust-lang#12599 - flodiebold:no-test-deps, r=flodiebold
Browse files Browse the repository at this point in the history
fix: Only apply `cfg(test)` for local crates

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.
  • Loading branch information
bors committed Jun 20, 2022
2 parents 439a513 + 07d78b6 commit 312ac83
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
2 changes: 0 additions & 2 deletions crates/project-model/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,6 @@ fn cargo_hello_world_project_model() {
"debug_assertions",
"feature=default",
"feature=std",
"test",
],
),
potential_cfg_options: CfgOptions(
Expand All @@ -1054,7 +1053,6 @@ fn cargo_hello_world_project_model() {
"feature=rustc-dep-of-std",
"feature=std",
"feature=use_std",
"test",
],
),
env: Env {
Expand Down
41 changes: 19 additions & 22 deletions crates/project-model/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,23 +541,25 @@ 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();
// Does any crate signal to rust-analyzer that they need the rustc_private crates?
let mut has_private = false;
// Next, create crates for each package, target pair
for pkg in cargo.packages() {
let mut cfg_options = &cfg_options;
let mut replaced_cfg_options;
let mut cfg_options = cfg_options.clone();

let overrides = match override_cfg {
CfgOverrides::Wildcard(cfg_diff) => Some(cfg_diff),
CfgOverrides::Selective(cfg_overrides) => cfg_overrides.get(&cargo[pkg].name),
};

// Add test cfg for local crates
if cargo[pkg].is_local {
cfg_options.insert_atom("test".into());
}

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
Expand All @@ -566,9 +568,7 @@ fn cargo_to_crate_graph(
// A more ideal solution might be to reanalyze crates based on where the cursor is and
// figure out the set of cfgs that would have to apply to make it active.

replaced_cfg_options = cfg_options.clone();
replaced_cfg_options.apply_diff(overrides.clone());
cfg_options = &replaced_cfg_options;
cfg_options.apply_diff(overrides.clone());
};

has_private |= cargo[pkg].metadata.rustc_private;
Expand All @@ -588,7 +588,7 @@ fn cargo_to_crate_graph(
&mut crate_graph,
&cargo[pkg],
build_scripts.get_output(pkg),
cfg_options,
cfg_options.clone(),
&mut |path| load_proc_macro(&cargo[tgt].name, path),
file_id,
&cargo[tgt].name,
Expand Down Expand Up @@ -753,8 +753,7 @@ fn handle_rustc_crates(
queue.push_back(dep.pkg);
}

let mut cfg_options = cfg_options;
let mut replaced_cfg_options;
let mut cfg_options = cfg_options.clone();

let overrides = match override_cfg {
CfgOverrides::Wildcard(cfg_diff) => Some(cfg_diff),
Expand All @@ -771,9 +770,7 @@ fn handle_rustc_crates(
// A more ideal solution might be to reanalyze crates based on where the cursor is and
// figure out the set of cfgs that would have to apply to make it active.

replaced_cfg_options = cfg_options.clone();
replaced_cfg_options.apply_diff(overrides.clone());
cfg_options = &replaced_cfg_options;
cfg_options.apply_diff(overrides.clone());
};

for &tgt in rustc_workspace[pkg].targets.iter() {
Expand All @@ -785,7 +782,7 @@ fn handle_rustc_crates(
crate_graph,
&rustc_workspace[pkg],
build_scripts.get_output(pkg),
cfg_options,
cfg_options.clone(),
&mut |path| load_proc_macro(&rustc_workspace[tgt].name, path),
file_id,
&rustc_workspace[tgt].name,
Expand Down Expand Up @@ -840,15 +837,21 @@ fn add_target_crate_root(
crate_graph: &mut CrateGraph,
pkg: &PackageData,
build_data: Option<&BuildScriptOutput>,
cfg_options: &CfgOptions,
cfg_options: CfgOptions,
load_proc_macro: &mut dyn FnMut(&AbsPath) -> ProcMacroLoadResult,
file_id: FileId,
cargo_name: &str,
is_proc_macro: bool,
) -> CrateId {
let edition = pkg.edition;
let mut potential_cfg_options = cfg_options.clone();
potential_cfg_options.extend(
pkg.features
.iter()
.map(|feat| CfgFlag::KeyValue { key: "feature".into(), value: feat.0.into() }),
);
let cfg_options = {
let mut opts = cfg_options.clone();
let mut opts = cfg_options;
for feature in pkg.active_features.iter() {
opts.insert_key_value("feature".into(), feature.into());
}
Expand All @@ -873,12 +876,6 @@ fn add_target_crate_root(
};

let display_name = CrateDisplayName::from_canonical_name(cargo_name.to_string());
let mut potential_cfg_options = cfg_options.clone();
potential_cfg_options.extend(
pkg.features
.iter()
.map(|feat| CfgFlag::KeyValue { key: "feature".into(), value: feat.0.into() }),
);
crate_graph.add_crate_root(
file_id,
edition,
Expand Down

0 comments on commit 312ac83

Please sign in to comment.