Repository navigation
Closed
Description
Preflight Checklist
- I have read the Contributing Guidelines for this project.
- I agree to follow the Code of Conduct that this project adheres to.
- I have searched the issue tracker for a bug report that matches the one I want to file, without success.
Electron Version
23.0.0
What operating system are you using?
Windows
Operating System Version
Windows 11, Version22H2, OS Build 22621.1265
What arch are you using?
x64
Last Known Working Electron version
22.3.1
Expected Behavior
proxyquire should load modules as it does with electron version 22.3.1.
Actual Behavior
Attempt to use proxyquire in tests with electron of version 23 an higher leads to the following error message:
TypeError: Script methods can only be called on script instances.
at Script.runInThisContext (node:vm:129:12)
at Object.runInThisContext (node:vm:313:38)
at wrapSafe (node:internal/modules/cjs/loader:1083:15)
at Module._compile (node:internal/modules/cjs/loader:1130:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1229:10)
at require.extensions.<computed> (C:\work\proxyquire-test\node_modules\proxyquire\lib\proxyquire.js:311:43)
at Module.load (node:internal/modules/cjs/loader:1044:32)
at Module._load (node:internal/modules/cjs/loader:885:12)
at f._load (node:electron/js2c/asar_bundle:2:13330)
at o._load (node:electron/js2c/renderer_init:2:3109)
at Module.require (node:internal/modules/cjs/loader:1068:19)
at Proxyquire._withoutCache (C:\work\proxyquire-test\node_modules\proxyquire\lib\proxyquire.js:222:12)
at Proxyquire.load (C:\work\proxyquire-test\node_modules\proxyquire\lib\proxyquire.js:129:15)
at Context.<anonymous> (C:\work\proxyquire-test\test.spec.js:7:5)
at callFn (C:\work\proxyquire-test\node_modules\mocha\lib\runnable.js:366:21)
at Runnable.run (C:\work\proxyquire-test\node_modules\mocha\lib\runnable.js:354:5)
at Runner.runTest (C:\work\proxyquire-test\node_modules\mocha\lib\runner.js:678:10)
at C:\work\proxyquire-test\node_modules\mocha\lib\runner.js:801:12
at next (C:\work\proxyquire-test\node_modules\mocha\lib\runner.js:593:14)
at C:\work\proxyquire-test\node_modules\mocha\lib\runner.js:603:7
at next (C:\work\proxyquire-test\node_modules\mocha\lib\runner.js:486:14)
at Immediate._onImmediate (C:\work\proxyquire-test\node_modules\mocha\lib\runner.js:571:5)
at process.processImmediate (node:internal/timers:471:21)
Testcase Gist URL
Additional Information
Workarounds:
- avoid calling window.open method completely
- separate electron-mocha runs to the one where window.open method is called and the one where proxyquire is used. If it happens in two different files, it is easy to do.
Details:
Issue is probably caused by node version upgrade in electron 23. But as it worked in electron 22, I consider it as a regression. I also could not reproduce this issue in non-electron environment.
Run testcase gist locally:
Run the following commands in the folder where all the files specified in the attached gist.
yarn
yarn start
Please upgrade/downgrade electron versions to see actual/expected results.
Reacted by Jason Axelson
Reacted by George Petrov and markleutloff
Activity
kyrylo-hrechykhin commentedon Feb 24, 2023
@zcbenz , @deepak1556 , could you please help analyzing this issue?
FYI: @miniak
zcbenz commentedon Feb 27, 2023
To simplify the issue, following code is enough to trigger the bug:
This bug is still mysterious to me though.
codebytere commentedon Feb 28, 2023
@kyrylo-hrechykhin @zcbenz Blink assumes that all
Contextscome with aScriptStateacross the board, which is what's ultimately causing this issue. It's why we initially deprecated vm in the renderer process in #26087.The stacktrace is as follows:
which confirms the above. Personally, I don't think there's really a good path forward here beyond to avoid tripping that faulty assumption in Blink (since patching whac-a-mole would not be a smart choice here), but i'm happy to be proven wrong!
gpetrov commentedon Mar 4, 2023
Getting the same error with just a simple require:
var Docker = require('dockerode')it is a major roadblock for us to upgrade to Electron 23+
codebytere commentedon Mar 5, 2023
@gpetrov
this restriction/deprecation has existed in Electron for nearly a dozen versions - were you not seeing this before with the same userland code? It'd help if you provided a reproducible sample for us to run.Edit: ah, i see the new aspect of this bug! I'll take a look this week - tracked it to 75d2caf
codebytere commentedon Mar 6, 2023
@gpetrov a repro from you with dockerode would still be helpful! Some preliminary research shows this seems to affect vm scripts run after
window.open('')specifically with no url.codebytere commentedon Mar 6, 2023
Got a preliminary fix up: #37506 cc @zcbenz
gpetrov commentedon Mar 6, 2023
@codebytere will do my best to isolate the case. We do use
window.opena lot but in this case the error happens just on the main window, somewhere after extensive js object creations and maybe some context switching. After that it happens in various modules not just dockerodegpetrov commentedon Mar 6, 2023
@codebytere definately window.open related. When I have that the next require fails directly.
Also got a crash in during the testing, not sure if it is related:
https://sentry.wappler.io/share/issue/be27b0431aa54a5c8f8f668e72776458/
gpetrov commentedon Apr 1, 2023
@codebytere still happens in 23.2.1 - any news about merging the fix?
gpetrov commentedon May 4, 2023
Jitsi problem is also related, we have similar iframe contexts that the error occur.
Will the solution also fix those iframe problems @codebytere
gpetrov commentedon May 11, 2023
what happen to the fix? @codebytere and @zcbenz - the problem still persist in the latest Electron 23, 24 and 25
gpetrov commentedon Jun 14, 2023
@codebytere - finally I was able to isolate our use case for this bug! It was when creating dynamically iframes in the global scope! and those have full url as src and not about:blank.
So this code causes also the error:
Is this the same problem?
codebytere commentedon Jun 14, 2023
@gpetrov do you have a more reproducible sample than that? given it doesn't run as-is i want to make sure i'm checking the right thing against the fix!
gpetrov commentedon Jun 14, 2023
@codebytere sure here is my full sample:
iframe_require_bug.zip
just extract, do npm install, run it, press the runTest button and check the devtools
codebytere commentedon Jun 14, 2023
@gpetrov great news:
Same bug - no error on my fix branch
gpetrov commentedon Jun 14, 2023
@codebytere that is awesome!
With the same sample on 25.1.0 i get:
So hope your fix gets merged quickly, as we need it desperately!
zcbenz commentedon Jun 23, 2023
This issue seems to be consist of 2 separate bugs:
vm.runInContextafterwindow.openwould throw an exception from Node;KochiyaOcean commentedon Aug 9, 2023
This issue still leaves unfixed since no release version contains the fix
gpetrov commentedon Sep 22, 2023
@codebytere please reopen the issue as it isn't fixed yet. Or do we need to post a new issue?
erikjalevik commentedon Mar 25, 2024
I ran into what looks like this issue in Electron 26, but seems fixed in 27.
(And hello again @gpetrov, seems we have all the same bugs!)