Skip to content

Commit fd3d2fd

Browse files
committed
fix(desktop): isolate shell refresh from cached environment
1 parent 3437923 commit fd3d2fd

2 files changed

Lines changed: 41 additions & 12 deletions

File tree

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ describe("DesktopShellEnvironment", () => {
177177
Effect.gen(function* () {
178178
const fs = yield* FileSystem.FileSystem;
179179
const stateDir = yield* fs.makeTempDirectoryScoped();
180+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner.pipe(
181+
Effect.provide(NodeServices.layer),
182+
);
183+
const executable = yield* HostProcess.ExecutablePath;
180184
const first = { SHELL: "/bin/zsh", PATH: "/usr/bin" };
181185
yield* runShellEnvironment({
182186
env: first,
@@ -187,6 +191,8 @@ describe("DesktopShellEnvironment", () => {
187191
PATH: "/old/bin:/usr/bin",
188192
__MISE_DIFF: "old-mise",
189193
SSH_AUTH_SOCK: "/old-session.sock",
194+
JAVA_HOME: "/old/java",
195+
SSL_CERT_FILE: "/old/cert.pem",
190196
}),
191197
});
192198
const cachePath = `${stateDir}/shell-environment-cache.json`;
@@ -200,17 +206,27 @@ describe("DesktopShellEnvironment", () => {
200206
PATH: "/launcher/bin",
201207
SSH_AUTH_SOCK: "/current-session.sock",
202208
};
209+
const source = [
210+
"const values = { PATH: '/fresh/bin:' + process.env.PATH, __MISE_DIFF: 'fresh-mise', JAVA_HOME: process.env.JAVA_HOME, SSL_CERT_FILE: process.env.SSL_CERT_FILE };",
211+
"for (const [name, value] of Object.entries(values)) {",
212+
" if (value !== undefined) process.stdout.write('__T3CODE_ENV_' + name + '_START__\\n' + value + '\\n__T3CODE_ENV_' + name + '_END__\\n');",
213+
"}",
214+
].join("\n");
203215
let calls = 0;
204216
yield* runShellEnvironment({
205217
env,
206218
stateDir,
207219
platform: "darwin",
208220
handler: () => "",
209-
processSpawner: ChildProcessSpawner.make(() => {
221+
processSpawner: ChildProcessSpawner.make((command) => {
210222
calls++;
211223
return Deferred.succeed(started, undefined).pipe(
212224
Effect.andThen(Deferred.await(release)),
213-
Effect.as(makeProcess(envOutput({ PATH: "/fresh/bin", __MISE_DIFF: "fresh-mise" }))),
225+
Effect.andThen(
226+
command._tag === "StandardCommand"
227+
? spawner.spawn(ChildProcess.make(executable, ["-e", source], command.options))
228+
: Effect.succeed(makeProcess("")),
229+
),
214230
);
215231
}),
216232
use: (service) =>
@@ -229,6 +245,8 @@ describe("DesktopShellEnvironment", () => {
229245
yield* service.awaitDiscovery;
230246
assert.equal(env.PATH, "/fresh/bin:/launcher/bin");
231247
assert.equal(env.__MISE_DIFF, "fresh-mise");
248+
assert.isUndefined(env.JAVA_HOME);
249+
assert.isUndefined(env.SSL_CERT_FILE);
232250
const fresh = yield* Stream.runHead(service.updates);
233251
assert.isTrue(Option.isSome(fresh));
234252
if (Option.isSome(fresh)) {
@@ -240,6 +258,8 @@ describe("DesktopShellEnvironment", () => {
240258
});
241259
const refreshed = yield* fs.readFileString(cachePath).pipe(Effect.flatMap(decodeCache));
242260
assert.equal(refreshed.environment.PATH, "/fresh/bin:/launcher/bin");
261+
assert.notProperty(refreshed.environment, "JAVA_HOME");
262+
assert.notProperty(refreshed.environment, "SSL_CERT_FILE");
243263
}).pipe(Effect.provide(NodeServices.layer)),
244264
);
245265

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")(
254254
readonly probe: DesktopShellEnvironmentProbe;
255255
readonly command: string;
256256
readonly args: ReadonlyArray<string>;
257+
readonly env: NodeJS.ProcessEnv;
257258
readonly timeout: Duration.Duration;
258259
readonly shell?: boolean;
259260
}): Effect.fn.Return<string, never, ChildProcessSpawner.ChildProcessSpawner> {
@@ -263,6 +264,8 @@ const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")(
263264
const child = yield* spawner.spawn(
264265
ChildProcess.make(input.command, input.args, {
265266
shell: input.shell ?? false,
267+
env: input.env,
268+
extendEnv: false,
266269
stdin: "ignore",
267270
stdout: "pipe",
268271
stderr: "ignore",
@@ -318,27 +321,32 @@ const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")(
318321
const readLoginShellEnvironment = (
319322
shell: string,
320323
names: ReadonlyArray<string>,
324+
env: NodeJS.ProcessEnv,
321325
): Effect.Effect<EnvironmentPatch, never, ChildProcessSpawner.ChildProcessSpawner> =>
322326
names.length === 0
323327
? Effect.succeed({})
324328
: runCommandOutput({
325329
probe: "login-shell",
326330
command: shell,
327331
args: ["-ilc", buildEnvironmentCaptureCommand(shell, names)],
332+
env,
328333
timeout: LOGIN_SHELL_TIMEOUT,
329334
}).pipe(Effect.map((output) => extractEnvironment(output, names)));
330335

331-
const readLaunchctlPath = runCommandOutput({
332-
probe: "launchctl-path",
333-
command: "/bin/launchctl",
334-
args: ["getenv", "PATH"],
335-
timeout: LAUNCHCTL_TIMEOUT,
336-
}).pipe(Effect.map(trimNonEmpty));
336+
const readLaunchctlPath = (env: NodeJS.ProcessEnv) =>
337+
runCommandOutput({
338+
env,
339+
probe: "launchctl-path",
340+
command: "/bin/launchctl",
341+
args: ["getenv", "PATH"],
342+
timeout: LAUNCHCTL_TIMEOUT,
343+
}).pipe(Effect.map(trimNonEmpty));
337344

338345
const readWindowsEnvironment = Effect.fn("desktop.shellEnvironment.readWindowsEnvironment")(
339346
function* (
340347
names: ReadonlyArray<string>,
341348
options: WindowsProbeOptions,
349+
env: NodeJS.ProcessEnv,
342350
): Effect.fn.Return<EnvironmentPatch, never, ChildProcessSpawner.ChildProcessSpawner> {
343351
if (names.length === 0) return {};
344352

@@ -355,6 +363,7 @@ const readWindowsEnvironment = Effect.fn("desktop.shellEnvironment.readWindowsEn
355363
probe: options.loadProfile ? "powershell-profile" : "powershell-no-profile",
356364
command,
357365
args,
366+
env,
358367
timeout: LOGIN_SHELL_TIMEOUT,
359368
});
360369
const environment = extractEnvironment(output, names);
@@ -373,8 +382,8 @@ const installWindowsEnvironment = Effect.fn("desktop.shellEnvironment.installWin
373382
): Effect.fn.Return<boolean, never, ChildProcessSpawner.ChildProcessSpawner> {
374383
const [noProfile, profile] = yield* Effect.all(
375384
[
376-
readWindowsEnvironment(["PATH"], { loadProfile: false }),
377-
readWindowsEnvironment(WINDOWS_PROFILE_ENV_NAMES, { loadProfile: true }),
385+
readWindowsEnvironment(["PATH"], { loadProfile: false }, config.env),
386+
readWindowsEnvironment(WINDOWS_PROFILE_ENV_NAMES, { loadProfile: true }, config.env),
378387
],
379388
{ concurrency: 2 },
380389
);
@@ -412,14 +421,14 @@ const installPosixEnvironment = Effect.fn("desktop.shellEnvironment.installPosix
412421
for (const shell of listLoginShellCandidates(config.platform, config.env.SHELL)) {
413422
Object.assign(
414423
shellEnvironment,
415-
yield* readLoginShellEnvironment(shell, LOGIN_SHELL_ENV_NAMES),
424+
yield* readLoginShellEnvironment(shell, LOGIN_SHELL_ENV_NAMES, config.env),
416425
);
417426
if (shellEnvironment.PATH) break;
418427
}
419428

420429
const launchctlPath =
421430
config.platform === "darwin" && !shellEnvironment.PATH
422-
? yield* readLaunchctlPath
431+
? yield* readLaunchctlPath(config.env)
423432
: Option.none<string>();
424433
const mergedPath = mergePaths(config.platform, [
425434
trimNonEmpty(shellEnvironment.PATH).pipe(Option.orElse(() => launchctlPath)),

0 commit comments

Comments
 (0)