diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts index 7808682dfd82b..02b8c4eee295a 100644 --- a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts +++ b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts @@ -184,30 +184,134 @@ 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.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( diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.ts b/apps/desktop/src/shell/DesktopShellEnvironment.ts index 351150aa4b987..a59345372a199 100644 --- a/apps/desktop/src/shell/DesktopShellEnvironment.ts +++ b/apps/desktop/src/shell/DesktopShellEnvironment.ts @@ -660,6 +660,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, @@ -696,6 +711,7 @@ export const make = Effect.gen(function* () { budget, }); const { prepared } = discovery; + stripAppImagePaths(fresh); const patch = Object.fromEntries( Object.keys(ShellEnvironmentPatch.fields).map((name) => [name, fresh[name] ?? null]), ); @@ -756,6 +772,7 @@ export const make = Effect.gen(function* () { return typeof value === "string" ? [[name, value]] : []; }), ); + stripAppImagePaths(cached); const baseline = { ...inherited }; const cachedPath = cached.PATH; if (cachedPath) {