Skip to content

The agent that grows with you

fix(desktop): a logged-out gh is asked again on the next update check

teknium17b085a734b53
diff --git a/apps/desktop/electron/github-api-auth.test.ts b/apps/desktop/electron/github-api-auth.test.ts
index 6c264c0bb29..506b6cb4f35 100644
--- a/apps/desktop/electron/github-api-auth.test.ts
+++ b/apps/desktop/electron/github-api-auth.test.ts
@@ -138,6 +138,14 @@ test('gh rung: argv-only spawn, stdin closed, bounded, cached for the process, a
assert.equal(await resolveGitHubCredential({ env, platform: 'darwin', exists, execFileFn: loggedOut.execFileFn }), null)
+ // "none" is not cached: a `gh auth login` after launch is picked up by the next check without a restart.
+ const nowLogged = fakeExecFile({ stdout: 'gho_after_login\n' })
+
+ assert.deepEqual(await resolveGitHubCredential({ env, platform: 'darwin', exists, execFileFn: nowLogged.execFileFn }), {
+ token: 'gho_after_login',
+ source: 'gh-cli'
+ })
+
forgetGhCliToken()
assert.equal(await resolveGitHubCredential({ env, platform: 'darwin', exists: () => false, execFileFn: logged.execFileFn }), null)
assert.equal(logged.calls.length, 1)
diff --git a/apps/desktop/electron/github-api-auth.ts b/apps/desktop/electron/github-api-auth.ts
index ca07d222bb6..ab3eaa60f6c 100644
--- a/apps/desktop/electron/github-api-auth.ts
+++ b/apps/desktop/electron/github-api-auth.ts
@@ -150,9 +150,19 @@ export function readGhCliToken({
let ghCliTokenPromise: Promise<string | null> | null = null
-/** `gh auth token`, asked at most once per process; the outcome (including "none") is cached. */
+/**
+ * `gh auth token`, cached for the process once it answers with a token. A "none" answer (gh missing,
+ * logged out, hung) is not cached: a `gh auth login` after launch is honoured by the next check, and
+ * the check is passive and hourly, so re-asking costs one bounded spawn per check at most.
+ */
export function githubTokenFromGhCli(options: GhCliOptions = {}): Promise<string | null> {
- ghCliTokenPromise ??= readGhCliToken(options)
+ ghCliTokenPromise ??= readGhCliToken(options).then(token => {
+ if (token === null) {
+ ghCliTokenPromise = null
+ }
+
+ return token
+ })
return ghCliTokenPromise
}