rust-lang/rust · error · io::Error

Could not run rustfmt, please make sure it is in your PATH.

Error message

Could not run rustfmt, please make sure it is in your PATH.

What it means

Returned by cargo-fmt's get_rustfmt_info when spawning the rustfmt child process fails with ErrorKind::NotFound. cargo-fmt translates the raw NotFound into a friendlier message directing the user to install/PATH rustfmt. All other spawn errors propagate unchanged. This is the entry-point info-gathering spawn (e.g. --help/version passthrough).

Source

Thrown at src/tools/rustfmt/src/cargo-fmt/main.rs:262

}

fn handle_command_status(status: Result<i32, io::Error>) -> i32 {
    match status {
        Err(e) => {
            print_usage_to_stderr(&e.to_string());
            FAILURE
        }
        Ok(status) => status,
    }
}

fn get_rustfmt_info(args: &[String]) -> Result<i32, io::Error> {
    let mut command = rustfmt_command()
        .stdout(std::process::Stdio::inherit())
        .args(args)
        .spawn()
        .map_err(|e| match e.kind() {
            io::ErrorKind::NotFound => io::Error::new(
                io::ErrorKind::Other,
                "Could not run rustfmt, please make sure it is in your PATH.",
            ),
            _ => e,
        })?;
    let result = command.wait()?;
    if result.success() {
        Ok(SUCCESS)
    } else {
        Ok(result.code().unwrap_or(SUCCESS))
    }
}

fn format_crate(
    verbosity: Verbosity,
    strategy: &CargoFmtStrategy,
    rustfmt_args: Vec<String>,
    manifest_path: Option<&Path>,

View on GitHub (pinned to 7088e4b63a)

Solutions

  1. Install the component: `rustup component add rustfmt` (or rustfmt-preview on nightly).
  2. Confirm the active toolchain has rustfmt: `rustup which rustfmt` / `rustfmt --version`.
  3. Ensure the directory containing rustfmt is on PATH for the shell/IDE launching cargo-fmt.
  4. If using a custom toolchain, build/install rustfmt into that toolchain's bin dir.

Example fix

# before: cargo fmt -> Could not run rustfmt...

# after
rustup component add rustfmt
cargo fmt
Defensive patterns

Strategy: validation

Validate before calling

fn rustfmt_present() -> bool {
    std::process::Command::new("rustfmt").arg("--version").output().is_ok()
}
// Before running cargo fmt: assert rustfmt_present().

Try / catch

match get_rustfmt_info(args) {
    Ok(code) => Ok(code),
    Err(e) if e.to_string().contains("make sure it is in your PATH") => {
        // instruct: rustup component add rustfmt
        Err(e)
    }
    Err(e) => Err(e),
}

Prevention

When it happens

Trigger: Running `cargo fmt` (which is cargo-fmt) when the rustfmt executable cannot be found on PATH - component not installed via rustup, custom toolchain missing rustfmt, or PATH not propagated to the shell. The .spawn() call returns NotFound and is mapped to this message.

Common situations: rustfmt/rustfmt-preview component not installed for the active toolchain; using a minimal/custom toolchain without rustfmt; running inside an IDE/clean environment with a stripped PATH; wrong toolchain selected by rustup.

Related errors


AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10). Data as JSON: /api/errors/d9009bc41080457c. Report an issue: GitHub.