electron / universal

Create Universal macOS applications from two x64 and arm64 Electron applications
MIT License
112 stars 43 forks source link

Resources Not Found when Using Native Dependencies #35

Closed jakobvogel closed 2 years ago

jakobvogel commented 2 years ago

Hello! First of all, many thanks for your work on Electron and its many sub-projects. We appreciate this a lot.

Disclaimer: I am aware that earlier issues relating to different asar files have been discussed before, but I did not find this particular case (or a solution) and decided to open a new issue. (Particularly pull-request #34 seems to tackle a very similar – if not the same – problem.)

TL;DR

When the universal app ends up with a "switch" app.asar and two architecture-specific files app-arm64.asar and app-x64.asar, the app will attempt to load files (for instance during window.loadFile('index.html') with window a BrowserWindow) from app.asar rather than the architecture-specific file, leading to a loading failure.

For a minimal example based on the quick-start example and using electron-builder, see: https://github.com/jakobvogel/universal-electron-test

Screenshot 2021-12-22 at 20 40 20

Background

In our app, we use keytar for password storage. Its prebuilt binaries are architecture specific. They end up in the app.asar files, causing the SHAs to differ while everything else contained in the two files is the same. Therefore, the universal build includes the architecture-specific switch, and the universal app is able to load, including the keytar dependency. However, when attempting to load a local HTML file for rendering the app, the app attempts to read from the (virtually empty) switch app.asar while the data is contained app-arm64.asar and app-x64.asar. The two original architecture-specific apps both work.

I am wondering whether there is a simple solution to this.

The only other alternative would be to build a universal keytar.node (hopefully) causing the two asar files to be equal again, correct? (Assuming that node files can be universal after all, and that the build system could be forced to use a universal build while creating the two original architecture-specific apps.)

Thanks again for your input.

MarshallOfSound commented 2 years ago

The fact it's loading your HTML file from app.asar implies a manual path resolution issue somewhere in your code or electron-builders code. loadFile does relative path resolution and shouldn't search app.asar automatically.

You could try working around this by doing path resolution yourself with path.resolve(__dirname, 'index.html')

jakobvogel commented 2 years ago

Many thanks for your comment @MarshallOfSound — manual path resolution via __dirname really fixes the issue. (Yeah! 😊) Your help is greatly appreciated! 🙇‍♂️