TypeScript
fix(desktop): a slow PowerShell profile delays Windows startup by 5 s, not 10 (#19467)
lukebbe500865f80
diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts b/apps/desktop/src/shell/DesktopShellEnvironment.test.tsindex 27f4298a49f..9e50257c9a0 100644--- a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts+++ b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts@@ -1,6 +1,9 @@import * as NodeServices from "@effect/platform-node/NodeServices";+import * as Clock from "effect/Clock";import { assert, describe, it } from "@effect/vitest";+import * as Deferred from "effect/Deferred";import * as Effect from "effect/Effect";+import * as Fiber from "effect/Fiber";import * as Layer from "effect/Layer";import * as Logger from "effect/Logger";import * as PlatformError from "effect/PlatformError";@@ -9,6 +12,7 @@ import * as Sink from "effect/Sink";import * as Stream from "effect/Stream";import * as ChildProcess from "effect/process/ChildProcess";import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";+import * as TestClock from "effect/testing/TestClock";import * as NodeOS from "node:os";import { vi } from "vite-plus/test";@@ -74,7 +78,10 @@ function runShellEnvironment(input: {readonly userShell?: string;readonly handler: (command: ChildProcess.Command,- ) => string | { output: string; exitCode: number };+ ) =>+ | string+ | { output: string; exitCode: number }+ | Effect.Effect<ChildProcessSpawner.ChildProcessHandle, PlatformError.PlatformError>;readonly failure?: PlatformError.PlatformError;}) {const layerEnvironment = Layer.succeed(@@ -88,6 +95,7 @@ function runShellEnvironment(input: {ChildProcessSpawner.make((command) => {if (input.failure !== undefined) return Effect.fail(input.failure);const result = input.handler(command);+ if (Effect.isEffect(result)) return result;return Effect.succeed(typeof result === "string"? makeProcess(result)@@ -489,6 +497,85 @@ describe("DesktopShellEnvironment", () => {}),);+ it.effect("waits at most 5 seconds in total when the PowerShell profile hangs", () =>+ Effect.gen(function* () {+ const env: NodeJS.ProcessEnv = { PATH: "C:\\Windows\\System32" };+ const profileShells: string[] = [];+ const started = yield* Deferred.make<void>();+ const completed = yield* Deferred.make<void>();+ const baseClock = yield* Clock.Clock;+ let wallTime = 0;+ const clockWithBackwardWallJump: Clock.Clock = {+ ...baseClock,+ currentTimeMillisUnsafe: () => wallTime,+ currentTimeMillis: Effect.sync(() => wallTime),+ };++ const install = yield* Effect.forkChild(+ runShellEnvironment({+ env,+ platform: "win32",+ handler: (command) => {+ if (command._tag !== "StandardCommand") return "";+ if (command.args.includes("-NoProfile")) {+ return envOutput({ PATH: "C:\\NoProfile\\Bin;C:\\Windows\\System32" });+ }+ profileShells.push(command.command);+ return Deferred.succeed(started, undefined).pipe(+ Effect.as(+ ChildProcessSpawner.makeHandle({+ ...makeProcess(""),+ stdout: Stream.never,+ all: Stream.never,+ exitCode: Effect.never,+ isRunning: Effect.succeed(true),+ }),+ ),+ );+ },+ }).pipe(+ Effect.tap(() => Deferred.succeed(completed, undefined)),+ Effect.provide(Logger.layer([])),+ Effect.provideService(Clock.Clock, clockWithBackwardWallJump),+ ),+ );+ yield* Deferred.await(started);+ wallTime = -60_000;+ yield* TestClock.adjust("5 seconds");+ assert.isTrue(yield* Deferred.isDone(completed), "finishes within the shared budget");+ assert.isFalse(yield* Fiber.join(install));+ assert.deepEqual(profileShells, ["pwsh.exe"]);+ assert.equal(env.PATH, "C:\\NoProfile\\Bin;C:\\Windows\\System32");+ }),+ );++ it.effect("falls back to Windows PowerShell when pwsh.exe is not installed", () =>+ Effect.gen(function* () {+ const env: NodeJS.ProcessEnv = { PATH: "C:\\Windows\\System32" };+ const missing = PlatformError.systemError({+ _tag: "NotFound",+ module: "ChildProcess",+ method: "spawn",+ pathOrDescriptor: "pwsh.exe",+ });++ const prepared = yield* runShellEnvironment({+ env,+ platform: "win32",+ handler: (command) => {+ if (command._tag !== "StandardCommand") return "";+ if (command.command === "pwsh.exe") return Effect.fail(missing);+ return command.args.includes("-NoProfile")+ ? envOutput({ PATH: "C:\\Windows\\System32" })+ : envOutput({ PATH: "C:\\Profile\\Node;C:\\Windows\\System32" });+ },+ }).pipe(Effect.provide(Logger.layer([])));++ assert.isTrue(prepared);+ assert.equal(env.PATH, "C:\\Profile\\Node;C:\\Windows\\System32");+ }),+ );+it.effect.each([false, true])("leaves Windows preparation incomplete when loadProfile=%s fails",(loadProfile) =>diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.ts b/apps/desktop/src/shell/DesktopShellEnvironment.tsindex ad21a8d6176..b033f1cead2 100644--- a/apps/desktop/src/shell/DesktopShellEnvironment.ts+++ b/apps/desktop/src/shell/DesktopShellEnvironment.ts@@ -1,4 +1,5 @@import { listLoginShellCandidates, setWindowsPath } from "@t3tools/shared/shell";+import * as Clock from "effect/Clock";import * as Context from "effect/Context";import * as Duration from "effect/Duration";import * as Effect from "effect/Effect";@@ -377,12 +378,16 @@ const readWindowsEnvironment = Effect.fn("desktop.shellEnvironment.readWindowsEncaptureWindowsEnvironmentCommand(names),];+ const deadline =+ (yield* Clock.monotonicTimeNanos) + Duration.toNanosUnsafe(LOGIN_SHELL_TIMEOUT);for (const command of WINDOWS_SHELL_CANDIDATES) {+ const remainingNanos = deadline - (yield* Clock.monotonicTimeNanos);+ if (remainingNanos <= 0n) break;const output = yield* runCommandOutput({probe: options.loadProfile ? "powershell-profile" : "powershell-no-profile",command,args,- timeout: LOGIN_SHELL_TIMEOUT,+ timeout: Duration.nanos(remainingNanos),});const environment = extractEnvironment(output, names);if (Object.keys(environment).length > 0) {