{"record":{"id":"88bbbe860d521782","repo":"Schniz/fnm","slug":"can-t-read-path-env-var","errorCode":null,"errorMessage":"Can't read PATH env var","messagePattern":"Can't read PATH env var","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/shell/powershell.rs","lineNumber":13,"sourceCode":"use crate::version_file_strategy::VersionFileStrategy;\n\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 {","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/Schniz/fnm/blob/86adc9676ceb2a509b21e75e74048b93c89f097d/src/shell/powershell.rs#L1-L31","documentation":"PowerShell's `Shell::path` reads the current process PATH with `std::env::var_os(\"PATH\")` so it can prepend fnm's bin dir and re-emit `$env:PATH = ...`. If no PATH variable exists in the environment at all, `var_os` returns None and this anyhow error is produced while running `fnm env --shell power-shell` or `fnm use`.","triggerScenarios":"`fnm env --shell power-shell` / `fnm use` in a process whose environment has no PATH — spawned via `env -i`, `Command::env_clear()` without re-adding PATH, a stripped CI/cron/scheduled-task environment, or a minimal container.","commonSituations":"Minimal Docker images invoked with a sanitized env; Windows scheduled tasks or services with no user environment; CI steps that `Remove-Item Env:PATH` or start powershell with `-Command` from a bare service; test harnesses that clear the environment.","solutions":["Restore a PATH before invoking fnm: PowerShell `$env:PATH = [Environment]::GetEnvironmentVariable('Path','Machine') + ';' + [Environment]::GetEnvironmentVariable('Path','User')`.","In containers/cron, export a baseline: `PATH=/usr/local/bin:/usr/bin:/bin` (Windows: `set PATH=C:\\Windows\\System32;C:\\Windows`).","If spawning fnm from code, replace `env_clear()` with selective env removal or re-pass PATH explicitly.","Update fnm; newer releases may degrade gracefully by emitting only fnm's bin dir."],"exampleFix":"# before (PowerShell, PATH was removed)\nRemove-Item Env:PATH\nfnm env --shell power-shell   # error: Can't read PATH env var\n\n# after\n$env:PATH = [Environment]::GetEnvironmentVariable('Path','Machine')\nfnm env --shell power-shell   # $env:PATH = ... emitted","handlingStrategy":"validation","validationCode":"// PowerShell: fail fast before calling fnm\nif (-not $env:PATH) { throw \"PATH is not set; refusing to run fnm env\" }\nfnm env --shell power-shell","typeGuard":"fn has_path_env() -> bool {\n    std::env::var_os(\"PATH\").is_some()\n}","tryCatchPattern":"$out = fnm env --shell power-shell 2>&1\nif ($LASTEXITCODE -ne 0 -and \"$out\" -match \"Can't read PATH env var\") {\n    $env:PATH = [Environment]::GetEnvironmentVariable('Path','Machine')\n    $out = fnm env --shell power-shell\n}","preventionTips":["Never spawn fnm from `env -i` or after env_clear() without re-adding PATH.","In cron/systemd/containers, always define a baseline PATH.","Add a PATH precheck to wrapper scripts that call fnm."],"tags":["rust","powershell","environment-variable","path","fnm-env"],"backgroundTag":"missing-env-var","analyzedSha":"86adc9676ceb2a509b21e75e74048b93c89f097d","analyzedAt":"2026-08-16T21:43:05.725Z","schemaVersion":2},"datasetVersion":"2026-08-16T23:17:17.608Z"}