fix(host-service): treat a gh that cannot start as no token

Merged
SA
2mo ago
+12-10

Problem

On a Mac where gh on the login-shell PATH is a binary with no slice this Mac can execute (errno 86 says only that the CPU type is wrong; an Intel-only build without Rosetta is the likely case), every host-service git procedure for a workspace with an https://github.com remote fails with an internal error. That includes local reads like git.getStatus and git.getBaseBranch, not only calls that need the network. It fails on every poll, and Sentry sees only 3 events an hour because of the host's repeat throttle.

Root cause

createGitEnvResolver asks the credential provider for a token before every git call. When git credential fill has nothing, LocalGitCredentialProvider falls back to gh auth token. That fallback is optional, and the code already resolves null for any failure execFile reports through its callback (gh missing, logged out, timed out).

Node sends only ENOENT, EACCES, EAGAIN, EMFILE and ENFILE through the callback. Any other spawn errno is thrown synchronously from ChildProcess.spawn. macOS errno 86 (EBADARCH, "Bad CPU type in executable") is one of those. The throw happened inside the new Promise executor, so the lookup rejected, and that rejection failed the env resolution for every git call.

The Sentry stack ends in ChildProcess.spawn called synchronously from the execFile inside fetchTokenViaGhCli. No onErrorNT tick appears in it, which confirms the synchronous path.

Fix

fetchTokenViaGhCli now uses promisify(execFile) inside a try, the same way the rest of host-service runs gh (see getGitHubUsernameViaGh in runtime/git/identity.ts). Both failure channels now land in one catch, so a gh that cannot start counts as "no token from gh", like a gh that is not installed. The result:

  • local git reads work again;
  • ctx.github() raises its existing PRECONDITION_FAILED remedy instead of an internal error.

fetchTokenViaGitCredential is left alone on purpose. If git itself cannot spawn, the git call that follows fails the same way, so turning that lookup into null would only move where the error is reported.

Verification

Repro: a PATH holding a git that finds no credential, and a gh that is a Mach-O file with a CPU type this Mac cannot run. Spawning it gives errno 86, the same as the field error.

Under the production runtime (Electron 41.10.3, ELECTRON_RUN_AS_NODE, Node v24.18.0):

callback shape REJECTED: spawn Unknown system error -86
promisified shape -> null (caught: spawn Unknown system error -86)
promisified success shape: {"stdout":"hi\n","stderr":""}

The last line checks that util.promisify.custom survives Electron's asar wrapper around execFile, so a success still resolves { stdout, stderr }.

Against LocalGitCredentialProvider itself (bun), before:

getCredentials REJECTED: EBADARCH: unknown error, posix_spawn '<tmp>/gh'
getToken REJECTED: EBADARCH: unknown error, posix_spawn '<tmp>/gh'

after:

getCredentials resolved; GIT_ASKPASS set: false
getToken resolved: null

bunx biome check packages/host-service/src/providers/git/LocalGitCredentialProvider/LocalGitCredentialProvider.ts

Checked 1 file in 12ms. No fixes applied.

cd packages/host-service && bun run typecheck

$ tsc --noEmit --emitDeclarationOnly false
(exit 0)

cd packages/host-service && bun test src/providers/git/, which includes the existing tests that resolve tokens through a stub gh:

 6 pass
 0 fail
 15 expect() calls
Ran 6 tests across 1 file.

No test added. The only portable way to make gh fail a spawn synchronously is a bad-architecture Mach-O stub, which only works on macOS, so a Linux CI run would skip it.

Refs HOST-SERVICE-6Q