Skip to content

Commit

Permalink
Provide an config option to not set cfg(test)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChayimFriedman2 committed Sep 29, 2024
1 parent 46fe025 commit 923cb99
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/tools/rust-analyzer/crates/load-cargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct LoadCargoConfig {
pub load_out_dirs_from_check: bool,
pub with_proc_macro_server: ProcMacroServerChoice,
pub prefill_caches: bool,
pub set_test: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -99,6 +100,7 @@ pub fn load_workspace(
vfs.file_id(&path)
},
extra_env,
load_config.set_test,
);
let proc_macros = {
let proc_macro_server = match &proc_macro_server {
Expand Down Expand Up @@ -531,6 +533,7 @@ mod tests {
load_out_dirs_from_check: false,
with_proc_macro_server: ProcMacroServerChoice::None,
prefill_caches: false,
set_test: true,
};
let (db, _vfs, _proc_macro) =
load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {}).unwrap();
Expand Down
2 changes: 2 additions & 0 deletions src/tools/rust-analyzer/crates/project-model/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ fn to_crate_graph(project_workspace: ProjectWorkspace) -> (CrateGraph, ProcMacro
}
},
&Default::default(),
true,
)
}

Expand Down Expand Up @@ -257,5 +258,6 @@ fn smoke_test_real_sysroot_cargo() {
}
},
&Default::default(),
true,
);
}
26 changes: 20 additions & 6 deletions src/tools/rust-analyzer/crates/project-model/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@ impl ProjectWorkspace {
&self,
load: FileLoader<'_>,
extra_env: &FxHashMap<String, String>,
set_test: bool,
) -> (CrateGraph, ProcMacroPaths) {
let _p = tracing::info_span!("ProjectWorkspace::to_crate_graph").entered();

Expand All @@ -741,6 +742,7 @@ impl ProjectWorkspace {
sysroot,
extra_env,
cfg_overrides,
set_test,
),
sysroot,
),
Expand All @@ -759,6 +761,7 @@ impl ProjectWorkspace {
rustc_cfg.clone(),
cfg_overrides,
build_scripts,
set_test,
),
sysroot,
),
Expand All @@ -772,6 +775,7 @@ impl ProjectWorkspace {
rustc_cfg.clone(),
cfg_overrides,
build_scripts,
set_test,
)
} else {
detached_file_to_crate_graph(
Expand All @@ -780,6 +784,7 @@ impl ProjectWorkspace {
file,
sysroot,
cfg_overrides,
set_test,
)
},
sysroot,
Expand Down Expand Up @@ -870,11 +875,12 @@ fn project_json_to_crate_graph(
sysroot: &Sysroot,
extra_env: &FxHashMap<String, String>,
override_cfg: &CfgOverrides,
set_test: bool,
) -> (CrateGraph, ProcMacroPaths) {
let mut res = (CrateGraph::default(), ProcMacroPaths::default());
let (crate_graph, proc_macros) = &mut res;
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 mut cfg_cache: FxHashMap<&str, Vec<CfgAtom>> = FxHashMap::default();
Expand Down Expand Up @@ -987,13 +993,14 @@ fn cargo_to_crate_graph(
rustc_cfg: Vec<CfgAtom>,
override_cfg: &CfgOverrides,
build_scripts: &WorkspaceBuildScripts,
set_test: bool,
) -> (CrateGraph, ProcMacroPaths) {
let _p = tracing::info_span!("cargo_to_crate_graph").entered();
let mut res = (CrateGraph::default(), ProcMacroPaths::default());
let crate_graph = &mut res.0;
let proc_macros = &mut res.1;
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);

Expand All @@ -1011,8 +1018,10 @@ fn cargo_to_crate_graph(
let mut cfg_options = cfg_options.clone();

if cargo[pkg].is_local {
// Add test cfg for local crates
cfg_options.insert_atom(sym::test.clone());
if set_test {
// Add test cfg for local crates
cfg_options.insert_atom(sym::test.clone());
}
cfg_options.insert_atom(sym::rust_analyzer.clone());
}

Expand Down Expand Up @@ -1173,14 +1182,17 @@ fn detached_file_to_crate_graph(
detached_file: &ManifestPath,
sysroot: &Sysroot,
override_cfg: &CfgOverrides,
set_test: bool,
) -> (CrateGraph, ProcMacroPaths) {
let _p = tracing::info_span!("detached_file_to_crate_graph").entered();
let mut crate_graph = CrateGraph::default();
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);
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());
override_cfg.apply(&mut cfg_options, "");
let cfg_options = Arc::new(cfg_options);
Expand Down Expand Up @@ -1404,6 +1416,7 @@ fn sysroot_to_crate_graph(
sysroot: &Sysroot,
rustc_cfg: Vec<CfgAtom>,
load: FileLoader<'_>,
set_test: bool,
) -> (SysrootPublicDeps, Option<CrateId>) {
let _p = tracing::info_span!("sysroot_to_crate_graph").entered();
match sysroot.mode() {
Expand All @@ -1426,6 +1439,7 @@ fn sysroot_to_crate_graph(
..Default::default()
},
&WorkspaceBuildScripts::default(),
set_test,
);

let mut pub_deps = vec![];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl flags::AnalysisStats {
ProcMacroServerChoice::Sysroot
},
prefill_caches: false,
set_test: true,
};

let build_scripts_time = if self.disable_build_scripts {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ impl flags::Diagnostics {
load_out_dirs_from_check: !self.disable_build_scripts,
with_proc_macro_server,
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) =
load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ impl flags::Lsif {
load_out_dirs_from_check: true,
with_proc_macro_server: ProcMacroServerChoice::Sysroot,
prefill_caches: false,
set_test: true,
};
let path = AbsPathBuf::assert_utf8(env::current_dir()?.join(self.path));
let root = ProjectManifest::discover_single(&path)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ impl flags::RunTests {
load_out_dirs_from_check: true,
with_proc_macro_server: ProcMacroServerChoice::Sysroot,
prefill_caches: false,
set_test: true,
};
let (ref db, _vfs, _proc_macro) =
load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ impl Tester {
load_out_dirs_from_check: false,
with_proc_macro_server: ProcMacroServerChoice::Sysroot,
prefill_caches: false,
set_test: true,
};
let (db, _vfs, _proc_macro) =
load_workspace(workspace, &cargo_config.extra_env, &load_cargo_config)?;
Expand Down
11 changes: 6 additions & 5 deletions src/tools/rust-analyzer/crates/rust-analyzer/src/cli/scip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ impl flags::Scip {
let now = Instant::now();

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 =
vfs::AbsPathBuf::assert_utf8(std::env::current_dir()?.join(&self.path)).normalize();

Expand All @@ -51,6 +46,12 @@ impl flags::Scip {
// FIXME @alibektas : What happens to errors without logging?
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 (db, vfs, _) = load_workspace_at(
root.as_path().as_ref(),
Expand Down
4 changes: 3 additions & 1 deletion src/tools/rust-analyzer/crates/rust-analyzer/src/cli/ssr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ impl flags::Ssr {
load_out_dirs_from_check: true,
with_proc_macro_server: ProcMacroServerChoice::Sysroot,
prefill_caches: false,
set_test: true,
};
let (ref db, vfs, _proc_macro) = load_workspace_at(
&std::env::current_dir()?,
Expand Down Expand Up @@ -50,11 +51,12 @@ impl flags::Search {
pub fn run(self) -> anyhow::Result<()> {
use ide_db::base_db::SourceRootDatabase;
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 {
load_out_dirs_from_check: true,
with_proc_macro_server: ProcMacroServerChoice::Sysroot,
prefill_caches: false,
set_test: true,
};
let (ref db, _vfs, _proc_macro) = load_workspace_at(
&std::env::current_dir()?,
Expand Down
4 changes: 3 additions & 1 deletion src/tools/rust-analyzer/crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,9 @@ config_data! {
/// set to a path relative to the workspace to use that path.
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.
checkOnSave | checkOnSave_enable: bool = true,

Expand Down Expand Up @@ -695,7 +698,6 @@ config_data! {
workspace_symbol_search_limit: usize = 128,
/// Workspace symbol search scope.
workspace_symbol_search_scope: WorkspaceSymbolSearchScopeDef = WorkspaceSymbolSearchScopeDef::Workspace,

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ fn integrated_highlighting_benchmark() {

let cargo_config = CargoConfig {
sysroot: Some(project_model::RustLibSource::Discover),
all_targets: true,
..CargoConfig::default()
};
let load_cargo_config = LoadCargoConfig {
load_out_dirs_from_check: true,
with_proc_macro_server: ProcMacroServerChoice::Sysroot,
prefill_caches: false,
set_test: true,
};

let (db, vfs, _proc_macro) = {
Expand Down Expand Up @@ -102,12 +104,14 @@ fn integrated_completion_benchmark() {

let cargo_config = CargoConfig {
sysroot: Some(project_model::RustLibSource::Discover),
all_targets: true,
..CargoConfig::default()
};
let load_cargo_config = LoadCargoConfig {
load_out_dirs_from_check: true,
with_proc_macro_server: ProcMacroServerChoice::Sysroot,
prefill_caches: true,
set_test: true,
};

let (db, vfs, _proc_macro) = {
Expand Down Expand Up @@ -279,12 +283,14 @@ fn integrated_diagnostics_benchmark() {

let cargo_config = CargoConfig {
sysroot: Some(project_model::RustLibSource::Discover),
all_targets: true,
..CargoConfig::default()
};
let load_cargo_config = LoadCargoConfig {
load_out_dirs_from_check: true,
with_proc_macro_server: ProcMacroServerChoice::Sysroot,
prefill_caches: true,
set_test: true,
};

let (db, vfs, _proc_macro) = {
Expand Down
10 changes: 8 additions & 2 deletions src/tools/rust-analyzer/crates/rust-analyzer/src/reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,12 @@ impl GlobalState {
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();
if self.config.expand_proc_macros() {
Expand Down Expand Up @@ -856,12 +861,13 @@ pub fn ws_to_crate_graph(
workspaces: &[ProjectWorkspace],
extra_env: &FxHashMap<String, String>,
mut load: impl FnMut(&AbsPath) -> Option<vfs::FileId>,
set_test: bool,
) -> (CrateGraph, Vec<ProcMacroPaths>, FxHashMap<CrateId, Arc<CrateWorkspaceData>>) {
let mut crate_graph = CrateGraph::default();
let mut proc_macro_paths = Vec::default();
let mut ws_data = FxHashMap::default();
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 mapping = crate_graph.extend(other, &mut crate_proc_macros);
Expand Down
5 changes: 5 additions & 0 deletions src/tools/rust-analyzer/docs/user/generated_config.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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 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`)::
+
--
Expand Down
10 changes: 10 additions & 0 deletions src/tools/rust-analyzer/editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"properties": {
Expand Down
1 change: 1 addition & 0 deletions src/tools/rust-analyzer/editors/code/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class Config {
"serverPath",
"server",
"files",
"cfg",
].map((opt) => `${this.rootSection}.${opt}`);

private readonly requiresWindowReloadOpts = ["testExplorer"].map(
Expand Down

0 comments on commit 923cb99

Please sign in to comment.