Schniz/fnm · error · anyhow::Error

Can't read path to cd.cmd

Error message

Can't read path to cd.cmd

What it means

After cd.cmd is written successfully, WindowsCmd's `use_on_cd` needs the file's path as a `&str` to emit a `doskey cd="<path>" $*` macro. If the fnm base directory (FNM_DIR or the OS data dir) contains non-UTF-8 bytes, `to_str()` returns None and this anyhow error is returned.

Source

Thrown at src/shell/windows_cmd/mod.rs:41

        Ok(format!("SET PATH={new_path}"))
    }

    fn set_env_var(&self, name: &str, value: &str) -> String {
        format!("SET {name}={value}")
    }

    fn use_on_cd(&self, config: &crate::config::FnmConfig) -> anyhow::Result<String> {
        let path = config.base_dir_with_default().join("cd.cmd");
        create_cd_file_at(&path).map_err(|source| {
            anyhow::anyhow!(
                "Can't create cd.cmd file for use-on-cd at {}: {}",
                path.display(),
                source
            )
        })?;
        let path = path
            .to_str()
            .ok_or_else(|| anyhow::anyhow!("Can't read path to cd.cmd"))?;
        Ok(format!("doskey cd=\"{path}\" $*"))
    }
}

fn create_cd_file_at(path: &std::path::Path) -> std::io::Result<()> {
    use std::io::Write;
    let cmd_contents = include_bytes!("./cd.cmd");
    let mut file = std::fs::File::create(path)?;
    file.write_all(cmd_contents)?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn use_on_cd_quotes_macro_path() {

View on GitHub (pinned to 86adc9676c)

Solutions

  1. Print the path and validate: `echo %FNM_DIR%` / `chcp` — if characters look garbled, the encoding is wrong.
  2. Set FNM_DIR explicitly to an ASCII path: `set FNM_DIR=C:\fnm`.
  3. Re-save any script that sets FNM_DIR as UTF-8/ASCII.
  4. Long term: rename the profile folder or use a junction with an ASCII name pointing at it.

Example fix

:: before (script saved as ANSI with accented FNM_DIR)
set FNM_DIR=C:\Users\josé\fnm
fnm env --shell cmd --use-on-cd   :: error: Can't read path to cd.cmd

:: after
set FNM_DIR=C:\fnm
fnm env --shell cmd --use-on-cd   :: doskey cd="C:\fnm\cd.cmd" $*
Defensive patterns

Strategy: validation

Validate before calling

fn base_dir_is_utf8() -> bool {
    std::env::var_os("FNM_DIR")
        .map(|v| PathBuf::from(v).to_str().is_some())
        .unwrap_or(true) // default OS dirs are UTF-8 in healthy setups
}

Type guard

fn is_utf8_path(p: &std::path::Path) -> bool {
    p.to_str().is_some()
}

Prevention

When it happens

Trigger: `fnm env --shell cmd --use-on-cd` when the resolved base dir path holds invalid UTF-8 — e.g. a Windows profile directory with a legacy-encoded username, or FNM_DIR exported from a script saved in a non-UTF-8 code page.

Common situations: Windows accounts with non-ASCII/legacy-encoded usernames making C:\Users\<name> non-UTF-8; FNM_DIR set inside an ANSI-encoded .bat file with accented characters; paths restored from old backups with mangled encodings.

Related errors


AI-assisted analysis of Schniz/fnm@86adc9676c (2026-08-16). Data as JSON: /api/errors/8645ae326205a5db. Report an issue: GitHub.