Skip to content

Commit

Permalink
feat(cargo-run): inherit jobserver from env
Browse files Browse the repository at this point in the history
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 tries to provide such an ability.

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 rate.
* Creating a jobsever driver with the new FIFO named pipe style is not
  yet supported as `[email protected]`. 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 Oct 6, 2023
1 parent 8d8243d commit 741f411
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/cargo/ops/cargo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ pub fn run(
process.display_env_vars();
}

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

config.shell().status("Running", process.to_string())?;

process.exec_replace()
Expand Down
7 changes: 3 additions & 4 deletions tests/testsuite/jobserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,10 @@ all:
)
.build();

p.process(make).env("CARGO", cargo_exe()).arg("-j2")
.with_status(2)
.with_stderr_contains("[..]no jobserver from env[..]")
.run();
// jobserver can be inherited from env
p.process(make).env("CARGO", cargo_exe()).arg("-j2").run();

// but not from `-j` flag
p.cargo("run -j2")
.with_status(101)
.with_stderr_contains("[..]no jobserver from env[..]")
Expand Down

0 comments on commit 741f411

Please sign in to comment.