Skip to content

Commit e1fad19

Browse files
fix(desktop): a login shell slower than 5s at launch still yields its PATH (pingdotgg#35033)
1 parent 7a39060 commit e1fad19

2 files changed

Lines changed: 91 additions & 9 deletions

File tree

‎apps/desktop/src/shell/DesktopShellEnvironment.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,52 @@ describe("DesktopShellEnvironment", () => {
637637
}),
638638
);
639639

640+
it.effect("recovers the login-shell PATH when the first probe outlasts the timeout", () =>
641+
Effect.gen(function* () {
642+
const env: NodeJS.ProcessEnv = { SHELL: "/bin/zsh", PATH: "/usr/bin:/bin:/usr/sbin:/sbin" };
643+
const output = envOutput({ PATH: "/opt/homebrew/bin:/usr/bin" });
644+
let shellProbes = 0;
645+
yield* runShellEnvironment({
646+
env,
647+
platform: "darwin",
648+
handler: () => "",
649+
processSpawner: ChildProcessSpawner.make((command) => {
650+
if (command._tag !== "StandardCommand" || command.command !== "/bin/zsh") {
651+
return Effect.succeed(makeProcess(""));
652+
}
653+
shellProbes += 1;
654+
if (shellProbes > 1) return Effect.succeed(makeProcess(output));
655+
// A cold start: the first login shell answers, but only after 7s.
656+
const slow = Effect.sleep("7 seconds");
657+
const stdout = Stream.fromEffect(slow.pipe(Effect.as(textEncoder.encode(output))));
658+
return Effect.succeed(
659+
ChildProcessSpawner.makeHandle({
660+
pid: ChildProcessSpawner.ProcessId(125),
661+
stdout,
662+
stderr: Stream.empty,
663+
all: stdout,
664+
exitCode: slow.pipe(Effect.as(ChildProcessSpawner.ExitCode(0))),
665+
isRunning: Effect.succeed(true),
666+
kill: () => Effect.void,
667+
stdin: Sink.drain,
668+
getInputFd: () => Sink.drain,
669+
getOutputFd: () => Stream.empty,
670+
unref: Effect.succeed(Effect.void),
671+
}),
672+
);
673+
}),
674+
use: (service) =>
675+
Effect.gen(function* () {
676+
yield* service.installIntoProcess;
677+
yield* TestClock.adjust("10 minutes");
678+
return true;
679+
}),
680+
}).pipe(Effect.provide(Logger.layer([])));
681+
682+
assert.equal(env.PATH, "/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin");
683+
}),
684+
);
685+
640686
it.effect("loads PowerShell profile environment on Windows", () =>
641687
Effect.gen(function* () {
642688
const env: NodeJS.ProcessEnv = {

‎apps/desktop/src/shell/DesktopShellEnvironment.ts‎

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ const LOCALE_ENV_NAMES = ["LANG", "LC_ALL", "LC_CTYPE"] as const;
117117
const FALLBACK_LC_CTYPE = "en_US.UTF-8";
118118
const WINDOWS_SHELL_CANDIDATES = ["pwsh.exe", "powershell.exe"] as const;
119119
const LOGIN_SHELL_TIMEOUT = Duration.seconds(5);
120+
// A profile can be slow only while the machine boots; one longer wait covers it.
121+
const SLOW_LOGIN_SHELL_TIMEOUT = Duration.seconds(20);
120122
const LAUNCHCTL_TIMEOUT = Duration.seconds(2);
121123
const PROCESS_TERMINATE_GRACE = Duration.seconds(1);
122124

@@ -261,7 +263,11 @@ const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")(
261263
readonly env: NodeJS.ProcessEnv;
262264
readonly timeout: Duration.Duration;
263265
readonly shell?: boolean;
264-
}): Effect.fn.Return<string, never, ChildProcessSpawner.ChildProcessSpawner> {
266+
}): Effect.fn.Return<
267+
string,
268+
DesktopShellEnvironmentCommandTimeoutError,
269+
ChildProcessSpawner.ChildProcessSpawner
270+
> {
265271
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
266272
const fiber = yield* Effect.scoped(
267273
Effect.gen(function* () {
@@ -317,29 +323,42 @@ const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")(
317323
return output.value;
318324
}
319325

320-
const error = new DesktopShellEnvironmentCommandTimeoutError({
326+
return yield* new DesktopShellEnvironmentCommandTimeoutError({
321327
probe: input.probe,
322328
executable: executableName(input.command),
323329
argumentCount: input.args.length,
324330
timeoutMs: Duration.toMillis(input.timeout),
325331
});
326-
yield* logShellEnvironmentCommandError(error);
327-
return "";
328332
});
329333

334+
const orEmptyOnTimeout = <R>(
335+
effect: Effect.Effect<string, DesktopShellEnvironmentCommandTimeoutError, R>,
336+
): Effect.Effect<string, never, R> =>
337+
effect.pipe(
338+
Effect.catchTags({
339+
DesktopShellEnvironmentCommandTimeoutError: (error) =>
340+
logShellEnvironmentCommandError(error).pipe(Effect.as("")),
341+
}),
342+
);
343+
330344
const readLoginShellEnvironment = (
331345
shell: string,
332346
names: ReadonlyArray<string>,
333347
env: NodeJS.ProcessEnv,
334-
): Effect.Effect<EnvironmentPatch, never, ChildProcessSpawner.ChildProcessSpawner> =>
348+
timeout: Duration.Duration,
349+
): Effect.Effect<
350+
EnvironmentPatch,
351+
DesktopShellEnvironmentCommandTimeoutError,
352+
ChildProcessSpawner.ChildProcessSpawner
353+
> =>
335354
names.length === 0
336355
? Effect.succeed({})
337356
: runCommandOutput({
338357
probe: "login-shell",
339358
command: shell,
340359
args: ["-ilc", buildEnvironmentCaptureCommand(shell, names)],
341360
env,
342-
timeout: LOGIN_SHELL_TIMEOUT,
361+
timeout,
343362
}).pipe(Effect.map((output) => extractEnvironment(output, names)));
344363

345364
const readLaunchctlPath = (env: NodeJS.ProcessEnv) =>
@@ -349,7 +368,7 @@ const readLaunchctlPath = (env: NodeJS.ProcessEnv) =>
349368
command: "/bin/launchctl",
350369
args: ["getenv", "PATH"],
351370
timeout: LAUNCHCTL_TIMEOUT,
352-
}).pipe(Effect.map(trimNonEmpty));
371+
}).pipe(orEmptyOnTimeout, Effect.map(trimNonEmpty));
353372

354373
const readWindowsEnvironment = Effect.fn("desktop.shellEnvironment.readWindowsEnvironment")(
355374
function* (
@@ -374,7 +393,7 @@ const readWindowsEnvironment = Effect.fn("desktop.shellEnvironment.readWindowsEn
374393
args,
375394
env,
376395
timeout: LOGIN_SHELL_TIMEOUT,
377-
});
396+
}).pipe(orEmptyOnTimeout);
378397
const environment = extractEnvironment(output, names);
379398
if (Object.keys(environment).length > 0) {
380399
return environment;
@@ -429,10 +448,27 @@ const installPosixEnvironment = Effect.fn("desktop.shellEnvironment.installPosix
429448
const userShells = [config.env.SHELL?.trim(), readUserLoginShell()];
430449

431450
let answeredBy: string | undefined;
451+
let waitedForSlowShell = false;
432452
for (const shell of listLoginShellCandidates(config.platform, config.env.SHELL)) {
453+
const read = (timeout: Duration.Duration) =>
454+
readLoginShellEnvironment(shell, LOGIN_SHELL_ENV_NAMES, config.env, timeout);
433455
Object.assign(
434456
shellEnvironment,
435-
yield* readLoginShellEnvironment(shell, LOGIN_SHELL_ENV_NAMES, config.env),
457+
yield* read(LOGIN_SHELL_TIMEOUT).pipe(
458+
Effect.catchTags({
459+
DesktopShellEnvironmentCommandTimeoutError: (error) => {
460+
const retry = !waitedForSlowShell;
461+
waitedForSlowShell = true;
462+
return logShellEnvironmentCommandError(error).pipe(
463+
Effect.andThen(retry ? read(SLOW_LOGIN_SHELL_TIMEOUT) : Effect.succeed({})),
464+
);
465+
},
466+
}),
467+
Effect.catchTags({
468+
DesktopShellEnvironmentCommandTimeoutError: (error) =>
469+
logShellEnvironmentCommandError(error).pipe(Effect.as({})),
470+
}),
471+
),
436472
);
437473
if (shellEnvironment.PATH) {
438474
answeredBy = shell;

0 commit comments

Comments
 (0)