#22813: Return clear error when exec cwd is missing instead of spawn ENOENT
size: S
trusted-contributor
## Problem
When a node exec command runs with a `cwd` that doesn't exist on the remote machine (e.g. a freshly registered agent whose workspace directory was never created), Node.js throws:
```
spawn /bin/sh ENOENT
```
This points at the shell binary rather than the real cause, sending users down a rabbit hole checking if `/bin/sh` is missing. The actual issue is that `child_process.spawn()` fails with ENOENT when its `cwd` option refers to a non-existent path.
## Fix
Check `fs.existsSync(cwd)` before calling `runCommand`. If the directory is absent, return a clear error immediately:
```
working directory not found: /home/user/.openclaw/workspace-agent
— create it on the node or check your agent workspace config
```
The check is synchronous (one `stat` call) and only runs when `params.cwd` is set.
## Test
Added `invoke.cwd.test.ts` with two cases:
- missing cwd → `ok: false`, error message contains `working directory not found` and the path, does **not** mention `/bin/sh`
- existing cwd (`os.tmpdir()`) → does not hit the cwd-not-found error path
Closes #22793
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Adds upfront `cwd` validation to prevent confusing `spawn /bin/sh ENOENT` errors when the working directory doesn't exist. The fix checks `fs.existsSync(cwd)` before spawning and returns a clear error message directing users to create the directory or check their agent workspace config.
**Key changes:**
- Added synchronous cwd existence check at invoke.ts:790-811
- Returns `INVALID_REQUEST` with descriptive error instead of letting spawn fail
- Sends `exec.denied` event with `reason: "cwd-not-found"`
- Comprehensive test coverage in new `invoke.cwd.test.ts`
**Issue found:**
- The validation is placed after the macOS exec host path (lines 578-659), so it won't protect macOS users when the companion app handles execution
<h3>Confidence Score: 3/5</h3>
- Safe to merge with a logical issue that limits effectiveness on macOS
- The PR improves error messaging and adds good test coverage, but the cwd validation is positioned after the macOS execution path, meaning macOS users won't benefit from the clearer error message when using the companion app. The fix is still valuable for other platforms and macOS fallback scenarios, but doesn't fully solve the stated problem across all execution paths.
- `src/node-host/invoke.ts` - the cwd validation should be moved before line 578 to ensure it executes on all platforms
<sub>Last reviewed commit: d0b5cf1</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
86.3%
#22812: fix(node-host): auto-create cwd before exec spawn to prevent mislea...
by aldoeliacim · 2026-02-21
85.3%
#8038: fix(exec): use spawnWithFallback to handle EBADF on macOS
by FelixFoster · 2026-02-03
75.7%
#9250: Fix: Add shell:true for Windows .cmd files to prevent spawn EINVAL ...
by vishaltandale00 · 2026-02-05
75.5%
#22929: Fix NODE_EXTRA_CA_CERTS missing from LaunchAgent environment on macOS
by Clawborn · 2026-02-21
73.1%
#9433: fix: pass raw command string to node exec instead of argv array
by dbottme · 2026-02-05
72.7%
#17815: fix: use $HOME as cwd for global update to prevent path-dedot panic
by frankekn · 2026-02-16
72.6%
#9200: Fix: Strip dangerous env vars from baseEnv in host execution
by vishaltandale00 · 2026-02-05
72.3%
#11529: fix(wizard): strip shell-style backslash escapes from workspace paths
by mcaxtr · 2026-02-07
71.9%
#22721: fix(exec): keep child runs attached on macOS for reliable output
by rylena · 2026-02-21
71.6%