{"record":{"id":"2285bd12d7c9da43","repo":"Schniz/fnm","slug":"can-t-read-path-env-var-2285bd","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/windows_cmd/mod.rs","lineNumber":15,"sourceCode":"use super::shell::Shell;\nuse std::path::Path;\n\n#[derive(Debug)]\npub struct WindowsCmd;\n\nimpl Shell for WindowsCmd {\n    fn to_clap_shell(&self) -> clap_complete::Shell {\n        // TODO: move to Option\n        panic!(\"Shell completion is not supported for Windows Command Prompt. Maybe try using PowerShell for a better experience?\");\n    }\n\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(|err| anyhow::anyhow!(\"Can't join paths: {err}\"))?;\n        let new_path = new_path\n            .to_str()\n            .ok_or_else(|| anyhow::anyhow!(\"Can't convert path to string\"))?;\n        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!(","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/Schniz/fnm/blob/86adc9676ceb2a509b21e75e74048b93c89f097d/src/shell/windows_cmd/mod.rs#L1-L33","documentation":"The Windows Command Prompt shell implementation reads the process PATH via `std::env::var_os(\"path\")` (case-insensitive on Windows, so the same variable) to prepend fnm's bin dir and emit `SET PATH=...`. If PATH is entirely absent from the environment, `var_os` yields None and this anyhow error is returned by `fnm env --shell cmd` / `fnm use`.","triggerScenarios":"Running `fnm env --shell cmd` in a process spawned with an empty or scrubbed environment (env -i, env_clear(), stripped scheduled task, or a service context) where PATH is not defined.","commonSituations":"Windows scheduled tasks / services with no inherited user env; CI agents that sanitize the environment; Docker Windows containers with a minimal env block; test harnesses clearing env before invoking the binary.","solutions":["Set a baseline PATH first: `set PATH=C:\\Windows\\System32;C:\\Windows;C:\\Windows\\System32\\Wbem`.","For scheduled tasks, explicitly add PATH in the action or via the task's environment settings.","When spawning fnm from code, keep or re-add PATH instead of `env_clear()`.","Prefer the PowerShell shell (`--shell power-shell`) once PATH is restored; both need PATH, so fixing the env is the real fix."],"exampleFix":":: before\ncmd /e:off /c \"set PATH=& fnm env --shell cmd\"   :: error: Can't read PATH env var\n\n:: after\nset PATH=%SystemRoot%\\System32;%SystemRoot%\nfnm env --shell cmd   :: SET PATH=... emitted","handlingStrategy":"validation","validationCode":"@echo off\nif \"%PATH%\"==\"\" (\n  echo PATH is not set; aborting before fnm runs\n  exit /b 1\n)\nfnm env --shell cmd","typeGuard":"fn has_path_env() -> bool {\n    std::env::var_os(\"path\").is_some() // case-insensitive on Windows\n}","tryCatchPattern":null,"preventionTips":["Define PATH explicitly in scheduled tasks and service wrappers that call fnm.","Don't strip the environment when shelling out to fnm from tests.","Prefer documented baseline PATH entries in container images."],"tags":["rust","cmd","windows","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"}