Skip to content

The agent that grows with you

fix(desktop): release Windows install cwd before loading main

yyyzl618451742737
diff --git a/apps/desktop/electron/entry.ts b/apps/desktop/electron/entry.ts
index abed288fe2a..ae3be8d579e 100644
--- a/apps/desktop/electron/entry.ts
+++ b/apps/desktop/electron/entry.ts
@@ -5,10 +5,15 @@ import path from 'node:path'
import { app } from 'electron'
import { resolveDesktopHermesHome } from './data-paths'
+import { releasePackagedWorkingDirectory } from './packaged-working-directory'
import { readDesktopLaunchConfig } from './renderer-heap-flags'
import { wslgLaunchArgs } from './wslg-launch'
import { spawnWslgLaunch } from './wslg-launch-process'
+// Run before main creates windows/IME integrations or spawns resident helpers.
+// Closing Hermes cannot release a CWD inherited by an out-of-tree process.
+releasePackagedWorkingDirectory(app.isPackaged || Boolean(process.env.HERMES_DESKTOP_IS_PACKAGED))
+
function configuredElectronFlags(env: NodeJS.ProcessEnv): string[] {
// Resolve the home exactly like main.ts does, through the shared resolver:
// HERMES_DATA_DIR_SUFFIX channel installs and profiles/-rooted HERMES_HOME
diff --git a/apps/desktop/electron/packaged-working-directory.test.ts b/apps/desktop/electron/packaged-working-directory.test.ts
new file mode 100644
index 00000000000..7a36c0e6980
--- /dev/null
+++ b/apps/desktop/electron/packaged-working-directory.test.ts
@@ -0,0 +1,80 @@
+import assert from 'node:assert/strict'
+import { spawn } from 'node:child_process'
+import { once } from 'node:events'
+import { mkdir, mkdtemp, rename, rm } from 'node:fs/promises'
+import os from 'node:os'
+import path from 'node:path'
+import { fileURLToPath } from 'node:url'
+
+import { build } from 'esbuild'
+import { test } from 'vitest'
+
+// Exercise the real entry point before main loads. The fixture models a resident
+// helper outside the app tree; only its inherited CWD can block the directory swap.
+test.skipIf(process.platform !== 'win32')(
+ 'packaged startup releases the install CWD before main can pass it to resident helpers',
+ async () => {
+ const root = await mkdtemp(path.join(os.tmpdir(), 'hermes-entry-cwd-'))
+ const install = path.join(root, 'win-unpacked')
+ const workspace = path.join(root, 'workspace')
+ await mkdir(install)
+ await mkdir(workspace)
+
+ try {
+ for (const [packaged, cwd, expected] of [
+ [false, install, install],
+ [true, install, os.homedir()],
+ [true, workspace, workspace]
+ ] as const) {
+ const fixture = path.join(root, 'entry.mjs')
+ await build({
+ entryPoints: [fileURLToPath(new URL('./entry.ts', import.meta.url))],
+ outfile: fixture,
+ bundle: true,
+ platform: 'node',
+ format: 'esm',
+ define: {
+ 'process.execPath': JSON.stringify(path.join(install, 'Hermes.exe')),
+ 'process.env.HERMES_DESKTOP_IS_PACKAGED': 'undefined'
+ },
+ plugins: [
+ {
+ name: 'entry-cwd-fixture',
+ setup(builder) {
+ builder.onResolve({ filter: /^electron$/ }, () => ({ path: 'electron', namespace: 'fixture' }))
+ builder.onResolve({ filter: /^\.\/main$/ }, () => ({ path: 'main', namespace: 'fixture' }))
+ builder.onLoad({ filter: /.*/, namespace: 'fixture' }, ({ path: name }) => ({
+ contents:
+ name === 'electron'
+ ? `export const app = { isPackaged: ${packaged} }`
+ : `console.log(process.cwd()); setInterval(() => {}, 1000)`
+ }))
+ }
+ }
+ ]
+ })
+ const child = spawn(process.execPath, [fixture], { cwd, stdio: ['ignore', 'pipe', 'pipe'] })
+ const exited = once(child, 'exit')
+
+ try {
+ const [output] = await once(child.stdout!, 'data')
+ assert.equal(String(output).trim().toLowerCase(), expected.toLowerCase())
+ const moved = `${install}.previous`
+
+ if (!packaged) {
+ // Reproduce the original Windows sharing violation on an isolated tree.
+ await assert.rejects(rename(install, moved), { code: 'EBUSY' })
+ } else {
+ await rename(install, moved)
+ await rename(moved, install)
+ }
+ } finally {
+ child.kill()
+ await exited
+ }
+ }
+ } finally {
+ await rm(root, { recursive: true, force: true })
+ }
+ }
+)
diff --git a/apps/desktop/electron/packaged-working-directory.ts b/apps/desktop/electron/packaged-working-directory.ts
new file mode 100644
index 00000000000..6f8f5a6640d
--- /dev/null
+++ b/apps/desktop/electron/packaged-working-directory.ts
@@ -0,0 +1,18 @@
+import os from 'node:os'
+import path from 'node:path'
+
+/** Keep resident children from inheriting a CWD that pins the replaceable app tree. */
+export function releasePackagedWorkingDirectory(isPackaged: boolean): void {
+ if (process.platform !== 'win32' || !isPackaged) {
+ return
+ }
+
+ const relative = path.relative(path.dirname(process.execPath), process.cwd())
+
+ // Preserve an explicitly supplied workspace outside the install directory.
+ if (relative === '..' || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) {
+ return
+ }
+
+ process.chdir(os.homedir())
+}