perf(desktop): reuse the prepared shell environment in the local backend (#17384)

Co-authored-by: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
Yash SinghandClaude Sonnet 5.5 authored and GitHub committed 2026-10-09 23:08:57 -05:00
1 parent aacefcd7e9
commit 03bd839554
11 files changed
+304 -66

No files matched your search

@@ -1,3 +1,4 @@
import { listLoginShellCandidates } from "@t3tools/shared/shell";
import * as Context from "effect/Context";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
@@ -5,6 +6,7 @@ import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
import * as Stream from "effect/Stream";
import * as ChildProcess from "effect/process/ChildProcess";
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
@@ -15,7 +17,6 @@ type EnvironmentPatch = Record<string, string>;
interface ShellEnvironmentConfig {
readonly env: NodeJS.ProcessEnv;
readonly platform: NodeJS.Platform;
readonly userShell: Option.Option<string>;
}
interface WindowsProbeOptions {
@@ -157,25 +158,6 @@ const mergePaths = (
return entries.length > 0 ? Option.some(entries.join(delimiter)) : Option.none();
};
const listLoginShellCandidates = (config: ShellEnvironmentConfig): ReadonlyArray<string> => {
const fallback =
config.platform === "darwin" ? "/bin/zsh" : config.platform === "linux" ? "/bin/bash" : "";
const seen = new Set<string>();
const candidates: string[] = [];
for (const candidate of [
trimNonEmpty(config.env.SHELL),
config.userShell,
trimNonEmpty(fallback),
]) {
if (Option.isNone(candidate) || seen.has(candidate.value)) continue;
seen.add(candidate.value);
candidates.push(candidate.value);
}
return candidates;
};
const knownWindowsCliDirs = (env: NodeJS.ProcessEnv): ReadonlyArray<string> => [
...trimNonEmpty(env.APPDATA).pipe(
Option.match({
@@ -267,33 +249,49 @@ const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")(
readonly shell?: boolean;
}): Effect.fn.Return<string, never, ChildProcessSpawner.ChildProcessSpawner> {
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const output = yield* spawner
.string(
ChildProcess.make(input.command, input.args, {
shell: input.shell ?? false,
stdin: "ignore",
stdout: "pipe",
stderr: "pipe",
killSignal: "SIGTERM",
forceKillAfter: PROCESS_TERMINATE_GRACE,
}),
)
.pipe(
Effect.mapError(
(cause) =>
new DesktopShellEnvironmentCommandError({
probe: input.probe,
executable: executableName(input.command),
argumentCount: input.args.length,
cause,
}),
),
Effect.catchTags({
DesktopShellEnvironmentCommandError: (error) =>
logShellEnvironmentCommandError(error).pipe(Effect.as("")),
}),
Effect.timeoutOption(input.timeout),
);
const output = yield* Effect.scoped(
Effect.gen(function* () {
const child = yield* spawner.spawn(
ChildProcess.make(input.command, input.args, {
shell: input.shell ?? false,
stdin: "ignore",
stdout: "pipe",
stderr: "pipe",
killSignal: "SIGTERM",
forceKillAfter: PROCESS_TERMINATE_GRACE,
}),
);
return yield* Effect.all([Stream.mkString(Stream.decodeText(child.stdout)), child.exitCode], {
concurrency: 2,
});
}),
).pipe(
Effect.mapError(
(cause) =>
new DesktopShellEnvironmentCommandError({
probe: input.probe,
executable: executableName(input.command),
argumentCount: input.args.length,
cause,
}),
),
Effect.filterOrFail(
([, exitCode]) => exitCode === 0,
([, exitCode]) =>
new DesktopShellEnvironmentCommandError({
probe: input.probe,
executable: executableName(input.command),
argumentCount: input.args.length,
cause: { exitCode },
}),
),
Effect.map(([stdout]) => stdout),
Effect.catchTags({
DesktopShellEnvironmentCommandError: (error) =>
logShellEnvironmentCommandError(error).pipe(Effect.as("")),
}),
Effect.timeoutOption(input.timeout),
);
if (Option.isSome(output)) {
return output.value;
}
@@ -406,7 +404,7 @@ const installPosixEnvironment = Effect.fn("desktop.shellEnvironment.installPosix
const fileSystem = yield* FileSystem.FileSystem;
const shellEnvironment: EnvironmentPatch = {};
for (const shell of listLoginShellCandidates(config)) {
for (const shell of listLoginShellCandidates(config.platform, config.env.SHELL)) {
Object.assign(
shellEnvironment,
yield* readLoginShellEnvironment(shell, LOGIN_SHELL_ENV_NAMES),
@@ -521,7 +519,6 @@ export const make = Effect.gen(function* () {
installShellEnvironment({
env: process.env,
platform: environment.platform,
userShell: Option.none(),
}).pipe(
Effect.provideService(FileSystem.FileSystem, fileSystem),
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),