Skip to content

Commit

Permalink
electron: correctly erase ELECTRON_RUN_AS_NODE
Browse files Browse the repository at this point in the history
This environment variable is trickling down various places, causing
issues when spawning Electron applications from terminals or debug
configurations.

Add `is-electron@^2.2.0` as dependency to `@theia/plugin-ext` to make it
easier to bundle the plugin host process as standalone.

Signed-off-by: Paul Maréchal <[email protected]>
  • Loading branch information
paul-marechal committed Jun 22, 2021
1 parent 165b565 commit ab27299
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ if (typeof process.versions.electron === 'undefined' && typeof process.env.THEIA
process.versions.electron = process.env.THEIA_ELECTRON_VERSION;
}`)}
// Erase the ELECTRON_RUN_AS_NODE variable from the environment, else Electron apps started using Theia will pick it up.
if ('ELECTRON_RUN_AS_NODE' in process.env) {
delete process.env.ELECTRON_RUN_AS_NODE;
}
const path = require('path');
const express = require('express');
const { Container } = require('inversify');
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-dev/src/node/hosted-instance-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ const PROCESS_OPTIONS = {
cwd: process.cwd(),
env: { ...process.env }
};
delete PROCESS_OPTIONS.env.ELECTRON_RUN_AS_NODE;

@injectable()
export abstract class AbstractHostedInstanceManager implements HostedInstanceManager {
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-ext/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"decompress": "^4.2.1",
"escape-html": "^1.0.3",
"filenamify": "^4.1.0",
"is-electron": "^2.2.0",
"jsonc-parser": "^2.2.0",
"lodash.clonedeep": "^4.5.0",
"macaddress": "^0.2.9",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import * as net from 'net';
import * as theia from '@theia/plugin';
import { CommunicationProvider } from '@theia/debug/lib/common/debug-model';
import { ChildProcess, spawn, fork, ForkOptions } from 'child_process';
import isElectron from 'is-electron';

/**
* Starts debug adapter process.
Expand All @@ -40,10 +41,13 @@ export function startDebugAdapter(executable: theia.DebugAdapterExecutable): Com
const { command, args } = executable;
if (command === 'node') {
if (Array.isArray(args) && args.length > 0) {
const isElectron = !!process.env['ELECTRON_RUN_AS_NODE'];
const forkOptions: ForkOptions = {
env: options.env,
execArgv: isElectron ? ['-e', 'delete process.env.ELECTRON_RUN_AS_NODE;require(process.argv[1])'] : [],
// When running in Electron, fork will automatically add ELECTRON_RUN_AS_NODE=1 to the env,
// but this will cause issues when debugging Electron apps, so we'll remove it.
execArgv: isElectron()
? ['-e', 'delete process.env.ELECTRON_RUN_AS_NODE;require(process.argv[1])']
: [],
silent: true
};
if (options.cwd) {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5855,7 +5855,7 @@ is-electron-renderer@^2.0.0:
resolved "https://registry.yarnpkg.com/is-electron-renderer/-/is-electron-renderer-2.0.1.tgz#a469d056f975697c58c98c6023eb0aa79af895a2"
integrity sha1-pGnQVvl1aXxYyYxgI+sKp5r4laI=

is-electron@^2.1.0:
is-electron@^2.1.0, is-electron@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/is-electron/-/is-electron-2.2.0.tgz#8943084f09e8b731b3a7a0298a7b5d56f6b7eef0"
integrity sha512-SpMppC2XR3YdxSzczXReBjqs2zGscWQpBIKqwXYBFic0ERaxNVgwLCHwOLZeESfdJQjX0RDvrJ1lBXX2ij+G1Q==
Expand Down

0 comments on commit ab27299

Please sign in to comment.