{"record":{"id":"39286eae9635a0ef","repo":"Schniz/fnm","slug":"can-t-read-path","errorCode":null,"errorMessage":"Can't read PATH","messagePattern":"Can't read PATH","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/shell/powershell.rs","lineNumber":20,"sourceCode":"\nuse super::Shell;\nuse indoc::formatdoc;\nuse std::path::Path;\n\n#[derive(Debug)]\npub struct PowerShell;\n\nimpl Shell for PowerShell {\n    fn path(&self, path: &Path) -> anyhow::Result<String> {\n        let current_path =\n            std::env::var_os(\"PATH\").ok_or_else(|| anyhow::anyhow!(\"Can't read PATH env var\"))?;\n        let mut split_paths: Vec<_> = std::env::split_paths(&current_path).collect();\n        split_paths.insert(0, path.to_path_buf());\n        let new_path = std::env::join_paths(split_paths)\n            .map_err(|source| anyhow::anyhow!(\"Can't join paths: {source}\"))?;\n        let new_path = new_path\n            .to_str()\n            .ok_or_else(|| anyhow::anyhow!(\"Can't read PATH\"))?;\n        Ok(self.set_env_var(\"PATH\", new_path))\n    }\n\n    fn set_env_var(&self, name: &str, value: &str) -> String {\n        format!(r#\"$env:{name} = \"{value}\"\"#)\n    }\n\n    fn use_on_cd(&self, config: &crate::config::FnmConfig) -> anyhow::Result<String> {\n        let version_file_exists_condition = if config.resolve_engines() {\n            \"(Test-Path .nvmrc) -Or (Test-Path .node-version) -Or (Test-Path package.json)\"\n        } else {\n            \"(Test-Path .nvmrc) -Or (Test-Path .node-version)\"\n        };\n        let autoload_hook = match config.version_file_strategy() {\n            VersionFileStrategy::Local => formatdoc!(\n                r\"\n                    If ({version_file_exists_condition}) {{ & fnm use --silent-if-unchanged }}\n                \",","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/Schniz/fnm/blob/86adc9676ceb2a509b21e75e74048b93c89f097d/src/shell/powershell.rs#L2-L38","documentation":"Once the new PATH is joined, PowerShell's `Shell::path` must render it inside a plain-string snippet (`$env:PATH = \"...\"`), so the joined OsString is converted with `to_str()`. If any pre-existing PATH entry holds bytes that are not valid UTF-8, the conversion returns None and this anyhow error fires.","triggerScenarios":"`fnm env --shell power-shell` when at least one existing PATH entry is non-UTF-8 — e.g. a directory created under an ANSI code page (Shift-JIS, Latin-1) on Windows, or a raw-byte path on Unix — so the joined string cannot be expressed as Rust `&str`.","commonSituations":"Non-English Windows profiles with legacy-encoded folder names in PATH (e.g. `C:\\Users\\< Shift-JIS name >\\bin`); mojibake entries pasted from webpages; WSL paths with invalid bytes; PATH edited via tooling that wrote the registry value in the wrong encoding.","solutions":["Identify suspicious entries: iterate `($env:PATH -split ';')` and look for garbled/replacement characters.","Remove or rename the offending entry (rename the folder to ASCII, or drop it from PATH via system settings).","Re-set PATH from a known-good value: `[Environment]::SetEnvironmentVariable('Path', $clean, 'User')`.","Ensure system locale / 'Beta: Use Unicode UTF-8 for worldwide language support' is consistent with how folders were named."],"exampleFix":"# before\n$env:PATH = \"C:\\Users\\<garbled-ansi-name>\\bin;\" + $env:PATH\nfnm env --shell power-shell   # error: Can't read PATH\n\n# after\n$env:PATH = \"C:\\Users\\me\\bin;\" + $env:PATH\nfnm env --shell power-shell   # ok","handlingStrategy":"validation","validationCode":"fn path_entries_are_utf8() -> bool {\n    std::env::var_os(\"PATH\")\n        .map(|p| std::env::split_paths(&p).all(|e| e.as_os_str().to_str().is_some()))\n        .unwrap_or(false)\n}","typeGuard":"fn is_utf8_os_string(s: &std::ffi::OsStr) -> bool {\n    s.to_str().is_some()\n}","tryCatchPattern":"if !path_entries_are_utf8() {\n    eprintln!(\"PATH contains non-UTF-8 entries; fix them before running fnm env\");\n    std::process::exit(1);\n}","preventionTips":["Keep PATH entries ASCII or proper UTF-8; rename legacy-encoded folders.","Use the Windows UTF-8 system locale when creating directories that will appear in PATH.","Set PATH from a verified script rather than accumulating entries from old installers."],"tags":["rust","powershell","utf-8","path","windows","fnm-env"],"backgroundTag":"non-utf8-path","analyzedSha":"86adc9676ceb2a509b21e75e74048b93c89f097d","analyzedAt":"2026-08-16T21:43:05.725Z","schemaVersion":2},"datasetVersion":"2026-08-16T23:17:17.608Z"}