Skip to content

Commit 9981a8c

Browse files
fix(github): ask gh from the server when the checkout folder is gone
The spawner checks cwd before running gh, so a removed worktree or moved project failed every credential lookup instead of using the server's gh login as before. Retry gh in the server directory when the checkout folder cannot be found. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 766af86 commit 9981a8c

2 files changed

Lines changed: 85 additions & 59 deletions

File tree

‎packages/source-control-github/src/server/GitHubCredentials.ts‎

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as Hex from "effect/encoding/Hex";
88
import * as Exit from "effect/Exit";
99
import * as Layer from "effect/Layer";
1010
import * as Option from "effect/Option";
11+
import * as PlatformError from "effect/PlatformError";
1112
import * as Redacted from "effect/Redacted";
1213
import * as Schema from "effect/Schema";
1314

@@ -152,39 +153,50 @@ export const make = Effect.gen(function* () {
152153
Effect.orDie,
153154
);
154155

156+
const runGh = (host: string, account: string | undefined, cwd: string) =>
157+
process.run({
158+
operation: "GitHubCredentials.get",
159+
command: "gh",
160+
args: [
161+
"auth",
162+
"token",
163+
"--hostname",
164+
host,
165+
...(account === undefined ? [] : ["--user", account]),
166+
],
167+
cwd,
168+
// Never let gh print the token into a debug log.
169+
env: { GH_DEBUG: "", GH_PROMPT_DISABLED: "1" },
170+
timeoutMs: 10_000,
171+
concurrency: GH_PROCESS_CONCURRENCY,
172+
});
173+
155174
const fromGh = (host: string, account: string | undefined, cwd: string) =>
156-
process
157-
.run({
158-
operation: "GitHubCredentials.get",
159-
command: "gh",
160-
args: [
161-
"auth",
162-
"token",
163-
"--hostname",
164-
host,
165-
...(account === undefined ? [] : ["--user", account]),
166-
],
167-
cwd,
168-
// Never let gh print the token into a debug log.
169-
env: { GH_DEBUG: "", GH_PROMPT_DISABLED: "1" },
170-
timeoutMs: 10_000,
171-
concurrency: GH_PROCESS_CONCURRENCY,
172-
})
173-
.pipe(
174-
Effect.mapError((error) =>
175-
isMissingExecutable(error)
176-
? new GitHubCliMissingError({ host })
177-
: // gh exits non-zero with "no oauth token" when it has no login for the host.
178-
error._tag === "VcsProcessExitError"
179-
? new GitHubNotSignedInError({ host, ...(account === undefined ? {} : { account }) })
180-
: new GitHubCliFailedError({ host, cause: error }),
181-
),
182-
Effect.map((output) => output.stdout.trim()),
183-
Effect.filterOrFail(
184-
(token) => token !== "",
185-
() => new GitHubNotSignedInError({ host, ...(account === undefined ? {} : { account }) }),
186-
),
187-
);
175+
runGh(host, account, cwd).pipe(
176+
// A checkout folder that is gone fails the spawner's cwd check; ask gh from the server.
177+
Effect.catchIf(
178+
(error) =>
179+
cwd !== workingDirectory &&
180+
error._tag === "VcsProcessSpawnError" &&
181+
PlatformError.isPlatformError(error.cause) &&
182+
error.cause.reason._tag === "NotFound" &&
183+
!isMissingExecutable(error),
184+
() => runGh(host, account, workingDirectory),
185+
),
186+
Effect.mapError((error) =>
187+
isMissingExecutable(error)
188+
? new GitHubCliMissingError({ host })
189+
: // gh exits non-zero with "no oauth token" when it has no login for the host.
190+
error._tag === "VcsProcessExitError"
191+
? new GitHubNotSignedInError({ host, ...(account === undefined ? {} : { account }) })
192+
: new GitHubCliFailedError({ host, cause: error }),
193+
),
194+
Effect.map((output) => output.stdout.trim()),
195+
Effect.filterOrFail(
196+
(token) => token !== "",
197+
() => new GitHubNotSignedInError({ host, ...(account === undefined ? {} : { account }) }),
198+
),
199+
);
188200

189201
const readSettings = (settings: ServerSettings) =>
190202
readSourceControlHostSettings(

‎packages/source-control-github/src/server/GitHubRest.test.ts‎

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -615,35 +615,49 @@ describe("GitHubCredentials", () => {
615615
Layer.provide(Layer.succeed(HttpClient.HttpClient, unexpectedHttp)),
616616
);
617617

618-
it.effect.each([
619-
{ module: "ChildProcess", method: "spawn", expected: "GitHubCliMissingError" },
620-
{ module: "FileSystem", method: "access", expected: "GitHubCliFailedError" },
621-
] as const)(
622-
"classifies a missing $module resource as $expected",
623-
({ module, method, expected }) =>
624-
Effect.gen(function* () {
625-
const credentials = yield* GitHubCredentials.GitHubCredentials;
626-
const error = yield* Effect.flip(credentials.get("git.acme.internal", "/repo"));
627-
expect(error._tag).toBe(expected);
628-
}).pipe(
629-
Effect.provide(
630-
credentialsWith(() =>
631-
Effect.fail(
632-
new VcsProcessSpawnError({
633-
operation: "GitHubCredentials.get",
634-
command: "gh",
635-
cwd: "/repo",
636-
cause: PlatformError.systemError({
637-
_tag: "NotFound",
638-
module,
639-
method,
640-
}),
641-
}),
642-
),
643-
),
618+
const missing = (cwd: string, module: "ChildProcess" | "FileSystem") =>
619+
new VcsProcessSpawnError({
620+
operation: "GitHubCredentials.get",
621+
command: "gh",
622+
cwd,
623+
cause: PlatformError.systemError({
624+
_tag: "NotFound",
625+
module,
626+
method: module === "ChildProcess" ? "spawn" : "access",
627+
}),
628+
});
629+
630+
it.effect("fails with GitHubCliMissingError when gh is not on PATH", () =>
631+
Effect.gen(function* () {
632+
const credentials = yield* GitHubCredentials.GitHubCredentials;
633+
const error = yield* Effect.flip(credentials.get("git.acme.internal", "/repo"));
634+
expect(error._tag).toBe("GitHubCliMissingError");
635+
}).pipe(
636+
Effect.provide(credentialsWith((input) => Effect.fail(missing(input.cwd, "ChildProcess")))),
637+
),
638+
);
639+
640+
it.effect("asks gh from the server directory when the checkout folder is gone", () => {
641+
const directories: Array<string> = [];
642+
return Effect.gen(function* () {
643+
const credentials = yield* GitHubCredentials.GitHubCredentials;
644+
const held = yield* credentials.get("git.acme.internal", "/removed-worktree");
645+
expect(Redacted.value(held.token)).toBe("server-token");
646+
expect(directories).toEqual(["/removed-worktree", "/server"]);
647+
}).pipe(
648+
Effect.provide(
649+
credentialsWith((input) =>
650+
Effect.suspend(() => {
651+
directories.push(input.cwd);
652+
return input.cwd === "/removed-worktree"
653+
? Effect.fail(missing(input.cwd, "FileSystem"))
654+
: Effect.succeed(TestSourceControlHost.processOutput("server-token"));
655+
}),
644656
),
645657
),
646-
);
658+
Effect.provideService(HostProcess.WorkingDirectory, "/server"),
659+
);
660+
});
647661

648662
it.effect("does not cache a gh failure that is not about signing in", () => {
649663
let calls = 0;

0 commit comments

Comments
 (0)