Skip to content

Commit 511beaa

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 ac1dc0b commit 511beaa

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

src/cargo/core/compiler/compilation.rs

Lines changed: 8 additions & 1 deletion
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

src/cargo/ops/cargo_run.rs

Lines changed: 7 additions & 1 deletion
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.

src/cargo/ops/cargo_test.rs

Lines changed: 7 additions & 1 deletion
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");

tests/testsuite/jobserver.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,33 +192,25 @@ test-runner:
192192
.env("CARGO", cargo_exe())
193193
.arg("run")
194194
.arg("-j2")
195-
.with_status(2)
196-
.with_stderr_contains("[..]no jobserver from env[..]")
197195
.run();
198196
p.process(make)
199197
.env("PATH", path)
200198
.env("CARGO", cargo_exe())
201199
.arg("run-runner")
202200
.arg("-j2")
203-
.with_status(2)
204201
.with_stderr_contains("[..]this is a runner[..]")
205-
.with_stderr_contains("[..]no jobserver from env[..]")
206202
.run();
207203
p.process(make)
208204
.env("CARGO", cargo_exe())
209205
.arg("test")
210206
.arg("-j2")
211-
.with_status(2)
212-
.with_stdout_contains("[..]no jobserver from env[..]")
213207
.run();
214208
p.process(make)
215209
.env("PATH", path)
216210
.env("CARGO", cargo_exe())
217211
.arg("test-runner")
218212
.arg("-j2")
219-
.with_status(2)
220213
.with_stderr_contains("[..]this is a runner[..]")
221-
.with_stdout_contains("[..]no jobserver from env[..]")
222214
.run();
223215

224216
// but not from `-j` flag

0 commit comments

Comments
 (0)