Skip to content

Commit 0b75a85

Browse files
Lucenx9maria-rcks
andauthored
fix(desktop): keep noisy shell probes from blocking PATH capture (#21913)
Co-authored-by: Simone <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 90f0646 commit 0b75a85

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @effect-diagnostics nodeBuiltinImport:off -- The process fixture runs real shell commands with isolated config files.
22
import * as NodeServices from "@effect/platform-node/NodeServices";
33
import { assert, describe, it } from "@effect/vitest";
4+
import * as HostProcess from "@t3tools/shared/HostProcess";
45
import * as Effect from "effect/Effect";
56
import * as Layer from "effect/Layer";
67
import * as Logger from "effect/Logger";
@@ -72,6 +73,7 @@ function runShellEnvironment(input: {
7273
command: ChildProcess.Command,
7374
) => string | { output: string; exitCode: number };
7475
readonly failure?: PlatformError.PlatformError;
76+
readonly processSpawner?: ChildProcessSpawner.ChildProcessSpawner["Service"];
7577
}) {
7678
const layerEnvironment = Layer.succeed(
7779
DesktopEnvironment.DesktopEnvironment,
@@ -82,6 +84,7 @@ function runShellEnvironment(input: {
8284
const layerSpawner = Layer.succeed(
8385
ChildProcessSpawner.ChildProcessSpawner,
8486
ChildProcessSpawner.make((command) => {
87+
if (input.processSpawner) return input.processSpawner.spawn(command);
8588
if (input.failure !== undefined) return Effect.fail(input.failure);
8689
const result = input.handler(command);
8790
return Effect.succeed(
@@ -118,6 +121,41 @@ function runShellEnvironment(input: {
118121
}
119122

120123
describe("DesktopShellEnvironment", () => {
124+
it.effect(
125+
"captures the login-shell PATH when startup writes more than a pipe buffer to stderr",
126+
() =>
127+
Effect.gen(function* () {
128+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner.pipe(
129+
Effect.provide(NodeServices.layer),
130+
);
131+
const executable = yield* HostProcess.ExecutablePath;
132+
const env: NodeJS.ProcessEnv = { SHELL: "/test-shell", PATH: "/usr/bin" };
133+
const commands: string[] = [];
134+
const source = [
135+
"setTimeout(() => process.exit(99), 3000).unref();",
136+
"process.stderr.write(Buffer.alloc(2 * 1024 * 1024, 120), () => {",
137+
`process.stdout.write(${JSON.stringify(envOutput({ PATH: "/shell-only/bin:/usr/bin" }))});`,
138+
"});",
139+
].join("\n");
140+
141+
yield* runShellEnvironment({
142+
env,
143+
platform: "linux",
144+
handler: () => "",
145+
processSpawner: ChildProcessSpawner.make((command) => {
146+
if (command._tag !== "StandardCommand") return Effect.succeed(makeProcess(""));
147+
commands.push(command.command);
148+
return command.command === "/test-shell"
149+
? spawner.spawn(ChildProcess.make(executable, ["-e", source], command.options))
150+
: Effect.succeed(makeProcess(""));
151+
}),
152+
}).pipe(Effect.provide(Logger.layer([])));
153+
154+
assert.equal(env.PATH, "/shell-only/bin:/usr/bin");
155+
assert.deepEqual(commands, ["/test-shell"]);
156+
}),
157+
);
158+
121159
it.effect("rejects PATH output from a failed shell and tries the next candidate", () =>
122160
Effect.gen(function* () {
123161
const env: NodeJS.ProcessEnv = { SHELL: "/bin/failing", PATH: "/usr/bin" };

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")(
255255
shell: input.shell ?? false,
256256
stdin: "ignore",
257257
stdout: "pipe",
258-
stderr: "pipe",
258+
stderr: "ignore",
259259
killSignal: "SIGTERM",
260260
forceKillAfter: PROCESS_TERMINATE_GRACE,
261261
}),

0 commit comments

Comments
 (0)