Skip to content

fix(win): resolve relative file that exists in the process cwd - #1006

Open
kana001-bit wants to merge 1 commit into
microsoft:mainfrom
kana001-bit:fix/win-get-shell-path-cwd-match
Open

kana001-bit wants to merge 1 commit into
microsoft:mainfrom
kana001-bit:fix/win-get-shell-path-cwd-match

Conversation

@kana001-bit

Copy link
Copy Markdown

Fixes #1005

On Windows, spawning a shell by bare name (e.g. cmd.exe) fails with File not found: and an empty path when the calling process's current working directory contains that file, e.g. when an Electron app is launched with its cwd set to C:\Windows\System32.

Cause

path_util::get_shell_path first checks whether the relative name exists in the process cwd, but in that case it returns shellpath, which is still empty:

if (file_exists(filename)) {
  return shellpath;   // empty
}

startProcess then rejects the empty path, so the error message also shows no path.

Fix

Return filename when it exists in the cwd. The resolved path is only used for the existence check in startProcess; the process itself is still created from the command line, and the cwd is part of the normal CreateProcessW search order, so the spawn behaves the same as it does from any other cwd.

Repro

process.chdir('C:/Windows/System32');
require('node-pty').spawn('cmd.exe', [], {}); // Error: File not found:

Tests

Added a relative file test to windowsTerminal.test.ts that changes the process cwd to %windir%\System32 and spawns cmd.exe. It fails with File not found: before the fix and passes after it, with useConptyDll both false and true.

Found while debugging a downstream report in Orca: stablyai/orca#17083

🤖 Generated with Claude Code

get_shell_path returned an empty string when the relative file name
existed in the current working directory, so spawning e.g. "cmd.exe"
from a process whose cwd is C:\Windows\System32 failed with
"File not found: " and an empty path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kana001-bit

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

kana001-bit added a commit to kana001-bit/orca that referenced this pull request Nov 1, 2026
node-pty's get_shell_path returned an empty path when a bare shell
name such as cmd.exe existed in the process cwd, so launching Orca
with cwd C:\Windows\System32 made Command Prompt and WSL panes fail
with "File not found: ". Patch it to return the cwd match, mirroring
microsoft/node-pty#1006.

Fixes stablyai#17083

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@robertobandini

Copy link
Copy Markdown

Hi kana001-bit! Thank you for the fix.

We hit the same bug in Freelens, an Electron app (freelensapp/freelens#2918): with wsl.exe as the terminal shell, the terminal failed with File not found: when the app started with C:\Windows\System32 as its current folder. I reproduced it with node-pty 1.2.0-beta.18 on Windows 11, and the full path works from the same folder.

While reading get_shell_path I found a second gap in the same function: the loop that splits Path on ; never adds the part after the last ;, so the last directory is not searched when Path does not end with ;. With node-pty 1.2.0-beta.18, started from C:\, cmd.exe fails with File not found: when Path is C:\NonExistent;C:\Windows\System32, and starts when it is C:\NonExistent;C:\Windows\System32; or C:\Windows\System32;C:\NonExistent. It may fit in this PR or in a follow-up.

In Freelens we're adding a lookup on PATH before calling node-pty (freelensapp/freelens#2941).

@kana001-bit

Copy link
Copy Markdown
Author

Thanks Roberto Bandini (@robertobandini), good to know it hit Freelens too, and that it still reproduces on 1.2.0-beta.18.

I confirmed the second gap: with Path set to C:\NonExistent;C:\Windows\System32, cmd.exe fails with File not found: , and it starts with a trailing ; or with the order reversed. To keep this PR focused, I opened a separate PR with its own test: #1013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows: spawning a bare shell name fails with "File not found: " when the process cwd contains that file

2 participants