Skip to content

Commit a88a986

Browse files
committed
Fix Electron bug caused by NODE_OPTIONS
We recently started setting NODE_OPTIONS, since it improves node interception. Unfortunately, while it does enable some extra interception with Electron, it also breaks the pause() event we use to set up the core Electron injection. Interception is considered to have failed, the debugger never disconnects, and we don't run the full Electron interception script, just the JS interception from NODE_OPTIONS. Instead, we now drop NODE_OPTIONS for Electron, ensuring the previous behaviour continues. No downside, since we guarantee that we can inject into it using the debugger anyway, so the resulting injection should be equivalent.
1 parent 31813cd commit a88a986

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/interceptors/electron.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,15 @@ export class ElectronInterceptor implements Interceptor {
5353

5454
const appProcess = spawn(cmd, [`--inspect-brk=${debugPort}`], {
5555
stdio: 'inherit',
56-
env: Object.assign({},
57-
process.env,
58-
getTerminalEnvVars(proxyPort, this.config.https, process.env)
59-
)
56+
env: {
57+
...process.env,
58+
...getTerminalEnvVars(proxyPort, this.config.https, process.env),
59+
// We have to disable NODE_OPTIONS injection. If set, the Electron
60+
// app never fires paused(). I suspect because --require changes the
61+
// startup process somehow. Regardless, we don't need it (we're injecting
62+
// manually anyway) so we just skip it here.
63+
NODE_OPTIONS: ''
64+
}
6065
});
6166

6267
let debugClient: ChromeRemoteInterface.CdpClient | undefined;
@@ -96,15 +101,20 @@ export class ElectronInterceptor implements Interceptor {
96101
_.remove(this.debugClients[proxyPort], c => c === debugClient);
97102
});
98103

104+
// These allow us to use the APIs below
105+
await debugClient.Runtime.enable();
106+
await debugClient.Debugger.enable();
107+
108+
// This starts watching for the initial pause event, which gives us the
109+
// inside-electron call frame to inject into (i.e. with require() available)
99110
const callFramePromise = new Promise<string>((resolve) => {
100111
debugClient!.Debugger.paused((stack) => {
101112
resolve(stack.callFrames[0].callFrameId);
102113
});
103114
});
104115

116+
// This confirms we're ready, and triggers pause():
105117
await debugClient.Runtime.runIfWaitingForDebugger();
106-
await debugClient.Runtime.enable();
107-
await debugClient.Debugger.enable();
108118

109119
const callFrameId = await callFramePromise;
110120

@@ -124,6 +134,7 @@ export class ElectronInterceptor implements Interceptor {
124134

125135
if (injectionResult.exceptionDetails) {
126136
const exception = injectionResult.exceptionDetails as any;
137+
console.log(exception);
127138

128139
addBreadcrumb("Evaluate error", {
129140
message: exception && exception.description,

0 commit comments

Comments
 (0)