Skip to content

fix(desktop): emit preload as .cjs so it loads under --no-sandbox - #47691

Merged
Hona merged 1 commit into
anomalyco:v2from
Hona:desktop-preload-cjs
Sep 6, 2026
Merged

fix(desktop): emit preload as .cjs so it loads under --no-sandbox#47691
Hona merged 1 commit into
anomalyco:v2from
Hona:desktop-preload-cjs

Conversation

@Hona

@Hona Hona commented Sep 6, 2026

Copy link
Copy Markdown
Member
  • packages/desktop/package.json is "type": "module", but the preload is built as CommonJS and was emitted as out/preload/index.js.

  • With the sandbox on (our default) Electron evaluates the preload as a plain script, so the extension never mattered.

  • With --no-sandbox, Electron loads non-.mjs preloads through Node's CJS loader (lib/renderer/init.tsModule._load). Node sees .js + "type": "module", treats it as ESM, and the preload dies on its first line:

    ReferenceError: require is not defined in ES module scope, you can use import instead
    

    contextBridge never runs, the renderer never signals ready, ready-to-show never fires, and the window stays hidden.

  • .cjs forces CommonJS regardless of the package type, so the same file works in both modes. This is also electron-vite's own default for a CJS preload in an ESM package; the .js override dates from refactor(desktop-electron): enable contextIsolation and sandbox #23523.

flowchart LR
  A[BrowserWindow preload] --> B{--no-sandbox?}
  B -- no --> C[Sandboxed renderer<br/>plain script + polyfilled require]
  B -- yes --> D[Module._load]
  D --> E{index.js<br/>type: module}
  D --> F[index.cjs]
  E -- ESM --> G[ReferenceError: require<br/>window never shown]
  F -- CJS --> H[contextBridge exposed]
  C --> H
Loading

Reproduced with Electron 42's Node (ELECTRON_RUN_AS_NODE=1) requiring the built preload from a CJS parent:

File Result
out/preload/index.js (before) require is not defined in ES module scope
out/preload/index.cjs (after) loads as CJS

Reported in #46691 (the crash itself is the install-dir ACL fixed in #46696; this only removes the hidden-window failure when users fall back to --no-sandbox). No change to the default sandboxed path.

@Hona
Hona requested a review from Brendonovich as a code owner September 6, 2026 23:02
Copilot AI lite review requested due to automatic review settings September 6, 2026 23:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@Hona
Hona enabled auto-merge (squash) September 6, 2026 23:04
@Hona
Hona disabled auto-merge September 6, 2026 23:14
@Hona
Hona merged commit 873d437 into anomalyco:v2 Sep 6, 2026
9 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants