diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts index 0c7869194323c..305e7e16b55b5 100644 --- a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts +++ b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts @@ -3,8 +3,8 @@ import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, describe, it } from "@effect/vitest"; import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Effect from "effect/Effect"; -import * as Fiber from "effect/Fiber"; import * as Deferred from "effect/Deferred"; +import * as Fiber from "effect/Fiber"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Logger from "effect/Logger"; @@ -163,30 +163,35 @@ function runShellEnvironment(input: { } describe("DesktopShellEnvironment", () => { - it.effect("does not wait for discovery when the cache is missing", () => - Effect.gen(function* () { - const started = yield* Deferred.make(); - const release = yield* Deferred.make(); - yield* runShellEnvironment({ - env: { SHELL: "/test-shell", PATH: "/usr/bin" }, - platform: "darwin", - handler: () => "", - processSpawner: ChildProcessSpawner.make(() => - Deferred.succeed(started, undefined).pipe( - Effect.andThen(Deferred.await(release)), - Effect.as(makeProcess(envOutput({ PATH: "/fresh/bin" }))), + it.effect( + "reports unprepared on a cache miss without waiting, then prepared after discovery", + () => + Effect.gen(function* () { + const started = yield* Deferred.make(); + const release = yield* Deferred.make(); + const preparedAfterDiscovery = yield* runShellEnvironment({ + env: { SHELL: "/test-shell", PATH: "/usr/bin" }, + platform: "darwin", + handler: () => "", + processSpawner: ChildProcessSpawner.make(() => + Deferred.succeed(started, undefined).pipe( + Effect.andThen(Deferred.await(release)), + Effect.as(makeProcess(envOutput({ PATH: "/fresh/bin" }))), + ), ), - ), - use: (service) => - Effect.gen(function* () { - const startup = yield* service.installIntoProcess.pipe(Effect.forkChild); - yield* Deferred.await(started); - assert.isDefined(startup.pollUnsafe()); - yield* Deferred.succeed(release, undefined); - return yield* service.awaitDiscovery; - }), - }); - }), + use: (service) => + Effect.gen(function* () { + const startup = yield* service.installIntoProcess.pipe(Effect.forkChild); + yield* Deferred.await(started); + assert.isDefined(startup.pollUnsafe()); + assert.isFalse(yield* Fiber.join(startup)); + yield* Deferred.succeed(release, undefined); + assert.isTrue(yield* service.awaitDiscovery); + return yield* service.installIntoProcess; + }), + }); + assert.isTrue(preparedAfterDiscovery); + }), ); it.effect( @@ -285,37 +290,40 @@ describe("DesktopShellEnvironment", () => { "not json", '{"version":2,"key":"old","environment":{"PATH":"/bad/bin"}}', '{"version":1,"key":"other-shell","environment":{"PATH":"/bad/bin"}}', - ])("ignores an unusable cache and still starts before discovery: %s", (contents) => - Effect.gen(function* () { - const fs = yield* FileSystem.FileSystem; - const stateDir = yield* fs.makeTempDirectoryScoped(); - yield* fs.writeFileString(`${stateDir}/shell-environment-cache.json`, contents); - const started = yield* Deferred.make(); - const release = yield* Deferred.make(); - const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin" }; - yield* runShellEnvironment({ - env, - stateDir, - platform: "darwin", - handler: () => "", - processSpawner: ChildProcessSpawner.make(() => - Deferred.succeed(started, undefined).pipe( - Effect.andThen(Deferred.await(release)), - Effect.as(makeProcess(envOutput({ PATH: "/fresh/bin" }))), + ])( + "ignores an unusable cache, reports unprepared and still starts before discovery: %s", + (contents) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const stateDir = yield* fs.makeTempDirectoryScoped(); + yield* fs.writeFileString(`${stateDir}/shell-environment-cache.json`, contents); + const started = yield* Deferred.make(); + const release = yield* Deferred.make(); + const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin" }; + yield* runShellEnvironment({ + env, + stateDir, + platform: "darwin", + handler: () => "", + processSpawner: ChildProcessSpawner.make(() => + Deferred.succeed(started, undefined).pipe( + Effect.andThen(Deferred.await(release)), + Effect.as(makeProcess(envOutput({ PATH: "/fresh/bin" }))), + ), ), - ), - use: (service) => - Effect.gen(function* () { - const startup = yield* service.installIntoProcess.pipe(Effect.forkChild); - yield* Deferred.await(started); - assert.isDefined(startup.pollUnsafe()); - assert.equal(env.PATH, "/usr/bin"); - yield* Deferred.succeed(release, undefined); - return yield* service.awaitDiscovery; - }), - }); - assert.equal(env.PATH, "/fresh/bin:/usr/bin"); - }).pipe(Effect.provide(NodeServices.layer)), + use: (service) => + Effect.gen(function* () { + const startup = yield* service.installIntoProcess.pipe(Effect.forkChild); + yield* Deferred.await(started); + assert.isDefined(startup.pollUnsafe()); + assert.isFalse(yield* Fiber.join(startup)); + assert.equal(env.PATH, "/usr/bin"); + yield* Deferred.succeed(release, undefined); + return yield* service.awaitDiscovery; + }), + }); + assert.equal(env.PATH, "/fresh/bin:/usr/bin"); + }).pipe(Effect.provide(NodeServices.layer)), ); it.effect( @@ -356,7 +364,7 @@ describe("DesktopShellEnvironment", () => { handler: () => "", use: (service) => Effect.gen(function* () { - yield* service.installIntoProcess; + assert.isTrue(yield* service.installIntoProcess); const replay = yield* Stream.runHead(service.updates); assert.isTrue(Option.isSome(replay)); if (Option.isSome(replay)) { diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.ts b/apps/desktop/src/shell/DesktopShellEnvironment.ts index 9d9e390c95496..0cd7359a5aad7 100644 --- a/apps/desktop/src/shell/DesktopShellEnvironment.ts +++ b/apps/desktop/src/shell/DesktopShellEnvironment.ts @@ -9,6 +9,7 @@ import { import * as Context from "effect/Context"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; +import * as Exit from "effect/Exit"; import * as Fiber from "effect/Fiber"; import * as Deferred from "effect/Deferred"; import * as Path from "effect/Path"; @@ -77,7 +78,7 @@ export class DesktopShellEnvironmentCommandTimeoutError extends Schema.TaggedErr export class DesktopShellEnvironment extends Context.Service< DesktopShellEnvironment, { - // Desktop owns discovery, including on cache misses; the backend receives updates over fd4. + // True once a matching cache or finished discovery prepared PATH; false keeps server hydration. readonly installIntoProcess: Effect.Effect; readonly awaitDiscovery: Effect.Effect; /** Whether the user's own login shell, not a fallback, gave the PATH a new terminal gets. */ @@ -644,12 +645,16 @@ export const make = Effect.gen(function* () { Effect.withSpan("desktop.shellEnvironment.refresh"), Effect.onExit((exit) => Deferred.done(discovered, exit)), ); - const installIntoProcess = yield* Effect.cached( + const applyCache = yield* Effect.cached( Effect.gen(function* () { const cache = yield* fileSystem .readFileString(cachePath) - .pipe(Effect.flatMap(decodeCache), Effect.option); - if (Option.isSome(cache) && cache.value.key === key) { + .pipe( + Effect.flatMap(decodeCache), + Effect.option, + Effect.map(Option.filter((entry) => entry.key === key)), + ); + if (Option.isSome(cache)) { const cached = Object.fromEntries( CACHED_ENV_NAMES.flatMap((name) => { const value = cache.value.environment[name]; @@ -675,7 +680,7 @@ export const make = Effect.gen(function* () { ); } yield* Effect.forkIn(refresh, scope); - return true; + return Option.isSome(cache); }).pipe(Effect.withSpan("desktop.shellEnvironment.installIntoProcess")), ); const updates = Stream.unwrap( @@ -688,6 +693,20 @@ export const make = Effect.gen(function* () { ); }), ); + const installIntoProcess = applyCache.pipe( + Effect.flatMap((cacheApplied) => + cacheApplied + ? Effect.succeed(true) + : Deferred.poll(discovered).pipe( + Effect.flatMap(Effect.transposeOption), + Effect.exit, + Effect.map( + (exit) => + Exit.isSuccess(exit) && Option.exists(exit.value, ({ prepared }) => prepared), + ), + ), + ), + ); return DesktopShellEnvironment.of({ installIntoProcess, awaitDiscovery: installIntoProcess.pipe(