{"id":"c2c312c4e8258da3","repo":"rust-lang/cargo","slug":"no-such-file-or-subcommand-cmd-is-dir-suggest","errorCode":null,"errorMessage":"no such file or subcommand `{cmd}`{is_dir}{suggested_command}{suggested_script}","messagePattern":"no such file or subcommand `(.+?)`(.+?)(.+?)(.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/bin/cargo/commands/run.rs","lineNumber":136,"sourceCode":"                    \"\".to_owned()\n                } else {\n                    format!(\n                        \" {}\",\n                        args.into_iter().map(|os| os.to_string_lossy()).join(\" \")\n                    )\n                };\n                format!(\n                    \"\\nhelp: there is a command with a similar name: `{suggested_command} {actual_args}{args}`\"\n                )\n            } else {\n                \"\".to_owned()\n            };\n            let suggested_script = if let Some(suggested_script) = suggested_script(cmd) {\n                format!(\"\\nhelp: there is a script with a similar name: `{suggested_script}`\")\n            } else {\n                \"\".to_owned()\n            };\n            return Err(anyhow::anyhow!(\n                \"no such file or subcommand `{cmd}`{is_dir}{suggested_command}{suggested_script}\"\n            )\n            .into());\n        }\n        (false, false) => {\n            // HACK: duplicating the above for minor tweaks but this will all go away on\n            // stabilization\n            let possible_commands = crate::list_commands(gctx);\n            let suggested_command = if let Some(suggested_command) = possible_commands\n                .keys()\n                .filter(|c| cmd.starts_with(c.as_str()))\n                .max_by_key(|c| c.len())\n            {\n                let actual_args = cmd.strip_prefix(suggested_command).unwrap();\n                let args = if args.is_empty() {\n                    \"\".to_owned()\n                } else {\n                    format!(","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/bin/cargo/commands/run.rs#L118-L154","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Enable script support by running on nightly with `-Zscript` (e.g. `cargo +nightly -Zscript script.rs`).","If you meant a built-in subcommand, check the spelling with `cargo --list` and re-run.","If the token is a directory, `cd` into it or pass `--manifest-path` to the real package instead of the directory name.","If a similarly-named third-party command exists, install it (e.g. `cargo install cargo-<name>`)."],"exampleFix":"// before\n$ cargo ./tools/gen.rs\nerror: no such file or subcommand `./tools/gen.rs`\n\n// after (nightly)\n$ cargo +nightly -Zscript ./tools/gen.rs","handlingStrategy":"validation","validationCode":"// Before launching cargo with a file-like token, check it is intended\nlet path = std::path::Path::new(cmd);\nif path.is_file() {\n    // script mode requires nightly + -Zscript; ensure the toolchain supports it\n    if !nightly_with_zscript {\n        eprintln!(\"{cmd} is a file; enable -Zscript on nightly or use a package\");\n        return;\n    }\n}\n// else: ensure cmd is a known subcommand or installed cargo-<cmd> binary","typeGuard":"fn is_known_subcommand(cmd: &str, gctx: &GlobalContext) -> bool {\n    crate::list_commands(gctx).contains_key(cmd)\n}\n","tryCatchPattern":null,"preventionTips":["Pin the toolchain in CI (rust-toolchain.toml) so -Zscript availability is explicit.","Validate user-provided command tokens before forwarding them to cargo.","Document that single-file scripts require nightly -Zscript."],"tags":["cargo","cli","subcommand","script","unstable"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}