Skip to content

The agent that grows with you

fix(desktop): exec get-windows helper from app.asar.unpacked so read_window_below works on packaged macOS builds

Douglas Jennewein763489a8b4f1
diff --git a/apps/desktop/electron/window-below.test.ts b/apps/desktop/electron/window-below.test.ts
index d1e2b684ebc..21bfde665ec 100644
--- a/apps/desktop/electron/window-below.test.ts
+++ b/apps/desktop/electron/window-below.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'
-import { type EnumeratedWindow, enumerationFailureNote, pickWindowBelow } from './window-below'
+import { type EnumeratedWindow, enumerationFailureNote, pickWindowBelow, resolveOutsideAsar } from './window-below'
const win = (pid: number, x = 0, y = 0, width = 800, height = 600, app = `app-${pid}`): EnumeratedWindow => ({
app,
@@ -145,3 +145,28 @@ describe('enumerationFailureNote', () => {
expect(note).not.toMatch(/ENOENT/)
})
})
+
+describe('resolveOutsideAsar', () => {
+ // The helper binary get-windows execs cannot run from inside the archive
+ // (execFile on a path through app.asar fails ENOTDIR), so the import must
+ // land on the unpacked copy electron-builder ships beside it.
+ it('redirects a packaged specifier into app.asar.unpacked', () => {
+ expect(
+ resolveOutsideAsar('file:///Applications/Hermes.app/Contents/Resources/app.asar/dist/node_modules/get-windows/index.js')
+ ).toBe('file:///Applications/Hermes.app/Contents/Resources/app.asar.unpacked/dist/node_modules/get-windows/index.js')
+ })
+
+ it('leaves a dev-tree specifier alone', () => {
+ const dev = 'file:///Users/dev/hermes-agent/node_modules/get-windows/index.js'
+
+ expect(resolveOutsideAsar(dev)).toBe(dev)
+ })
+
+ // Only the exact archive segment counts — a directory that merely starts
+ // with the name must not be rewritten.
+ it('requires app.asar to be a complete path segment', () => {
+ const lookalike = 'file:///opt/app.asar-tools/node_modules/get-windows/index.js'
+
+ expect(resolveOutsideAsar(lookalike)).toBe(lookalike)
+ })
+})
diff --git a/apps/desktop/electron/window-below.ts b/apps/desktop/electron/window-below.ts
index 3faa078d562..9ccce04baf8 100644
--- a/apps/desktop/electron/window-below.ts
+++ b/apps/desktop/electron/window-below.ts
@@ -148,6 +148,15 @@ export const enumerationFailed = <T>(result: EnumerationFailure | T): result is
const describeError = (error: unknown): string =>
error instanceof Error ? error.message : String(error ?? 'unknown error')
+/**
+ * Redirect a resolved module specifier out of `app.asar` so its files exist on
+ * the real filesystem. `/app.asar/` only ever appears as a complete path
+ * segment in a packaged build (electron-builder names the archive exactly
+ * that), so in dev — where nothing is archived — this is a no-op.
+ */
+export const resolveOutsideAsar = (specifier: string): string =>
+ specifier.replace('/app.asar/', '/app.asar.unpacked/')
+
let getWindowsModule: Promise<GetWindowsModule | EnumerationFailure> | null = null
const loadGetWindows = (): Promise<GetWindowsModule | EnumerationFailure> => {
@@ -169,7 +178,18 @@ const loadGetWindows = (): Promise<GetWindowsModule | EnumerationFailure> => {
// scripts/stage-native-deps.mjs writes a staged lib/windows.js that requires
// the binding directly, so it is the more reliable of the two everywhere.
getWindowsModule ??= (async () => {
- const staged = path.join(app.getAppPath(), 'dist', 'node_modules', 'get-windows', 'index.js')
+ // Both specifiers are redirected out of app.asar in a packaged build.
+ // get-windows does its real work by exec'ing a helper binary whose path it
+ // derives from its own module URL, so a module imported through an
+ // `/app.asar/` path derives an in-archive helper path and the spawn fails
+ // ENOTDIR: the kernel sees app.asar as a file. Electron rewrites asar
+ // paths for child_process only once that module has been pulled through
+ // the CJS loader, which this ESM main process never does (every import
+ // here is `from 'node:child_process'`). Pointing the import at the
+ // unpacked copy gives get-windows a real path to derive from.
+ const staged = resolveOutsideAsar(
+ path.join(app.getAppPath(), 'dist', 'node_modules', 'get-windows', 'index.js')
+ )
let stagedError = 'not staged in this build'
if (fs.existsSync(staged)) {
@@ -181,7 +201,7 @@ const loadGetWindows = (): Promise<GetWindowsModule | EnumerationFailure> => {
}
try {
- return (await import('get-windows')) as GetWindowsModule
+ return (await import(resolveOutsideAsar(import.meta.resolve('get-windows')))) as GetWindowsModule
} catch (error) {
return {
reason: