macOS terminal cwd detection mis-parses lsof output when an open file path contains "cwd"
- Dominant language
- TypeScript
- Stars
- 193k
- Forks
- 42.4k
- PR merge metrics
- PR metrics pending
Description
On macOS `TerminalProcess.getCwd()` resolves the shell's cwd with
```
lsof -OPln -p | grep cwd
```
then takes `stdout.substring(stdout.indexOf('/'), stdout.length - 1)`, assuming grep matched exactly one line ([terminalProcess.ts#L618](https://github.com/microsoft/vscode/blob/main/src/vs/platform/terminal/node/terminalProcess.ts#L618)).
`grep cwd` matches anywhere in the line, not just the FD column. If the process has any open file whose path contains the substring "cwd", the parsed value becomes several lines glued together. With Nix this actually happens: store hashes are base32, and the current gettext-0.25.1 hash starts with "cwdz", so every shell linking it triggers the bug:
```
$ lsof -OPln -p 1375 | grep cwd
fish 1375 501 cwd DIR 1,15 1088 182604824 /Users/andyl/Projects/cheese-backend-py
fish 1375 501 txt REG 1,14 227968 35515 /nix/store/cwdz2vy0kkc17aw2s1b3sw7wspy8rs7j-gettext-0.25.1/lib/libintl.8.dylib
```
Anything consuming the value gets a bogus path. The visible symptom: terminal session restore fails for every terminal after every VS Code update or restart, with
```
The terminal process failed to launch: Starting directory (cwd) "/Users/andyl/Projects/MyClaw
fish 78190 501 txt REG 1,19 227968 35515 /nix/store/cwdz2vy0kkc17aw2s1b3sw7wspy8rs7j-gettext-0.25.1/lib/libintl.8.dylib" does not exist.
```
Split terminals inheriting cwd hit the same code path.
**Steps to reproduce:**
1. macOS, shell installed via Nix so that it maps a store path containing "cwd" (fish + gettext-0.25.1 currently does)
2. Open a terminal, quit and relaunch VS Code so persistent sessions revive it
3. Revive fails with the error above
**Suggested fix:** query the FD directly with field output, which is unambiguous:
```
lsof -a -p -d cwd -F n
```
and take the line starting with `n`. Or minimally, `grep ' cwd '` so only the FD column can match. Related: #318863 complains about the cost of this same invocation.
VS Code 1.128.0, also present on current main. Happy to send a PR.
Contributor guide
Assessment
This issue has not been assessed yet.