rust-lang/cargo · error · anyhow::Error

could not find .rs file in rustc args

Error message

could not find .rs file in rustc args

What it means

Thrown in FixArgs::from_args (src/ops/cargo_fix/mod.rs:1272) after parsing all proxy arguments: none of them was an existing .rs source file. The handle_arg closure only sets `file` when it finds an argument whose extension is `.rs` and which exists on disk; if none matched, `file` stays None and bail fires.

Source

Thrown at src/ops/cargo_fix/mod.rs:1272

                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,
            reason = "internal only, no reason for config support"
        )]
        let prepare_for_edition = env::var(EDITION_ENV_INTERNAL).ok().map(|v| {
            let enabled_edition = enabled_edition.unwrap_or(Edition::Edition2015);
            let mode = EditionFixMode::from_str(&v);
            mode.next_edition(enabled_edition)
        });

        #[expect(
            clippy::disallowed_methods,

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Ensure the .rs source file exists at the path cargo expects (it is usually src/main.rs or src/lib.rs).
  2. Re-run `cargo fix` after `cargo clean` to rebuild the unit graph against current files.
  3. If the file is generated, make sure the generator (build.rs / xtask) runs before fix.
  4. Avoid wrappers that strip the source .rs argument from the rustc command line.

Example fix

# before: src/cli.rs deleted, `cargo fix` cannot find a .rs arg
rm src/cli.rs && cargo fix

# after: restore the file then fix
git checkout src/cli.rs
cargo fix --allow-dirty
Defensive patterns

Strategy: validation

Validate before calling

// Before fix, ensure each target's .rs source still exists.
fn sources_exist(pkg: &cargo::core::Package) -> Result<(), String> {
    for t in pkg.targets() {
        if let Some(p) = t.src_path().path() {
            if !p.is_file() { return Err(format!("missing {}", p.display())); }
        }
    }
    Ok(())
}

Type guard

fn all_target_srcs_exist(pkg: &cargo::core::Package) -> bool {
    pkg.targets().iter().all(|t|
        t.src_path().path().map(|p| p.is_file()).unwrap_or(true))
}

Try / catch

match FixArgs::from_args(argv) {
    Err(e) if e.to_string().contains("could not find .rs file") => {
        eprintln!("a source .rs file is missing; restore it or run cargo clean");
        return Err(e);
    }
    r => r,
}

Prevention

When it happens

Trigger: Cargo-fix proxy mode where the rustc invocation's arguments contain no .rs path that exists (e.g. only --cfg/--emit flags, or a .rs path that was deleted/renamed between cargo's scheduling and the fix proxy run). The final `file.ok_or_else` fails.

Common situations: A source file was removed or moved after cargo scheduled the build but before the fix proxy executed. A generated source file that does not exist yet. Filesystem case mismatch on case-sensitive volumes. A wrapper that rewrites rustc args and drops the source path.

Related errors


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