Skip to content

Commit 38ac331

Browse files
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 <Nashenas88@users.noreply.github.com> Co-authored-by: Paul Daniel Faria <nashenas88@users.noreply.github.com>
2 parents 5ed9818 + dbceaf5 commit 38ac331

File tree

3 files changed

+20
-40
lines changed

3 files changed

+20
-40
lines changed

crates/ra_project_model/src/lib.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ impl ProjectWorkspace {
250250

251251
pub fn to_crate_graph(
252252
&self,
253-
default_cfg_options: &CfgOptions,
253+
target: Option<&str>,
254254
extern_source_roots: &FxHashMap<PathBuf, ExternSourceId>,
255255
proc_macro_client: &ProcMacroClient,
256256
load: &mut dyn FnMut(&Path) -> Option<FileId>,
@@ -269,7 +269,7 @@ impl ProjectWorkspace {
269269
json_project::Edition::Edition2018 => Edition::Edition2018,
270270
};
271271
let cfg_options = {
272-
let mut opts = default_cfg_options.clone();
272+
let mut opts = CfgOptions::default();
273273
for cfg in &krate.cfg {
274274
match cfg.find('=') {
275275
None => opts.insert_atom(cfg.into()),
@@ -343,18 +343,13 @@ impl ProjectWorkspace {
343343
}
344344
}
345345
ProjectWorkspace::Cargo { cargo, sysroot } => {
346+
let mut cfg_options = get_rustc_cfg_options(target);
347+
346348
let sysroot_crates: FxHashMap<_, _> = sysroot
347349
.crates()
348350
.filter_map(|krate| {
349351
let file_id = load(&sysroot[krate].root)?;
350352

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-
358353
let env = Env::default();
359354
let extern_source = ExternSource::default();
360355
let proc_macro = vec![];
@@ -365,7 +360,7 @@ impl ProjectWorkspace {
365360
file_id,
366361
Edition::Edition2018,
367362
Some(crate_name),
368-
cfg_options,
363+
cfg_options.clone(),
369364
env,
370365
extern_source,
371366
proc_macro,
@@ -396,6 +391,10 @@ impl ProjectWorkspace {
396391

397392
let mut pkg_to_lib_crate = FxHashMap::default();
398393
let mut pkg_crates = FxHashMap::default();
394+
395+
// Add test cfg for non-sysroot crates
396+
cfg_options.insert_atom("test".into());
397+
399398
// Next, create crates for each package, target pair
400399
for pkg in cargo.packages() {
401400
let mut lib_tgt = None;
@@ -404,7 +403,7 @@ impl ProjectWorkspace {
404403
if let Some(file_id) = load(root) {
405404
let edition = cargo[pkg].edition;
406405
let cfg_options = {
407-
let mut opts = default_cfg_options.clone();
406+
let mut opts = cfg_options.clone();
408407
for feature in cargo[pkg].features.iter() {
409408
opts.insert_key_value("feature".into(), feature.into());
410409
}
@@ -561,7 +560,7 @@ impl ProjectWorkspace {
561560
}
562561
}
563562

564-
pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions {
563+
fn get_rustc_cfg_options(target: Option<&str>) -> CfgOptions {
565564
let mut cfg_options = CfgOptions::default();
566565

567566
// Some nightly-only cfgs, which are required for stdlib
@@ -579,7 +578,7 @@ pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions {
579578
let mut cmd = Command::new(ra_toolchain::rustc());
580579
cmd.args(&["--print", "cfg", "-O"]);
581580
if let Some(target) = target {
582-
cmd.args(&["--target", target.as_str()]);
581+
cmd.args(&["--target", target]);
583582
}
584583
let output = output(cmd)?;
585584
Ok(String::from_utf8(output.stdout)?)
@@ -601,6 +600,8 @@ pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions {
601600
Err(e) => log::error!("failed to get rustc cfgs: {:#}", e),
602601
}
603602

603+
cfg_options.insert_atom("debug_assertion".into());
604+
604605
cfg_options
605606
}
606607

crates/rust-analyzer/src/cli/load_cargo.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ use crossbeam_channel::{unbounded, Receiver};
88
use ra_db::{ExternSourceId, FileId, SourceRootId};
99
use ra_ide::{AnalysisChange, AnalysisHost};
1010
use ra_project_model::{
11-
get_rustc_cfg_options, CargoConfig, PackageRoot, ProcMacroClient, ProjectManifest,
12-
ProjectWorkspace,
11+
CargoConfig, PackageRoot, ProcMacroClient, ProjectManifest, ProjectWorkspace,
1312
};
1413
use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch};
1514
use rustc_hash::{FxHashMap, FxHashSet};
@@ -148,26 +147,14 @@ pub(crate) fn load(
148147
}
149148
}
150149

151-
// FIXME: cfg options?
152-
let default_cfg_options = {
153-
let mut opts = get_rustc_cfg_options(None);
154-
opts.insert_atom("test".into());
155-
opts.insert_atom("debug_assertion".into());
156-
opts
157-
};
158-
159-
let crate_graph = ws.to_crate_graph(
160-
&default_cfg_options,
161-
&extern_source_roots,
162-
proc_macro_client,
163-
&mut |path: &Path| {
150+
let crate_graph =
151+
ws.to_crate_graph(None, &extern_source_roots, proc_macro_client, &mut |path: &Path| {
164152
// Some path from metadata will be non canonicalized, e.g. /foo/../bar/lib.rs
165153
let path = path.canonicalize().ok()?;
166154
let vfs_file = vfs.load(&path);
167155
log::debug!("vfs file {:?} -> {:?}", path, vfs_file);
168156
vfs_file.map(vfs_file_to_id)
169-
},
170-
);
157+
});
171158
log::debug!("crate graph: {:?}", crate_graph);
172159
analysis_change.set_crate_graph(crate_graph);
173160

crates/rust-analyzer/src/global_state.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use ra_flycheck::{Flycheck, FlycheckConfig};
1515
use ra_ide::{
1616
Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId,
1717
};
18-
use ra_project_model::{get_rustc_cfg_options, ProcMacroClient, ProjectWorkspace};
18+
use ra_project_model::{ProcMacroClient, ProjectWorkspace};
1919
use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsRoot, VfsTask, Watch};
2020
use relative_path::RelativePathBuf;
2121
use stdx::format_to;
@@ -135,14 +135,6 @@ impl GlobalState {
135135
}
136136
}
137137

138-
// FIXME: Read default cfgs from config
139-
let default_cfg_options = {
140-
let mut opts = get_rustc_cfg_options(config.cargo.target.as_ref());
141-
opts.insert_atom("test".into());
142-
opts.insert_atom("debug_assertion".into());
143-
opts
144-
};
145-
146138
let proc_macro_client = match &config.proc_macro_srv {
147139
None => ProcMacroClient::dummy(),
148140
Some((path, args)) => match ProcMacroClient::extern_process(path.into(), args) {
@@ -168,7 +160,7 @@ impl GlobalState {
168160
};
169161
for ws in workspaces.iter() {
170162
crate_graph.extend(ws.to_crate_graph(
171-
&default_cfg_options,
163+
config.cargo.target.as_deref(),
172164
&extern_source_roots,
173165
&proc_macro_client,
174166
&mut load,

0 commit comments

Comments
 (0)