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

Error: unable to find a config file for the given path: `{}`

Error message

Error: unable to find a config file for the given path: `{}`

What it means

Returned by rustfmt's config_path resolver when the user passes --config-path pointing at a path that either does not exist, or is a directory containing no rustfmt.toml/.rustfmt.toml. ErrorKind::NotFound with the offending path interpolated. It fires from config_path_not_found for both the non-existent-file case and the directory-without-config case.

Source

Thrown at src/tools/rustfmt/src/config/mod.rs:524

            // `NotFound` => file not found
            // `NotADirectory` => rare case where expected directory is a file
            // Otherwise, return the error
            Err(e) => {
                if !matches!(e.kind(), ErrorKind::NotFound | ErrorKind::NotADirectory) {
                    let ctx = format!("Failed to get metadata for config file {:?}", &config_file);
                    let err = anyhow::Error::new(e).context(ctx);
                    return Err(Error::new(ErrorKind::Other, err));
                }
            }
            _ => {}
        }
    }
    Ok(None)
}

fn config_path(options: &dyn CliOptions) -> Result<Option<PathBuf>, Error> {
    let config_path_not_found = |path: &str| -> Result<Option<PathBuf>, Error> {
        Err(Error::new(
            ErrorKind::NotFound,
            format!(
                "Error: unable to find a config file for the given path: `{}`",
                path
            ),
        ))
    };

    // Read the config_path and convert to parent dir if a file is provided.
    // If a config file cannot be found from the given path, return error.
    match options.config_path() {
        Some(path) if !path.exists() => config_path_not_found(path.to_str().unwrap()),
        Some(path) if path.is_dir() => {
            let config_file_path = get_toml_path(path)?;
            if config_file_path.is_some() {
                Ok(config_file_path)
            } else {
                config_path_not_found(path.to_str().unwrap())

View on GitHub (pinned to 7088e4b63a)

Solutions

  1. Verify the path exists: `ls -l <path>` from the same working directory rustfmt uses.
  2. If passing a directory, add a rustfmt.toml (or .rustfmt.toml) inside it.
  3. If passing a file, ensure the filename is correct and the file is readable.
  4. Drop --config-path to let rustfmt auto-discover, or create the config with `rustfmt --print-default-config > rustfmt.toml`.

Example fix

# before
rustfmt --config-path ./confg/rustfmt.toml src/lib.rs   # typo

# after
rustfmt --config-path ./config/rustfmt.toml src/lib.rs
Defensive patterns

Strategy: validation

Validate before calling

fn config_path_valid(p: &std::path::Path) -> io::Result<()> {
    if !p.exists() {
        return Err(io::Error::new(io::ErrorKind::NotFound, format!("missing: {p:?}")));
    }
    if p.is_dir() {
        let has = ["rustfmt.toml", ".rustfmt.toml"].iter().any(|n| p.join(n).is_file());
        if !has { return Err(io::Error::new(io::ErrorKind::NotFound, "no config in dir")); }
    }
    Ok(())
}

Try / catch

match config_path(opts) {
    Ok(p) => Ok(p),
    Err(e) if e.kind() == io::ErrorKind::NotFound
        && e.to_string().contains("unable to find a config file") => {
        // correct --config-path or create the file
        Err(e)
    }
    Err(e) => Err(e),
}

Prevention

When it happens

Trigger: Running `rustfmt --config-path <p>` where <p> does not exist, or where <p> is a directory that contains neither rustfmt.toml nor .rustfmt.toml. get_toml_path returns None for the directory and config_path_not_found raises the error.

Common situations: Typo in --config-path; pointing at a file that was deleted/moved; expecting a config in a directory that does not have one; relative path resolved against an unexpected working directory.

Related errors


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