Skip to content

Use response files when spawning rustc commands when needed - #2789

Closed
ranger-ross wants to merge 1 commit into
mozilla:mainfrom
ranger-ross:fix-arg-files
Closed

ranger-ross wants to merge 1 commit into
mozilla:mainfrom
ranger-ross:fix-arg-files

Conversation

@ranger-ross

Copy link
Copy Markdown
Contributor

This is a follow up on #2782.

Unfortunately, I overlooked the fact that sccache modifies the args and does not pass the exact args to rustc that it receives.
This leads to the expanded args getting passed to rustc which blows up on windows when the command is large enough.

The solution is to have sccache create its own arg file (when needed) to pass the args to rustc.

fixes #2787
(I tested the repo in question and it fails without these changes and builds successfully with them)

Comment thread src/compiler/rust.rs
Comment on lines +1128 to +1138
// On Windows there is a hard limit of ~32KB, so we cut off at 30KB to
// give some buffer just incase.
#[cfg(windows)]
let threshold: usize = 30 * 1024;
// On unix the limit is defined by ARG_MAX. If its not explicitly set we set it to 1MB
// which is fairly large but lower than the ~2MB that it defaults to on most systems.
#[cfg(unix)]
let threshold: usize = std::env::var("ARG_MAX")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(1024 * 1024);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides ARG_MAX, this might fail if a single argument in the list exceeds MAX_ARG_STRLEN, which is 32 page sizes (128kb on most systems). This is a real issue (#2815) and I can confirm that lowering this threshold to 128kb solves it.

Even though setting a defensive limit solves the problem (32kb would cover both Windows and a single arg problem), it needs at least a comment explanation or a separate single-argument threshold to make it more transparent and less fragile to future changes.

@codecov-commenter

codecov-commenter commented Aug 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 63.04348% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.80%. Comparing base (33d12fb) to head (0d7d7e1).

Files with missing lines Patch % Lines
src/compiler/rust.rs 57.50% 17 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2789      +/-   ##
==========================================
- Coverage   78.81%   78.80%   -0.01%     
==========================================
  Files          75       75              
  Lines       45354    45399      +45     
==========================================
+ Hits        35745    35777      +32     
- Misses       9609     9622      +13     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ranger-ross

Copy link
Copy Markdown
Contributor Author

CI failures should be unrelated to this change.
But I raised #2790 to fix it :)

@ranger-ross

Copy link
Copy Markdown
Contributor Author

@sylvestre friendly bump on this PR :)

I'd be nice to get this PR merged before we stabilize the new Cargo build-dir layout. (which we are planning to do in the next beta branch)

@user2589

Copy link
Copy Markdown

this PR also fixes #2815
(indirectly through setting env ARG_MAX or directly if the default threshold is lowered below 128K)

@TMysliwiec

Copy link
Copy Markdown

+1 for getting this merged. It most likely fixes a regression for us on Windows CI: #2397 (comment)

@sylvestre

Copy link
Copy Markdown
Collaborator

sorry but it needs to be rebased

Comment thread src/compiler/rust.rs
}
}

fn maybe_write_response_file(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a unit test for maybe_write_response_file: below and above the threshold, and check the content of the written file

Comment thread src/compiler/rust.rs
// On unix the limit is defined by ARG_MAX. If its not explicitly set we set it to 1MB
// which is fairly large but lower than the ~2MB that it defaults to on most systems.
#[cfg(unix)]
let threshold: usize = std::env::var("ARG_MAX")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ARG_MAX isn't normally an env var, it's a sysconf value.
could it be SCCACHE_ARG_MAX (and documented), or read from sysconf?

Comment thread src/compiler/rust.rs
let line = arg
.to_str()
.ok_or_else(|| anyhow!("rustc argument not valid UTF-8: {:?}", arg))?;
writeln!(rsp_file, "{}", line).context("Failed to write response file")?;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rustc takes one arg per line, so an arg containing a newline would be split in two.
should we error out (or skip the rsp file) in that case?

@sylvestre

Copy link
Copy Markdown
Collaborator

thanks for tackling this!
It needs a rebase on main. Please also add a test for the response file (see the inline comment).

@ranger-ross

Copy link
Copy Markdown
Contributor Author

as far as I can tell, #3026 makes this PR unneeded.
(It's a bit hard to tell since the PR description is AI generated and hard to read)

Gonna close this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rust on Windows: failed to spawn Command

5 participants