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

fix(profiling-node): Add binary path to support @electron/rebuild #15112

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

timfish
Copy link
Collaborator

@timfish timfish commented Jan 21, 2025

To work with Electron, native modules need to be rebuilt with Electrons modified Node.js headers. This can be achieved using @electron/rebuild but this results in the binary being outputted into the standard node-gyp output directory.

This PR ensures that @electron/rebuild output path is attempted first.

@timfish timfish requested a review from JonasBa January 21, 2025 15:39
@@ -54,6 +54,13 @@ export function importCppBindingsModule(): PrivateV8CpuProfilerBindings {
return createdRequire(`${binaryPath}.node`);
}

// @electron/rebuild generates the binary in the generic node-gyp directory
try {
return createdRequire('../../build/Release/sentry_cpu_profiler.node');
Copy link
Member

Choose a reason for hiding this comment

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

Does it actually place it here? I though it moves it into pkg/bin/$platform-$abi/smth.node?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It actually outputs binaries in both locations!

On macOS I get a binaries at:

  • ./bin/darwin-arm64-132/profiling-node.node
  • ./build/Release/sentry_cpu_profiler.node

The binaries are identical and work in Electron and the build/Release path doesn't vary by platform/arch so I chose that one 🤷‍♂️

The @electron/rebuild tests at least point to similar paths:
https://github.com/electron/rebuild/blob/8e8e6f94f8bac41044cf30b7898bdcaa04df0a2f/test/rebuild.ts#L150

Copy link
Member

Choose a reason for hiding this comment

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

Ahh, that is good to know! I dont have a clean cmd so I usually always have some version of the binary that is built via postinstall or something in the build/release repo.

Copy link
Member

Choose a reason for hiding this comment

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

I think this condition that you added here will mean that we always prefer this freshly built binary, which may have been built outside of just electron/rebuild, like for example someone running node-gyp directly. In any case, I think this is fine.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah yes. Maybe we do a check for Electron first?

Checking for process.versions.electron is probably enough...

Copy link
Member

Choose a reason for hiding this comment

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

I think we should, yes.

We should check to ensure that we default ship this build/release binding or that one is always generated as part of postintall, as it will otherwise break setups that bundle .node files as the path is statically analyzable, but the file wont actually exist which will result in an error

Copy link
Collaborator Author

@timfish timfish Jan 21, 2025

Choose a reason for hiding this comment

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

or that one is always generated as part of postintall

A binary isn't always generated ☹️

otherwise break setups that bundle .node files as the path is statically analyzable, but the file wont actually exist

Mmmm... not sure what to do about that. Can we rely on the install script running? I guess not always!

Maybe we could already have a zero byte file at ./build/Release/sentry_cpu_profiler.node so that bundlers don't complain about it missing but it gets overwritten.

Then if you're running in Electron and @electron/rebuild hasn't run, the loading with throw and we catch it and explain in the debug message that you need to run @electron/rebuild?

@timfish
Copy link
Collaborator Author

timfish commented Jan 22, 2025

@JonasBa are there any tests for the bundler issues/warnings? If not I might add those now!

@AbhiPrasad
Copy link
Member

We need to backport this as well right?

@timfish
Copy link
Collaborator Author

timfish commented Jan 22, 2025

We need to backport this as well right?

Possibly, depends on how long it takes to get it ready to merge 🤣

I'd want to add some bundler tests to ensure this doesn't regress any bundler issues.

@timfish timfish marked this pull request as draft January 22, 2025 14:33
@timfish
Copy link
Collaborator Author

timfish commented Jan 22, 2025

I'm not convinced the existing bundler tests actually cover all potential bundling issues.

esbuild does not support resolving createRequire but webpack does!

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.

3 participants