diff --git a/packages/shared/src/shell.test.ts b/packages/shared/src/shell.test.ts index 8f81723486f55..7e83eada2dfeb 100644 --- a/packages/shared/src/shell.test.ts +++ b/packages/shared/src/shell.test.ts @@ -4,12 +4,15 @@ import { it as effectIt } from "@effect/vitest"; import * as HostProcess from "@t3tools/shared/HostProcess"; import { DesktopHostTelemetryMessage } from "@t3tools/contracts"; import * as Schema from "effect/Schema"; +import * as Scheduler from "effect/Scheduler"; +import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Fiber from "effect/Fiber"; import * as FileSystem from "effect/FileSystem"; import * as Path from "effect/Path"; import * as Option from "effect/Option"; import * as TestClock from "effect/testing/TestClock"; +import * as Tracer from "effect/Tracer"; import * as NodeChildProcess from "node:child_process"; import * as NodeFS from "node:fs"; import * as NodeOS from "node:os"; @@ -28,6 +31,7 @@ import { gitForWindowsBash, isCommandAvailable, listLoginShellCandidates, + locateXcodeGitWithXcrun, mergePathEntries, mergePathValues, npmShimExecutable, @@ -50,14 +54,36 @@ const decodeDesktopHostMessage = Schema.decodeUnknownSync(DesktopHostTelemetryMe const nativeProbe = vi.hoisted(() => ({ execFile: vi.fn(), original: undefined as unknown as typeof import("node:child_process").execFile, + spawn: vi.fn(), })); vi.mock("node:child_process", async (importOriginal) => { const original = await importOriginal(); nativeProbe.original = original.execFile; - return { ...original, execFile: nativeProbe.execFile.mockImplementation(original.execFile) }; + return { + ...original, + execFile: nativeProbe.execFile.mockImplementation(original.execFile), + spawn: nativeProbe.spawn.mockImplementation(original.spawn), + }; }); +// A child that cannot start emits `error` and never `exit`. +const exitOf = (child: NodeChildProcess.ChildProcess) => + child.exitCode !== null || child.signalCode !== null + ? Promise.resolve({ code: child.exitCode, signal: child.signalCode }) + : new Promise<{ code: number | null; signal: NodeJS.Signals | null }>((resolve, reject) => { + const onExit = (code: number | null, signal: NodeJS.Signals | null) => { + child.off("error", onError); + resolve({ code, signal }); + }; + const onError = (error: Error) => { + child.off("exit", onExit); + reject(error); + }; + child.once("exit", onExit); + child.once("error", onError); + }); + const withWindowsEnvironmentMocks = ( effect: Effect.Effect, readEnvironment: ( @@ -1172,19 +1198,25 @@ effectIt.layer(NodeServices.layer)("resolveSpawnCommand", (it) => { readonly platform?: NodeJS.Platform; readonly gitOnPath: string; readonly xcrunGit: string | undefined; + readonly asyncPathLookup?: boolean; }) => { const files = new Set([options.gitOnPath, ...(options.xcrunGit ? [options.xcrunGit] : [])]); const host = { xcrunGit: options.xcrunGit, xcrunCalls: 0 }; + const lookUp = (command: string) => + command === "git" ? options.gitOnPath : files.has(command) ? command : undefined; const resolve = resolveSpawnCommand("git", ["status"], { env: { PATH: "/fake/bin" } }).pipe( Effect.map((resolved) => resolved.command), Effect.provideService(HostProcess.Platform, options.platform ?? "darwin"), - Effect.provideService(SpawnExecutableResolution, (command) => - command === "git" ? options.gitOnPath : files.has(command) ? command : undefined, + Effect.provideService( + SpawnExecutableResolution, + options.asyncPathLookup ? async (command) => lookUp(command) : lookUp, + ), + Effect.provideService(XcodeGitLocation, () => + Effect.sync(() => { + host.xcrunCalls += 1; + return host.xcrunGit; + }), ), - Effect.provideService(XcodeGitLocation, () => { - host.xcrunCalls += 1; - return host.xcrunGit; - }), ); return { files, host, resolve }; }; @@ -1200,6 +1232,170 @@ effectIt.layer(NodeServices.layer)("resolveSpawnCommand", (it) => { }).pipe(Effect.provideService(CommandResolutionCache, new Map())), ); + // The default PATH lookup answers asynchronously. + it.effect("checks the Xcode git through an asynchronous PATH lookup", () => + Effect.gen(function* () { + const found = setup({ + gitOnPath: "/usr/bin/git", + xcrunGit: XCODE_GIT, + asyncPathLookup: true, + }); + expect(yield* found.resolve).toBe(XCODE_GIT); + expect(yield* found.resolve).toBe(XCODE_GIT); + expect(found.host.xcrunCalls).toBe(1); + + const missing = setup({ + gitOnPath: "/usr/bin/git", + xcrunGit: XCODE_GIT, + asyncPathLookup: true, + }); + missing.files.delete(XCODE_GIT); + expect(yield* missing.resolve).toBe("git"); + }).pipe(Effect.provideService(CommandResolutionCache, new Map())), + ); + + // A small yield budget lets the scheduler switch callers between any two operations. + it.effect.each([undefined, 3, 4, 5, 6] as const)( + "waits for xcrun without blocking, sharing one lookup between callers (yield budget %s)", + (budget) => + Effect.gen(function* () { + let lookup = yield* Deferred.make(); + let xcrunCalls = 0; + const resolve = resolveSpawnCommand("git", ["status"], { + env: { PATH: "/fake/bin" }, + }).pipe( + Effect.map((resolved) => resolved.command), + Effect.provideService(HostProcess.Platform, "darwin"), + Effect.provideService(SpawnExecutableResolution, (command) => + command === "git" ? "/usr/bin/git" : command === XCODE_GIT ? command : undefined, + ), + Effect.provideService(XcodeGitLocation, () => { + xcrunCalls += 1; + return Deferred.await(lookup); + }), + budget === undefined + ? (effect) => effect + : Effect.provideService(Scheduler.MaxOpsBeforeYield, budget), + ); + const callers = Effect.forEach([1, 2, 3], () => + Effect.forkChild(resolve, { startImmediately: true }), + ); + const first = yield* callers; + yield* Deferred.succeed(lookup, XCODE_GIT); + expect(yield* Fiber.joinAll(first)).toEqual([XCODE_GIT, XCODE_GIT, XCODE_GIT]); + expect(xcrunCalls).toBe(1); + + // Once the answer expires, the next callers share one new lookup. + lookup = yield* Deferred.make(); + yield* TestClock.adjust("31 seconds"); + const second = yield* callers; + yield* Deferred.succeed(lookup, XCODE_GIT); + expect(yield* Fiber.joinAll(second)).toEqual([XCODE_GIT, XCODE_GIT, XCODE_GIT]); + expect(xcrunCalls).toBe(2); + }).pipe(Effect.provideService(CommandResolutionCache, new Map())), + ); + + it.effect("the xcrun lookup resolves nothing when xcrun is missing or fails", () => + Effect.gen(function* () { + const env = { PATH: "/usr/bin:/bin", DEVELOPER_DIR: "/nonexistent/t3-developer-dir" }; + const locate = yield* XcodeGitLocation; + expect(yield* locate(env)).toBeUndefined(); + expect( + yield* locateXcodeGitWithXcrun(env, { xcrun: "/nonexistent/t3-xcrun" }), + ).toBeUndefined(); + }), + ); + + it.effect("the xcrun lookup records its own trace span", () => + Effect.gen(function* () { + const spans: Array = []; + const tracer = Tracer.make({ + span: (options) => { + const span = new Tracer.NativeSpan(options); + spans.push(span); + return span; + }, + }); + yield* locateXcodeGitWithXcrun( + { PATH: "/usr/bin:/bin" }, + { xcrun: "/nonexistent/t3-xcrun" }, + ).pipe(Effect.provideService(Tracer.Tracer, tracer)); + const lookups = spans.filter((span) => span.name === "shell.locateXcodeGitWithXcrun"); + expect(lookups).toHaveLength(1); + expect(lookups[0]!.status._tag).toBe("Ended"); + }), + ); + + it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")( + "the xcrun lookup settles at its deadline when a descendant keeps stdout open", + () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-xcrun-" }); + const descendantPid = path.join(directory, "descendant.pid"); + const xcrun = path.join(directory, "xcrun"); + yield* fs.writeFileString( + xcrun, + `#!/bin/sh\nsleep 600 &\necho $! > '${descendantPid}'\necho /fake/git\n`, + ); + yield* fs.chmod(xcrun, 0o755); + const stopDescendant = fs.readFileString(descendantPid).pipe( + Effect.flatMap((pid) => Effect.sync(() => process.kill(Number(pid.trim())))), + Effect.ignore, + ); + yield* Effect.addFinalizer(() => stopDescendant); + const lookup = yield* locateXcodeGitWithXcrun({ PATH: "/usr/bin:/bin" }, { xcrun }).pipe( + Effect.forkChild({ startImmediately: true }), + ); + const child = nativeProbe.spawn.mock.results.at(-1) + ?.value as NodeChildProcess.ChildProcess; + yield* Effect.promise(() => exitOf(child)); + + // xcrun has exited and printed a path, but its descendant still holds stdout open. + expect(lookup.pollUnsafe()).toBeUndefined(); + yield* TestClock.adjust("5 seconds"); + expect(yield* Fiber.join(lookup)).toBeUndefined(); + }).pipe(Effect.scoped), + ); + + it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")( + "the xcrun lookup kills an xcrun that ignores SIGTERM at its deadline", + () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-xcrun-" }); + const xcrun = path.join(directory, "xcrun"); + yield* fs.writeFileString(xcrun, "#!/bin/sh\ntrap '' TERM\necho ready\nexec sleep 600\n"); + yield* fs.chmod(xcrun, 0o755); + const lookup = yield* locateXcodeGitWithXcrun({ PATH: "/usr/bin:/bin" }, { xcrun }).pipe( + Effect.forkChild({ startImmediately: true }), + ); + const child = nativeProbe.spawn.mock.results.at(-1) + ?.value as NodeChildProcess.ChildProcess; + const exited = exitOf(child); + // The fixture prints its first line once the trap is in place. Resolving with the + // failure instead of rejecting leaves no rejection unhandled if it never starts. + const ready = new Promise((resolve) => { + child.stdout?.once("data", () => resolve(undefined)); + exited.then( + () => resolve(new Error("the fake xcrun exited before it was ready")), + resolve, + ); + }); + const kill = vi.spyOn(child, "kill"); + yield* Effect.addFinalizer(() => Effect.sync(() => child.kill("SIGKILL"))); + expect(yield* Effect.promise(() => ready)).toBeUndefined(); + + expect(lookup.pollUnsafe()).toBeUndefined(); + yield* TestClock.adjust("5 seconds"); + expect(yield* Fiber.join(lookup)).toBeUndefined(); + expect(kill).toHaveBeenCalledWith("SIGKILL"); + expect(yield* Effect.promise(() => exited)).toEqual({ code: null, signal: "SIGKILL" }); + }).pipe(Effect.scoped), + ); + it.effect("keeps git when PATH has another git or the host is not macOS", () => Effect.gen(function* () { const homebrew = setup({ gitOnPath: "/opt/homebrew/bin/git", xcrunGit: XCODE_GIT }); @@ -1222,17 +1418,70 @@ effectIt.layer(NodeServices.layer)("resolveSpawnCommand", (it) => { }).pipe(Effect.provideService(CommandResolutionCache, new Map())), ); - it.effect("looks again when the resolved git disappears", () => + it.effect.each([undefined, 3] as const)( + "looks again when the resolved git disappears (yield budget %s)", + (budget) => + Effect.gen(function* () { + const betaGit = "/Applications/Xcode-beta.app/Contents/Developer/usr/bin/git"; + const { files, host, resolve } = setup({ + gitOnPath: "/usr/bin/git", + xcrunGit: XCODE_GIT, + }); + const budgeted = + budget === undefined + ? resolve + : resolve.pipe(Effect.provideService(Scheduler.MaxOpsBeforeYield, budget)); + expect(yield* budgeted).toBe(XCODE_GIT); + + files.delete(XCODE_GIT); + files.add(betaGit); + host.xcrunGit = betaGit; + expect(yield* budgeted).toBe(betaGit); + expect(host.xcrunCalls).toBe(2); + }).pipe(Effect.provideService(CommandResolutionCache, new Map())), + ); + + it.effect("callers that find the cached git gone share one new lookup", () => Effect.gen(function* () { const betaGit = "/Applications/Xcode-beta.app/Contents/Developer/usr/bin/git"; - const { files, host, resolve } = setup({ gitOnPath: "/usr/bin/git", xcrunGit: XCODE_GIT }); + const files = new Set([XCODE_GIT]); + let xcrunGit = XCODE_GIT; + let xcrunCalls = 0; + // Each check of the cached git waits here until the test releases it. + let gateCachedGit = false; + const gates: Array<() => void> = []; + const resolve = resolveSpawnCommand("git", ["status"], { + env: { PATH: "/fake/bin" }, + }).pipe( + Effect.map((resolved) => resolved.command), + Effect.provideService(HostProcess.Platform, "darwin"), + Effect.provideService(SpawnExecutableResolution, async (command) => { + if (gateCachedGit && command === XCODE_GIT) { + await new Promise((release) => gates.push(release)); + } + return command === "git" ? "/usr/bin/git" : files.has(command) ? command : undefined; + }), + Effect.provideService(XcodeGitLocation, () => + Effect.sync(() => { + xcrunCalls += 1; + return xcrunGit; + }), + ), + ); expect(yield* resolve).toBe(XCODE_GIT); files.delete(XCODE_GIT); files.add(betaGit); - host.xcrunGit = betaGit; - expect(yield* resolve).toBe(betaGit); - expect(host.xcrunCalls).toBe(2); + xcrunGit = betaGit; + gateCachedGit = true; + const first = yield* Effect.forkChild(resolve, { startImmediately: true }); + const second = yield* Effect.forkChild(resolve, { startImmediately: true }); + expect(gates.length).toBe(2); + yield* Effect.sync(() => gates[0]!()); + expect(yield* Fiber.join(first)).toBe(betaGit); + yield* Effect.sync(() => gates[1]!()); + expect(yield* Fiber.join(second)).toBe(betaGit); + expect(xcrunCalls).toBe(2); }).pipe(Effect.provideService(CommandResolutionCache, new Map())), ); }); diff --git a/packages/shared/src/shell.ts b/packages/shared/src/shell.ts index 0ba42dc3078d6..20fd46bf37d92 100644 --- a/packages/shared/src/shell.ts +++ b/packages/shared/src/shell.ts @@ -6,6 +6,7 @@ import * as NodeFS from "node:fs"; import * as Cache from "effect/Cache"; import * as Clock from "effect/Clock"; import * as Duration from "effect/Duration"; +import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as FileSystem from "effect/FileSystem"; @@ -962,22 +963,64 @@ export function npmShimExecutable( return isFile(executable) ? executable : undefined; } -export type XcodeGitLocator = (env: NodeJS.ProcessEnv) => string | undefined; +export type XcodeGitLocator = (env: NodeJS.ProcessEnv) => Effect.Effect; + +const XCRUN_MAX_OUTPUT_BYTES = 1024 * 1024; +const XCRUN_DEADLINE = Duration.seconds(5); + +// Stopping it kills xcrun with SIGKILL, which it cannot ignore, and stops reading a stdout +// that a descendant may still hold open after xcrun exits. +const runXcrunFindGit = (xcrun: string, env: NodeJS.ProcessEnv) => + Effect.callback>((resume) => { + let child: NodeChildProcess.ChildProcess; + try { + child = NodeChildProcess.spawn(xcrun, ["--find", "git"], { + env, + stdio: ["ignore", "pipe", "ignore"], + }); + } catch { + resume(Effect.succeedNone); + return; + } + const stop = () => { + child.kill("SIGKILL"); + child.unref(); + child.stdout?.destroy(); + }; + const chunks: Array = []; + let bytes = 0; + child.stdout?.on("data", (chunk: Buffer) => { + bytes += chunk.byteLength; + if (bytes > XCRUN_MAX_OUTPUT_BYTES) { + stop(); + resume(Effect.succeedNone); + return; + } + chunks.push(chunk); + }); + child.once("error", () => resume(Effect.succeedNone)); + child.once("close", (code) => { + const located = Buffer.concat(chunks).toString("utf8").trim(); + resume( + code === 0 && NodePath.isAbsolute(located) + ? Effect.succeedSome(located) + : Effect.succeedNone, + ); + }); + return Effect.sync(stop); + }); // xcrun honors DEVELOPER_DIR and the `xcode-select` choice, as the shim does. -function locateXcodeGitWithXcrun(env: NodeJS.ProcessEnv): string | undefined { - try { - const located = NodeChildProcess.execFileSync("/usr/bin/xcrun", ["--find", "git"], { - encoding: "utf8", - env, - timeout: 5_000, - stdio: ["ignore", "pipe", "ignore"], - }).trim(); - return NodePath.isAbsolute(located) ? located : undefined; - } catch { - return undefined; - } -} +// It takes ~15 ms and reruns when the cached answer expires, so it must not block the event loop. +export const locateXcodeGitWithXcrun = Effect.fn("shell.locateXcodeGitWithXcrun")(function* ( + env: NodeJS.ProcessEnv, + options: { readonly xcrun?: string } = {}, +) { + return yield* runXcrunFindGit(options.xcrun ?? "/usr/bin/xcrun", env).pipe( + Effect.timeoutOption(XCRUN_DEADLINE), + Effect.map((found) => Option.getOrUndefined(Option.flatten(found))), + ); +}); export const XcodeGitLocation = Context.Reference( "@t3tools/shared/shell/XcodeGitLocation", @@ -986,6 +1029,29 @@ export const XcodeGitLocation = Context.Reference( const MACOS_GIT_SHIM = "/usr/bin/git"; +// Callers that miss the cache together wait for one xcrun run instead of starting their own. +const xcodeGitLookups = new WeakMap< + Map, + Map> +>(); + +const lookupXcodeGit = Effect.fn("shell.lookupXcodeGit")( + function* ( + env: NodeJS.ProcessEnv, + resolveExecutable: SpawnExecutableResolver, + locateXcodeGit: XcodeGitLocator, + ) { + const onPath = yield* Effect.promise(async () => resolveExecutable("git", "darwin", env)); + if (onPath !== MACOS_GIT_SHIM) return null; + const located = yield* locateXcodeGit(env); + return located !== undefined && + (yield* Effect.promise(async () => resolveExecutable(located, "darwin", env))) !== undefined + ? located + : null; + }, + Effect.catchCause(() => Effect.succeed(null)), +); + /** * Skip the xcrun shim's per-spawn lookup only when git resolves to /usr/bin/git. * User-selected git on PATH still wins, children keep PATH, and lookup failures use `git`. @@ -1003,28 +1069,52 @@ const resolveMacosGit = Effect.fnUntraced(function* ( resolvePathEnvironmentVariable(env), env.DEVELOPER_DIR ?? "", ].join(COMMAND_RESOLUTION_CACHE_KEY_SEPARATOR); - const nowNanos = yield* Clock.currentTimeNanos; - const cached = cache.get(cacheKey); - // An Xcode update or removal deletes the binary; look again instead of failing the spawn. - if (cached !== undefined && cached.expiresAtNanos > nowNanos) { - const cachedPath = cached.resolvedPath; - if ( - cachedPath === null || - (yield* Effect.promise(async () => resolveExecutable(cachedPath, "darwin", env))) !== - undefined - ) { - return cachedPath ?? "git"; + const pending = xcodeGitLookups.get(cache) ?? new Map>(); + xcodeGitLookups.set(cache, pending); + for (;;) { + const nowNanos = yield* Clock.currentTimeNanos; + const cached = cache.get(cacheKey); + // An Xcode update or removal deletes the binary; look again instead of failing the spawn. + if (cached !== undefined && cached.expiresAtNanos > nowNanos) { + const cachedPath = cached.resolvedPath; + if ( + cachedPath === null || + (yield* Effect.promise(async () => resolveExecutable(cachedPath, "darwin", env))) !== + undefined + ) { + return cachedPath ?? "git"; + } } + // Callers join a lookup as soon as it is registered, and the uninterruptible region makes + // sure a registered lookup is started. Detached, a caller that stops waiting does not stop it. + // A caller whose cached answer was replaced meanwhile checks the new one instead. + const lookup = yield* Effect.uninterruptible( + Effect.suspend(() => { + const existing = pending.get(cacheKey); + if (existing !== undefined) return Effect.succeed(existing); + if (cache.get(cacheKey) !== cached) return Effect.undefined; + const shared = Deferred.makeUnsafe(); + pending.set(cacheKey, shared); + return lookupXcodeGit(env, resolveExecutable, locateXcodeGit).pipe( + Effect.tap((realGit) => + Effect.sync(() => cacheCommandResolution(cache, cacheKey, realGit, nowNanos)), + ), + Effect.onExit((exit) => + Effect.sync(() => { + if (pending.get(cacheKey) === shared) pending.delete(cacheKey); + }).pipe( + Effect.andThen( + Deferred.done(shared, Exit.isSuccess(exit) ? exit : Exit.succeed(null)), + ), + ), + ), + Effect.forkDetach, + Effect.as(shared), + ); + }), + ); + if (lookup !== undefined) return (yield* Deferred.await(lookup)) ?? "git"; } - const onPath = yield* Effect.promise(async () => resolveExecutable("git", "darwin", env)); - const located = onPath === MACOS_GIT_SHIM ? locateXcodeGit(env) : undefined; - const realGit = - located !== undefined && - (yield* Effect.promise(async () => resolveExecutable(located, "darwin", env))) !== undefined - ? located - : null; - cacheCommandResolution(cache, cacheKey, realGit, nowNanos); - return realGit ?? "git"; }); export interface SpawnCommandOptions extends CommandAvailabilityOptions {