Skip to content

Commit

Permalink
Support inheriting jobserver fd for external subcommands
Browse files Browse the repository at this point in the history
If cargo detects the existence of a jobserver, cargo should pass the
jobserver down to the external subcommand. Here are the reasons:

1. The existence of jobserver implies the user "expects" the amount of
   job is under control. However, before this commit, external
   subcommands cannnot benefit from the global view of the jobserver.
2. `cargo-clippy` as an external subcommand migth also love to respect
   the jobserver protocol.
3. There are several well-known external subcommands calling "cargo"
   interally (cargo-fuzz, cargo-tarpaulin, etc.)

Caveats:

Job without special prefix `+` might still be considered as a sub-make
and would inherit the jobserver, though I don't see it as an issue.

According to GNU Make Manual "13.1.1 POSIX Jobserver Interaction" [^1],
if `--jobserver-auth` option is available in `MAKEFLAGS` but the file
descriptors are closed, it means that the calling `make` didn't consider
our tool awas a recursive `make` invocation. I make an assumption that
if those fds are still open, we are happy to use those jobserver tokens.

[^1]: https://www.gnu.org/software/make/manual/make.html#POSIX-Jobserver
  • Loading branch information
weihanglo committed Mar 26, 2022
1 parent b099d39 commit c1b90b9
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,12 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> Cli
};

let cargo_exe = config.cargo_exe()?;
let err = match ProcessBuilder::new(&command)
.env(cargo::CARGO_ENV, cargo_exe)
.args(args)
.exec_replace()
{
let mut cmd = ProcessBuilder::new(&command);
cmd.env(cargo::CARGO_ENV, cargo_exe).args(args);
if let Some(client) = config.jobserver_from_env() {
cmd.inherit_jobserver(client);
}
let err = match cmd.exec_replace() {
Ok(()) => return Ok(()),
Err(e) => e,
};
Expand Down

0 comments on commit c1b90b9

Please sign in to comment.