Skip to content

fix(unix): don't rewrite app.asar.unpacked paths a second time - #924

Open
arthur791004 wants to merge 1 commit into
microsoft:mainfrom
arthur791004:fix-asar-unpacked-double-replace
Open

arthur791004 wants to merge 1 commit into
microsoft:mainfrom
arthur791004:fix-asar-unpacked-double-replace

Conversation

@arthur791004

Copy link
Copy Markdown

Summary

Fixes #923.

src/unixTerminal.ts#L19-L20 does:

helperPath = helperPath.replace('app.asar', 'app.asar.unpacked');
helperPath = helperPath.replace('node_modules.asar', 'node_modules.asar.unpacked');

String.prototype.replace with a string argument matches the first occurrence. When the resolved `helperPath` already contains `app.asar.unpacked/…` (e.g. a caller of node-pty that itself lives inside `app.asar.unpacked`, bypassing Electron's asar shim — see #923 for the concrete repro), the substring `app.asar` matches the prefix of `app.asar.unpacked` and we produce a bogus `…/app.asar.unpacked.unpacked/…` path. `posix_spawn` then fails with `ENOENT` and node-pty surfaces the misleading `posix_spawnp failed.`.

This PR guards each rewrite so it only fires when the unpacked variant isn't already present.

Test plan

  • Existing unix-terminal tests still pass (`npm test` → 17/17).
  • Manual repro on macOS arm64 from the linked issue's setup: before this patch `pty.spawn` throws `posix_spawnp failed.`; after this patch it spawns normally.

helperPath.replace('app.asar', 'app.asar.unpacked') matches the first
occurrence. When helperPath already contains 'app.asar.unpacked/...'
(e.g. a caller of node-pty that itself lives inside app.asar.unpacked,
bypassing Electron's asar shim), the substring 'app.asar' matches the
prefix of 'app.asar.unpacked' and we produce 'app.asar.unpacked.unpacked/...'
— a path that doesn't exist on disk. posix_spawn then fails with ENOENT
and node-pty throws the misleading "posix_spawnp failed.".

Skip each rewrite when the unpacked variant is already present.

Fixes microsoft#923.
@feiiiiii5

Copy link
Copy Markdown

Hi — we hit this exact bug in a packaged Electron app on macOS 26 and verified your fix end-to-end. Sharing the evidence and a suggestion:

Reproduction (real runtime): loading node-pty 1.1.0 from an asar layout where it lives under app.asar.unpacked (the standard layout for native modules), every PTY spawn fails with posix_spawnp failed. even for /usr/bin/true. Captured helperPath passed to the native fork: .../app.asar.unpacked.unpacked/node_modules/node-pty/build/Release/spawn-helper. Plain child_process.spawn works, and a byte-level replication of the native spawn sequence with the correct helper path succeeds — confirming the failure is purely the rewritten helper path.

Your patch works: with the guarded rewrite applied, a real /bin/bash PTY spawn succeeds in the same runtime.

Suggestion — this needs tests: the inline rewrite is untestable as written. I've opened #953 which extracts resolveSpawnHelperPath() into a pure function and adds 4 regression tests (RED before the guard: the two "already-unpacked" cases produce app.asar.unpacked.unpacked; GREEN after; full macOS suite 28 passing, eslint clean). Happy to fold those tests into this PR instead if you'd prefer that shape, or rebase #953 on top of this one.

@arthur791004

Copy link
Copy Markdown
Author

Thanks for the verification and for adding the tests — that's a good improvement over what I had here. I'm fine either way 🙂

@feiiiiii5

Copy link
Copy Markdown

Thanks! Happy to defer to whichever variant is easier to maintain upstream — glad the tests are useful either way.

@fine405

Copy link
Copy Markdown

Downstream confirmation from OpenAI Codex Desktop: this exact path-rewrite failure is present in build 26.825.51511 on macOS and is tracked at openai/codex#41439.

In the shipped bundle:

  • The real helper exists at .../app.asar.unpacked/node_modules/node-pty/build/Release/spawn-helper.
  • The current unconditional replacement produces .../app.asar.unpacked.unpacked/node_modules/node-pty/build/Release/spawn-helper.
  • The rewritten path does not exist, and the integrated terminal never attaches while backend shell execution remains available.

The Xcode license and first-launch setup are already complete in this reproduction, so that reported workaround is not applicable. The guard in this PR directly addresses the observed bundled path. Linking the downstream report here to help with impact/priority; no private paths or logs are included.

@SquallLeonhart13

Copy link
Copy Markdown

Independent production confirmation for this PR, in case it helps with prioritization.

Environment

  • Packaged Electron app (Principal ADE v0.0.859), macOS 26 / arm64
  • Electron 38.1.0, node-pty 1.1.0
  • Layout: the PTY daemon is a child process spawned with ELECTRON_RUN_AS_NODE=1 and its bundle lives in Contents/Resources/app.asar.unpacked/dist/main/pty-daemon.cjs
  • node-pty is unpacked to Contents/Resources/app.asar.unpacked/node_modules/node-pty (via electron-builder asarUnpack)

Observed failure

Every terminal session failed with the generic error:

Error: posix_spawnp failed.
    at new UnixTerminal (.../node_modules/node-pty/lib/unixTerminal.js:92:24)

The helper path node-pty passed to pty.fork was:

/Applications/Principal AI.app/Contents/Resources/app.asar.unpacked.unpacked/node_modules/node-pty/build/Release/spawn-helper

fs.existsSync on that path is false, so posix_spawn returns ENOENT and node-pty reports posix_spawnp failed.

Why it cost time to find

The generic error initially points at code signing / hardened runtime. We ruled those out: the app and the unpacked spawn-helper are both Developer ID-signed by the same team, codesign --verify --deep --strict passes, the helper is 0755, and running it directly works. The failure is purely the rewritten path string.

Minimal reproduction with the shipped app

  • require('…/app.asar.unpacked/node_modules/node-pty').spawn('/bin/zsh', …) → posix_spawnp failed
  • require('…/app.asar/node_modules/node-pty').spawn('/bin/zsh', …) → spawns normally
  • Copying the exact same unpacked node-pty tree to a path that does not contain app.asar.unpacked → spawns normally

So, as noted in #923, the only variable is whether the resolved __dirname already contains app.asar.unpacked.

Our current workaround

We did not patch node_modules; we resolve node-pty through the asar path when packaged, so __dirname stays asar-shimmed and the existing single rewrite lands on the real unpacked helper. This is a workaround for the upstream behavior, not a fix, and we would drop it once this PR (or equivalent) ships.

Suggestion

#953 had the right idea extracting resolveSpawnHelperPath() with regression tests for both the packed and already-unpacked forms. Even if you keep the inline guard from this PR, folding those tests in would prevent a future regression of the same rewrite. Happy to test a beta/preview release against our packaged layout if that helps.

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.

helperPath replace produces 'app.asar.unpacked.unpacked/…' when caller is itself in app.asar.unpacked → posix_spawnp ENOENT

4 participants