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

After packaging Electron app with Realm we get error from Realm library #6303

Closed
bimusiek opened this issue Dec 5, 2023 · 14 comments
Closed

Comments

@bimusiek
Copy link
Contributor

bimusiek commented Dec 5, 2023

How frequently does the bug occur?

Always

Description

While in development mode (Electron with devserver), everything works as expected.
However, after the app is packed with electron-builder though, we get an error:

Uncaught TypeError: k.injectInjectables is not a function
    at 4875 (main.c4a42f82.js:formatted:2)
    at n (main.c4a42f82.js:formatted:2)
    at 713 (main.c4a42f82.js:formatted:2)
    at n (main.c4a42f82.js:formatted:2)
    at main.c4a42f82.js:formatted:2
    at main.c4a42f82.js:formatted:2
    at main.c4a42f82.js:formatted:2

CleanShot%202023-12-05%20at%2016 10 35@2x

Stacktrace & log output

CleanShot 2023-12-05 at 16 23 23@2x

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

12.3.1

What services are you using?

Atlas Device Sync

Are you using encryption?

No

Platform OS and version(s)

Electron 19.0.8

Build environment

Which debugger for React Native: ..

Cocoapods version

No response

@bimusiek
Copy link
Contributor Author

bimusiek commented Dec 5, 2023

Debuggin more, I have tried to add asarUnpack as in https://github.com/realm/realm-studio/blob/channel/major-14/package.json but it did not help.

I located k value:
CleanShot 2023-12-05 at 19 47 42@2x

but not sure why is it a string.

@bimusiek
Copy link
Contributor Author

bimusiek commented Dec 5, 2023

I see that Realm Studio is using Realm v11, so this could be an issue?

AsarUnpacked I have tried is :

./node_modules/realm/generated/ts/realm.node

@kraenhansen
Copy link
Member

kraenhansen commented Dec 5, 2023

@bimusiek Thanks for reporting back on your progress here.

The main reason we use asarUnpack of the realm.node binary is for perform code-signing (via the mac.binaries property). I wonder if / how you're bundling you app's JS code? Perhaps you're using a WebPack configuration that resolves the realm.node module differently in dev vs prod mode?

@bimusiek
Copy link
Contributor Author

bimusiek commented Dec 5, 2023

With v11, the error is:

Uncaught TypeError: Cannot read properties of undefined (reading 'indexOf')

when it tries to import realm.node. Not sure why... but at least I found a duplicate:
#4741

However, there was no soluton for this issue. I see that Realm Studio uses require as a workaround, instead of import Realm from 'realm'.

@bimusiek
Copy link
Contributor Author

bimusiek commented Dec 5, 2023

@kraenhansen We use craco with react-scripts.

For dev mode, we do: craco start & electron build/electron.js.
For building we first package the app with craco build then use electron-builder to package everything together.

And for both we use webpack-node-externals:

webpackConfig.externals = [
    nodeExternals({
        allowlist: [/^(?!.*(^realm)$)/i],
        additionalModuleDirs: [path.resolve(__dirname, '../../../node_modules')],
    }),
]

Could it be too old version of Electron though? I am working on it for past few hours trying different things and different combinations of versions and solutions.

@bimusiek
Copy link
Contributor Author

bimusiek commented Dec 5, 2023

Investigating further I found a way to reproduce it in dev server as well. Cray cray issue.

Basically, we are using pnpm for our monorepo with react native, electron and web source files.
Due to issue with electron-builder & pnpm, we had to make special build process where we copy all files, change npmrc to enable hoisting option and then build everything.

node-linker=hoisted
public-hoist-pattern=*

If I run dev server inside this hoisted node-linker option, I get the same error.
So looks like, we have to find another workaround for pnpm or switch back to yarn/npm.

I will close the issue as this is related to PNPM hoisting issue, not Realm.

@bimusiek bimusiek closed this as completed Dec 5, 2023
@bimusiek
Copy link
Contributor Author

bimusiek commented Dec 6, 2023

Ok, just checked without node-linker=hoisted, then dev server works correctly. But then electron-builder packages app incorrectly :D

Basically, Realm + PNPM + Electron does not cooperate. 👎
Switching build process to yarn 😢

@kraenhansen
Copy link
Member

kraenhansen commented Dec 7, 2023

@bimusiek I agree - this is super unfortunate; there's a lot of libraries and tools which make assumptions on the relative paths of file instead of relying on the node module resolution algorithm.

Please let us know if there's anything we can do or document to make Realm + Electron a smoother experience.

@bimusiek
Copy link
Contributor Author

bimusiek commented Dec 7, 2023

@kraenhansen The example is a great step, I would add there electron-builder at least to prove it builds properly on all platforms. Then it would be amazing boilerplate for new devs starting with Realm and Electron.

Maybe you have a tip or an idea what could be wrong with Windows build?
We have been doing last tests before release and unfortunately on Windows we get an error:

not a valid Win32 application

We are building on MacOS with Parallels installed, so I hoped it could pick proper file and it did in v11. Trying now to reproduce it locally and then downgrade to v11 to see if that is of some help.

@kraenhansen
Copy link
Member

Are you still applying this workaround? #3828 (comment)

@bimusiek
Copy link
Contributor Author

bimusiek commented Dec 7, 2023

@kraenhansen If we will ever have a chance of meeting in real life, I owe you a beer! 👍 I have removed this workaround while debugging previous issue and it got lost in the set of changes.

Thank you, not sure why I did not find it...

@kraenhansen
Copy link
Member

kraenhansen commented Dec 7, 2023

Great to hear! We should probably just get that file included in the package ourselves. It's just very strange that this is the way Electron builder detects prebuilds and I'd love to find a less arbitrary long term solution 🤔

@bimusiek
Copy link
Contributor Author

bimusiek commented Dec 7, 2023

Just a polish idiom for you, which translate roughly to: "Better is an enemy of good" 😄

Having just a workaround for anyone that tries Realm with Electron for the first time can be beneficial. Even if it is just a workaround. And then after, there is time to ask, how are we gonna do it right 🔎

@kraenhansen
Copy link
Member

You're right .. I merged the workaround 👍

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants