TypeScript
fix(desktop): an AppImage launch drops earlier launches' mount dirs from the server PATH (#18808)
Igor Makowski5bed8e4df6fb
diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts b/apps/desktop/src/shell/DesktopShellEnvironment.test.tsindex aee5a0bc81b..27f4298a49f 100644--- a/apps/desktop/src/shell/DesktopShellEnvironment.test.ts+++ b/apps/desktop/src/shell/DesktopShellEnvironment.test.ts@@ -530,6 +530,49 @@ describe("DesktopShellEnvironment", () => {}),);+ it.effect("drops earlier AppImage launches' mount dirs from the search paths on linux", () =>+ Effect.gen(function* () {+ const current = "/var/tmp/.mount_cur";+ const env: NodeJS.ProcessEnv = {+ SHELL: "/bin/zsh",+ APPIMAGE: "/home/me/T3-Code.AppImage",+ APPDIR: `${current}/`,+ PATH: `${current}/usr/bin:/tmp/.mount_old/usr/bin:/usr/bin`,+ LD_LIBRARY_PATH: `/tmp/.mount_old/usr/lib:${current}/usr/lib::/opt/lib`,+ XDG_DATA_DIRS: "/tmp/.mount_old/usr/share:/var/tmp/.mount_cur2/usr/share",+ };++ yield* runShellEnvironment({+ env,+ platform: "linux",+ handler: () => envOutput({ PATH: "/tmp/.mount_older/usr/bin:/home/me/.local/bin" }),+ });++ assert.equal(env.PATH, `/home/me/.local/bin:${current}/usr/bin:/usr/bin`);+ assert.equal(env.LD_LIBRARY_PATH, `${current}/usr/lib::/opt/lib`);+ assert.equal(env.XDG_DATA_DIRS, undefined);+ }),+ );++ it.effect("keeps AppImage-looking search paths outside an AppImage launch", () =>+ Effect.gen(function* () {+ const env: NodeJS.ProcessEnv = {+ SHELL: "/bin/zsh",+ PATH: "/tmp/.mount_old/usr/bin:/usr/bin",+ LD_LIBRARY_PATH: "/tmp/.mount_old/usr/lib",+ };++ yield* runShellEnvironment({+ env,+ platform: "linux",+ handler: () => envOutput({ PATH: "/usr/bin" }),+ });++ assert.equal(env.PATH, "/usr/bin:/tmp/.mount_old/usr/bin");+ assert.equal(env.LD_LIBRARY_PATH, "/tmp/.mount_old/usr/lib");+ }),+ );+it.effect("prefers login-shell desktop session hints over inherited values on linux", () =>Effect.gen(function* () {const env: NodeJS.ProcessEnv = {diff --git a/apps/desktop/src/shell/DesktopShellEnvironment.ts b/apps/desktop/src/shell/DesktopShellEnvironment.tsindex 8fa8a60b2e6..ad21a8d6176 100644--- a/apps/desktop/src/shell/DesktopShellEnvironment.ts+++ b/apps/desktop/src/shell/DesktopShellEnvironment.ts@@ -114,6 +114,37 @@ const readEnvPath = (env: NodeJS.ProcessEnv): Option.Option<string> =>const normalizeRuntimeDir = (value: string): string => value.replace(/\/+$/u, "");+const APPIMAGE_SEARCH_PATH_ENV_NAMES = ["PATH", "LD_LIBRARY_PATH", "XDG_DATA_DIRS"] as const;++// The AppImage runtime mounts each launch at `$TMPDIR/.mount_<id>` and prepends+// it to the search paths above. A launch started from a shell that inherited an+// earlier launch's env carries that mount too, and once it is unmounted every+// spawn that walks PATH (git first of all) fails with ENOTCONN.+const APPIMAGE_MOUNT_ROOT = /^(.*?\/\.mount_[^/]+)(?:\/|$)/u;++const stripStaleAppImageMounts = (env: NodeJS.ProcessEnv): void => {+ if (env.APPIMAGE === undefined && env.APPDIR === undefined) return;+ const appDir = env.APPDIR === undefined ? undefined : normalizeRuntimeDir(env.APPDIR);+ for (const name of APPIMAGE_SEARCH_PATH_ENV_NAMES) {+ const value = env[name];+ if (value === undefined) continue;+ // glibc's loader also splits LD_LIBRARY_PATH on `;`.+ const segments = value.split(name === "LD_LIBRARY_PATH" ? /[:;]/u : ":");+ const kept = segments.filter((segment) => {+ const mountRoot = APPIMAGE_MOUNT_ROOT.exec(segment)?.[1];+ return mountRoot === undefined || mountRoot === appDir;+ });+ if (kept.length === segments.length) continue;+ if (kept.length === 0) {+ delete env[name];+ } else {+ // An empty segment is the current dir to the loader and to PATH lookup,+ // but a lone one joins to "", which reads as unset.+ env[name] = kept.join(":") || ":";+ }+ }+};+const linuxRuntimeDirCandidates = (env: NodeJS.ProcessEnv,uid: number | undefined,@@ -430,6 +461,9 @@ const installPosixEnvironment = Effect.fn("desktop.shellEnvironment.installPosixif (Option.isSome(mergedPath)) {config.env.PATH = mergedPath.value;}+ // The server, relaunch and updater all inherit process.env, so scrubbing+ // here keeps earlier AppImage launches' mounts out of every one of them.+ stripStaleAppImageMounts(config.env);if (!config.env.SSH_AUTH_SOCK && shellEnvironment.SSH_AUTH_SOCK) {config.env.SSH_AUTH_SOCK = shellEnvironment.SSH_AUTH_SOCK;}