Skip to content

The agent that grows with you

fix(desktop): a home folder startup cannot enter no longer crashes it

Teknium331b2c0887da
diff --git a/apps/desktop/electron/packaged-working-directory.test.ts b/apps/desktop/electron/packaged-working-directory.test.ts
index f827d697ef9..1072f83806a 100644
--- a/apps/desktop/electron/packaged-working-directory.test.ts
+++ b/apps/desktop/electron/packaged-working-directory.test.ts
@@ -10,7 +10,7 @@ import { fileURLToPath } from 'node:url'
import { build } from 'esbuild'
import { test } from 'vitest'
-import { packagedWorkingDirectoryTarget } from './packaged-working-directory'
+import { packagedWorkingDirectoryTarget, releasePackagedWorkingDirectory } from './packaged-working-directory'
// The resident helper (an IME cloud service, a WSL/ssh helper) that the app spawns
// WITHOUT a cwd of its own, so it inherits the app's. It outlives the app and holds
@@ -51,6 +51,24 @@ test('a packaged Windows app leaves a CWD inside its install folder and keeps on
}
})
+// Review fold 3: this runs before main loads, so a home folder that cannot be entered must not
+// crash startup; the app keeps its CWD as it did before the move existed.
+test('a home folder that cannot be entered leaves the CWD and does not crash startup', () => {
+ const facts = {
+ platform: 'win32',
+ isPackaged: true,
+ execPath: 'C:\\Hermes\\win-unpacked\\Hermes.exe',
+ cwd: 'C:\\Hermes\\win-unpacked',
+ homedir: 'H:\\offline-roaming-profile'
+ } as const
+
+ const unreachable = (directory: string) => {
+ throw Object.assign(new Error(`ENOENT: no such file or directory, chdir '${directory}'`), { code: 'ENOENT' })
+ }
+
+ assert.equal(releasePackagedWorkingDirectory(true, facts, unreachable), null)
+})
+
// Exercise the real entry point before main loads: the main stand-in spawns the
// helper, the app is killed, and the directory swap runs with only the orphaned
// helper (exe outside the app tree) still alive.
diff --git a/apps/desktop/electron/packaged-working-directory.ts b/apps/desktop/electron/packaged-working-directory.ts
index 0088e8f5b54..33e225c96c6 100644
--- a/apps/desktop/electron/packaged-working-directory.ts
+++ b/apps/desktop/electron/packaged-working-directory.ts
@@ -28,17 +28,33 @@ export function packagedWorkingDirectoryTarget(facts: WorkingDirectoryFacts): st
return facts.homedir
}
-/** Keep resident children from inheriting a CWD that pins the replaceable app tree. */
-export function releasePackagedWorkingDirectory(isPackaged: boolean): void {
- const target = packagedWorkingDirectoryTarget({
- platform: process.platform,
- isPackaged,
- execPath: process.execPath,
- cwd: process.cwd(),
- homedir: os.homedir()
- })
-
- if (target) {
- process.chdir(target)
+function hostFacts(isPackaged: boolean): WorkingDirectoryFacts {
+ return { platform: process.platform, isPackaged, execPath: process.execPath, cwd: process.cwd(), homedir: os.homedir() }
+}
+
+/**
+ * Keep resident children from inheriting a CWD that pins the replaceable app tree. Runs before
+ * main loads, so it never throws: a home folder that is missing or unreachable (a roaming profile
+ * offline) leaves the CWD where it is, as before this move existed. Returns the new CWD or null.
+ */
+export function releasePackagedWorkingDirectory(
+ isPackaged: boolean,
+ facts: WorkingDirectoryFacts = hostFacts(isPackaged),
+ chdir: (directory: string) => void = directory => process.chdir(directory)
+): string | null {
+ const target = packagedWorkingDirectoryTarget(facts)
+
+ if (!target) {
+ return null
+ }
+
+ try {
+ chdir(target)
+
+ return target
+ } catch (error) {
+ console.warn(`[startup] kept the working directory: could not move it to ${target}: ${(error as Error).message}`)
+
+ return null
}
}