Schniz/fnm · error · anyhow::Error

Can't convert path to string

Error message

Can't convert path to string

What it means

After rejoining the PATH under WindowsCmd, the result must be converted to a `&str` to emit `SET PATH=...`. If the joined PATH contains any non-UTF-8 bytes (from a pre-existing entry), `to_str()` returns None and this anyhow error is produced.

Source

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

#[derive(Debug)]
pub struct WindowsCmd;

impl Shell for WindowsCmd {
    fn to_clap_shell(&self) -> clap_complete::Shell {
        // TODO: move to Option
        panic!("Shell completion is not supported for Windows Command Prompt. Maybe try using PowerShell for a better experience?");
    }

    fn path(&self, path: &Path) -> anyhow::Result<String> {
        let current_path =
            std::env::var_os("path").ok_or_else(|| anyhow::anyhow!("Can't read PATH env var"))?;
        let mut split_paths: Vec<_> = std::env::split_paths(&current_path).collect();
        split_paths.insert(0, path.to_path_buf());
        let new_path = std::env::join_paths(split_paths)
            .map_err(|err| anyhow::anyhow!("Can't join paths: {err}"))?;
        let new_path = new_path
            .to_str()
            .ok_or_else(|| anyhow::anyhow!("Can't convert path to string"))?;
        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()

View on GitHub (pinned to 86adc9676c)

Solutions

  1. Inspect PATH entries for garbled characters (`echo %PATH%`); rename the offending folders to ASCII names.
  2. Rebuild PATH from scratch with known-good ASCII entries.
  3. Save any .bat/.cmd scripts that modify PATH as UTF-8 (or plain ASCII).
  4. Enable the Windows UTF-8 system locale option and recreate affected directories.

Example fix

:: before
set PATH=C:\Users\<ANSI-garbled>\bin;%PATH%
fnm env --shell cmd   :: error: Can't convert path to string

:: after
set PATH=C:\Users\me\bin;%PATH%
fnm env --shell cmd   :: ok
Defensive patterns

Strategy: validation

Validate before calling

fn path_entries_are_utf8() -> bool {
    std::env::var_os("path")
        .map(|p| std::env::split_paths(&p).all(|e| e.as_os_str().to_str().is_some()))
        .unwrap_or(false)
}

Type guard

fn is_utf8_os_string(s: &std::ffi::OsStr) -> bool {
    s.to_str().is_some()
}

Prevention

When it happens

Trigger: `fnm env --shell cmd` when some existing PATH entry uses a legacy ANSI encoding (non-UTF-8 bytes) so the joined OsString cannot round-trip through Rust's UTF-8 strings.

Common situations: Windows profiles where a folder in PATH was created under a non-Unicode code page (e.g. Cyrillic/Shift-JIS CP paths); env vars written by old installers in OEM encoding; batch files saved as ANSI setting PATH entries with accented characters.

Related errors


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