Skip to content

The agent that grows with you

test(desktop): pin that renderer and icon probes never stat inside app.asar

brooklyn!dcf57280889b
diff --git a/apps/desktop/electron/app-icon.test.ts b/apps/desktop/electron/app-icon.test.ts
index ffe0a81b439..3dde0499492 100644
--- a/apps/desktop/electron/app-icon.test.ts
+++ b/apps/desktop/electron/app-icon.test.ts
@@ -54,23 +54,44 @@ test('decodingFileProbe rejects a directory', () => {
// #96857: on a packaged build every path inside app.asar is stat'ed through Electron's asar
// fs.Stats shim, which emits Node's DEP0180 ("fs.Stats constructor is deprecated") on every launch.
-// dist/** is asarUnpack'ed, so the real on-disk copy must be tried before any in-archive rung;
-// resolveAppIcon stops at the first decodable candidate and never reaches the archive.
-test('the unpacked on-disk icon precedes every in-archive PNG rung', () => {
+// A packaged tree always has real on-disk icons (dist/** is asarUnpack'ed; extraResources copies
+// icon.ico to resources/), so resolving the ladder must never probe inside the archive, even though
+// the packed copies exist too.
+test('resolving a packaged icon never probes a path inside app.asar', () => {
+ const resources = path.join('/opt', 'Hermes', 'resources')
+ const appRoot = path.join(resources, 'app.asar')
+ const unpackedPathFor = (p: string) => p.replace(/app\.asar(?=$|[\\/])/, 'app.asar.unpacked')
+ const unpackedIcon = path.join(unpackedPathFor(appRoot), 'dist', 'apple-touch-icon.png')
+
+ const resourcesIco = path.join(resources, 'icon.ico')
+
+ // package.json build: files (packed) + asarUnpack dist/** + extraResources icon.ico.
+ const shipped = new Set([
+ resourcesIco,
+ path.join(appRoot, 'assets', 'icon.ico'),
+ path.join(appRoot, 'public', 'apple-touch-icon.png'),
+ path.join(appRoot, 'dist', 'apple-touch-icon.png'),
+ unpackedIcon
+ ])
+
for (const isWindows of [false, true]) {
- const appRoot = path.join('/opt/Hermes/resources', 'app.asar')
-
- const candidates = appIconCandidates({
- isWindows,
- appRoot,
- resourcesPath: '/opt/Hermes/resources',
- unpackedPathFor: p => p.replace(/app\.asar(?=$|[\\/])/, 'app.asar.unpacked')
- })
-
- const pngs = candidates.filter(c => c.endsWith('.png'))
- const firstInArchive = pngs.findIndex(c => c.startsWith(appRoot + path.sep))
- const unpacked = pngs.findIndex(c => c.includes('app.asar.unpacked'))
- assert.ok(unpacked !== -1 && unpacked < firstInArchive, `unpacked first (isWindows=${isWindows}): ${pngs.join(', ')}`)
+ const probed: string[] = []
+
+ const picked = resolveAppIcon(
+ appIconCandidates({ isWindows, appRoot, resourcesPath: resources, unpackedPathFor }),
+ p => {
+ probed.push(p)
+
+ return shipped.has(p)
+ }
+ )
+
+ assert.equal(picked, isWindows ? resourcesIco : unpackedIcon)
+ assert.deepEqual(
+ probed.filter(p => p.startsWith(appRoot + path.sep)),
+ [],
+ `isWindows=${isWindows}`
+ )
}
})
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 412a0550037..f1cafcf45bb 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -405,7 +405,7 @@ import {
oauthLoginLoadUrlOptions,
resolveRemoteRequestHeaders
} from './remote-ws-headers'
-import { missingRendererAssets } from './renderer-bundle'
+import { missingRendererAssets, presentRendererIndexes } from './renderer-bundle'
import { planLaunchSwitches, readDesktopLaunchConfig } from './renderer-heap-flags'
import { loadRendererLoadErrorPage } from './renderer-load-error-page'
import { attachRendererConsoleCapture, formatRendererBoundaryReport } from './renderer-log'
@@ -5039,14 +5039,7 @@ function resolveRendererIndexWithMissing(): { index: string; missing: string[] }
// unpackedPathFor is a no-op outside an asar, so both candidates collapse
// to APP_ROOT/dist and the original order is preserved.
const candidates = IS_PACKAGED ? [webDistIndex, asarIndex] : [asarIndex, webDistIndex]
- const present = [...new Set(candidates)].filter(candidate => {
- try {
- fs.readFileSync(candidate, 'utf8')
- return true
- } catch {
- return false
- }
- })
+ const present = presentRendererIndexes(candidates)
// index.html and the hashed chunks it names are one generation. An update
// that replaces only one of the two shipped copies (app.asar vs
diff --git a/apps/desktop/electron/renderer-bundle.test.ts b/apps/desktop/electron/renderer-bundle.test.ts
index b4c763a1b46..8443a7b5cff 100644
--- a/apps/desktop/electron/renderer-bundle.test.ts
+++ b/apps/desktop/electron/renderer-bundle.test.ts
@@ -7,6 +7,7 @@ import {
missingRendererAssets,
parseLazyChunkRefs,
parseModuleAssetRefs,
+ presentRendererIndexes,
type RendererBundleDeps
} from './renderer-bundle'
@@ -124,6 +125,59 @@ test('missingRendererAssets: an unreadable index is not treated as torn', () =>
assert.deepEqual(missingRendererAssets(INDEX_PATH, deps), [])
})
+// #96857: a stat on a path inside app.asar goes through Electron's asar shim, which constructs the
+// deprecated fs.Stats and prints DEP0180 on every packaged launch. Choosing a renderer copy must
+// answer from reads and existence checks alone, for the in-archive copy as much as the unpacked one.
+test('renderer index probes never stat, so app.asar paths never construct fs.Stats', () => {
+ const resources = path.join('/opt', 'Hermes', 'resources')
+ const copies = ['app.asar.unpacked', 'app.asar'].map(root => path.join(resources, root, 'dist'))
+ const files = new Map<string, string>()
+
+ for (const dir of copies) {
+ files.set(path.join(dir, 'index.html'), INDEX_HTML)
+
+ for (const chunk of ['assets/index-a1b2c3.js', 'assets/shiki-block-COiz1pEN.js']) {
+ files.set(path.join(dir, chunk), '')
+ }
+ }
+
+ const statted: string[] = []
+
+ const stat = (file: string) => {
+ statted.push(file)
+ throw new Error('DEP0180: fs.Stats constructor is deprecated')
+ }
+
+ const fsStub = {
+ readFileSync: (file: string) => {
+ const body = files.get(file)
+
+ if (body === undefined) {
+ throw Object.assign(new Error(`ENOENT: ${file}`), { code: 'ENOENT' })
+ }
+
+ return body
+ },
+ existsSync: (file: string) => files.has(file),
+ statSync: stat,
+ lstatSync: stat
+ }
+
+ const [unpackedIndex, asarIndex] = copies.map(dir => path.join(dir, 'index.html'))
+
+ const present = presentRendererIndexes(
+ [unpackedIndex, asarIndex, unpackedIndex, path.join('/nope', 'index.html')],
+ fsStub
+ )
+
+ assert.deepEqual(present, [unpackedIndex, asarIndex])
+ assert.deepEqual(
+ present.map(index => missingRendererAssets(index, fsStub)),
+ [[], []]
+ )
+ assert.deepEqual(statted, [])
+})
+
// ---------------------------------------------------------------------------
// Lazy-chunk (__vite__mapDeps) awareness — #93479. index.html only names the
// boot-critical modules; the chunks behind React.lazy() routes (syntax-diff-*,
diff --git a/apps/desktop/electron/renderer-bundle.ts b/apps/desktop/electron/renderer-bundle.ts
index f2ae4af46c8..275cdfe80fd 100644
--- a/apps/desktop/electron/renderer-bundle.ts
+++ b/apps/desktop/electron/renderer-bundle.ts
@@ -74,6 +74,24 @@ export interface RendererBundleDeps {
existsSync?: (file: string) => boolean
}
+/** The readable `index.html` candidates, deduped, in order. Probed by reading, never by stat:
+ * statting a path inside app.asar goes through Electron's asar shim, which constructs the
+ * deprecated fs.Stats and prints DEP0180 on every packaged launch (#96857). */
+export function presentRendererIndexes(
+ candidates: readonly string[],
+ { readFileSync = fs.readFileSync }: RendererBundleDeps = {}
+): string[] {
+ return [...new Set(candidates)].filter(candidate => {
+ try {
+ readFileSync(candidate, 'utf8')
+
+ return true
+ } catch {
+ return false
+ }
+ })
+}
+
/** Vite's build-time graph avoids opening megabytes of lazy chunks at startup.
* Only trust a manifest paired with this index's entry and preload generation.
* Old builds, malformed manifests and interrupted replacements retain the