| author | |
| committer | |
| log | f50a7cddc19336e3f4de820e198564f7e7f75455 |
| tree | d3abbe4e48a3428621f6facb48e6a80d9e5b1d45 |
| parent | 97487d9959554854698bf92e672b758e2c3fdeab |
| signature | Signed by PGP key B5690EEEBB952194 |
Co-authored-by: codex <codex@users.noreply.github.com>2 files changed, 348 insertions(+), 51 deletions(-)
scripts/dev-runner.test.ts+206-11| ... | @@ -1,8 +1,16 @@ | ... | @@ -1,8 +1,16 @@ |
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; | 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import * as NodeOS from "node:os"; | 2 | import * as NodeOS from "node:os"; |
| 3 | import * as NetService from "@t3tools/shared/Net"; | ||
| 4 | import { HostProcessPlatform } from "@t3tools/shared/hostProcess"; | ||
| 3 | import { assert, describe, it } from "@effect/vitest"; | 5 | import { assert, describe, it } from "@effect/vitest"; |
| 6 | import * as ConfigProvider from "effect/ConfigProvider"; | ||
| 4 | import * as Effect from "effect/Effect"; | 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Layer from "effect/Layer"; | ||
| 5 | import * as Path from "effect/Path"; | 9 | import * as Path from "effect/Path"; |
| 10 | import * as PlatformError from "effect/PlatformError"; | ||
| 11 | import * as Sink from "effect/Sink"; | ||
| 12 | import * as Stream from "effect/Stream"; | ||
| 13 | import { ChildProcessSpawner } from "effect/unstable/process"; | ||
| 6 | 14 | ||
| 7 | import { | 15 | import { |
| 8 | checkPortAvailabilityOnHosts, | 16 | checkPortAvailabilityOnHosts, |
| ... | @@ -11,8 +19,49 @@ import { | ... | @@ -11,8 +19,49 @@ import { |
| 11 | getDevRunnerModeArgs, | 19 | getDevRunnerModeArgs, |
| 12 | resolveModePortOffsets, | 20 | resolveModePortOffsets, |
| 13 | resolveOffset, | 21 | resolveOffset, |
| 22 | runDevRunnerWithInput, | ||
| 14 | } from "./dev-runner.ts"; | 23 | } from "./dev-runner.ts"; |
| 15 | 24 | ||
| 25 | const emptyConfigLayer = ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })); | ||
| 26 | const netServiceLayer = Layer.succeed(NetService.NetService, { | ||
| 27 | canListenOnHost: () => Effect.succeed(true), | ||
| 28 | isPortAvailableOnLoopback: () => Effect.succeed(true), | ||
| 29 | reserveLoopbackPort: () => Effect.succeed(49_152), | ||
| 30 | findAvailablePort: (port) => Effect.succeed(port), | ||
| 31 | }); | ||
| 32 | |||
| 33 | function mockProcess(exit: number | PlatformError.PlatformError) { | ||
| 34 | return ChildProcessSpawner.makeHandle({ | ||
| 35 | pid: ChildProcessSpawner.ProcessId(1), | ||
| 36 | exitCode: | ||
| 37 | typeof exit === "number" | ||
| 38 | ? Effect.succeed(ChildProcessSpawner.ExitCode(exit)) | ||
| 39 | : Effect.fail(exit), | ||
| 40 | isRunning: Effect.succeed(false), | ||
| 41 | kill: () => Effect.void, | ||
| 42 | unref: Effect.succeed(Effect.void), | ||
| 43 | stdin: Sink.drain, | ||
| 44 | stdout: Stream.empty, | ||
| 45 | stderr: Stream.empty, | ||
| 46 | all: Stream.empty, | ||
| 47 | getInputFd: () => Sink.drain, | ||
| 48 | getOutputFd: () => Stream.empty, | ||
| 49 | }); | ||
| 50 | } | ||
| 51 | |||
| 52 | const devServerInput = { | ||
| 53 | mode: "dev:server", | ||
| 54 | t3Home: "/tmp/t3code-dev-runner", | ||
| 55 | noBrowser: undefined, | ||
| 56 | autoBootstrapProjectFromCwd: undefined, | ||
| 57 | logWebSocketEvents: undefined, | ||
| 58 | host: undefined, | ||
| 59 | port: 13_773, | ||
| 60 | devUrl: undefined, | ||
| 61 | dryRun: false, | ||
| 62 | runArgs: ["--inspect", "secret-token-value"], | ||
| 63 | } as const; | ||
| 64 | |||
| 16 | it.layer(NodeServices.layer)("dev-runner", (it) => { | 65 | it.layer(NodeServices.layer)("dev-runner", (it) => { |
| 17 | describe("getDevRunnerModeArgs", () => { | 66 | describe("getDevRunnerModeArgs", () => { |
| 18 | it.effect("lets Vite+ honor the desktop dev task graph", () => | 67 | it.effect("lets Vite+ honor the desktop dev task graph", () => |
| ... | @@ -42,8 +91,8 @@ it.layer(NodeServices.layer)("dev-runner", (it) => { | ... | @@ -42,8 +91,8 @@ it.layer(NodeServices.layer)("dev-runner", (it) => { |
| 42 | 91 | ||
| 43 | describe("resolveOffset", () => { | 92 | describe("resolveOffset", () => { |
| 44 | it.effect("uses explicit T3CODE_PORT_OFFSET when provided", () => | 93 | it.effect("uses explicit T3CODE_PORT_OFFSET when provided", () => |
| 45 | Effect.sync(() => { | 94 | Effect.gen(function* () { |
| 46 | const result = resolveOffset({ portOffset: 12, devInstance: undefined }); | 95 | const result = yield* resolveOffset({ portOffset: 12, devInstance: undefined }); |
| 47 | assert.deepStrictEqual(result, { | 96 | assert.deepStrictEqual(result, { |
| 48 | offset: 12, | 97 | offset: 12, |
| 49 | source: "T3CODE_PORT_OFFSET=12", | 98 | source: "T3CODE_PORT_OFFSET=12", |
| ... | @@ -52,23 +101,27 @@ it.layer(NodeServices.layer)("dev-runner", (it) => { | ... | @@ -52,23 +101,27 @@ it.layer(NodeServices.layer)("dev-runner", (it) => { |
| 52 | ); | 101 | ); |
| 53 | 102 | ||
| 54 | it.effect("hashes non-numeric instance values", () => | 103 | it.effect("hashes non-numeric instance values", () => |
| 55 | Effect.sync(() => { | 104 | Effect.gen(function* () { |
| 56 | const result = resolveOffset({ portOffset: undefined, devInstance: "feature-branch" }); | 105 | const result = yield* resolveOffset({ |
| 106 | portOffset: undefined, | ||
| 107 | devInstance: "feature-branch", | ||
| 108 | }); | ||
| 57 | assert.ok(result.offset >= 1); | 109 | assert.ok(result.offset >= 1); |
| 58 | assert.ok(result.offset <= 3000); | 110 | assert.ok(result.offset <= 3000); |
| 59 | }), | 111 | }), |
| 60 | ); | 112 | ); |
| 61 | 113 | ||
| 62 | it.effect("throws for negative port offset", () => | 114 | it.effect("returns structured context for a negative port offset", () => |
| 63 | Effect.gen(function* () { | 115 | Effect.gen(function* () { |
| 64 | const error = yield* Effect.flip( | 116 | const error = yield* resolveOffset({ portOffset: -1, devInstance: undefined }).pipe( |
| 65 | Effect.try({ | 117 | Effect.flip, |
| 66 | try: () => resolveOffset({ portOffset: -1, devInstance: undefined }), | ||
| 67 | catch: (cause) => String(cause), | ||
| 68 | }), | ||
| 69 | ); | 118 | ); |
| 70 | 119 | ||
| 71 | assert.ok(error.includes("Invalid T3CODE_PORT_OFFSET")); | 120 | assert.equal(error._tag, "DevRunnerInvalidPortOffsetError"); |
| 121 | assert.equal(error.configKey, "T3CODE_PORT_OFFSET"); | ||
| 122 | assert.equal(error.portOffset, -1); | ||
| 123 | assert.equal(error.minimum, 0); | ||
| 124 | assert.ok(!("cause" in error)); | ||
| 72 | }), | 125 | }), |
| 73 | ); | 126 | ); |
| 74 | }); | 127 | }); |
| ... | @@ -289,6 +342,28 @@ it.layer(NodeServices.layer)("dev-runner", (it) => { | ... | @@ -289,6 +342,28 @@ it.layer(NodeServices.layer)("dev-runner", (it) => { |
| 289 | assert.equal(offset, 59_802); | 342 | assert.equal(offset, 59_802); |
| 290 | }), | 343 | }), |
| 291 | ); | 344 | ); |
| 345 | |||
| 346 | it.effect("reports the exhausted range and required port set", () => | ||
| 347 | Effect.gen(function* () { | ||
| 348 | const error = yield* findFirstAvailableOffset({ | ||
| 349 | startOffset: 51_763, | ||
| 350 | requireServerPort: true, | ||
| 351 | requireWebPort: false, | ||
| 352 | checkPortAvailability: () => Effect.succeed(true), | ||
| 353 | }).pipe(Effect.flip); | ||
| 354 | |||
| 355 | if (error._tag !== "DevRunnerPortExhaustedError") { | ||
| 356 | assert.fail(`Unexpected error: ${error._tag}`); | ||
| 357 | } | ||
| 358 | assert.equal(error.startOffset, 51_763); | ||
| 359 | assert.equal(error.requireServerPort, true); | ||
| 360 | assert.equal(error.requireWebPort, false); | ||
| 361 | assert.equal(error.baseServerPort, 13_773); | ||
| 362 | assert.equal(error.baseWebPort, 5_733); | ||
| 363 | assert.equal(error.maximumPort, 65_535); | ||
| 364 | assert.ok(!("cause" in error)); | ||
| 365 | }), | ||
| 366 | ); | ||
| 292 | }); | 367 | }); |
| 293 | 368 | ||
| 294 | describe("checkPortAvailabilityOnHosts", () => { | 369 | describe("checkPortAvailabilityOnHosts", () => { |
| ... | @@ -395,4 +470,124 @@ it.layer(NodeServices.layer)("dev-runner", (it) => { | ... | @@ -395,4 +470,124 @@ it.layer(NodeServices.layer)("dev-runner", (it) => { |
| 395 | }), | 470 | }), |
| 396 | ); | 471 | ); |
| 397 | }); | 472 | }); |
| 473 | |||
| 474 | describe("runDevRunnerWithInput", () => { | ||
| 475 | it.effect("preserves invalid configuration as the exact cause", () => | ||
| 476 | Effect.gen(function* () { | ||
| 477 | const error = yield* runDevRunnerWithInput({ ...devServerInput, dryRun: true }).pipe( | ||
| 478 | Effect.provide( | ||
| 479 | Layer.merge( | ||
| 480 | netServiceLayer, | ||
| 481 | ConfigProvider.layer( | ||
| 482 | ConfigProvider.fromEnv({ env: { T3CODE_PORT_OFFSET: "not-an-integer" } }), | ||
| 483 | ), | ||
| 484 | ), | ||
| 485 | ), | ||
| 486 | Effect.flip, | ||
| 487 | ); | ||
| 488 | |||
| 489 | if (error._tag !== "DevRunnerConfigurationError") { | ||
| 490 | assert.fail(`Unexpected error: ${error._tag}`); | ||
| 491 | } | ||
| 492 | assert.deepStrictEqual(error.configKeys, ["T3CODE_PORT_OFFSET", "T3CODE_DEV_INSTANCE"]); | ||
| 493 | assert.ok(error.cause !== undefined); | ||
| 494 | assert.ok(!error.message.includes(String((error.cause as Error).message))); | ||
| 495 | }), | ||
| 496 | ); | ||
| 497 | |||
| 498 | it.effect("preserves process spawn context and the exact platform cause", () => { | ||
| 499 | const cause = PlatformError.systemError({ | ||
| 500 | _tag: "NotFound", | ||
| 501 | module: "ChildProcess", | ||
| 502 | method: "spawn", | ||
| 503 | description: "vp was not found", | ||
| 504 | }); | ||
| 505 | const spawnerLayer = Layer.succeed( | ||
| 506 | ChildProcessSpawner.ChildProcessSpawner, | ||
| 507 | ChildProcessSpawner.make(() => Effect.fail(cause)), | ||
| 508 | ); | ||
| 509 | |||
| 510 | return Effect.gen(function* () { | ||
| 511 | const error = yield* runDevRunnerWithInput(devServerInput).pipe( | ||
| 512 | Effect.provide(Layer.mergeAll(emptyConfigLayer, netServiceLayer, spawnerLayer)), | ||
| 513 | Effect.provideService(HostProcessPlatform, "linux"), | ||
| 514 | Effect.flip, | ||
| 515 | ); | ||
| 516 | |||
| 517 | if (error._tag !== "DevRunnerProcessError") { | ||
| 518 | assert.fail(`Unexpected error: ${error._tag}`); | ||
| 519 | } | ||
| 520 | assert.equal(error.operation, "spawn"); | ||
| 521 | assert.equal(error.mode, "dev:server"); | ||
| 522 | assert.equal(error.executable, "vp"); | ||
| 523 | assert.equal(error.argumentCount, 5); | ||
| 524 | assert.equal(error.shell, false); | ||
| 525 | assert.equal(error.cause, cause); | ||
| 526 | assert.ok(!error.message.includes(cause.message)); | ||
| 527 | assert.notProperty(error, "args"); | ||
| 528 | assert.notInclude(error.message, "secret-token-value"); | ||
| 529 | }); | ||
| 530 | }); | ||
| 531 | |||
| 532 | it.effect("reports non-zero exits without manufacturing a cause", () => { | ||
| 533 | const spawnerLayer = Layer.succeed( | ||
| 534 | ChildProcessSpawner.ChildProcessSpawner, | ||
| 535 | ChildProcessSpawner.make(() => Effect.succeed(mockProcess(17))), | ||
| 536 | ); | ||
| 537 | |||
| 538 | return Effect.gen(function* () { | ||
| 539 | const error = yield* runDevRunnerWithInput(devServerInput).pipe( | ||
| 540 | Effect.provide(Layer.mergeAll(emptyConfigLayer, netServiceLayer, spawnerLayer)), | ||
| 541 | Effect.provideService(HostProcessPlatform, "linux"), | ||
| 542 | Effect.flip, | ||
| 543 | ); | ||
| 544 | |||
| 545 | if (error._tag !== "DevRunnerProcessExitError") { | ||
| 546 | assert.fail(`Unexpected error: ${error._tag}`); | ||
| 547 | } | ||
| 548 | assert.equal(error.mode, "dev:server"); | ||
| 549 | assert.equal(error.executable, "vp"); | ||
| 550 | assert.equal(error.argumentCount, 5); | ||
| 551 | assert.equal(error.shell, false); | ||
| 552 | assert.equal(error.exitCode, 17); | ||
| 553 | assert.ok(!("cause" in error)); | ||
| 554 | assert.notProperty(error, "args"); | ||
| 555 | assert.notInclude(error.message, "secret-token-value"); | ||
| 556 | }); | ||
| 557 | }); | ||
| 558 | |||
| 559 | it.effect("preserves wait-for-exit failures as the exact cause", () => { | ||
| 560 | const cause = PlatformError.systemError({ | ||
| 561 | _tag: "Unknown", | ||
| 562 | module: "ChildProcess", | ||
| 563 | method: "exitCode", | ||
| 564 | description: "process status became unavailable", | ||
| 565 | }); | ||
| 566 | const spawnerLayer = Layer.succeed( | ||
| 567 | ChildProcessSpawner.ChildProcessSpawner, | ||
| 568 | ChildProcessSpawner.make(() => Effect.succeed(mockProcess(cause))), | ||
| 569 | ); | ||
| 570 | |||
| 571 | return Effect.gen(function* () { | ||
| 572 | const error = yield* runDevRunnerWithInput(devServerInput).pipe( | ||
| 573 | Effect.provide(Layer.mergeAll(emptyConfigLayer, netServiceLayer, spawnerLayer)), | ||
| 574 | Effect.provideService(HostProcessPlatform, "linux"), | ||
| 575 | Effect.flip, | ||
| 576 | ); | ||
| 577 | |||
| 578 | if (error._tag !== "DevRunnerProcessError") { | ||
| 579 | assert.fail(`Unexpected error: ${error._tag}`); | ||
| 580 | } | ||
| 581 | assert.equal(error.operation, "wait-for-exit"); | ||
| 582 | assert.equal(error.mode, "dev:server"); | ||
| 583 | assert.equal(error.executable, "vp"); | ||
| 584 | assert.equal(error.argumentCount, 5); | ||
| 585 | assert.equal(error.shell, false); | ||
| 586 | assert.equal(error.cause, cause); | ||
| 587 | assert.ok(!error.message.includes(cause.message)); | ||
| 588 | assert.notProperty(error, "args"); | ||
| 589 | assert.notInclude(error.message, "secret-token-value"); | ||
| 590 | }); | ||
| 591 | }); | ||
| 592 | }); | ||
| 398 | }); | 593 | }); |
scripts/dev-runner.ts+142-40| ... | @@ -8,7 +8,6 @@ import * as NetService from "@t3tools/shared/Net"; | ... | @@ -8,7 +8,6 @@ import * as NetService from "@t3tools/shared/Net"; |
| 8 | import { HostProcessEnvironment } from "@t3tools/shared/hostProcess"; | 8 | import { HostProcessEnvironment } from "@t3tools/shared/hostProcess"; |
| 9 | import { resolveSpawnCommand } from "@t3tools/shared/shell"; | 9 | import { resolveSpawnCommand } from "@t3tools/shared/shell"; |
| 10 | import * as Config from "effect/Config"; | 10 | import * as Config from "effect/Config"; |
| 11 | import * as Data from "effect/Data"; | ||
| 12 | import * as Effect from "effect/Effect"; | 11 | import * as Effect from "effect/Effect"; |
| 13 | import * as Hash from "effect/Hash"; | 12 | import * as Hash from "effect/Hash"; |
| 14 | import * as Layer from "effect/Layer"; | 13 | import * as Layer from "effect/Layer"; |
| ... | @@ -57,10 +56,87 @@ export function getDevRunnerModeArgs(mode: DevMode): ReadonlyArray<string> { | ... | @@ -57,10 +56,87 @@ export function getDevRunnerModeArgs(mode: DevMode): ReadonlyArray<string> { |
| 57 | return MODE_ARGS[mode]; | 56 | return MODE_ARGS[mode]; |
| 58 | } | 57 | } |
| 59 | 58 | ||
| 60 | class DevRunnerError extends Data.TaggedError("DevRunnerError")<{ | 59 | export class DevRunnerConfigurationError extends Schema.TaggedErrorClass<DevRunnerConfigurationError>()( |
| 61 | readonly message: string; | 60 | "DevRunnerConfigurationError", |
| 62 | readonly cause?: unknown; | 61 | { |
| 63 | }> {} | 62 | configKeys: Schema.Array(Schema.String), |
| 63 | cause: Schema.Defect(), | ||
| 64 | }, | ||
| 65 | ) { | ||
| 66 | override get message(): string { | ||
| 67 | return `Failed to read dev-runner configuration: ${this.configKeys.join(", ")}.`; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | export class DevRunnerInvalidPortOffsetError extends Schema.TaggedErrorClass<DevRunnerInvalidPortOffsetError>()( | ||
| 72 | "DevRunnerInvalidPortOffsetError", | ||
| 73 | { | ||
| 74 | configKey: Schema.Literal("T3CODE_PORT_OFFSET"), | ||
| 75 | portOffset: Schema.Number, | ||
| 76 | minimum: Schema.Number, | ||
| 77 | }, | ||
| 78 | ) { | ||
| 79 | override get message(): string { | ||
| 80 | return `${this.configKey} must be at least ${this.minimum}; received ${this.portOffset}.`; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | export class DevRunnerPortExhaustedError extends Schema.TaggedErrorClass<DevRunnerPortExhaustedError>()( | ||
| 85 | "DevRunnerPortExhaustedError", | ||
| 86 | { | ||
| 87 | startOffset: Schema.Number, | ||
| 88 | requireServerPort: Schema.Boolean, | ||
| 89 | requireWebPort: Schema.Boolean, | ||
| 90 | baseServerPort: Schema.Number, | ||
| 91 | baseWebPort: Schema.Number, | ||
| 92 | maximumPort: Schema.Number, | ||
| 93 | }, | ||
| 94 | ) { | ||
| 95 | override get message(): string { | ||
| 96 | return `No required dev ports were available from offset ${this.startOffset} through maximum port ${this.maximumPort}.`; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | export class DevRunnerProcessError extends Schema.TaggedErrorClass<DevRunnerProcessError>()( | ||
| 101 | "DevRunnerProcessError", | ||
| 102 | { | ||
| 103 | operation: Schema.Literals(["spawn", "wait-for-exit"]), | ||
| 104 | mode: Schema.Literals(["dev", "dev:server", "dev:web", "dev:desktop"]), | ||
| 105 | executable: Schema.Literal("vp"), | ||
| 106 | argumentCount: Schema.Number, | ||
| 107 | shell: Schema.Boolean, | ||
| 108 | cause: Schema.Defect(), | ||
| 109 | }, | ||
| 110 | ) { | ||
| 111 | override get message(): string { | ||
| 112 | return `Dev-runner process operation "${this.operation}" failed for mode "${this.mode}".`; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | export class DevRunnerProcessExitError extends Schema.TaggedErrorClass<DevRunnerProcessExitError>()( | ||
| 117 | "DevRunnerProcessExitError", | ||
| 118 | { | ||
| 119 | mode: Schema.Literals(["dev", "dev:server", "dev:web", "dev:desktop"]), | ||
| 120 | executable: Schema.Literal("vp"), | ||
| 121 | argumentCount: Schema.Number, | ||
| 122 | shell: Schema.Boolean, | ||
| 123 | exitCode: Schema.Number, | ||
| 124 | }, | ||
| 125 | ) { | ||
| 126 | override get message(): string { | ||
| 127 | return `Dev-runner process exited with code ${this.exitCode} in mode "${this.mode}".`; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | export const DevRunnerError = Schema.Union([ | ||
| 132 | DevRunnerConfigurationError, | ||
| 133 | DevRunnerInvalidPortOffsetError, | ||
| 134 | DevRunnerPortExhaustedError, | ||
| 135 | DevRunnerProcessError, | ||
| 136 | DevRunnerProcessExitError, | ||
| 137 | ]); | ||
| 138 | export type DevRunnerError = typeof DevRunnerError.Type; | ||
| 139 | export const isDevRunnerError = Schema.is(DevRunnerError); | ||
| 64 | 140 | ||
| 65 | const optionalStringConfig = (name: string): Config.Config<string | undefined> => | 141 | const optionalStringConfig = (name: string): Config.Config<string | undefined> => |
| 66 | Config.string(name).pipe( | 142 | Config.string(name).pipe( |
| ... | @@ -96,28 +172,40 @@ const OffsetConfig = Config.all({ | ... | @@ -96,28 +172,40 @@ const OffsetConfig = Config.all({ |
| 96 | export function resolveOffset(config: { | 172 | export function resolveOffset(config: { |
| 97 | readonly portOffset: number | undefined; | 173 | readonly portOffset: number | undefined; |
| 98 | readonly devInstance: string | undefined; | 174 | readonly devInstance: string | undefined; |
| 99 | }): { readonly offset: number; readonly source: string } { | 175 | }): Effect.Effect< |
| 176 | { readonly offset: number; readonly source: string }, | ||
| 177 | DevRunnerInvalidPortOffsetError | ||
| 178 | > { | ||
| 100 | if (config.portOffset !== undefined) { | 179 | if (config.portOffset !== undefined) { |
| 101 | if (config.portOffset < 0) { | 180 | if (config.portOffset < 0) { |
| 102 | throw new Error(`Invalid T3CODE_PORT_OFFSET: ${config.portOffset}`); | 181 | return Effect.fail( |
| 182 | new DevRunnerInvalidPortOffsetError({ | ||
| 183 | configKey: "T3CODE_PORT_OFFSET", | ||
| 184 | portOffset: config.portOffset, | ||
| 185 | minimum: 0, | ||
| 186 | }), | ||
| 187 | ); | ||
| 103 | } | 188 | } |
| 104 | return { | 189 | return Effect.succeed({ |
| 105 | offset: config.portOffset, | 190 | offset: config.portOffset, |
| 106 | source: `T3CODE_PORT_OFFSET=${config.portOffset}`, | 191 | source: `T3CODE_PORT_OFFSET=${config.portOffset}`, |
| 107 | }; | 192 | }); |
| 108 | } | 193 | } |
| 109 | 194 | ||
| 110 | const seed = config.devInstance?.trim(); | 195 | const seed = config.devInstance?.trim(); |
| 111 | if (!seed) { | 196 | if (!seed) { |
| 112 | return { offset: 0, source: "default ports" }; | 197 | return Effect.succeed({ offset: 0, source: "default ports" }); |
| 113 | } | 198 | } |
| 114 | 199 | ||
| 115 | if (/^\d+$/.test(seed)) { | 200 | if (/^\d+$/.test(seed)) { |
| 116 | return { offset: Number(seed), source: `numeric T3CODE_DEV_INSTANCE=${seed}` }; | 201 | return Effect.succeed({ |
| 202 | offset: Number(seed), | ||
| 203 | source: `numeric T3CODE_DEV_INSTANCE=${seed}`, | ||
| 204 | }); | ||
| 117 | } | 205 | } |
| 118 | 206 | ||
| 119 | const offset = ((Hash.string(seed) >>> 0) % MAX_HASH_OFFSET) + 1; | 207 | const offset = ((Hash.string(seed) >>> 0) % MAX_HASH_OFFSET) + 1; |
| 120 | return { offset, source: `hashed T3CODE_DEV_INSTANCE=${seed}` }; | 208 | return Effect.succeed({ offset, source: `hashed T3CODE_DEV_INSTANCE=${seed}` }); |
| 121 | } | 209 | } |
| 122 | 210 | ||
| 123 | function resolveBaseDir(baseDir: string | undefined): Effect.Effect<string, never, Path.Path> { | 211 | function resolveBaseDir(baseDir: string | undefined): Effect.Effect<string, never, Path.Path> { |
| ... | @@ -275,7 +363,7 @@ export function findFirstAvailableOffset<R = NetService.NetService>({ | ... | @@ -275,7 +363,7 @@ export function findFirstAvailableOffset<R = NetService.NetService>({ |
| 275 | requireServerPort, | 363 | requireServerPort, |
| 276 | requireWebPort, | 364 | requireWebPort, |
| 277 | checkPortAvailability, | 365 | checkPortAvailability, |
| 278 | }: FindFirstAvailableOffsetInput<R>): Effect.Effect<number, DevRunnerError, R> { | 366 | }: FindFirstAvailableOffsetInput<R>): Effect.Effect<number, DevRunnerPortExhaustedError, R> { |
| 279 | return Effect.gen(function* () { | 367 | return Effect.gen(function* () { |
| 280 | const checkPort = (checkPortAvailability ?? | 368 | const checkPort = (checkPortAvailability ?? |
| 281 | defaultCheckPortAvailability) as PortAvailabilityCheck<R>; | 369 | defaultCheckPortAvailability) as PortAvailabilityCheck<R>; |
| ... | @@ -311,8 +399,13 @@ export function findFirstAvailableOffset<R = NetService.NetService>({ | ... | @@ -311,8 +399,13 @@ export function findFirstAvailableOffset<R = NetService.NetService>({ |
| 311 | } | 399 | } |
| 312 | } | 400 | } |
| 313 | 401 | ||
| 314 | return yield* new DevRunnerError({ | 402 | return yield* new DevRunnerPortExhaustedError({ |
| 315 | message: `No available dev ports found from offset ${startOffset}. Tried server=${BASE_SERVER_PORT}+n web=${BASE_WEB_PORT}+n up to port ${MAX_PORT}.`, | 403 | startOffset, |
| 404 | requireServerPort, | ||
| 405 | requireWebPort, | ||
| 406 | baseServerPort: BASE_SERVER_PORT, | ||
| 407 | baseWebPort: BASE_WEB_PORT, | ||
| 408 | maximumPort: MAX_PORT, | ||
| 316 | }); | 409 | }); |
| 317 | }); | 410 | }); |
| 318 | } | 411 | } |
| ... | @@ -333,7 +426,7 @@ export function resolveModePortOffsets<R = NetService.NetService>({ | ... | @@ -333,7 +426,7 @@ export function resolveModePortOffsets<R = NetService.NetService>({ |
| 333 | checkPortAvailability, | 426 | checkPortAvailability, |
| 334 | }: ResolveModePortOffsetsInput<R>): Effect.Effect< | 427 | }: ResolveModePortOffsetsInput<R>): Effect.Effect< |
| 335 | { readonly serverOffset: number; readonly webOffset: number }, | 428 | { readonly serverOffset: number; readonly webOffset: number }, |
| 336 | DevRunnerError, | 429 | DevRunnerPortExhaustedError, |
| 337 | R | 430 | R |
| 338 | > { | 431 | > { |
| 339 | return Effect.gen(function* () { | 432 | return Effect.gen(function* () { |
| ... | @@ -397,21 +490,14 @@ export function runDevRunnerWithInput(input: DevRunnerCliInput) { | ... | @@ -397,21 +490,14 @@ export function runDevRunnerWithInput(input: DevRunnerCliInput) { |
| 397 | const { portOffset, devInstance } = yield* OffsetConfig.pipe( | 490 | const { portOffset, devInstance } = yield* OffsetConfig.pipe( |
| 398 | Effect.mapError( | 491 | Effect.mapError( |
| 399 | (cause) => | 492 | (cause) => |
| 400 | new DevRunnerError({ | 493 | new DevRunnerConfigurationError({ |
| 401 | message: "Failed to read T3CODE_PORT_OFFSET/T3CODE_DEV_INSTANCE configuration.", | 494 | configKeys: ["T3CODE_PORT_OFFSET", "T3CODE_DEV_INSTANCE"], |
| 402 | cause, | 495 | cause, |
| 403 | }), | 496 | }), |
| 404 | ), | 497 | ), |
| 405 | ); | 498 | ); |
| 406 | 499 | ||
| 407 | const { offset, source } = yield* Effect.try({ | 500 | const { offset, source } = yield* resolveOffset({ portOffset, devInstance }); |
| 408 | try: () => resolveOffset({ portOffset, devInstance }), | ||
| 409 | catch: (cause) => | ||
| 410 | new DevRunnerError({ | ||
| 411 | message: cause instanceof Error ? cause.message : String(cause), | ||
| 412 | cause, | ||
| 413 | }), | ||
| 414 | }); | ||
| 415 | 501 | ||
| 416 | const { serverOffset, webOffset } = yield* resolveModePortOffsets({ | 502 | const { serverOffset, webOffset } = yield* resolveModePortOffsets({ |
| 417 | mode: input.mode, | 503 | mode: input.mode, |
| ... | @@ -453,6 +539,12 @@ export function runDevRunnerWithInput(input: DevRunnerCliInput) { | ... | @@ -453,6 +539,12 @@ export function runDevRunnerWithInput(input: DevRunnerCliInput) { |
| 453 | [...MODE_ARGS[input.mode], ...input.runArgs], | 539 | [...MODE_ARGS[input.mode], ...input.runArgs], |
| 454 | { env }, | 540 | { env }, |
| 455 | ); | 541 | ); |
| 542 | const processContext = { | ||
| 543 | mode: input.mode, | ||
| 544 | executable: "vp" as const, | ||
| 545 | argumentCount: spawnCommand.args.length, | ||
| 546 | shell: spawnCommand.shell, | ||
| 547 | } as const; | ||
| 456 | const child = yield* ChildProcess.make(spawnCommand.command, spawnCommand.args, { | 548 | const child = yield* ChildProcess.make(spawnCommand.command, spawnCommand.args, { |
| 457 | stdin: "inherit", | 549 | stdin: "inherit", |
| 458 | stdout: "inherit", | 550 | stdout: "inherit", |
| ... | @@ -465,24 +557,34 @@ export function runDevRunnerWithInput(input: DevRunnerCliInput) { | ... | @@ -465,24 +557,34 @@ export function runDevRunnerWithInput(input: DevRunnerCliInput) { |
| 465 | // which would put the runner in a new group and require manual forwarding. | 557 | // which would put the runner in a new group and require manual forwarding. |
| 466 | detached: false, | 558 | detached: false, |
| 467 | forceKillAfter: "1500 millis", | 559 | forceKillAfter: "1500 millis", |
| 468 | }); | 560 | }).pipe( |
| 561 | Effect.mapError( | ||
| 562 | (cause) => | ||
| 563 | new DevRunnerProcessError({ | ||
| 564 | ...processContext, | ||
| 565 | operation: "spawn", | ||
| 566 | cause, | ||
| 567 | }), | ||
| 568 | ), | ||
| 569 | ); | ||
| 469 | 570 | ||
| 470 | const exitCode = yield* child.exitCode; | 571 | const exitCode = yield* child.exitCode.pipe( |
| 572 | Effect.mapError( | ||
| 573 | (cause) => | ||
| 574 | new DevRunnerProcessError({ | ||
| 575 | ...processContext, | ||
| 576 | operation: "wait-for-exit", | ||
| 577 | cause, | ||
| 578 | }), | ||
| 579 | ), | ||
| 580 | ); | ||
| 471 | if (exitCode !== 0) { | 581 | if (exitCode !== 0) { |
| 472 | return yield* new DevRunnerError({ | 582 | return yield* new DevRunnerProcessExitError({ |
| 473 | message: `vp run exited with code ${exitCode}`, | 583 | ...processContext, |
| 584 | exitCode, | ||
| 474 | }); | 585 | }); |
| 475 | } | 586 | } |
| 476 | }).pipe( | 587 | }); |
| 477 | Effect.mapError((cause) => | ||
| 478 | cause instanceof DevRunnerError | ||
| 479 | ? cause | ||
| 480 | : new DevRunnerError({ | ||
| 481 | message: cause instanceof Error ? cause.message : "dev-runner failed", | ||
| 482 | cause, | ||
| 483 | }), | ||
| 484 | ), | ||
| 485 | ); | ||
| 486 | } | 588 | } |
| 487 | 589 | ||
| 488 | const devRunnerCli = Command.make("dev-runner", { | 590 | const devRunnerCli = Command.make("dev-runner", { |