Files
hermes-agent/apps/desktop/electron/renderer-bundle.ts
T
brooklyn! 529ee80ac0 Recover from a half-replaced desktop bundle instead of requiring a reinstall (#85887)
* fix(desktop): load the intact renderer bundle when an update tears one copy

index.html and the hashed chunks it names are one generation. A packaged app
ships that bundle twice (inside app.asar and, via asarUnpack, beside it in
app.asar.unpacked), so an update that replaces the app while its files are
locked can leave the two copies from different generations. resolveRendererIndex
took the first index.html that existed, so it could pick the torn one and the
window died on its first lazy import with "Failed to fetch dynamically imported
module" -- with no way out, because every relaunch reloaded the same copy.

Check each candidate's declared modules and prefer a complete generation; when
both are torn, log which files are missing and how to repair instead of leaving
the crash unexplained.

* fix(cli): rebuild the desktop app when its renderer bundle is half-replaced

The content stamp hashes the SOURCE tree, which an interrupted update leaves
intact, so `hermes desktop` reported "up to date" and skipped the rebuild that
would repair a torn bundle -- the app relaunched into the same crash and
reinstalling looked like the only option.

Treat a bundle whose index.html names missing chunks as stale regardless of the
stamp, and say so on the way into the rebuild.
2026-08-14 02:21:20 -05:00

72 lines
2.7 KiB
TypeScript

/**
* Renderer bundle generation check.
*
* `index.html` and the hashed chunks under `dist/assets/` are ONE generation:
* every `lazy()` route resolves to a filename baked into that generation's
* module graph. A self-update that replaces the package while its files are
* locked (antivirus, a still-running instance, an interrupted Windows replace)
* can leave the two copies electron-builder ships — inside `app.asar` and,
* because `asarUnpack` lists `dist/**`, beside it in `app.asar.unpacked` —
* from DIFFERENT generations. The window then loads an `index.html` whose
* chunks are gone and dies on the first lazy import:
*
* Failed to fetch dynamically imported module:
* …/app.asar/dist/assets/shiki-block-COiz1pEN.js
*
* The app looks permanently broken (every relaunch reloads the same torn copy),
* yet the OTHER copy is usually intact. This makes that checkable, so the
* loader can prefer a complete generation and only report a repair when both
* are torn.
*
* Pure + injectable so it is testable without booting Electron. `fs` here is
* Electron's asar-aware fs: paths inside `app.asar` read like real files.
*/
import fs from 'node:fs'
import path from 'node:path'
// The modules the browser fetches before any app code runs: Vite emits them as
// `<script type="module" src>` plus `<link rel="modulepreload" href>`.
const TAG_WITH_URL = /<(?:script|link)\b[^>]*\b(?:src|href)=["']([^"']+)["'][^>]*>/gi
const MODULE_TAG = /\btype=["']module["']|\brel=["']modulepreload["']/i
export function parseModuleAssetRefs(html: string): string[] {
const refs: string[] = []
for (const [tag, href] of String(html ?? '').matchAll(TAG_WITH_URL)) {
// Absolute/CDN URLs aren't part of this bundle's generation.
if (MODULE_TAG.test(tag) && !/^[a-z]+:|^\/\//i.test(href)) {
refs.push(href.replace(/^\.\//, '').split(/[?#]/)[0])
}
}
return refs
}
export interface RendererBundleDeps {
readFileSync?: (file: string, encoding: 'utf8') => string
existsSync?: (file: string) => boolean
}
/**
* The module files `indexPath` declares but that do not exist beside it.
*
* Empty ⇒ a complete generation (or an index naming nothing checkable — the
* caller's own existence gate owns unreadable/missing files). Non-empty ⇒ torn:
* loading it produces the "Failed to fetch dynamically imported module" crash.
*/
export function missingRendererAssets(indexPath: string, deps: RendererBundleDeps = {}): string[] {
const { readFileSync = fs.readFileSync, existsSync = fs.existsSync } = deps
const dir = path.dirname(indexPath)
let html: string
try {
html = readFileSync(indexPath, 'utf8')
} catch {
return []
}
return parseModuleAssetRefs(html).filter(ref => !existsSync(path.join(dir, ref)))
}