Skip to content

Commit

Permalink
Auto merge of #10546 - weihanglo:issue-10381-rustc-argfile, r=epage
Browse files Browse the repository at this point in the history
Retry command invocation with argfile

### What does this PR try to resolve?

Fixes #10381

The goal is to invoke `rustc` and `rustdoc` with ``@path`` arg file as a fallback.

Since the `-Zcheck-cfg-features`[^1] has already been implemented, in the foreseeable future cargo will probably hit the “**command line too big**” more often on Windows. `rustdoc` and `rustc` both support ``@path`` argfile [^2][^3], so we can leverage the feature for the fallback logic.

The idea behind this PR is that we put the **retry logic** in `ProcessBuilder::exec*` functions and reuse them as much as possible.

### How should we test and review this PR?

Review it commit by commit is recommended.

Argfile fallback is hard to tested with integration tests, so I added some unit tests for `cargo_util::ProcessBuilder` and `cargo::ops::fix::FixArgs`.

If you do want to test the support of argfile manually, a new environment variable `__CARGO_TEST_FORCE_ARGFILE` is added for the sake of forcing cargo to use argfile for rustc invocations. For example, `__CARGO_TEST_FORCE_ARGFILE cargo test --test testsuite -- fix::` is usually a good start to test this feature.

[^1]: https://doc.rust-lang.org/beta/cargo/reference/unstable.html?highlight=check#check-cfg-features
[^2]: https://doc.rust-lang.org/rustc/command-line-arguments.html#path-load-command-line-flags-from-a-path
[^3]: https://doc.rust-lang.org/rustdoc/command-line-arguments.html#path-load-command-line-flags-from-a-path
  • Loading branch information
bors committed Apr 11, 2022
2 parents f51e799 + 275b0c6 commit 1073915
Show file tree
Hide file tree
Showing 13 changed files with 466 additions and 119 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ jobs:

# Deny warnings on CI to avoid warnings getting into the codebase.
- run: cargo test --features 'deny-warnings'
- name: Check operability of rustc invocation with argfile
env:
__CARGO_TEST_FORCE_ARGFILE: 1
run: |
# This only tests `cargo fix` because fix-proxy-mode is one of the most
# complicated subprocess management in Cargo.
cargo test --test testsuite --features 'deny-warnings' -- fix::
- run: cargo test --features 'deny-warnings' --manifest-path crates/cargo-test-support/Cargo.toml
env:
CARGO_TARGET_DIR: target
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ path = "src/cargo/lib.rs"
atty = "0.2"
bytesize = "1.0"
cargo-platform = { path = "crates/cargo-platform", version = "0.1.2" }
cargo-util = { path = "crates/cargo-util", version = "0.1.2" }
cargo-util = { path = "crates/cargo-util", version = "0.1.3" }
crates-io = { path = "crates/crates-io", version = "0.34.0" }
crossbeam-utils = "0.8"
curl = { version = "0.4.41", features = ["http2"] }
Expand Down
11 changes: 10 additions & 1 deletion crates/cargo-test-support/src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,17 @@ pub fn echo_wrapper() -> PathBuf {
.file(
"src/main.rs",
r#"
use std::fs::read_to_string;
use std::path::PathBuf;
fn main() {
let args = std::env::args().collect::<Vec<_>>();
// Handle args from `@path` argfile for rustc
let args = std::env::args()
.flat_map(|p| if let Some(p) = p.strip_prefix("@") {
read_to_string(p).unwrap().lines().map(String::from).collect()
} else {
vec![p]
})
.collect::<Vec<_>>();
eprintln!("WRAPPER CALLED: {}", args[1..].join(" "));
let status = std::process::Command::new(&args[1])
.args(&args[2..]).status().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/cargo-util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-util"
version = "0.1.2"
version = "0.1.3"
edition = "2021"
license = "MIT OR Apache-2.0"
homepage = "https://github.com/rust-lang/cargo"
Expand Down
Loading

0 comments on commit 1073915

Please sign in to comment.