Skip to content

Commit

Permalink
feat: inherit jobserver from env for all kinds of runner
Browse files Browse the repository at this point in the history
External subcommands are already able to inherit the jobserver from
env since rust-lang#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.
  • Loading branch information
weihanglo committed Jan 19, 2024
1 parent ac1dc0b commit 678f3ff
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
8 changes: 7 additions & 1 deletion src/cargo/core/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,13 @@ impl<'cfg> Compilation<'cfg> {
} else {
ProcessBuilder::new(cmd)
};
self.fill_env(builder, pkg, script_meta, kind, false)
let mut builder = self.fill_env(builder, pkg, script_meta, kind, false)?;

if let Some(client) = self.config.jobserver_from_env() {
builder.inherit_jobserver(client);
}

Ok(builder)
}

/// Prepares a new process with an appropriate environment to run against
Expand Down
8 changes: 0 additions & 8 deletions tests/testsuite/jobserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,33 +192,25 @@ test-runner:
.env("CARGO", cargo_exe())
.arg("run")
.arg("-j2")
.with_status(2)
.with_stderr_contains("[..]no jobserver from env[..]")
.run();
p.process(make)
.env("PATH", path)
.env("CARGO", cargo_exe())
.arg("run-runner")
.arg("-j2")
.with_status(2)
.with_stderr_contains("[..]this is a runner[..]")
.with_stderr_contains("[..]no jobserver from env[..]")
.run();
p.process(make)
.env("CARGO", cargo_exe())
.arg("test")
.arg("-j2")
.with_status(2)
.with_stdout_contains("[..]no jobserver from env[..]")
.run();
p.process(make)
.env("PATH", path)
.env("CARGO", cargo_exe())
.arg("test-runner")
.arg("-j2")
.with_status(2)
.with_stderr_contains("[..]this is a runner[..]")
.with_stdout_contains("[..]no jobserver from env[..]")
.run();

// but not from `-j` flag
Expand Down

0 comments on commit 678f3ff

Please sign in to comment.