Skip to content

Commit 55078f0

Browse files
committed
Make check workspace
1 parent 15a1505 commit 55078f0

File tree

3 files changed

+89
-85
lines changed

3 files changed

+89
-85
lines changed

src/tools/rust-analyzer/crates/rust-analyzer/src/config.rs

Lines changed: 84 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -77,72 +77,6 @@ config_data! {
7777
cachePriming_numThreads: NumThreads = NumThreads::Physical,
7878

7979

80-
/// Check all targets and tests (`--all-targets`). Defaults to
81-
/// `#rust-analyzer.cargo.allTargets#`.
82-
check_allTargets | checkOnSave_allTargets: Option<bool> = None,
83-
/// Cargo command to use for `cargo check`.
84-
check_command | checkOnSave_command: String = "check".to_owned(),
85-
/// Extra arguments for `cargo check`.
86-
check_extraArgs | checkOnSave_extraArgs: Vec<String> = vec![],
87-
/// Extra environment variables that will be set when running `cargo check`.
88-
/// Extends `#rust-analyzer.cargo.extraEnv#`.
89-
check_extraEnv | checkOnSave_extraEnv: FxHashMap<String, String> = FxHashMap::default(),
90-
/// List of features to activate. Defaults to
91-
/// `#rust-analyzer.cargo.features#`.
92-
///
93-
/// Set to `"all"` to pass `--all-features` to Cargo.
94-
check_features | checkOnSave_features: Option<CargoFeaturesDef> = None,
95-
/// List of `cargo check` (or other command specified in `check.command`) diagnostics to ignore.
96-
///
97-
/// For example for `cargo check`: `dead_code`, `unused_imports`, `unused_variables`,...
98-
check_ignore: FxHashSet<String> = FxHashSet::default(),
99-
/// Specifies the invocation strategy to use when running the check command.
100-
/// If `per_workspace` is set, the command will be executed for each workspace.
101-
/// If `once` is set, the command will be executed once.
102-
/// This config only has an effect when `#rust-analyzer.check.overrideCommand#`
103-
/// is set.
104-
check_invocationStrategy | checkOnSave_invocationStrategy: InvocationStrategy = InvocationStrategy::PerWorkspace,
105-
/// Whether to pass `--no-default-features` to Cargo. Defaults to
106-
/// `#rust-analyzer.cargo.noDefaultFeatures#`.
107-
check_noDefaultFeatures | checkOnSave_noDefaultFeatures: Option<bool> = None,
108-
/// Override the command rust-analyzer uses instead of `cargo check` for
109-
/// diagnostics on save. The command is required to output json and
110-
/// should therefore include `--message-format=json` or a similar option
111-
/// (if your client supports the `colorDiagnosticOutput` experimental
112-
/// capability, you can use `--message-format=json-diagnostic-rendered-ansi`).
113-
///
114-
/// If you're changing this because you're using some tool wrapping
115-
/// Cargo, you might also want to change
116-
/// `#rust-analyzer.cargo.buildScripts.overrideCommand#`.
117-
///
118-
/// If there are multiple linked projects/workspaces, this command is invoked for
119-
/// each of them, with the working directory being the workspace root
120-
/// (i.e., the folder containing the `Cargo.toml`). This can be overwritten
121-
/// by changing `#rust-analyzer.check.invocationStrategy#`.
122-
///
123-
/// If `$saved_file` is part of the command, rust-analyzer will pass
124-
/// the absolute path of the saved file to the provided command. This is
125-
/// intended to be used with non-Cargo build systems.
126-
/// Note that `$saved_file` is experimental and may be removed in the future.
127-
///
128-
/// An example command would be:
129-
///
130-
/// ```bash
131-
/// cargo check --workspace --message-format=json --all-targets
132-
/// ```
133-
/// .
134-
check_overrideCommand | checkOnSave_overrideCommand: Option<Vec<String>> = None,
135-
/// Check for specific targets. Defaults to `#rust-analyzer.cargo.target#` if empty.
136-
///
137-
/// Can be a single target, e.g. `"x86_64-unknown-linux-gnu"` or a list of targets, e.g.
138-
/// `["aarch64-apple-darwin", "x86_64-apple-darwin"]`.
139-
///
140-
/// Aliased as `"checkOnSave.targets"`.
141-
check_targets | checkOnSave_targets | checkOnSave_target: Option<CheckOnSaveTargets> = None,
142-
/// Whether `--workspace` should be passed to `cargo check`.
143-
/// If false, `-p <package>` will be passed instead.
144-
check_workspace: bool = true,
145-
14680
/// List of rust-analyzer diagnostics to disable.
14781
diagnostics_disabled: FxHashSet<String> = FxHashSet::default(),
14882
/// Whether to show native rust-analyzer diagnostics.
@@ -432,6 +366,73 @@ config_data! {
432366
/// Run the check command for diagnostics on save.
433367
checkOnSave | checkOnSave_enable: bool = true,
434368

369+
370+
/// Check all targets and tests (`--all-targets`). Defaults to
371+
/// `#rust-analyzer.cargo.allTargets#`.
372+
check_allTargets | checkOnSave_allTargets: Option<bool> = None,
373+
/// Cargo command to use for `cargo check`.
374+
check_command | checkOnSave_command: String = "check".to_owned(),
375+
/// Extra arguments for `cargo check`.
376+
check_extraArgs | checkOnSave_extraArgs: Vec<String> = vec![],
377+
/// Extra environment variables that will be set when running `cargo check`.
378+
/// Extends `#rust-analyzer.cargo.extraEnv#`.
379+
check_extraEnv | checkOnSave_extraEnv: FxHashMap<String, String> = FxHashMap::default(),
380+
/// List of features to activate. Defaults to
381+
/// `#rust-analyzer.cargo.features#`.
382+
///
383+
/// Set to `"all"` to pass `--all-features` to Cargo.
384+
check_features | checkOnSave_features: Option<CargoFeaturesDef> = None,
385+
/// List of `cargo check` (or other command specified in `check.command`) diagnostics to ignore.
386+
///
387+
/// For example for `cargo check`: `dead_code`, `unused_imports`, `unused_variables`,...
388+
check_ignore: FxHashSet<String> = FxHashSet::default(),
389+
/// Specifies the invocation strategy to use when running the check command.
390+
/// If `per_workspace` is set, the command will be executed for each workspace.
391+
/// If `once` is set, the command will be executed once.
392+
/// This config only has an effect when `#rust-analyzer.check.overrideCommand#`
393+
/// is set.
394+
check_invocationStrategy | checkOnSave_invocationStrategy: InvocationStrategy = InvocationStrategy::PerWorkspace,
395+
/// Whether to pass `--no-default-features` to Cargo. Defaults to
396+
/// `#rust-analyzer.cargo.noDefaultFeatures#`.
397+
check_noDefaultFeatures | checkOnSave_noDefaultFeatures: Option<bool> = None,
398+
/// Override the command rust-analyzer uses instead of `cargo check` for
399+
/// diagnostics on save. The command is required to output json and
400+
/// should therefore include `--message-format=json` or a similar option
401+
/// (if your client supports the `colorDiagnosticOutput` experimental
402+
/// capability, you can use `--message-format=json-diagnostic-rendered-ansi`).
403+
///
404+
/// If you're changing this because you're using some tool wrapping
405+
/// Cargo, you might also want to change
406+
/// `#rust-analyzer.cargo.buildScripts.overrideCommand#`.
407+
///
408+
/// If there are multiple linked projects/workspaces, this command is invoked for
409+
/// each of them, with the working directory being the workspace root
410+
/// (i.e., the folder containing the `Cargo.toml`). This can be overwritten
411+
/// by changing `#rust-analyzer.check.invocationStrategy#`.
412+
///
413+
/// If `$saved_file` is part of the command, rust-analyzer will pass
414+
/// the absolute path of the saved file to the provided command. This is
415+
/// intended to be used with non-Cargo build systems.
416+
/// Note that `$saved_file` is experimental and may be removed in the future.
417+
///
418+
/// An example command would be:
419+
///
420+
/// ```bash
421+
/// cargo check --workspace --message-format=json --all-targets
422+
/// ```
423+
/// .
424+
check_overrideCommand | checkOnSave_overrideCommand: Option<Vec<String>> = None,
425+
/// Check for specific targets. Defaults to `#rust-analyzer.cargo.target#` if empty.
426+
///
427+
/// Can be a single target, e.g. `"x86_64-unknown-linux-gnu"` or a list of targets, e.g.
428+
/// `["aarch64-apple-darwin", "x86_64-apple-darwin"]`.
429+
///
430+
/// Aliased as `"checkOnSave.targets"`.
431+
check_targets | checkOnSave_targets | checkOnSave_target: Option<CheckOnSaveTargets> = None,
432+
/// Whether `--workspace` should be passed to `cargo check`.
433+
/// If false, `-p <package>` will be passed instead.
434+
check_workspace: bool = true,
435+
435436
/// Additional arguments to `rustfmt`.
436437
rustfmt_extraArgs: Vec<String> = vec![],
437438
/// Advanced option, fully override the command rust-analyzer uses for
@@ -1018,7 +1019,7 @@ impl Config {
10181019
config.source_root_parent_map = source_root_map;
10191020
}
10201021

1021-
if config.check_command().is_empty() {
1022+
if config.check_command(None).is_empty() {
10221023
config.validation_errors.0.push(Arc::new(ConfigErrorInner::Json {
10231024
config_key: "/check/command".to_owned(),
10241025
error: serde_json::Error::custom("expected a non-empty string"),
@@ -1721,12 +1722,12 @@ impl Config {
17211722
self.diagnostics_enable().to_owned()
17221723
}
17231724

1724-
pub fn diagnostics_map(&self) -> DiagnosticsMapConfig {
1725+
pub fn diagnostics_map(&self, source_root: Option<SourceRootId>) -> DiagnosticsMapConfig {
17251726
DiagnosticsMapConfig {
17261727
remap_prefix: self.diagnostics_remapPrefix().clone(),
17271728
warnings_as_info: self.diagnostics_warningsAsInfo().clone(),
17281729
warnings_as_hint: self.diagnostics_warningsAsHint().clone(),
1729-
check_ignore: self.check_ignore().clone(),
1730+
check_ignore: self.check_ignore(source_root).clone(),
17301731
}
17311732
}
17321733

@@ -1740,13 +1741,13 @@ impl Config {
17401741

17411742
pub fn check_extra_args(&self, source_root: Option<SourceRootId>) -> Vec<String> {
17421743
let mut extra_args = self.extra_args(source_root).clone();
1743-
extra_args.extend_from_slice(self.check_extraArgs());
1744+
extra_args.extend_from_slice(self.check_extraArgs(source_root));
17441745
extra_args
17451746
}
17461747

17471748
pub fn check_extra_env(&self, source_root: Option<SourceRootId>) -> FxHashMap<String, String> {
17481749
let mut extra_env = self.cargo_extraEnv(source_root).clone();
1749-
extra_env.extend(self.check_extraEnv().clone());
1750+
extra_env.extend(self.check_extraEnv(source_root).clone());
17501751
extra_env
17511752
}
17521753

@@ -1871,8 +1872,8 @@ impl Config {
18711872
}
18721873
}
18731874

1874-
pub fn flycheck_workspace(&self) -> bool {
1875-
*self.check_workspace()
1875+
pub fn flycheck_workspace(&self, source_root: Option<SourceRootId>) -> bool {
1876+
*self.check_workspace(source_root)
18761877
}
18771878

18781879
pub(crate) fn cargo_test_options(&self, source_root: Option<SourceRootId>) -> CargoOptions {
@@ -1893,15 +1894,15 @@ impl Config {
18931894
}
18941895

18951896
pub(crate) fn flycheck(&self, source_root: Option<SourceRootId>) -> FlycheckConfig {
1896-
match &self.check_overrideCommand() {
1897+
match &self.check_overrideCommand(source_root) {
18971898
Some(args) if !args.is_empty() => {
18981899
let mut args = args.clone();
18991900
let command = args.remove(0);
19001901
FlycheckConfig::CustomCommand {
19011902
command,
19021903
args,
19031904
extra_env: self.check_extra_env(source_root),
1904-
invocation_strategy: match self.check_invocationStrategy() {
1905+
invocation_strategy: match self.check_invocationStrategy(source_root) {
19051906
InvocationStrategy::Once => crate::flycheck::InvocationStrategy::Once,
19061907
InvocationStrategy::PerWorkspace => {
19071908
crate::flycheck::InvocationStrategy::PerWorkspace
@@ -1910,10 +1911,10 @@ impl Config {
19101911
}
19111912
}
19121913
Some(_) | None => FlycheckConfig::CargoCommand {
1913-
command: self.check_command().clone(),
1914+
command: self.check_command(source_root).clone(),
19141915
options: CargoOptions {
19151916
target_triples: self
1916-
.check_targets()
1917+
.check_targets(source_root)
19171918
.clone()
19181919
.and_then(|targets| match &targets.0[..] {
19191920
[] => None,
@@ -1923,17 +1924,19 @@ impl Config {
19231924
self.cargo_target(source_root).clone().into_iter().collect()
19241925
}),
19251926
all_targets: self
1926-
.check_allTargets()
1927+
.check_allTargets(source_root)
19271928
.unwrap_or(*self.cargo_allTargets(source_root)),
19281929
no_default_features: self
1929-
.check_noDefaultFeatures()
1930+
.check_noDefaultFeatures(source_root)
19301931
.unwrap_or(*self.cargo_noDefaultFeatures(source_root)),
19311932
all_features: matches!(
1932-
self.check_features().as_ref().unwrap_or(self.cargo_features(source_root)),
1933+
self.check_features(source_root)
1934+
.as_ref()
1935+
.unwrap_or(self.cargo_features(source_root)),
19331936
CargoFeaturesDef::All
19341937
),
19351938
features: match self
1936-
.check_features()
1939+
.check_features(source_root)
19371940
.clone()
19381941
.unwrap_or_else(|| self.cargo_features(source_root).clone())
19391942
{

src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/notification.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
306306
let file_id = state.vfs.read().0.file_id(&vfs_path);
307307
if let Some(file_id) = file_id {
308308
let world = state.snapshot();
309+
let source_root_id = world.analysis.source_root_id(file_id).ok();
309310
let mut updated = false;
310311
let task = move || -> std::result::Result<(), ide::Cancelled> {
311312
// Is the target binary? If so we let flycheck run only for the workspace that contains the crate.
@@ -391,9 +392,9 @@ fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
391392
for (id, package) in workspace_ids.clone() {
392393
if id == flycheck.id() {
393394
updated = true;
394-
match package
395-
.filter(|_| !world.config.flycheck_workspace() || target.is_some())
396-
{
395+
match package.filter(|_| {
396+
!world.config.flycheck_workspace(source_root_id) || target.is_some()
397+
}) {
397398
Some(package) => flycheck
398399
.restart_for_package(package, target.clone().map(TupleExt::head)),
399400
None => flycheck.restart_workspace(saved_file.clone()),

src/tools/rust-analyzer/crates/rust-analyzer/src/main_loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ impl GlobalState {
925925
FlycheckMessage::AddDiagnostic { id, workspace_root, diagnostic } => {
926926
let snap = self.snapshot();
927927
let diagnostics = crate::diagnostics::to_proto::map_rust_diagnostic_to_lsp(
928-
&self.config.diagnostics_map(),
928+
&self.config.diagnostics_map(None),
929929
&diagnostic,
930930
&workspace_root,
931931
&snap,

0 commit comments

Comments
 (0)