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>
This commit is contained in:
bors[bot] 2020-06-08 16:20:45 +00:00 committed by GitHub
commit 38ac331f7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 40 deletions

View file

@ -8,8 +8,7 @@ use crossbeam_channel::{unbounded, Receiver};
use ra_db::{ExternSourceId, FileId, SourceRootId};
use ra_ide::{AnalysisChange, AnalysisHost};
use ra_project_model::{
get_rustc_cfg_options, CargoConfig, PackageRoot, ProcMacroClient, ProjectManifest,
ProjectWorkspace,
CargoConfig, PackageRoot, ProcMacroClient, ProjectManifest, ProjectWorkspace,
};
use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch};
use rustc_hash::{FxHashMap, FxHashSet};
@ -148,26 +147,14 @@ pub(crate) fn load(
}
}
// FIXME: cfg options?
let default_cfg_options = {
let mut opts = get_rustc_cfg_options(None);
opts.insert_atom("test".into());
opts.insert_atom("debug_assertion".into());
opts
};
let crate_graph = ws.to_crate_graph(
&default_cfg_options,
&extern_source_roots,
proc_macro_client,
&mut |path: &Path| {
let crate_graph =
ws.to_crate_graph(None, &extern_source_roots, proc_macro_client, &mut |path: &Path| {
// Some path from metadata will be non canonicalized, e.g. /foo/../bar/lib.rs
let path = path.canonicalize().ok()?;
let vfs_file = vfs.load(&path);
log::debug!("vfs file {:?} -> {:?}", path, vfs_file);
vfs_file.map(vfs_file_to_id)
},
);
});
log::debug!("crate graph: {:?}", crate_graph);
analysis_change.set_crate_graph(crate_graph);

View file

@ -15,7 +15,7 @@ use ra_flycheck::{Flycheck, FlycheckConfig};
use ra_ide::{
Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId,
};
use ra_project_model::{get_rustc_cfg_options, ProcMacroClient, ProjectWorkspace};
use ra_project_model::{ProcMacroClient, ProjectWorkspace};
use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsRoot, VfsTask, Watch};
use relative_path::RelativePathBuf;
use stdx::format_to;
@ -135,14 +135,6 @@ impl GlobalState {
}
}
// FIXME: Read default cfgs from config
let default_cfg_options = {
let mut opts = get_rustc_cfg_options(config.cargo.target.as_ref());
opts.insert_atom("test".into());
opts.insert_atom("debug_assertion".into());
opts
};
let proc_macro_client = match &config.proc_macro_srv {
None => ProcMacroClient::dummy(),
Some((path, args)) => match ProcMacroClient::extern_process(path.into(), args) {
@ -168,7 +160,7 @@ impl GlobalState {
};
for ws in workspaces.iter() {
crate_graph.extend(ws.to_crate_graph(
&default_cfg_options,
config.cargo.target.as_deref(),
&extern_source_roots,
&proc_macro_client,
&mut load,