{"record":{"id":"8645ae326205a5db","repo":"Schniz/fnm","slug":"can-t-read-path-to-cd-cmd","errorCode":null,"errorMessage":"Can't read path to cd.cmd","messagePattern":"Can't read path to cd\\.cmd","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/shell/windows_cmd/mod.rs","lineNumber":41,"sourceCode":"        Ok(format!(\"SET PATH={new_path}\"))\n    }\n\n    fn set_env_var(&self, name: &str, value: &str) -> String {\n        format!(\"SET {name}={value}\")\n    }\n\n    fn use_on_cd(&self, config: &crate::config::FnmConfig) -> anyhow::Result<String> {\n        let path = config.base_dir_with_default().join(\"cd.cmd\");\n        create_cd_file_at(&path).map_err(|source| {\n            anyhow::anyhow!(\n                \"Can't create cd.cmd file for use-on-cd at {}: {}\",\n                path.display(),\n                source\n            )\n        })?;\n        let path = path\n            .to_str()\n            .ok_or_else(|| anyhow::anyhow!(\"Can't read path to cd.cmd\"))?;\n        Ok(format!(\"doskey cd=\\\"{path}\\\" $*\"))\n    }\n}\n\nfn create_cd_file_at(path: &std::path::Path) -> std::io::Result<()> {\n    use std::io::Write;\n    let cmd_contents = include_bytes!(\"./cd.cmd\");\n    let mut file = std::fs::File::create(path)?;\n    file.write_all(cmd_contents)?;\n    Ok(())\n}\n\n#[cfg(test)]\nmod tests {\n    use super::*;\n\n    #[test]\n    fn use_on_cd_quotes_macro_path() {","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/Schniz/fnm/blob/86adc9676ceb2a509b21e75e74048b93c89f097d/src/shell/windows_cmd/mod.rs#L23-L59","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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.","solutions":["Print the path and validate: `echo %FNM_DIR%` / `chcp` — if characters look garbled, the encoding is wrong.","Set FNM_DIR explicitly to an ASCII path: `set FNM_DIR=C:\\fnm`.","Re-save any script that sets FNM_DIR as UTF-8/ASCII.","Long term: rename the profile folder or use a junction with an ASCII name pointing at it."],"exampleFix":":: before (script saved as ANSI with accented FNM_DIR)\nset FNM_DIR=C:\\Users\\josé\\fnm\nfnm env --shell cmd --use-on-cd   :: error: Can't read path to cd.cmd\n\n:: after\nset FNM_DIR=C:\\fnm\nfnm env --shell cmd --use-on-cd   :: doskey cd=\"C:\\fnm\\cd.cmd\" $*","handlingStrategy":"validation","validationCode":"fn base_dir_is_utf8() -> bool {\n    std::env::var_os(\"FNM_DIR\")\n        .map(|v| PathBuf::from(v).to_str().is_some())\n        .unwrap_or(true) // default OS dirs are UTF-8 in healthy setups\n}","typeGuard":"fn is_utf8_path(p: &std::path::Path) -> bool {\n    p.to_str().is_some()\n}","tryCatchPattern":null,"preventionTips":["Set FNM_DIR to an ASCII literal in scripts.","Keep Windows account names / profile paths ASCII where tooling depends on them.","Re-encode any legacy .bat files that set fnm variables."],"tags":["rust","cmd","windows","utf-8","use-on-cd"],"backgroundTag":"non-utf8-path","analyzedSha":"86adc9676ceb2a509b21e75e74048b93c89f097d","analyzedAt":"2026-08-16T21:43:05.725Z","schemaVersion":2},"datasetVersion":"2026-08-16T23:17:17.608Z"}