electron: [Bug]: proxyquire can't load modules after window.open method is called

Preflight Checklist

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

https://gist.github.com/kyrylo-hrechykhin/f0afe7c469145c51d4df39fbea97ec95

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.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 3
  • Comments: 21 (9 by maintainers)

Most upvoted comments

@codebytere please reopen the issue as it isn’t fixed yet. Or do we need to post a new issue?

@codebytere that is awesome!

With the same sample on 25.1.0 i get:

Screenshot 2023-06-14 at 21 45 14

So hope your fix gets merged quickly, as we need it desperately!

@gpetrov great news:

Screenshot 2023-06-14 at 9 24 34 PM

Same bug - no error on my fix branch

@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 https://github.com/electron/electron/commit/75d2caf451c925a826229ae27264c9b54de9b6db

Getting the same error with just a simple require:

var Docker = require('dockerode')

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 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 require (node:internal/modules/cjs/helpers:103:18)

it is a major roadblock for us to upgrade to Electron 23+

To simplify the issue, following code is enough to trigger the bug:

const vm = require('node:vm');
const context = { x: 2 };
vm.createContext(context);

window.open('');
vm.runInContext('x += 40; var y = 17;', context);

This bug is still mysterious to me though.

@zcbenz , @deepak1556 , could you please help analyzing this issue? FYI: @miniak