Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App entrypoint fails to load for ESM app if arm64 and x64 apps don't match #90

Open
bendemboski opened this issue Feb 1, 2024 · 1 comment · May be fixed by #101
Open

App entrypoint fails to load for ESM app if arm64 and x64 apps don't match #90

bendemboski opened this issue Feb 1, 2024 · 1 comment · May be fixed by #101
Labels
bug Something isn't working

Comments

@bendemboski
Copy link

If have an app whose main process is implemented as ES modules, and when I add a native dependency and don't merge the ASARs, the app fails to run with:

Uncaught Exception:
Error [ERR_REQUIRE_ESM]: require() of ES Module /private/var/folders/x5/lg2888612qx6k46g8t0b3llm0000gn/T/AppTranslocation/012E8191-C14B-424C-A98A-B6E43D98B159/d/Rowan Patents.app/Contents/Resources/app-arm64.asar/src/index.mjs not supported.
Instead change the require of /private/var/folders/x5/lg2888612qx6k46g8t0b3llm0000gn/T/AppTranslocation/012E8191-C14B-424C-A98A-B6E43D98B159/d/Rowan Patents.app/Contents/Resources/app-arm64.asar/src/index.mjs to a dynamic import() which is available in all CommonJS modules.
at l._load (node:electron/js2c/asar_bundle:2:13642)
at Object.<anonymous> (/private/var/folders/x5/lg2888612qx6k46g8t0b3llm0000gn/T/AppTranslocation/012E8191-C14B-424C-A98A-B6E43D98B159/d/Rowan Patents.app/Contents/Resources/app.asar/index.js:27:1)
at l._load (node:electron/js2c/asar_bundle:2:13642)
at node:electron/js2c/browser_init:2:120247
at node:electron/js2c/browser_init:2:120456
at node:electron/js2c/browser_init:2:120460
at l._load (node:electron/js2c/asar_bundle:2:13642)

I have worked around it using patch-package and replacing has-asar.js with a has-asar.mjs that looks like

import { app } from 'electron';
import path from 'path';

if (process.arch === 'arm64') {
  setPaths('arm64');
} else {
  setPaths('x64');
}

function setPaths(platform) {
  // This should return the full path, ending in something like
  // Notion.app/Contents/Resources/app.asar
  const appPath = app.getAppPath();
  const asarFile = `app-${platform}.asar`;

  // Maybe we'll handle this in Electron one day
  if (path.basename(appPath) === 'app.asar') {
    const platformAppPath = path.join(path.dirname(appPath), asarFile);

    // This is an undocumented API. It exists.
    app.setAppPath(platformAppPath);
  }

  process._archPath = `../${asarFile}`;
}

await import(`${process._archPath}/src/index.mjs`);

However, I'm not sure how to turn this into a general purpose solution because:

  1. I hard-coded the path to my application's entry point (src/index.mjs) and I'm not sure a good way to determine it dynamically.
  2. I'm not sure what process._archPath should actually be in this case?
@erickzhao erickzhao added the bug Something isn't working label Jun 12, 2024
@erickzhao erickzhao changed the title Does not work with ESM apps App entrypoint fails to load for ESM app if arm64 and x64 apps don't match Jun 13, 2024
@erickzhao
Copy link
Member

Hey @bendemboski, I was taking a look at this today and it seems like the repro case happens whenever the x64 and arm64 ASARs diverge.

Converting the asar.js shim into its ESM equivalent indeed seems to fix the problem on my end.

Don't have time to put up a PR today but this seems to work according to my local testing.

// has-asar.mts
import { app } from 'electron';
import { createRequire } from 'node:module';
import path from 'node:path';

if (process.arch === 'arm64') {
  await setPaths('arm64');
} else {
  await setPaths('x64');
}

async function setPaths(platform: string) {
  // This should return the full path, ending in something like
  // Notion.app/Contents/Resources/app.asar
  const appPath = app.getAppPath();
  const asarFile = `app-${platform}.asar`;

  // Maybe we'll handle this in Electron one day
  if (path.basename(appPath) === 'app.asar') {
    const platformAppPath = path.join(path.dirname(appPath), asarFile);

    // This is an undocumented API. It exists.
    app.setAppPath(platformAppPath);
  }

  const require = createRequire(import.meta.url);
  process._archPath = require.resolve(`../${asarFile}`);
  await import(process._archPath);
}

@erickzhao erickzhao linked a pull request Jun 13, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants