Skip to content

fix: Only apply cfg(test) for local crates #12599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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