From f4c184ec5e88f4895b95c16f73388ab686e5a340 Mon Sep 17 00:00:00 2001 From: Lakshmi Tanmay Date: Wed, 4 Nov 2026 18:11:34 +0530 Subject: [PATCH 1/2] fix(desktop): remove stale AppImage mounts from cached PATH --- .../src/shell/DesktopShellEnvironment.test.ts | 169 ++++++++++++++---- .../src/shell/DesktopShellEnvironment.ts | 17 ++ 2 files changed, 156 insertions(+), 30 deletions(-) diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts index c00e2ea7c65a2..085a48b3567be 100644 --- a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts +++ b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts @@ -163,35 +163,139 @@ function runShellEnvironment(input: { } describe("DesktopShellEnvironment", () => { - it.effect("does not wait for discovery when the cache is missing", () => - Effect.gen(function* () { - const started = yield* Deferred.make(); - const release = yield* Deferred.make(); - yield* runShellEnvironment({ - env: { SHELL: "/test-shell", PATH: "/usr/bin" }, - platform: "darwin", - handler: () => "", - processSpawner: ChildProcessSpawner.make(() => - Deferred.succeed(started, undefined).pipe( - Effect.andThen(Deferred.await(release)), - Effect.as(makeProcess(envOutput({ PATH: "/fresh/bin" }))), - ), - ), - use: (service) => - Effect.gen(function* () { - const startup = yield* service.installIntoProcess.pipe(Effect.forkChild); - yield* Deferred.await(started); - assert.isDefined(startup.pollUnsafe()); - yield* Deferred.succeed(release, undefined); - return yield* service.awaitDiscovery; + it.effect.each([ + { platform: "linux" as const, runtime: { APPDIR: "/opt/t3.AppDir/" }, scrub: true }, + { platform: "linux" as const, runtime: { APPIMAGE: "/opt/T3.AppImage" }, scrub: true }, + { platform: "linux" as const, runtime: {}, scrub: false }, + { platform: "darwin" as const, runtime: { APPDIR: "/opt/t3.AppDir/" }, scrub: false }, + ])( + "keeps AppImage mounts out of cached and discovered PATH: %j", + ({ platform, runtime, scrub }) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const stateDir = yield* fs.makeTempDirectoryScoped(); + const cachePath = `${stateDir}/shell-environment-cache.json`; + const inheritedPath = + "/tmp/.mount_T3-Codold123/usr/bin:/launcher/bin:/opt/t3.AppDir/usr/bin"; + const cachedPath = "/cached/bin:/tmp/.mount_T3-Codcache1/usr/bin"; + const discoveredPath = "/fresh/bin:/tmp/.mount_T3-Codprobe1/usr/bin"; + const retainedPath = "/tmp/tools:/tmp/.mount_Other123/usr/bin:/opt/t3.AppDir-backup/bin"; + const cleanInherited = scrub + ? `/launcher/bin${runtime.APPDIR ? "" : ":/opt/t3.AppDir/usr/bin"}` + : inheritedPath; + const cleanCached = scrub ? "/cached/bin" : cachedPath; + const cleanDiscovered = scrub ? "/fresh/bin" : discoveredPath; + const env: NodeJS.ProcessEnv = { + ...runtime, + SHELL: "/bin/bash", + PATH: `${inheritedPath}:${retainedPath}`, + }; + yield* fs.writeFileString( + cachePath, + encodeCache({ + version: 1, + key: JSON.stringify([platform, "/test-home", "/bin/bash", ""]), + environment: { PATH: cachedPath, __MISE_DIFF: "cached-mise" }, }), - }); - }), + ); + const started = yield* Deferred.make(); + const release = yield* Deferred.make(); + const probePath = yield* Deferred.make(); + yield* runShellEnvironment({ + env, + platform, + stateDir, + handler: () => "", + processSpawner: ChildProcessSpawner.make((command) => + Effect.gen(function* () { + const value = + command._tag === "StandardCommand" ? command.options.env?.PATH : undefined; + const path = typeof value === "string" ? value : undefined; + yield* Deferred.succeed(probePath, path); + yield* Deferred.succeed(started, undefined); + yield* Deferred.await(release); + return makeProcess( + envOutput({ PATH: `${discoveredPath}:${path}`, __MISE_DIFF: "fresh-mise" }), + ); + }), + ), + use: (service) => + Effect.gen(function* () { + yield* service.installIntoProcess; + const expectedCached = `${cleanCached}:${cleanInherited}:${retainedPath}`; + assert.equal(env.PATH, expectedCached); + assert.equal(env.__MISE_DIFF, "cached-mise"); + const cached = yield* Stream.runHead(service.updates); + assert.equal(Option.getOrThrow(cached).environment.PATH, expectedCached); + yield* Deferred.await(started); + assert.equal(yield* Deferred.await(probePath), `${cleanInherited}:${retainedPath}`); + yield* Deferred.succeed(release, undefined); + yield* service.awaitDiscovery; + const expectedFresh = `${cleanDiscovered}:${cleanInherited}:${retainedPath}`; + assert.equal(env.PATH, expectedFresh); + assert.equal(env.__MISE_DIFF, "fresh-mise"); + const fresh = yield* Stream.runHead(service.updates); + assert.equal(Option.getOrThrow(fresh).environment.PATH, expectedFresh); + return true; + }), + }); + const cached = yield* fs.readFileString(cachePath).pipe(Effect.flatMap(decodeCache)); + assert.equal(cached.environment.PATH, env.PATH); + assert.equal(cached.environment.__MISE_DIFF, "fresh-mise"); + }).pipe(Effect.provide(NodeServices.layer)), ); - it.effect( - "reuses cached paths immediately and streams fresh discovery without keeping removed paths", - () => + it.effect.each([ + { platform: "darwin" as const, env: { PATH: "/usr/bin" }, expectedPath: "/usr/bin" }, + { + platform: "linux" as const, + env: { APPDIR: "/tmp/.mount_T3-Codnew123", PATH: ":/tmp/.mount_T3-Codold123/bin::/usr/bin:" }, + expectedPath: "::/usr/bin:", + }, + { + platform: "linux" as const, + env: { APPDIR: "/tmp/.mount_T3-Codnew123", PATH: "/tmp/.mount_T3-Codnew123/bin" }, + expectedPath: undefined, + }, + ])( + "does not wait for discovery when the cache is missing: %j", + ({ platform, env: initial, expectedPath }) => + Effect.gen(function* () { + const started = yield* Deferred.make(); + const release = yield* Deferred.make(); + const env: NodeJS.ProcessEnv = { SHELL: "/test-shell", ...initial }; + let probePath: string | undefined; + yield* runShellEnvironment({ + env, + platform, + handler: () => "", + processSpawner: ChildProcessSpawner.make((command) => + Effect.gen(function* () { + const value = + command._tag === "StandardCommand" ? command.options.env?.PATH : undefined; + probePath = typeof value === "string" ? value : undefined; + yield* Deferred.succeed(started, undefined); + yield* Deferred.await(release); + return makeProcess(envOutput({ PATH: "/fresh/bin" })); + }), + ), + use: (service) => + Effect.gen(function* () { + const startup = yield* service.installIntoProcess.pipe(Effect.forkChild); + yield* Deferred.await(started); + assert.isDefined(startup.pollUnsafe()); + assert.equal(env.PATH, expectedPath); + assert.equal(probePath, expectedPath); + yield* Deferred.succeed(release, undefined); + return yield* service.awaitDiscovery; + }), + }); + }), + ); + + it.effect.each(["darwin", "linux"] as const)( + "reuses cached paths immediately and streams fresh discovery without keeping removed paths on %s", + (platform) => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const stateDir = yield* fs.makeTempDirectoryScoped(); @@ -199,11 +303,12 @@ describe("DesktopShellEnvironment", () => { Effect.provide(NodeServices.layer), ); const executable = yield* HostProcess.ExecutablePath; - const first = { SHELL: "/bin/zsh", PATH: "/usr/bin" }; + const runtime = platform === "linux" ? { APPDIR: "/tmp/.mount_T3-Codnew123" } : {}; + const first = { ...runtime, SHELL: "/bin/zsh", PATH: "/usr/bin" }; yield* runShellEnvironment({ env: first, stateDir, - platform: "darwin", + platform, handler: () => envOutput({ PATH: "/old/bin:/usr/bin", @@ -220,8 +325,12 @@ describe("DesktopShellEnvironment", () => { const started = yield* Deferred.make(); const release = yield* Deferred.make(); const env: NodeJS.ProcessEnv = { + ...runtime, SHELL: "/bin/zsh", - PATH: "/launcher/bin", + PATH: + platform === "linux" + ? "/tmp/.mount_T3-Codold123/bin:/launcher/bin:/tmp/.mount_T3-Codnew123/usr/bin" + : "/launcher/bin", SSH_AUTH_SOCK: "/current-session.sock", }; const source = [ @@ -234,7 +343,7 @@ describe("DesktopShellEnvironment", () => { yield* runShellEnvironment({ env, stateDir, - platform: "darwin", + platform, handler: () => "", processSpawner: ChildProcessSpawner.make((command) => { calls++; diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.ts b/apps/desktop/src/shell/DesktopShellEnvironment.ts index f0983ca622490..282a4a422497f 100644 --- a/apps/desktop/src/shell/DesktopShellEnvironment.ts +++ b/apps/desktop/src/shell/DesktopShellEnvironment.ts @@ -621,6 +621,21 @@ export const make = Effect.gen(function* () { const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; const scope = yield* Scope.Scope; const env = process.env; + const isLinuxAppImage = + environment.platform === "linux" && (env.APPIMAGE !== undefined || env.APPDIR !== undefined); + const appDir = env.APPDIR?.replace(/\/+$/u, ""); + const stripAppImagePaths = (target: NodeJS.ProcessEnv) => { + if (!isLinuxAppImage || target.PATH === undefined) return; + // Cached PATHs can outlive the AppImage mount that originally contributed them. + const kept = target.PATH.split(":").filter( + (entry) => + !(appDir && (entry === appDir || entry.startsWith(`${appDir}/`))) && + !/^\/tmp\/\.mount_T3-[^/]+(?:\/|$)/u.test(entry), + ); + if (kept.length > 0) target.PATH = kept.join(":"); + else delete target.PATH; + }; + stripAppImagePaths(env); const inherited = { ...env }; const key = encodeCacheKey([ environment.platform, @@ -655,6 +670,7 @@ export const make = Effect.gen(function* () { platform: environment.platform, }); const { prepared } = discovery; + stripAppImagePaths(fresh); const patch = Object.fromEntries( Object.keys(ShellEnvironmentPatch.fields).map((name) => [name, fresh[name] ?? null]), ); @@ -695,6 +711,7 @@ export const make = Effect.gen(function* () { return typeof value === "string" ? [[name, value]] : []; }), ); + stripAppImagePaths(cached); const baseline = { ...inherited }; const cachedPath = cached.PATH; if (cachedPath) { From 4989fe23732ab209a187263d052f6a8bd6577d33 Mon Sep 17 00:00:00 2001 From: Lakshmi Tanmay Date: Wed, 4 Nov 2026 18:27:20 +0530 Subject: [PATCH 2/2] test(desktop): avoid repeating AppImage PATH coverage --- .../src/shell/DesktopShellEnvironment.test.ts | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts index 085a48b3567be..23f002b85e3a4 100644 --- a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts +++ b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts @@ -293,9 +293,9 @@ describe("DesktopShellEnvironment", () => { }), ); - it.effect.each(["darwin", "linux"] as const)( - "reuses cached paths immediately and streams fresh discovery without keeping removed paths on %s", - (platform) => + it.effect( + "reuses cached paths immediately and streams fresh discovery without keeping removed paths", + () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const stateDir = yield* fs.makeTempDirectoryScoped(); @@ -303,12 +303,11 @@ describe("DesktopShellEnvironment", () => { Effect.provide(NodeServices.layer), ); const executable = yield* HostProcess.ExecutablePath; - const runtime = platform === "linux" ? { APPDIR: "/tmp/.mount_T3-Codnew123" } : {}; - const first = { ...runtime, SHELL: "/bin/zsh", PATH: "/usr/bin" }; + const first = { SHELL: "/bin/zsh", PATH: "/usr/bin" }; yield* runShellEnvironment({ env: first, stateDir, - platform, + platform: "darwin", handler: () => envOutput({ PATH: "/old/bin:/usr/bin", @@ -325,12 +324,8 @@ describe("DesktopShellEnvironment", () => { const started = yield* Deferred.make(); const release = yield* Deferred.make(); const env: NodeJS.ProcessEnv = { - ...runtime, SHELL: "/bin/zsh", - PATH: - platform === "linux" - ? "/tmp/.mount_T3-Codold123/bin:/launcher/bin:/tmp/.mount_T3-Codnew123/usr/bin" - : "/launcher/bin", + PATH: "/launcher/bin", SSH_AUTH_SOCK: "/current-session.sock", }; const source = [ @@ -343,7 +338,7 @@ describe("DesktopShellEnvironment", () => { yield* runShellEnvironment({ env, stateDir, - platform, + platform: "darwin", handler: () => "", processSpawner: ChildProcessSpawner.make((command) => { calls++;