diff --git a/apps/server/src/open.test.ts b/apps/server/src/open.test.ts index 5c19e218ff..7b71262842 100644 --- a/apps/server/src/open.test.ts +++ b/apps/server/src/open.test.ts @@ -1,7 +1,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, it } from "@effect/vitest"; import { assertSuccess } from "@effect/vitest/utils"; -import { FileSystem, Path, Effect, Layer, Sink, Stream } from "effect"; +import { FileSystem, Path, Effect, Sink, Stream } from "effect"; import { ChildProcessSpawner } from "effect/unstable/process"; import { @@ -490,6 +490,7 @@ it.layer(NodeServices.layer)("launchDetached", (it) => { kill: () => Effect.void, unref: Effect.sync(() => { unrefCalled = true; + return Effect.void; }), stdin: Sink.drain, stdout: Stream.empty, @@ -505,7 +506,7 @@ it.layer(NodeServices.layer)("launchDetached", (it) => { command: process.execPath, args: ["-e", "process.exit(0)"], }).pipe( - Effect.provide(Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner)), + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), Effect.result, ); diff --git a/apps/server/src/open.ts b/apps/server/src/open.ts index ac610267b5..f265a80719 100644 --- a/apps/server/src/open.ts +++ b/apps/server/src/open.ts @@ -190,31 +190,42 @@ const launchDetachedWithSpawner = ( return yield* new OpenError({ message: `Editor command not found: ${launch.command}` }); } - const isWin32 = process.platform === "win32"; - const child = yield* spawner - .spawn( - ChildProcess.make(launch.command, isWin32 ? launch.args.map((a) => `"${a}"`) : [ - ...launch.args, - ], { - detached: true, - stdin: "ignore", - stdout: "ignore", - stderr: "ignore", - shell: isWin32, - }), - ) - .pipe( - Effect.mapError( - (cause) => new OpenError({ message: "failed to spawn detached process", cause }), - ), - ); - yield* child.unref; + yield* Effect.scoped( + Effect.gen(function* () { + const isWin32 = process.platform === "win32"; + const child = yield* spawner + .spawn( + ChildProcess.make( + launch.command, + isWin32 ? launch.args.map((a) => `"${a}"`) : [...launch.args], + { + detached: true, + stdin: "ignore", + stdout: "ignore", + stderr: "ignore", + shell: isWin32, + }, + ), + ) + .pipe( + Effect.mapError( + (cause) => new OpenError({ message: "failed to spawn detached process", cause }), + ), + ); + const _reref = yield* child.unref.pipe( + Effect.mapError( + (cause) => new OpenError({ message: "failed to unref detached process", cause }), + ), + ); + }), + ); }); export const launchDetached = (launch: EditorLaunch) => - Effect.flatMap(ChildProcessSpawner.ChildProcessSpawner, (spawner) => - launchDetachedWithSpawner(launch, spawner), - ); + Effect.gen(function* () { + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; + return yield* launchDetachedWithSpawner(launch, spawner); + }); const make = Effect.gen(function* () { const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; @@ -228,10 +239,10 @@ const make = Effect.gen(function* () { Effect.tryPromise({ try: () => open.default(target), catch: (cause) => new OpenError({ message: "Browser auto-open failed", cause }), - }), + }).pipe(Effect.asVoid), openInEditor: (input) => Effect.flatMap(resolveEditorLaunch(input), (launch) => - launchDetachedWithSpawner(launch, spawner) + launchDetachedWithSpawner(launch, spawner), ), } satisfies OpenShape; }); diff --git a/apps/server/src/orchestration/decider.clock.test.ts b/apps/server/src/orchestration/decider.clock.test.ts index aa8aad061b..8740019861 100644 --- a/apps/server/src/orchestration/decider.clock.test.ts +++ b/apps/server/src/orchestration/decider.clock.test.ts @@ -1,9 +1,4 @@ -import { - CommandId, - EventId, - ProjectId, - type OrchestrationEvent, -} from "@t3tools/contracts"; +import { CommandId, EventId, ProjectId } from "@t3tools/contracts"; import { assert, it } from "@effect/vitest"; import { DateTime, Duration, Effect, Layer } from "effect"; import { TestClock } from "effect/testing"; @@ -49,10 +44,15 @@ it.effect("uses the Effect clock for generated project update timestamps", () => }, readModel, }); - const event = (Array.isArray(result) ? result[0] : result) as Extract< - Omit, - { type: "project.meta-updated" } - >; + if (Array.isArray(result)) { + assert.fail("expected a single project meta-updated event"); + return; + } + if (result.type !== "project.meta-updated") { + assert.fail(`expected project.meta-updated, received ${result.type}`); + return; + } + const event = result; assert.equal(event.occurredAt, expectedNow); assert.equal(event.payload.updatedAt, expectedNow);