Skip to content

Commit baf606a

Browse files
committed
feat: inherit jobserver from env for all kinds of runner
External subcommands are already able to inherit the jobserver from env since #10511. However, users reported that they've expected `cargo run` to behave the same as external subcommands. A popular example "cargo-xtask" pattern is used as scripting to run arbitrary tasks. Users may want to invoke `cargo run` from Make and expect some parallelism. This PR provides such an ability to the general `target.<...>.runner`, which affects `cargo test`, `cargo bench`, and `cargo run`. Note that this PR doesn't create any jobserver client if there is no existing jobserver from the environment. Nor `-j`/`--jobs` would create a new client. Reasons for this decision: * There might be crates don't want the jobserver to pollute their file descriptors, although they might be rare * Creating a jobsever driver with the new FIFO named pipe style is not yet supported as of `jobserver@0.1.26`. Once we can create a named pipe-based jobserver, it will be less risky and inheritance by default can be implemented.
1 parent aa6793d commit baf606a

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

Diff for: src/cargo/core/compiler/compilation.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ impl<'cfg> Compilation<'cfg> {
248248
kind: CompileKind,
249249
pkg: &Package,
250250
script_meta: Option<Metadata>,
251+
jobserver_client: Option<&jobserver::Client>,
251252
) -> CargoResult<ProcessBuilder> {
252253
let builder = if let Some((runner, args)) = self.target_runner(kind) {
253254
let mut builder = ProcessBuilder::new(runner);
@@ -257,7 +258,13 @@ impl<'cfg> Compilation<'cfg> {
257258
} else {
258259
ProcessBuilder::new(cmd)
259260
};
260-
self.fill_env(builder, pkg, script_meta, kind, false)
261+
let mut builder = self.fill_env(builder, pkg, script_meta, kind, false)?;
262+
263+
if let Some(client) = jobserver_client {
264+
builder.inherit_jobserver(client);
265+
}
266+
267+
Ok(builder)
261268
}
262269

263270
/// Prepares a new process with an appropriate environment to run against

Diff for: src/cargo/ops/cargo_run.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,13 @@ pub fn run(
9292
Err(_) => path.to_path_buf(),
9393
};
9494
let pkg = bins[0].0;
95-
let mut process = compile.target_process(exe, unit.kind, pkg, *script_meta)?;
95+
let mut process = compile.target_process(
96+
exe,
97+
unit.kind,
98+
pkg,
99+
*script_meta,
100+
config.jobserver_from_env(),
101+
)?;
96102

97103
// Sets the working directory of the child process to the current working
98104
// directory of the parent process.

Diff for: src/cargo/ops/cargo_test.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,13 @@ fn cmd_builds(
370370
),
371371
};
372372

373-
let mut cmd = compilation.target_process(path, unit.kind, &unit.pkg, *script_meta)?;
373+
let mut cmd = compilation.target_process(
374+
path,
375+
unit.kind,
376+
&unit.pkg,
377+
*script_meta,
378+
config.jobserver_from_env(),
379+
)?;
374380
cmd.args(test_args);
375381
if unit.target.harness() && config.shell().verbosity() == Verbosity::Quiet {
376382
cmd.arg("--quiet");

Diff for: tests/testsuite/jobserver.rs

-8
Original file line numberDiff line numberDiff line change
@@ -185,31 +185,23 @@ test-runner:
185185
.env("CARGO", cargo_exe())
186186
.arg("run")
187187
.arg("-j2")
188-
.with_status(2)
189-
.with_stderr_contains("[..]no jobserver from env[..]")
190188
.run();
191189
p.process(make)
192190
.env("CARGO", cargo_exe())
193191
.arg("run-runner")
194192
.arg("-j2")
195-
.with_status(2)
196193
.with_stderr_contains("[..]this is a runner[..]")
197-
.with_stderr_contains("[..]no jobserver from env[..]")
198194
.run();
199195
p.process(make)
200196
.env("CARGO", cargo_exe())
201197
.arg("test")
202198
.arg("-j2")
203-
.with_status(2)
204-
.with_stdout_contains("[..]no jobserver from env[..]")
205199
.run();
206200
p.process(make)
207201
.env("CARGO", cargo_exe())
208202
.arg("test-runner")
209203
.arg("-j2")
210-
.with_status(2)
211204
.with_stderr_contains("[..]this is a runner[..]")
212-
.with_stdout_contains("[..]no jobserver from env[..]")
213205
.run();
214206

215207
// but not from `-j` flag

0 commit comments

Comments
 (0)