Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/rust-analyzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ toml.workspace = true
walkdir = "2.5.0"
semver.workspace = true
memchr = "2.7.5"
libc.workspace = true
cargo_metadata.workspace = true
process-wrap.workspace = true
dhat = { version = "0.3.3", optional = true }
Expand Down
29 changes: 29 additions & 0 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,11 @@ config_data! {
/// Note: The option must be specified as an array of command line arguments, with
/// the first argument being the name of the command to run.
check_overrideCommand | checkOnSave_overrideCommand: Option<Vec<String>> = None,
/// Set the priority of the check command.
///
/// On Unix, this will use `nice` to set the priority.
/// On Windows, this will use `BELOW_NORMAL_PRIORITY_CLASS`.
check_priority | checkOnSave_priority: ProcessPriority = ProcessPriority::Normal,
/// Check for specific targets. Defaults to `#rust-analyzer.cargo.target#` if empty.
///
/// Can be a single target, e.g. `"x86_64-unknown-linux-gnu"` or a list of targets, e.g.
Expand Down Expand Up @@ -2414,6 +2419,7 @@ impl Config {
extra_env: self.extra_env(source_root).clone(),
target_dir_config: self.target_dir_from_config(source_root),
set_test: true,
priority: crate::flycheck::ProcessPriority::Normal,
}
}

Expand All @@ -2432,6 +2438,10 @@ impl Config {
crate::flycheck::InvocationStrategy::PerWorkspace
}
},
priority: match self.check_priority(source_root) {
ProcessPriority::Normal => crate::flycheck::ProcessPriority::Normal,
ProcessPriority::Low => crate::flycheck::ProcessPriority::Low,
},
}
}
Some(_) | None => FlycheckConfig::CargoCommand {
Expand Down Expand Up @@ -2472,6 +2482,10 @@ impl Config {
extra_env: self.check_extra_env(source_root),
target_dir_config: self.target_dir_from_config(source_root),
set_test: *self.cfg_setTest(source_root),
priority: match self.check_priority(source_root) {
ProcessPriority::Normal => crate::flycheck::ProcessPriority::Normal,
ProcessPriority::Low => crate::flycheck::ProcessPriority::Low,
},
},
ansi_color_output: self.color_diagnostic_output(),
},
Expand Down Expand Up @@ -2865,6 +2879,13 @@ enum CargoFeaturesDef {
Selected(Vec<String>),
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
#[serde(rename_all = "snake_case")]
pub(crate) enum ProcessPriority {
Normal,
Low,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub(crate) enum InvocationStrategy {
Expand Down Expand Up @@ -3787,6 +3808,14 @@ fn field_props(field: &str, ty: &str, doc: &[&str], default: &str) -> serde_json
"The command will be executed once with the opened project as the working directory."
],
},
"ProcessPriority" => set! {
"type": "string",
"enum": ["normal", "low"],
"enumDescriptions": [
"Run with normal priority.",
"Run with low priority (e.g., nice on Unix)."
],
},
"Option<CheckOnSaveTargets>" => set! {
"anyOf": [
{
Expand Down
46 changes: 45 additions & 1 deletion crates/rust-analyzer/src/flycheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ pub(crate) enum InvocationStrategy {
PerWorkspace,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum ProcessPriority {
Normal,
Low,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct CargoOptions {
pub(crate) target_tuples: Vec<String>,
Expand All @@ -48,6 +55,7 @@ pub(crate) struct CargoOptions {
pub(crate) extra_test_bin_args: Vec<String>,
pub(crate) extra_env: FxHashMap<String, Option<String>>,
pub(crate) target_dir_config: TargetDirectoryConfig,
pub(crate) priority: ProcessPriority,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -101,6 +109,7 @@ pub(crate) enum FlycheckConfig {
args: Vec<String>,
extra_env: FxHashMap<String, Option<String>>,
invocation_strategy: InvocationStrategy,
priority: ProcessPriority,
},
}

Expand Down Expand Up @@ -700,9 +709,16 @@ impl FlycheckActor {
self.ws_target_dir.as_ref().map(Utf8PathBuf::as_path),
);
cmd.args(&options.extra_args);
set_priority(&mut cmd, options.priority);
Some(cmd)
}
FlycheckConfig::CustomCommand { command, args, extra_env, invocation_strategy } => {
FlycheckConfig::CustomCommand {
command,
args,
extra_env,
invocation_strategy,
priority,
} => {
let root = match invocation_strategy {
InvocationStrategy::Once => &*self.root,
InvocationStrategy::PerWorkspace => {
Expand Down Expand Up @@ -734,6 +750,7 @@ impl FlycheckActor {
}
}

set_priority(&mut cmd, *priority);
Some(cmd)
}
}
Expand All @@ -745,6 +762,33 @@ impl FlycheckActor {
}
}

fn set_priority(cmd: &mut Command, priority: ProcessPriority) {
match priority {
ProcessPriority::Normal => (),
ProcessPriority::Low => {
#[cfg(unix)]
{
use std::os::unix::process::CommandExt;
unsafe {
cmd.pre_exec(|| {
if libc::setpriority(libc::PRIO_PROCESS, 0, 10) != 0 {
return Err(std::io::Error::last_os_error());
}
Ok(())
});
}
}
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
cmd.creation_flags(
windows_sys::Win32::System::Threading::BELOW_NORMAL_PRIORITY_CLASS,
);
}
}
}
}

#[allow(clippy::large_enum_variant)]
enum CargoCheckMessage {
CompilerArtifact(cargo_metadata::Artifact),
Expand Down
10 changes: 10 additions & 0 deletions docs/book/src/configuration_generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,16 @@ Note: The option must be specified as an array of command line arguments, with
the first argument being the name of the command to run.


## rust-analyzer.check.priority {#check.priority}

Default: `"normal"`

Set the priority of the check command.

On Unix, this will use `nice` to set the priority.
On Windows, this will use `BELOW_NORMAL_PRIORITY_CLASS`.


## rust-analyzer.check.targets {#check.targets}

Default: `null`
Expand Down
18 changes: 18 additions & 0 deletions editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,24 @@
}
}
},
{
"title": "Check",
"properties": {
"rust-analyzer.check.priority": {
"markdownDescription": "Set the priority of the check command.\n\nOn Unix, this will use `nice` to set the priority.\nOn Windows, this will use `BELOW_NORMAL_PRIORITY_CLASS`.",
"default": "normal",
"type": "string",
"enum": [
"normal",
"low"
],
"enumDescriptions": [
"Run with normal priority.",
"Run with low priority (e.g., nice on Unix)."
]
}
}
},
{
"title": "Check",
"properties": {
Expand Down