diff --git a/src/cargo/core/compiler/build_config.rs b/src/cargo/core/compiler/build_config.rs
index 885b124b976..5d4d754bff1 100644
--- a/src/cargo/core/compiler/build_config.rs
+++ b/src/cargo/core/compiler/build_config.rs
@@ -1,4 +1,5 @@
use crate::core::compiler::CompileKind;
+use crate::util::config::JobsConfig;
use crate::util::interning::InternedString;
use crate::util::{CargoResult, Config, RustfixDiagnosticServer};
use anyhow::{bail, Context as _};
@@ -64,7 +65,7 @@ impl BuildConfig {
/// * `target.$target.libfoo.metadata`
pub fn new(
config: &Config,
- jobs: Option,
+ jobs: Option,
keep_going: bool,
requested_targets: &[String],
mode: CompileMode,
@@ -78,11 +79,22 @@ impl BuildConfig {
its environment, ignoring the `-j` parameter",
)?;
}
- let jobs = match jobs.or(cfg.jobs) {
+ let jobs = match jobs.or(cfg.jobs.clone()) {
None => default_parallelism()?,
- Some(0) => anyhow::bail!("jobs may not be 0"),
- Some(j) if j < 0 => (default_parallelism()? as i32 + j).max(1) as u32,
- Some(j) => j as u32,
+ Some(value) => match value {
+ JobsConfig::Integer(j) => match j {
+ 0 => anyhow::bail!("jobs may not be 0"),
+ j if j < 0 => (default_parallelism()? as i32 + j).max(1) as u32,
+ j => j as u32,
+ },
+ JobsConfig::String(j) => match j.as_str() {
+ "default" => default_parallelism()?,
+ _ => {
+ anyhow::bail!(
+ format!("could not parse `{j}`. Number of parallel jobs should be `default` or a number."))
+ }
+ },
+ },
};
if config.cli_unstable().build_std.is_some() && requested_kinds[0].is_host() {
diff --git a/src/cargo/ops/cargo_fetch.rs b/src/cargo/ops/cargo_fetch.rs
index bac3f0278d8..273bce28492 100644
--- a/src/cargo/ops/cargo_fetch.rs
+++ b/src/cargo/ops/cargo_fetch.rs
@@ -2,6 +2,7 @@ use crate::core::compiler::standard_lib;
use crate::core::compiler::{BuildConfig, CompileMode, RustcTargetData};
use crate::core::{PackageSet, Resolve, Workspace};
use crate::ops;
+use crate::util::config::JobsConfig;
use crate::util::CargoResult;
use crate::util::Config;
use std::collections::HashSet;
@@ -20,7 +21,7 @@ pub fn fetch<'a>(
ws.emit_warnings()?;
let (mut packages, resolve) = ops::resolve_ws(ws)?;
- let jobs = Some(1);
+ let jobs = Some(JobsConfig::Integer(1));
let keep_going = false;
let config = ws.config();
let build_config = BuildConfig::new(
diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs
index f80848c7596..37bc6bd0767 100644
--- a/src/cargo/ops/cargo_package.rs
+++ b/src/cargo/ops/cargo_package.rs
@@ -13,6 +13,7 @@ use crate::core::{registry::PackageRegistry, resolver::HasDevUnits};
use crate::core::{Feature, Shell, Verbosity, Workspace};
use crate::core::{Package, PackageId, PackageSet, Resolve, SourceId};
use crate::sources::PathSource;
+use crate::util::config::JobsConfig;
use crate::util::errors::CargoResult;
use crate::util::toml::TomlManifest;
use crate::util::{self, human_readable_bytes, restricted_names, Config, FileLock};
@@ -31,7 +32,7 @@ pub struct PackageOpts<'cfg> {
pub check_metadata: bool,
pub allow_dirty: bool,
pub verify: bool,
- pub jobs: Option,
+ pub jobs: Option,
pub keep_going: bool,
pub to_package: ops::Packages,
pub targets: Vec,
@@ -198,7 +199,7 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult
Number of parallel jobs to run. May also be specified with the
build.jobsconfig value. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
-parallel jobs to the number of logical CPUs plus provided value.
+parallel jobs to the number of logical CPUs plus provided value. If
+a string default is provided, it sets the value back to defaults.
Should not be 0.
Number of parallel jobs to run. May also be specified with the
build.jobsconfig value. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
-parallel jobs to the number of logical CPUs plus provided value.
+parallel jobs to the number of logical CPUs plus provided value. If
+a string default is provided, it sets the value back to defaults.
Should not be 0.
Number of parallel jobs to run. May also be specified with the
build.jobsconfig value. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
-parallel jobs to the number of logical CPUs plus provided value.
+parallel jobs to the number of logical CPUs plus provided value. If
+a string default is provided, it sets the value back to defaults.
Should not be 0.
diff --git a/src/doc/src/commands/cargo-run.md b/src/doc/src/commands/cargo-run.md
index f6f5ec2a343..bd3e4724a4b 100644
--- a/src/doc/src/commands/cargo-run.md
+++ b/src/doc/src/commands/cargo-run.md
@@ -299,7 +299,8 @@ requires the -Z unstable-options flag to enable (see
Number of parallel jobs to run. May also be specified with the
build.jobsconfig value. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
-parallel jobs to the number of logical CPUs plus provided value.
+parallel jobs to the number of logical CPUs plus provided value. If
+a string default is provided, it sets the value back to defaults.
Should not be 0.
diff --git a/src/doc/src/commands/cargo-rustc.md b/src/doc/src/commands/cargo-rustc.md
index 946298af914..2147d617cd5 100644
--- a/src/doc/src/commands/cargo-rustc.md
+++ b/src/doc/src/commands/cargo-rustc.md
@@ -396,7 +396,8 @@ requires the -Z unstable-options flag to enable (see
Number of parallel jobs to run. May also be specified with the
build.jobsconfig value. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
-parallel jobs to the number of logical CPUs plus provided value.
+parallel jobs to the number of logical CPUs plus provided value. If
+a string default is provided, it sets the value back to defaults.
Should not be 0.
diff --git a/src/doc/src/commands/cargo-rustdoc.md b/src/doc/src/commands/cargo-rustdoc.md
index 8467da2a32c..22c1c8322bb 100644
--- a/src/doc/src/commands/cargo-rustdoc.md
+++ b/src/doc/src/commands/cargo-rustdoc.md
@@ -376,7 +376,8 @@ requires the -Z unstable-options flag to enable (see
Number of parallel jobs to run. May also be specified with the
build.jobsconfig value. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
-parallel jobs to the number of logical CPUs plus provided value.
+parallel jobs to the number of logical CPUs plus provided value. If
+a string default is provided, it sets the value back to defaults.
Should not be 0.
diff --git a/src/doc/src/commands/cargo-test.md b/src/doc/src/commands/cargo-test.md
index 24fcc70ffa7..c447ce96be6 100644
--- a/src/doc/src/commands/cargo-test.md
+++ b/src/doc/src/commands/cargo-test.md
@@ -503,7 +503,8 @@ includes an option to control the number of threads used:
Number of parallel jobs to run. May also be specified with the
build.jobsconfig value. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
-parallel jobs to the number of logical CPUs plus provided value.
+parallel jobs to the number of logical CPUs plus provided value. If
+a string default is provided, it sets the value back to defaults.
Should not be 0.
diff --git a/src/doc/src/reference/config.md b/src/doc/src/reference/config.md
index c57a45f6759..30053bb18a2 100644
--- a/src/doc/src/reference/config.md
+++ b/src/doc/src/reference/config.md
@@ -370,13 +370,14 @@ recursive_example = "rr --example recursions"
The `[build]` table controls build-time operations and compiler settings.
##### `build.jobs`
-* Type: integer
+* Type: integer or string
* Default: number of logical CPUs
* Environment: `CARGO_BUILD_JOBS`
Sets the maximum number of compiler processes to run in parallel. If negative,
it sets the maximum number of compiler processes to the number of logical CPUs
-plus provided value. Should not be 0.
+plus provided value. Should not be 0. If a string `default` is provided, it sets
+the value back to defaults.
Can be overridden with the `--jobs` CLI option.
diff --git a/src/etc/man/cargo-bench.1 b/src/etc/man/cargo-bench.1
index 44ff593fdd8..993dd3415b8 100644
--- a/src/etc/man/cargo-bench.1
+++ b/src/etc/man/cargo-bench.1
@@ -497,7 +497,8 @@ Rust test harness runs benchmarks serially in a single thread.
Number of parallel jobs to run. May also be specified with the
\fBbuild.jobs\fR \fIconfig value\fR \&. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
-parallel jobs to the number of logical CPUs plus provided value.
+parallel jobs to the number of logical CPUs plus provided value. If
+a string \fBdefault\fR is provided, it sets the value back to defaults.
Should not be 0.
.RE
.sp
diff --git a/src/etc/man/cargo-build.1 b/src/etc/man/cargo-build.1
index 80ae4ac9092..4ee6a0d7636 100644
--- a/src/etc/man/cargo-build.1
+++ b/src/etc/man/cargo-build.1
@@ -412,7 +412,8 @@ Unstable (nightly\-only) flags to Cargo. Run \fBcargo \-Z help\fR for details.
Number of parallel jobs to run. May also be specified with the
\fBbuild.jobs\fR \fIconfig value\fR \&. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
-parallel jobs to the number of logical CPUs plus provided value.
+parallel jobs to the number of logical CPUs plus provided value. If
+a string \fBdefault\fR is provided, it sets the value back to defaults.
Should not be 0.
.RE
.sp
diff --git a/src/etc/man/cargo-check.1 b/src/etc/man/cargo-check.1
index cf7a66d89d7..1aada2a217e 100644
--- a/src/etc/man/cargo-check.1
+++ b/src/etc/man/cargo-check.1
@@ -393,7 +393,8 @@ Unstable (nightly\-only) flags to Cargo. Run \fBcargo \-Z help\fR for details.
Number of parallel jobs to run. May also be specified with the
\fBbuild.jobs\fR \fIconfig value\fR \&. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
-parallel jobs to the number of logical CPUs plus provided value.
+parallel jobs to the number of logical CPUs plus provided value. If
+a string \fBdefault\fR is provided, it sets the value back to defaults.
Should not be 0.
.RE
.sp
diff --git a/src/etc/man/cargo-doc.1 b/src/etc/man/cargo-doc.1
index 63ce2a05036..24621e9f6e2 100644
--- a/src/etc/man/cargo-doc.1
+++ b/src/etc/man/cargo-doc.1
@@ -360,7 +360,8 @@ Unstable (nightly\-only) flags to Cargo. Run \fBcargo \-Z help\fR for details.
Number of parallel jobs to run. May also be specified with the
\fBbuild.jobs\fR \fIconfig value\fR \&. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
-parallel jobs to the number of logical CPUs plus provided value.
+parallel jobs to the number of logical CPUs plus provided value. If
+a string \fBdefault\fR is provided, it sets the value back to defaults.
Should not be 0.
.RE
.sp
diff --git a/src/etc/man/cargo-fix.1 b/src/etc/man/cargo-fix.1
index 51b1e3fd64e..7f2a34cda68 100644
--- a/src/etc/man/cargo-fix.1
+++ b/src/etc/man/cargo-fix.1
@@ -488,7 +488,8 @@ Unstable (nightly\-only) flags to Cargo. Run \fBcargo \-Z help\fR for details.
Number of parallel jobs to run. May also be specified with the
\fBbuild.jobs\fR \fIconfig value\fR \&. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
-parallel jobs to the number of logical CPUs plus provided value.
+parallel jobs to the number of logical CPUs plus provided value. If
+a string \fBdefault\fR is provided, it sets the value back to defaults.
Should not be 0.
.RE
.sp
diff --git a/src/etc/man/cargo-install.1 b/src/etc/man/cargo-install.1
index a9eb6266b89..4308ec8d563 100644
--- a/src/etc/man/cargo-install.1
+++ b/src/etc/man/cargo-install.1
@@ -338,7 +338,8 @@ May also be specified with the \fBnet.offline\fR \fIconfig value\fR \&. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
-parallel jobs to the number of logical CPUs plus provided value.
+parallel jobs to the number of logical CPUs plus provided value. If
+a string \fBdefault\fR is provided, it sets the value back to defaults.
Should not be 0.
.RE
.sp
diff --git a/src/etc/man/cargo-package.1 b/src/etc/man/cargo-package.1
index 9f4847d7d12..8a7b1c191cf 100644
--- a/src/etc/man/cargo-package.1
+++ b/src/etc/man/cargo-package.1
@@ -234,7 +234,8 @@ May also be specified with the \fBnet.offline\fR \fIconfig value\fR \&. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
-parallel jobs to the number of logical CPUs plus provided value.
+parallel jobs to the number of logical CPUs plus provided value. If
+a string \fBdefault\fR is provided, it sets the value back to defaults.
Should not be 0.
.RE
.sp
diff --git a/src/etc/man/cargo-publish.1 b/src/etc/man/cargo-publish.1
index a54a7bcda60..d18f9e6907c 100644
--- a/src/etc/man/cargo-publish.1
+++ b/src/etc/man/cargo-publish.1
@@ -184,7 +184,8 @@ May also be specified with the \fBnet.offline\fR \fIconfig value\fR \&. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
-parallel jobs to the number of logical CPUs plus provided value.
+parallel jobs to the number of logical CPUs plus provided value. If
+a string \fBdefault\fR is provided, it sets the value back to defaults.
Should not be 0.
.RE
.sp
diff --git a/src/etc/man/cargo-run.1 b/src/etc/man/cargo-run.1
index 7a85298ccd4..1c182ad1a2a 100644
--- a/src/etc/man/cargo-run.1
+++ b/src/etc/man/cargo-run.1
@@ -297,7 +297,8 @@ Unstable (nightly\-only) flags to Cargo. Run \fBcargo \-Z help\fR for details.
Number of parallel jobs to run. May also be specified with the
\fBbuild.jobs\fR \fIconfig value\fR \&. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
-parallel jobs to the number of logical CPUs plus provided value.
+parallel jobs to the number of logical CPUs plus provided value. If
+a string \fBdefault\fR is provided, it sets the value back to defaults.
Should not be 0.
.RE
.sp
diff --git a/src/etc/man/cargo-rustc.1 b/src/etc/man/cargo-rustc.1
index 6e901d9ecad..50df99656b6 100644
--- a/src/etc/man/cargo-rustc.1
+++ b/src/etc/man/cargo-rustc.1
@@ -411,7 +411,8 @@ Unstable (nightly\-only) flags to Cargo. Run \fBcargo \-Z help\fR for details.
Number of parallel jobs to run. May also be specified with the
\fBbuild.jobs\fR \fIconfig value\fR \&. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
-parallel jobs to the number of logical CPUs plus provided value.
+parallel jobs to the number of logical CPUs plus provided value. If
+a string \fBdefault\fR is provided, it sets the value back to defaults.
Should not be 0.
.RE
.sp
diff --git a/src/etc/man/cargo-rustdoc.1 b/src/etc/man/cargo-rustdoc.1
index 0c9a0e74a7d..1792c6e2fbd 100644
--- a/src/etc/man/cargo-rustdoc.1
+++ b/src/etc/man/cargo-rustdoc.1
@@ -379,7 +379,8 @@ Unstable (nightly\-only) flags to Cargo. Run \fBcargo \-Z help\fR for details.
Number of parallel jobs to run. May also be specified with the
\fBbuild.jobs\fR \fIconfig value\fR \&. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
-parallel jobs to the number of logical CPUs plus provided value.
+parallel jobs to the number of logical CPUs plus provided value. If
+a string \fBdefault\fR is provided, it sets the value back to defaults.
Should not be 0.
.RE
.sp
diff --git a/src/etc/man/cargo-test.1 b/src/etc/man/cargo-test.1
index 1ee2f76727f..cbae4c18693 100644
--- a/src/etc/man/cargo-test.1
+++ b/src/etc/man/cargo-test.1
@@ -523,7 +523,8 @@ cargo test \-j 2 \-\- \-\-test\-threads=2
Number of parallel jobs to run. May also be specified with the
\fBbuild.jobs\fR \fIconfig value\fR \&. Defaults to
the number of logical CPUs. If negative, it sets the maximum number of
-parallel jobs to the number of logical CPUs plus provided value.
+parallel jobs to the number of logical CPUs plus provided value. If
+a string \fBdefault\fR is provided, it sets the value back to defaults.
Should not be 0.
.RE
.sp
diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs
index 6dbef022e95..8cb064a6f92 100644
--- a/tests/testsuite/build.rs
+++ b/tests/testsuite/build.rs
@@ -5566,6 +5566,8 @@ fn good_jobs() {
p.cargo("build --jobs 1").run();
p.cargo("build --jobs -1").run();
+
+ p.cargo("build --jobs default").run();
}
#[cargo_test]
@@ -5599,8 +5601,8 @@ fn invalid_jobs() {
.run();
p.cargo("build --jobs over9000")
- .with_status(1)
- .with_stderr("error: Invalid value: could not parse `over9000` as a number")
+ .with_status(101)
+ .with_stderr("error: could not parse `over9000`. Number of parallel jobs should be `default` or a number.")
.run();
}
diff --git a/tests/testsuite/config.rs b/tests/testsuite/config.rs
index b0f9d167b85..5d4163818c3 100644
--- a/tests/testsuite/config.rs
+++ b/tests/testsuite/config.rs
@@ -1,7 +1,7 @@
//! Tests for config settings.
use cargo::core::{PackageIdSpec, Shell};
-use cargo::util::config::{self, Config, Definition, SslVersionConfig, StringList};
+use cargo::util::config::{self, Config, Definition, JobsConfig, SslVersionConfig, StringList};
use cargo::util::interning::InternedString;
use cargo::util::toml::{self as cargo_toml, TomlDebugInfo, VecStringOrBool as VSOB};
use cargo::CargoResult;
@@ -1651,3 +1651,63 @@ fn debuginfo_parsing() {
.ends_with("could not load config key `profile.dev.debug`"));
}
}
+
+#[cargo_test]
+fn build_jobs_missing() {
+ write_config(
+ "\
+[build]
+",
+ );
+
+ let config = new_config();
+
+ assert!(config
+ .get::