rust-lang/cargo · error · anyhow::Error
expected rustc as first argument
Error message
expected rustc as first argument
What it means
Thrown in FixArgs::from_args (src/ops/cargo_fix/mod.rs:1250-1262) when the first argument was an `@path` argfile (read successfully) but the argfile's first line — which must be the rustc executable path — was missing. The argfile parsing expects line 1 = rustc, remaining lines = args.
Source
Thrown at src/ops/cargo_fix/mod.rs:1262
}
}
other.push(path.into());
Ok(())
};
if let Some(argfile_path) = rustc.to_str().unwrap_or_default().strip_prefix("@") {
// Because cargo in fix-proxy-mode might hit the command line size limit,
// cargo fix need handle `@path` argfile for this special case.
if argv.next().is_some() {
bail!("argfile `@path` cannot be combined with other arguments");
}
let contents = fs::read_to_string(argfile_path)
.with_context(|| format!("failed to read argfile at `{argfile_path}`"))?;
let mut iter = contents.lines().map(OsString::from);
rustc = iter
.next()
.map(PathBuf::from)
.ok_or_else(|| anyhow::anyhow!("expected rustc as first argument"))?;
for arg in iter {
handle_arg(arg)?;
}
} else {
for arg in argv {
handle_arg(arg)?;
}
}
let file = file.ok_or_else(|| anyhow::anyhow!("could not find .rs file in rustc args"))?;
#[expect(
clippy::disallowed_methods,
reason = "internal only, no reason for config support"
)]
let idioms = env::var(IDIOMS_ENV_INTERNAL).is_ok();
#[expect(
clippy::disallowed_methods,View on GitHub (pinned to 0e07a15537)
Solutions
- Re-run `cargo fix` to let cargo regenerate the argfile.
- Check disk space / permissions in the temp directory where argfiles are written.
- Avoid the argfile path by shortening the command line (fewer features/targets).
- Reinstall/upgrade cargo if the argfile generation is buggy in your version.
Example fix
# before: argfile contents missing the rustc line # (file at @path starts with --cfg, no rustc) # after: regenerate via a clean run cargo clean cargo fix --allow-dirty
Defensive patterns
Strategy: validation
Validate before calling
// For an @path argfile, require a non-empty first line as the rustc path.
fn argfile_ok(contents: &str) -> bool {
contents.lines().next().map(|l| !l.trim().is_empty()).unwrap_or(false)
} Type guard
fn argfile_has_rustc(contents: &str) -> bool {
argfile_ok(contents)
} Try / catch
match FixArgs::from_args(argv) {
Err(e) if e.to_string().contains("expected rustc as first argument") => {
eprintln!("argfile is missing its rustc line; re-run cargo fix");
return Err(e);
}
r => r,
} Prevention
- Keep temp dir writable and with free space so argfiles are written fully.
- Shorten command lines to avoid the argfile path under cargo fix.
When it happens
Trigger: Cargo-fix proxy mode with an `@path` argfile whose contents are empty or do not start with a rustc path. Triggered when the command line is too long and cargo writes an argfile, but that file is malformed/truncated.
Common situations: A truncated/empty argfile from a filesystem issue or a crashed writer. A third-party tool that generates the argfile incorrectly. Race condition where the argfile was overwritten between write and read.
Related errors
- expected rustc or `@path` as first argument
- could not find .rs file in rustc args
- able to invoke rustc
- malformed output when learning about crate-type {} informati
- output of --print={request} missing when learning about targ
AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06).
Data as JSON: /data/errors/9a7826bb7a2db8cc.json.
Report an issue: GitHub.