From 993a70b18ca45914566dd68dc0c7840f58cb659d Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Thu, 5 Oct 2023 17:27:23 +0800 Subject: [PATCH] feat(cargo-run): inherit jobserver from env 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 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. --- src/cargo/ops/cargo_run.rs | 4 ++++ tests/testsuite/jobserver.rs | 7 +++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/cargo/ops/cargo_run.rs b/src/cargo/ops/cargo_run.rs index adf144ac2ed4..b6df6979fb71 100644 --- a/src/cargo/ops/cargo_run.rs +++ b/src/cargo/ops/cargo_run.rs @@ -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() diff --git a/tests/testsuite/jobserver.rs b/tests/testsuite/jobserver.rs index 39c8e5ede166..56d8e7c7582a 100644 --- a/tests/testsuite/jobserver.rs +++ b/tests/testsuite/jobserver.rs @@ -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[..]")