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

  1. Re-run `cargo fix` to let cargo regenerate the argfile.
  2. Check disk space / permissions in the temp directory where argfiles are written.
  3. Avoid the argfile path by shortening the command line (fewer features/targets).
  4. 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

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


AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06). Data as JSON: /data/errors/9a7826bb7a2db8cc.json. Report an issue: GitHub.