Provide an config option to not set cfg(test)

This commit is contained in:
Chayim Refael Friedman 2024-09-09 23:24:34 +03:00
parent 546339a7be
commit 4ea09dd9f6
16 changed files with 73 additions and 15 deletions

View file

@ -30,6 +30,7 @@ pub struct LoadCargoConfig {
pub load_out_dirs_from_check: bool, pub load_out_dirs_from_check: bool,
pub with_proc_macro_server: ProcMacroServerChoice, pub with_proc_macro_server: ProcMacroServerChoice,
pub prefill_caches: bool, pub prefill_caches: bool,
pub set_test: bool,
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@ -99,6 +100,7 @@ pub fn load_workspace(
vfs.file_id(&path) vfs.file_id(&path)
}, },
extra_env, extra_env,
load_config.set_test,
); );
let proc_macros = { let proc_macros = {
let proc_macro_server = match &proc_macro_server { let proc_macro_server = match &proc_macro_server {
@ -531,6 +533,7 @@ mod tests {
load_out_dirs_from_check: false, load_out_dirs_from_check: false,
with_proc_macro_server: ProcMacroServerChoice::None, with_proc_macro_server: ProcMacroServerChoice::None,
prefill_caches: false, prefill_caches: false,
set_test: true,
}; };
let (db, _vfs, _proc_macro) = let (db, _vfs, _proc_macro) =
load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {}).unwrap(); load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {}).unwrap();

View file

@ -136,6 +136,7 @@ fn to_crate_graph(project_workspace: ProjectWorkspace) -> (CrateGraph, ProcMacro
} }
}, },
&Default::default(), &Default::default(),
true,
) )
} }
@ -257,5 +258,6 @@ fn smoke_test_real_sysroot_cargo() {
} }
}, },
&Default::default(), &Default::default(),
true,
); );
} }

View file

@ -728,6 +728,7 @@ impl ProjectWorkspace {
&self, &self,
load: FileLoader<'_>, load: FileLoader<'_>,
extra_env: &FxHashMap<String, String>, extra_env: &FxHashMap<String, String>,
set_test: bool,
) -> (CrateGraph, ProcMacroPaths) { ) -> (CrateGraph, ProcMacroPaths) {
let _p = tracing::info_span!("ProjectWorkspace::to_crate_graph").entered(); let _p = tracing::info_span!("ProjectWorkspace::to_crate_graph").entered();
@ -741,6 +742,7 @@ impl ProjectWorkspace {
sysroot, sysroot,
extra_env, extra_env,
cfg_overrides, cfg_overrides,
set_test,
), ),
sysroot, sysroot,
), ),
@ -759,6 +761,7 @@ impl ProjectWorkspace {
rustc_cfg.clone(), rustc_cfg.clone(),
cfg_overrides, cfg_overrides,
build_scripts, build_scripts,
set_test,
), ),
sysroot, sysroot,
), ),
@ -772,6 +775,7 @@ impl ProjectWorkspace {
rustc_cfg.clone(), rustc_cfg.clone(),
cfg_overrides, cfg_overrides,
build_scripts, build_scripts,
set_test,
) )
} else { } else {
detached_file_to_crate_graph( detached_file_to_crate_graph(
@ -780,6 +784,7 @@ impl ProjectWorkspace {
file, file,
sysroot, sysroot,
cfg_overrides, cfg_overrides,
set_test,
) )
}, },
sysroot, sysroot,
@ -870,11 +875,12 @@ fn project_json_to_crate_graph(
sysroot: &Sysroot, sysroot: &Sysroot,
extra_env: &FxHashMap<String, String>, extra_env: &FxHashMap<String, String>,
override_cfg: &CfgOverrides, override_cfg: &CfgOverrides,
set_test: bool,
) -> (CrateGraph, ProcMacroPaths) { ) -> (CrateGraph, ProcMacroPaths) {
let mut res = (CrateGraph::default(), ProcMacroPaths::default()); let mut res = (CrateGraph::default(), ProcMacroPaths::default());
let (crate_graph, proc_macros) = &mut res; let (crate_graph, proc_macros) = &mut res;
let (public_deps, libproc_macro) = let (public_deps, libproc_macro) =
sysroot_to_crate_graph(crate_graph, sysroot, rustc_cfg.clone(), load); sysroot_to_crate_graph(crate_graph, sysroot, rustc_cfg.clone(), load, set_test);
let r_a_cfg_flag = CfgAtom::Flag(sym::rust_analyzer.clone()); let r_a_cfg_flag = CfgAtom::Flag(sym::rust_analyzer.clone());
let mut cfg_cache: FxHashMap<&str, Vec<CfgAtom>> = FxHashMap::default(); let mut cfg_cache: FxHashMap<&str, Vec<CfgAtom>> = FxHashMap::default();
@ -987,13 +993,14 @@ fn cargo_to_crate_graph(
rustc_cfg: Vec<CfgAtom>, rustc_cfg: Vec<CfgAtom>,
override_cfg: &CfgOverrides, override_cfg: &CfgOverrides,
build_scripts: &WorkspaceBuildScripts, build_scripts: &WorkspaceBuildScripts,
set_test: bool,
) -> (CrateGraph, ProcMacroPaths) { ) -> (CrateGraph, ProcMacroPaths) {
let _p = tracing::info_span!("cargo_to_crate_graph").entered(); let _p = tracing::info_span!("cargo_to_crate_graph").entered();
let mut res = (CrateGraph::default(), ProcMacroPaths::default()); let mut res = (CrateGraph::default(), ProcMacroPaths::default());
let crate_graph = &mut res.0; let crate_graph = &mut res.0;
let proc_macros = &mut res.1; let proc_macros = &mut res.1;
let (public_deps, libproc_macro) = let (public_deps, libproc_macro) =
sysroot_to_crate_graph(crate_graph, sysroot, rustc_cfg.clone(), load); sysroot_to_crate_graph(crate_graph, sysroot, rustc_cfg.clone(), load, set_test);
let cfg_options = CfgOptions::from_iter(rustc_cfg); let cfg_options = CfgOptions::from_iter(rustc_cfg);
@ -1011,8 +1018,10 @@ fn cargo_to_crate_graph(
let mut cfg_options = cfg_options.clone(); let mut cfg_options = cfg_options.clone();
if cargo[pkg].is_local { if cargo[pkg].is_local {
// Add test cfg for local crates if set_test {
cfg_options.insert_atom(sym::test.clone()); // Add test cfg for local crates
cfg_options.insert_atom(sym::test.clone());
}
cfg_options.insert_atom(sym::rust_analyzer.clone()); cfg_options.insert_atom(sym::rust_analyzer.clone());
} }
@ -1173,14 +1182,17 @@ fn detached_file_to_crate_graph(
detached_file: &ManifestPath, detached_file: &ManifestPath,
sysroot: &Sysroot, sysroot: &Sysroot,
override_cfg: &CfgOverrides, override_cfg: &CfgOverrides,
set_test: bool,
) -> (CrateGraph, ProcMacroPaths) { ) -> (CrateGraph, ProcMacroPaths) {
let _p = tracing::info_span!("detached_file_to_crate_graph").entered(); let _p = tracing::info_span!("detached_file_to_crate_graph").entered();
let mut crate_graph = CrateGraph::default(); let mut crate_graph = CrateGraph::default();
let (public_deps, _libproc_macro) = let (public_deps, _libproc_macro) =
sysroot_to_crate_graph(&mut crate_graph, sysroot, rustc_cfg.clone(), load); sysroot_to_crate_graph(&mut crate_graph, sysroot, rustc_cfg.clone(), load, set_test);
let mut cfg_options = CfgOptions::from_iter(rustc_cfg); let mut cfg_options = CfgOptions::from_iter(rustc_cfg);
cfg_options.insert_atom(sym::test.clone()); if set_test {
cfg_options.insert_atom(sym::test.clone());
}
cfg_options.insert_atom(sym::rust_analyzer.clone()); cfg_options.insert_atom(sym::rust_analyzer.clone());
override_cfg.apply(&mut cfg_options, ""); override_cfg.apply(&mut cfg_options, "");
let cfg_options = Arc::new(cfg_options); let cfg_options = Arc::new(cfg_options);
@ -1404,6 +1416,7 @@ fn sysroot_to_crate_graph(
sysroot: &Sysroot, sysroot: &Sysroot,
rustc_cfg: Vec<CfgAtom>, rustc_cfg: Vec<CfgAtom>,
load: FileLoader<'_>, load: FileLoader<'_>,
set_test: bool,
) -> (SysrootPublicDeps, Option<CrateId>) { ) -> (SysrootPublicDeps, Option<CrateId>) {
let _p = tracing::info_span!("sysroot_to_crate_graph").entered(); let _p = tracing::info_span!("sysroot_to_crate_graph").entered();
match sysroot.mode() { match sysroot.mode() {
@ -1426,6 +1439,7 @@ fn sysroot_to_crate_graph(
..Default::default() ..Default::default()
}, },
&WorkspaceBuildScripts::default(), &WorkspaceBuildScripts::default(),
set_test,
); );
let mut pub_deps = vec![]; let mut pub_deps = vec![];

View file

@ -84,6 +84,7 @@ impl flags::AnalysisStats {
ProcMacroServerChoice::Sysroot ProcMacroServerChoice::Sysroot
}, },
prefill_caches: false, prefill_caches: false,
set_test: true,
}; };
let build_scripts_time = if self.disable_build_scripts { let build_scripts_time = if self.disable_build_scripts {

View file

@ -39,6 +39,8 @@ impl flags::Diagnostics {
load_out_dirs_from_check: !self.disable_build_scripts, load_out_dirs_from_check: !self.disable_build_scripts,
with_proc_macro_server, with_proc_macro_server,
prefill_caches: false, prefill_caches: false,
// We don't pass `--all-targets` so we also set `cfg(test)` to false.
set_test: false,
}; };
let (db, _vfs, _proc_macro) = let (db, _vfs, _proc_macro) =
load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?; load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?;

View file

@ -284,6 +284,7 @@ impl flags::Lsif {
load_out_dirs_from_check: true, load_out_dirs_from_check: true,
with_proc_macro_server: ProcMacroServerChoice::Sysroot, with_proc_macro_server: ProcMacroServerChoice::Sysroot,
prefill_caches: false, prefill_caches: false,
set_test: true,
}; };
let path = AbsPathBuf::assert_utf8(env::current_dir()?.join(self.path)); let path = AbsPathBuf::assert_utf8(env::current_dir()?.join(self.path));
let root = ProjectManifest::discover_single(&path)?; let root = ProjectManifest::discover_single(&path)?;

View file

@ -22,6 +22,7 @@ impl flags::RunTests {
load_out_dirs_from_check: true, load_out_dirs_from_check: true,
with_proc_macro_server: ProcMacroServerChoice::Sysroot, with_proc_macro_server: ProcMacroServerChoice::Sysroot,
prefill_caches: false, prefill_caches: false,
set_test: true,
}; };
let (ref db, _vfs, _proc_macro) = let (ref db, _vfs, _proc_macro) =
load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?; load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?;

View file

@ -96,6 +96,7 @@ impl Tester {
load_out_dirs_from_check: false, load_out_dirs_from_check: false,
with_proc_macro_server: ProcMacroServerChoice::Sysroot, with_proc_macro_server: ProcMacroServerChoice::Sysroot,
prefill_caches: false, prefill_caches: false,
set_test: true,
}; };
let (db, _vfs, _proc_macro) = let (db, _vfs, _proc_macro) =
load_workspace(workspace, &cargo_config.extra_env, &load_cargo_config)?; load_workspace(workspace, &cargo_config.extra_env, &load_cargo_config)?;

View file

@ -24,11 +24,6 @@ impl flags::Scip {
let now = Instant::now(); let now = Instant::now();
let no_progress = &|s| (eprintln!("rust-analyzer: Loading {s}")); let no_progress = &|s| (eprintln!("rust-analyzer: Loading {s}"));
let load_cargo_config = LoadCargoConfig {
load_out_dirs_from_check: true,
with_proc_macro_server: ProcMacroServerChoice::Sysroot,
prefill_caches: true,
};
let root = let root =
vfs::AbsPathBuf::assert_utf8(std::env::current_dir()?.join(&self.path)).normalize(); vfs::AbsPathBuf::assert_utf8(std::env::current_dir()?.join(&self.path)).normalize();
@ -51,6 +46,12 @@ impl flags::Scip {
// FIXME @alibektas : What happens to errors without logging? // FIXME @alibektas : What happens to errors without logging?
error!(?error_sink, "Config Error(s)"); error!(?error_sink, "Config Error(s)");
} }
let load_cargo_config = LoadCargoConfig {
load_out_dirs_from_check: true,
with_proc_macro_server: ProcMacroServerChoice::Sysroot,
prefill_caches: true,
set_test: config.cfg_set_test(None),
};
let cargo_config = config.cargo(None); let cargo_config = config.cargo(None);
let (db, vfs, _) = load_workspace_at( let (db, vfs, _) = load_workspace_at(
root.as_path().as_ref(), root.as_path().as_ref(),

View file

@ -19,6 +19,7 @@ impl flags::Ssr {
load_out_dirs_from_check: true, load_out_dirs_from_check: true,
with_proc_macro_server: ProcMacroServerChoice::Sysroot, with_proc_macro_server: ProcMacroServerChoice::Sysroot,
prefill_caches: false, prefill_caches: false,
set_test: true,
}; };
let (ref db, vfs, _proc_macro) = load_workspace_at( let (ref db, vfs, _proc_macro) = load_workspace_at(
&std::env::current_dir()?, &std::env::current_dir()?,
@ -50,11 +51,12 @@ impl flags::Search {
pub fn run(self) -> anyhow::Result<()> { pub fn run(self) -> anyhow::Result<()> {
use ide_db::base_db::SourceRootDatabase; use ide_db::base_db::SourceRootDatabase;
use ide_db::symbol_index::SymbolsDatabase; use ide_db::symbol_index::SymbolsDatabase;
let cargo_config = CargoConfig::default(); let cargo_config = CargoConfig { all_targets: true, ..CargoConfig::default() };
let load_cargo_config = LoadCargoConfig { let load_cargo_config = LoadCargoConfig {
load_out_dirs_from_check: true, load_out_dirs_from_check: true,
with_proc_macro_server: ProcMacroServerChoice::Sysroot, with_proc_macro_server: ProcMacroServerChoice::Sysroot,
prefill_caches: false, prefill_caches: false,
set_test: true,
}; };
let (ref db, _vfs, _proc_macro) = load_workspace_at( let (ref db, _vfs, _proc_macro) = load_workspace_at(
&std::env::current_dir()?, &std::env::current_dir()?,

View file

@ -574,6 +574,9 @@ config_data! {
/// set to a path relative to the workspace to use that path. /// set to a path relative to the workspace to use that path.
cargo_targetDir | rust_analyzerTargetDir: Option<TargetDirectory> = None, cargo_targetDir | rust_analyzerTargetDir: Option<TargetDirectory> = None,
/// Set `cfg(test)` for local crates. Defaults to true.
cfg_setTest: bool = true,
/// Run the check command for diagnostics on save. /// Run the check command for diagnostics on save.
checkOnSave | checkOnSave_enable: bool = true, checkOnSave | checkOnSave_enable: bool = true,
@ -695,7 +698,6 @@ config_data! {
workspace_symbol_search_limit: usize = 128, workspace_symbol_search_limit: usize = 128,
/// Workspace symbol search scope. /// Workspace symbol search scope.
workspace_symbol_search_scope: WorkspaceSymbolSearchScopeDef = WorkspaceSymbolSearchScopeDef::Workspace, workspace_symbol_search_scope: WorkspaceSymbolSearchScopeDef = WorkspaceSymbolSearchScopeDef::Workspace,
} }
} }

View file

@ -36,12 +36,14 @@ fn integrated_highlighting_benchmark() {
let cargo_config = CargoConfig { let cargo_config = CargoConfig {
sysroot: Some(project_model::RustLibSource::Discover), sysroot: Some(project_model::RustLibSource::Discover),
all_targets: true,
..CargoConfig::default() ..CargoConfig::default()
}; };
let load_cargo_config = LoadCargoConfig { let load_cargo_config = LoadCargoConfig {
load_out_dirs_from_check: true, load_out_dirs_from_check: true,
with_proc_macro_server: ProcMacroServerChoice::Sysroot, with_proc_macro_server: ProcMacroServerChoice::Sysroot,
prefill_caches: false, prefill_caches: false,
set_test: true,
}; };
let (db, vfs, _proc_macro) = { let (db, vfs, _proc_macro) = {
@ -102,12 +104,14 @@ fn integrated_completion_benchmark() {
let cargo_config = CargoConfig { let cargo_config = CargoConfig {
sysroot: Some(project_model::RustLibSource::Discover), sysroot: Some(project_model::RustLibSource::Discover),
all_targets: true,
..CargoConfig::default() ..CargoConfig::default()
}; };
let load_cargo_config = LoadCargoConfig { let load_cargo_config = LoadCargoConfig {
load_out_dirs_from_check: true, load_out_dirs_from_check: true,
with_proc_macro_server: ProcMacroServerChoice::Sysroot, with_proc_macro_server: ProcMacroServerChoice::Sysroot,
prefill_caches: true, prefill_caches: true,
set_test: true,
}; };
let (db, vfs, _proc_macro) = { let (db, vfs, _proc_macro) = {
@ -279,12 +283,14 @@ fn integrated_diagnostics_benchmark() {
let cargo_config = CargoConfig { let cargo_config = CargoConfig {
sysroot: Some(project_model::RustLibSource::Discover), sysroot: Some(project_model::RustLibSource::Discover),
all_targets: true,
..CargoConfig::default() ..CargoConfig::default()
}; };
let load_cargo_config = LoadCargoConfig { let load_cargo_config = LoadCargoConfig {
load_out_dirs_from_check: true, load_out_dirs_from_check: true,
with_proc_macro_server: ProcMacroServerChoice::Sysroot, with_proc_macro_server: ProcMacroServerChoice::Sysroot,
prefill_caches: true, prefill_caches: true,
set_test: true,
}; };
let (db, vfs, _proc_macro) = { let (db, vfs, _proc_macro) = {

View file

@ -703,7 +703,12 @@ impl GlobalState {
vfs.file_id(&vfs_path) vfs.file_id(&vfs_path)
}; };
ws_to_crate_graph(&self.workspaces, self.config.extra_env(None), load) ws_to_crate_graph(
&self.workspaces,
self.config.extra_env(None),
load,
self.config.cfg_set_test(None),
)
}; };
let mut change = ChangeWithProcMacros::new(); let mut change = ChangeWithProcMacros::new();
if self.config.expand_proc_macros() { if self.config.expand_proc_macros() {
@ -856,12 +861,13 @@ pub fn ws_to_crate_graph(
workspaces: &[ProjectWorkspace], workspaces: &[ProjectWorkspace],
extra_env: &FxHashMap<String, String>, extra_env: &FxHashMap<String, String>,
mut load: impl FnMut(&AbsPath) -> Option<vfs::FileId>, mut load: impl FnMut(&AbsPath) -> Option<vfs::FileId>,
set_test: bool,
) -> (CrateGraph, Vec<ProcMacroPaths>, FxHashMap<CrateId, Arc<CrateWorkspaceData>>) { ) -> (CrateGraph, Vec<ProcMacroPaths>, FxHashMap<CrateId, Arc<CrateWorkspaceData>>) {
let mut crate_graph = CrateGraph::default(); let mut crate_graph = CrateGraph::default();
let mut proc_macro_paths = Vec::default(); let mut proc_macro_paths = Vec::default();
let mut ws_data = FxHashMap::default(); let mut ws_data = FxHashMap::default();
for ws in workspaces { for ws in workspaces {
let (other, mut crate_proc_macros) = ws.to_crate_graph(&mut load, extra_env); let (other, mut crate_proc_macros) = ws.to_crate_graph(&mut load, extra_env, set_test);
let ProjectWorkspace { toolchain, target_layout, .. } = ws; let ProjectWorkspace { toolchain, target_layout, .. } = ws;
let mapping = crate_graph.extend(other, &mut crate_proc_macros); let mapping = crate_graph.extend(other, &mut crate_proc_macros);

View file

@ -158,6 +158,11 @@ building from locking the `Cargo.lock` at the expense of duplicating build artif
Set to `true` to use a subdirectory of the existing target directory or Set to `true` to use a subdirectory of the existing target directory or
set to a path relative to the workspace to use that path. set to a path relative to the workspace to use that path.
-- --
[[rust-analyzer.cfg.setTest]]rust-analyzer.cfg.setTest (default: `true`)::
+
--
Set `cfg(test)` for local crates. Defaults to true.
--
[[rust-analyzer.checkOnSave]]rust-analyzer.checkOnSave (default: `true`):: [[rust-analyzer.checkOnSave]]rust-analyzer.checkOnSave (default: `true`)::
+ +
-- --

View file

@ -853,6 +853,16 @@
} }
} }
}, },
{
"title": "cfg",
"properties": {
"rust-analyzer.cfg.setTest": {
"markdownDescription": "Set `cfg(test)` for local crates. Defaults to true.",
"default": true,
"type": "boolean"
}
}
},
{ {
"title": "general", "title": "general",
"properties": { "properties": {

View file

@ -24,6 +24,7 @@ export class Config {
"serverPath", "serverPath",
"server", "server",
"files", "files",
"cfg",
].map((opt) => `${this.rootSection}.${opt}`); ].map((opt) => `${this.rootSection}.${opt}`);
private readonly requiresWindowReloadOpts = ["testExplorer"].map( private readonly requiresWindowReloadOpts = ["testExplorer"].map(