Skip to content

Commit 5bed8e4

Browse files
Mnigosjuliusmarmingeclaude
authored
fix(desktop): an AppImage launch drops earlier launches' mount dirs from the server PATH (#18808)
Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Claude Fable 5.1 <[email protected]>
1 parent 9957790 commit 5bed8e4

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,49 @@ describe("DesktopShellEnvironment", () => {
530530
}),
531531
);
532532

533+
it.effect("drops earlier AppImage launches' mount dirs from the search paths on linux", () =>
534+
Effect.gen(function* () {
535+
const current = "/var/tmp/.mount_cur";
536+
const env: NodeJS.ProcessEnv = {
537+
SHELL: "/bin/zsh",
538+
APPIMAGE: "/home/me/T3-Code.AppImage",
539+
APPDIR: `${current}/`,
540+
PATH: `${current}/usr/bin:/tmp/.mount_old/usr/bin:/usr/bin`,
541+
LD_LIBRARY_PATH: `/tmp/.mount_old/usr/lib:${current}/usr/lib::/opt/lib`,
542+
XDG_DATA_DIRS: "/tmp/.mount_old/usr/share:/var/tmp/.mount_cur2/usr/share",
543+
};
544+
545+
yield* runShellEnvironment({
546+
env,
547+
platform: "linux",
548+
handler: () => envOutput({ PATH: "/tmp/.mount_older/usr/bin:/home/me/.local/bin" }),
549+
});
550+
551+
assert.equal(env.PATH, `/home/me/.local/bin:${current}/usr/bin:/usr/bin`);
552+
assert.equal(env.LD_LIBRARY_PATH, `${current}/usr/lib::/opt/lib`);
553+
assert.equal(env.XDG_DATA_DIRS, undefined);
554+
}),
555+
);
556+
557+
it.effect("keeps AppImage-looking search paths outside an AppImage launch", () =>
558+
Effect.gen(function* () {
559+
const env: NodeJS.ProcessEnv = {
560+
SHELL: "/bin/zsh",
561+
PATH: "/tmp/.mount_old/usr/bin:/usr/bin",
562+
LD_LIBRARY_PATH: "/tmp/.mount_old/usr/lib",
563+
};
564+
565+
yield* runShellEnvironment({
566+
env,
567+
platform: "linux",
568+
handler: () => envOutput({ PATH: "/usr/bin" }),
569+
});
570+
571+
assert.equal(env.PATH, "/usr/bin:/tmp/.mount_old/usr/bin");
572+
assert.equal(env.LD_LIBRARY_PATH, "/tmp/.mount_old/usr/lib");
573+
}),
574+
);
575+
533576
it.effect("prefers login-shell desktop session hints over inherited values on linux", () =>
534577
Effect.gen(function* () {
535578
const env: NodeJS.ProcessEnv = {

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,37 @@ const readEnvPath = (env: NodeJS.ProcessEnv): Option.Option<string> =>
114114

115115
const normalizeRuntimeDir = (value: string): string => value.replace(/\/+$/u, "");
116116

117+
const APPIMAGE_SEARCH_PATH_ENV_NAMES = ["PATH", "LD_LIBRARY_PATH", "XDG_DATA_DIRS"] as const;
118+
119+
// The AppImage runtime mounts each launch at `$TMPDIR/.mount_<id>` and prepends
120+
// it to the search paths above. A launch started from a shell that inherited an
121+
// earlier launch's env carries that mount too, and once it is unmounted every
122+
// spawn that walks PATH (git first of all) fails with ENOTCONN.
123+
const APPIMAGE_MOUNT_ROOT = /^(.*?\/\.mount_[^/]+)(?:\/|$)/u;
124+
125+
const stripStaleAppImageMounts = (env: NodeJS.ProcessEnv): void => {
126+
if (env.APPIMAGE === undefined && env.APPDIR === undefined) return;
127+
const appDir = env.APPDIR === undefined ? undefined : normalizeRuntimeDir(env.APPDIR);
128+
for (const name of APPIMAGE_SEARCH_PATH_ENV_NAMES) {
129+
const value = env[name];
130+
if (value === undefined) continue;
131+
// glibc's loader also splits LD_LIBRARY_PATH on `;`.
132+
const segments = value.split(name === "LD_LIBRARY_PATH" ? /[:;]/u : ":");
133+
const kept = segments.filter((segment) => {
134+
const mountRoot = APPIMAGE_MOUNT_ROOT.exec(segment)?.[1];
135+
return mountRoot === undefined || mountRoot === appDir;
136+
});
137+
if (kept.length === segments.length) continue;
138+
if (kept.length === 0) {
139+
delete env[name];
140+
} else {
141+
// An empty segment is the current dir to the loader and to PATH lookup,
142+
// but a lone one joins to "", which reads as unset.
143+
env[name] = kept.join(":") || ":";
144+
}
145+
}
146+
};
147+
117148
const linuxRuntimeDirCandidates = (
118149
env: NodeJS.ProcessEnv,
119150
uid: number | undefined,
@@ -430,6 +461,9 @@ const installPosixEnvironment = Effect.fn("desktop.shellEnvironment.installPosix
430461
if (Option.isSome(mergedPath)) {
431462
config.env.PATH = mergedPath.value;
432463
}
464+
// The server, relaunch and updater all inherit process.env, so scrubbing
465+
// here keeps earlier AppImage launches' mounts out of every one of them.
466+
stripStaleAppImageMounts(config.env);
433467
if (!config.env.SSH_AUTH_SOCK && shellEnvironment.SSH_AUTH_SOCK) {
434468
config.env.SSH_AUTH_SOCK = shellEnvironment.SSH_AUTH_SOCK;
435469
}

0 commit comments

Comments
 (0)