From ab27299400b96374643b9139166afcc22195eff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20Mar=C3=A9chal?= Date: Tue, 30 Mar 2021 11:40:43 -0400 Subject: [PATCH] electron: correctly erase ELECTRON_RUN_AS_NODE MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../src/generator/backend-generator.ts | 5 +++++ packages/plugin-dev/src/node/hosted-instance-manager.ts | 1 - packages/plugin-ext/package.json | 1 + .../src/plugin/node/debug/plugin-debug-adapter-starter.ts | 8 ++++++-- yarn.lock | 2 +- 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/dev-packages/application-manager/src/generator/backend-generator.ts b/dev-packages/application-manager/src/generator/backend-generator.ts index 7020a4eb90d5d..6d9ff269b23a7 100644 --- a/dev-packages/application-manager/src/generator/backend-generator.ts +++ b/dev-packages/application-manager/src/generator/backend-generator.ts @@ -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'); diff --git a/packages/plugin-dev/src/node/hosted-instance-manager.ts b/packages/plugin-dev/src/node/hosted-instance-manager.ts index 73d7cd5ad5fd6..2cb40de8fcc2b 100644 --- a/packages/plugin-dev/src/node/hosted-instance-manager.ts +++ b/packages/plugin-dev/src/node/hosted-instance-manager.ts @@ -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 { diff --git a/packages/plugin-ext/package.json b/packages/plugin-ext/package.json index b4984352adc3a..4688a3db2c420 100644 --- a/packages/plugin-ext/package.json +++ b/packages/plugin-ext/package.json @@ -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", diff --git a/packages/plugin-ext/src/plugin/node/debug/plugin-debug-adapter-starter.ts b/packages/plugin-ext/src/plugin/node/debug/plugin-debug-adapter-starter.ts index 9301b3eff1f34..a96e1151843bc 100644 --- a/packages/plugin-ext/src/plugin/node/debug/plugin-debug-adapter-starter.ts +++ b/packages/plugin-ext/src/plugin/node/debug/plugin-debug-adapter-starter.ts @@ -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. @@ -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) { diff --git a/yarn.lock b/yarn.lock index e2107e12be0a4..7e183cffebc7b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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==