rust-lang/cargo · error

no such file or subcommand `{cmd}`{is_dir}{suggested_command

Error message

no such file or subcommand `{cmd}`{is_dir}{suggested_command}{suggested_script}

What it means

Cargo raises this in exec_manifest_command when the user invokes `cargo <cmd>` where <cmd> resolves to a file path that exists (e.g. `cargo foo.rs`) but the `-Zscript` unstable feature is not enabled. Cargo cannot run a file as a script without that feature, so it reports the unknown command and appends suggestions for similarly-named built-in/third-party commands or scripts.

Source

Thrown at src/bin/cargo/commands/run.rs:136

                    "".to_owned()
                } else {
                    format!(
                        " {}",
                        args.into_iter().map(|os| os.to_string_lossy()).join(" ")
                    )
                };
                format!(
                    "\nhelp: there is a command with a similar name: `{suggested_command} {actual_args}{args}`"
                )
            } else {
                "".to_owned()
            };
            let suggested_script = if let Some(suggested_script) = suggested_script(cmd) {
                format!("\nhelp: there is a script with a similar name: `{suggested_script}`")
            } else {
                "".to_owned()
            };
            return Err(anyhow::anyhow!(
                "no such file or subcommand `{cmd}`{is_dir}{suggested_command}{suggested_script}"
            )
            .into());
        }
        (false, false) => {
            // HACK: duplicating the above for minor tweaks but this will all go away on
            // stabilization
            let possible_commands = crate::list_commands(gctx);
            let suggested_command = if let Some(suggested_command) = possible_commands
                .keys()
                .filter(|c| cmd.starts_with(c.as_str()))
                .max_by_key(|c| c.len())
            {
                let actual_args = cmd.strip_prefix(suggested_command).unwrap();
                let args = if args.is_empty() {
                    "".to_owned()
                } else {
                    format!(

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Enable script support by running on nightly with `-Zscript` (e.g. `cargo +nightly -Zscript script.rs`).
  2. If you meant a built-in subcommand, check the spelling with `cargo --list` and re-run.
  3. If the token is a directory, `cd` into it or pass `--manifest-path` to the real package instead of the directory name.
  4. If a similarly-named third-party command exists, install it (e.g. `cargo install cargo-<name>`).

Example fix

// before
$ cargo ./tools/gen.rs
error: no such file or subcommand `./tools/gen.rs`

// after (nightly)
$ cargo +nightly -Zscript ./tools/gen.rs
Defensive patterns

Strategy: validation

Validate before calling

// Before launching cargo with a file-like token, check it is intended
let path = std::path::Path::new(cmd);
if path.is_file() {
    // script mode requires nightly + -Zscript; ensure the toolchain supports it
    if !nightly_with_zscript {
        eprintln!("{cmd} is a file; enable -Zscript on nightly or use a package");
        return;
    }
}
// else: ensure cmd is a known subcommand or installed cargo-<cmd> binary

Type guard

fn is_known_subcommand(cmd: &str, gctx: &GlobalContext) -> bool {
    crate::list_commands(gctx).contains_key(cmd)
}

Prevention

When it happens

Trigger: Running `cargo ./script.rs` or `cargo path/to/file.rs` when the file exists on disk but the nightly `-Zscript` feature flag is not passed. Also reached when `is_manifest_command` matched the token but `gctx.cli_unstable().script` is false while `manifest_path.is_file()` is true (the (true, false) match arm at run.rs:100-103 falls through to the (true, true)/(false, false) logic? Actually the (true,false) arm returns its own earlier error; this errorIndex 20 is the (true, true)-but-still-fails or the directory/file variant handled in the (false, true) arm). Concretely: file exists, script enabled path not taken, and the command lookup finds no exact command.

Common situations: Developers on stable Cargo trying to run a single `.rs` file as a script; users who typo a subcommand that happens to name an existing directory; CI that passes a script path assuming script support is stable.

Related errors


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