Schniz/fnm · error · anyhow::Error

Can't read PATH env var

Error message

Can't read PATH env var

What it means

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`.

Source

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

use super::shell::Shell;
use std::path::Path;

#[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!(

View on GitHub (pinned to 86adc9676c)

Solutions

  1. Set a baseline PATH first: `set PATH=C:\Windows\System32;C:\Windows;C:\Windows\System32\Wbem`.
  2. For scheduled tasks, explicitly add PATH in the action or via the task's environment settings.
  3. When spawning fnm from code, keep or re-add PATH instead of `env_clear()`.
  4. Prefer the PowerShell shell (`--shell power-shell`) once PATH is restored; both need PATH, so fixing the env is the real fix.

Example fix

:: before
cmd /e:off /c "set PATH=& fnm env --shell cmd"   :: error: Can't read PATH env var

:: after
set PATH=%SystemRoot%\System32;%SystemRoot%
fnm env --shell cmd   :: SET PATH=... emitted
Defensive patterns

Strategy: validation

Validate before calling

@echo off
if "%PATH%"=="" (
  echo PATH is not set; aborting before fnm runs
  exit /b 1
)
fnm env --shell cmd

Type guard

fn has_path_env() -> bool {
    std::env::var_os("path").is_some() // case-insensitive on Windows
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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