Skip to content

Commit

Permalink
electron: patch process.versions.electron
Browse files Browse the repository at this point in the history
When forking, Electron 4 forgets to set `process.versions.electron` in
the sub-process, making some packages wrongly guess their environment.

This commit manually patches this variable.

Signed-off-by: Paul Maréchal <paul.marechal@ericsson.com>
  • Loading branch information
paul-marechal committed Mar 23, 2020
1 parent ae5591e commit 4e05f0b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ export class BackendGenerator extends AbstractGenerator {
protected compileServer(backendModules: Map<string, string>): string {
return `// @ts-check
require('reflect-metadata');
// Patch electron version if missing, see https://github.com/eclipse-theia/theia/pull/7361#pullrequestreview-377065146
if (typeof process.versions.electron === 'undefined' && typeof process.env.THEIA_ELECTRON_VERSION === 'string') {
process.versions.electron = process.env.THEIA_ELECTRON_VERSION;
}
const path = require('path');
const express = require('express');
const { Container } = require('inversify');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,16 +327,13 @@ app.on('ready', () => {
// Set the electron version for both the dev and the production mode. (https://github.com/eclipse-theia/theia/issues/3254)
// Otherwise, the forked backend processes will not know that they're serving the electron frontend.
const { versions } = process;
// @ts-ignore
if (versions && typeof versions.electron !== 'undefined') {
// @ts-ignore
process.env.THEIA_ELECTRON_VERSION = versions.electron;
}
// The forked backend should patch its \`process.versions.electron\` with this value if it is missing.
process.env.THEIA_ELECTRON_VERSION = process.versions.electron;
const mainPath = join(__dirname, '..', 'backend', 'main');
// We need to distinguish between bundled application and development mode when starting the clusters.
// See: https://github.com/electron/electron/issues/6337#issuecomment-230183287
// We spawn a separate process for the backend for Express to not run in the Electron main process.
// See: https://github.com/eclipse-theia/theia/pull/7361#issuecomment-601272212
// But when in development mode, we will not spawn a new process, to ease with debugging (devMode).
if (devMode) {
process.env[ElectronSecurityToken] = JSON.stringify(electronSecurityToken);
require(mainPath).then(address => {
Expand Down
6 changes: 2 additions & 4 deletions dev-packages/application-package/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ class ElectronEnv {
/**
* `true` if running in electron. Otherwise, `false`.
*
* Can be called from both the `main` and the render process. Also works for forked cluster workers.
* Can be called from both the `main` and the render process.
*/
is(): boolean {
// When forking a new process from the cluster, we can rely neither on `process.versions` nor `process.argv`.
// Se we look into the `process.env` as well. `is-electron` does not do it for us.
return isElectron() || typeof process !== 'undefined' && typeof process.env === 'object' && !!process.env.THEIA_ELECTRON_VERSION;
return isElectron();
}

/**
Expand Down

0 comments on commit 4e05f0b

Please sign in to comment.