#22812: fix(node-host): auto-create cwd before exec spawn to prevent misleading ENOENT
size: XS
trusted-contributor
## Problem
When `exec` runs on a node and the agent workspace directory doesn't exist on the remote machine, Node.js throws `spawn /bin/sh ENOENT`. This misleadingly points at the shell binary rather than the real cause: the specified `cwd` directory is missing.
This blocks all exec commands on newly registered agent/node pairs until the user manually creates the workspace directory, and the error message sends users on a wild goose chase debugging shell binary paths.
## Fix
Add a best-effort `fs.mkdirSync(cwd, { recursive: true })` in `runCommand()` before spawning the child process. The `recursive: true` flag is a no-op if the directory already exists. The try/catch ensures that if mkdir fails (e.g. permission denied), the original spawn error still surfaces.
## Changes
- `src/node-host/invoke.ts`: Auto-create `cwd` directory before `spawn()` call
Fixes #22793
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Auto-creates the working directory (`cwd`) before spawning child processes in `runCommand()` to prevent misleading `ENOENT` errors. When the `cwd` doesn't exist, Node.js previously surfaced the error as "spawn /bin/sh ENOENT", incorrectly pointing at the shell binary instead of the missing directory. The fix uses `fs.mkdirSync(cwd, { recursive: true })` in a try/catch block - if directory creation fails, the original spawn error will still surface, ensuring proper error reporting.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- The change is minimal, focused, and follows established patterns in the codebase. The fix uses `recursive: true` which is a no-op for existing directories, and the try/catch ensures that failures don't mask underlying issues. The approach matches similar `mkdirSync` patterns used throughout the codebase (found in 100+ locations). No security concerns - the code properly handles errors and doesn't introduce new attack vectors.
- No files require special attention
<sub>Last reviewed commit: 2311143</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#23287: fix(node-host): improve ENOENT error when exec workspace dir is mis...
by SidQin-cyber · 2026-02-22
87.9%
#22813: Return clear error when exec cwd is missing instead of spawn ENOENT
by Clawborn · 2026-02-21
85.3%
#8038: fix(exec): use spawnWithFallback to handle EBADF on macOS
by FelixFoster · 2026-02-03
75.3%
#9433: fix: pass raw command string to node exec instead of argv array
by dbottme · 2026-02-05
74.4%
#9250: Fix: Add shell:true for Windows .cmd files to prevent spawn EINVAL ...
by vishaltandale00 · 2026-02-05
73.7%
#17815: fix: use $HOME as cwd for global update to prevent path-dedot panic
by frankekn · 2026-02-16
71.3%
#11878: fix: add missing error handler to execDocker child process
by Yida-Dev · 2026-02-08
70.9%
#10729: fix: replace existsSync+readFileSync with try-catch in env-file
by Yida-Dev · 2026-02-06
70.8%
#9200: Fix: Strip dangerous env vars from baseEnv in host execution
by vishaltandale00 · 2026-02-05
70.4%
#15746: docs(nodes): add exec cwd validation errors to troubleshooting
by andrewdamelio · 2026-02-13
70.3%