Skip to content

The agent that grows with you

fix(desktop): the image restart drops its mount from PATH when APPDIR was lost

Teknium7196dd018e37
diff --git a/apps/desktop/electron/relaunch-target.test.ts b/apps/desktop/electron/relaunch-target.test.ts
index 3a845dad30b..41f94bf382a 100644
--- a/apps/desktop/electron/relaunch-target.test.ts
+++ b/apps/desktop/electron/relaunch-target.test.ts
@@ -1,5 +1,5 @@
import type * as childProcess from 'node:child_process'
-import { spawn } from 'node:child_process'
+import { type ChildProcess, spawn } from 'node:child_process'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
@@ -120,4 +120,31 @@ describe('relaunchNow', () => {
}
}
)
+
+ // A launch that lost APPDIR still runs from its type-2 mount, which ownAppImage
+ // reads off process.execPath; the restart drops that mount's entries all the same.
+ it('strips the mount it runs from when APPDIR was lost', () => {
+ let childEnv: NodeJS.ProcessEnv | undefined
+ vi.mocked(spawn).mockImplementationOnce(((_file: string, _args: string[], options: childProcess.SpawnOptions) => {
+ childEnv = options.env
+
+ return { on: () => undefined, unref: () => undefined } as unknown as ChildProcess
+ }) as typeof spawn)
+ const mount = '/run/user/1000/.mount_HermesQ7x2Lp'
+ const image = '/home/u/Apps/Hermes.AppImage'
+
+ relaunchNow(
+ { releaseSingleInstanceLock: () => undefined },
+ [],
+ image,
+ {
+ APPIMAGE: image,
+ LD_LIBRARY_PATH: `${mount}/usr/lib`,
+ PATH: `${mount}:${mount}/usr/bin:/tmp/.mount_IdeAbC123/usr/bin:/usr/bin`
+ },
+ `${mount}/hermes`
+ )
+
+ expect(childEnv).toEqual({ APPIMAGE: image, PATH: '/tmp/.mount_IdeAbC123/usr/bin:/usr/bin' })
+ })
})
diff --git a/apps/desktop/electron/relaunch-target.ts b/apps/desktop/electron/relaunch-target.ts
index b4d1e182511..286c6184dda 100644
--- a/apps/desktop/electron/relaunch-target.ts
+++ b/apps/desktop/electron/relaunch-target.ts
@@ -117,6 +117,22 @@ for f in /proc/self/fd/*; do
done
exec "$0" "$@"`
+/**
+ * The root of the AppImage mount `execPath` runs from: APPDIR, or when that
+ * was lost the `.mount_<name><random>` directory on the executable's path
+ * (see ownAppImage).
+ */
+function mountRoot(execPath: string, env: NodeJS.ProcessEnv): string | undefined {
+ if (env.APPDIR) {
+ return posix.resolve(env.APPDIR)
+ }
+
+ const segments = posix.dirname(posix.resolve(execPath)).split('/')
+ const mount = segments.findIndex(segment => segment.startsWith('.mount_'))
+
+ return mount < 0 ? undefined : segments.slice(0, mount + 1).join('/')
+}
+
/**
* `env` without the PATH-style entries inside this mount, which the image's
* AppRun prepended (PATH, LD_LIBRARY_PATH, XDG_DATA_DIRS, ...). The new
@@ -124,8 +140,8 @@ exec "$0" "$@"`
* child stops holding it: a lookup through it while its FUSE daemon exits
* fails with ENOTCONN, which aborted the new AppRun's `env bash`.
*/
-function withoutMountEntries(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
- const appDir = env.APPDIR && posix.resolve(env.APPDIR)
+function withoutMountEntries(env: NodeJS.ProcessEnv, execPath: string): NodeJS.ProcessEnv {
+ const appDir = mountRoot(execPath, env)
if (!appDir) {
return env
@@ -166,7 +182,8 @@ export function relaunchNow(
host: RelaunchHost,
args: string[],
target: string = relaunchTarget(),
- env: NodeJS.ProcessEnv = process.env
+ env: NodeJS.ProcessEnv = process.env,
+ execPath: string = process.execPath
): void {
host.releaseSingleInstanceLock()
@@ -176,7 +193,7 @@ export function relaunchNow(
// reached through a symlinked $TMPDIR) needs APPDIR/APPIMAGE, which no AppRun
// restores, and another app's AppImage env is the user's, as app.relaunch kept it.
const ownImage = env.APPIMAGE && posix.resolve(target) === posix.resolve(env.APPIMAGE)
- const childEnv = ownImage ? withoutMountEntries(env) : env
+ const childEnv = ownImage ? withoutMountEntries(env, execPath) : env
const child = spawn('/bin/sh', ['-c', EXEC_WITH_STDIO_ONLY, target, ...args], {
detached: true,