npm / node-which

Like which(1) unix command. Find the first instance of an executable in the PATH.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] `process.env.PATH` can be undefined on Windows

ericcornelissen opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

which might fail to resolve the executable on Windows in a threaded context (e.g. a Worker threads) due to PATH being undefined, instead being accessible (only) as Path.

Prior work on this topic in this project exists, starting with #34. It suggests there used to be an alternative lookup, but it was removed for a different reason (as I understand it). The removal happened in #35 / c7a1ac9, stating that:

Reading from a Path environ on Unix systems sometimes causes strange behavior

which is too cryptic for me to understand 😅 Nevertheless, it seems like a fair concern so I think a solution to the problems should take it into account. I'd also like to point out that issues on the Node.js repository on this topic suggest PATH being undefined is 1) known behavior, and 2) not considered a bug.

Running the reproducing script (see below) myself in various settings, I must admit I was not able to reproduce this issue with 100% accuracy. It somehow seems to depend on the terminal in which I run the script, in particular: I can reproduce it using the Windows terminal with both CMD and PowerShell (but not bash, coming with git for Windows) but not with any shell in the VSCode embedded terminal...

Expected Behavior

which does not fail to resolve the executable on Windows if PATH is undefined.

My suggestion would be to consider process.env.Path when process.env.PATH is undefined, possibly conditionally on whether which is running on Windows (per c7a1ac9).

Steps To Reproduce

  1. On windows
  2. With this script (or this project):
    // file: repro.js
    
    import * as process from "node:process";
    import { Worker, isMainThread } from 'node:worker_threads';
    
    if (isMainThread) {
        console.log("[MAIN]", "PATH:", process.env.PATH);
        new Worker("./repro.js");
    } else {
        console.log("[WORK]", "PATH:", process.env.PATH);
    }
  3. Run node repro.js from PowerShell or CMD in Windows Terminal (Windows 11) or PowerShell/Command prompt (earlier Windows versions).
  4. Observe that the [MAIN] PATH: prints the full path but the [WORK] PATH: is undefined.

Environment

  • npm: v9.8.1
  • Node: v18.16.1
  • OS: Windows 11 Home (22H2)
  • platform: Virtual Machine (LXC)