Auto merge of #12599 - flodiebold:no-test-deps, r=flodiebold
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.
This commit is contained in:
commit
312ac83caf
@ -1041,7 +1041,6 @@ fn cargo_hello_world_project_model() {
|
||||
"debug_assertions",
|
||||
"feature=default",
|
||||
"feature=std",
|
||||
"test",
|
||||
],
|
||||
),
|
||||
potential_cfg_options: CfgOptions(
|
||||
@ -1054,7 +1053,6 @@ fn cargo_hello_world_project_model() {
|
||||
"feature=rustc-dep-of-std",
|
||||
"feature=std",
|
||||
"feature=use_std",
|
||||
"test",
|
||||
],
|
||||
),
|
||||
env: Env {
|
||||
|
@ -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();
|
||||
@ -550,14 +548,18 @@ fn cargo_to_crate_graph(
|
||||
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
|
||||
@ -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;
|
||||
@ -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,
|
||||
@ -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),
|
||||
@ -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() {
|
||||
@ -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,
|
||||
@ -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());
|
||||
}
|
||||
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user