Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

electron: fix backend forking #7386

Merged
merged 3 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -290,6 +290,8 @@ app.on('ready', () => {
// Check whether we are in bundled application or development mode.
// @ts-ignore
const devMode = process.defaultApp || /node_modules[\/]electron[\/]/.test(process.execPath);
// Check if we should run everything as one process.
const noBackendFork = process.argv.includes('--no-cluster');
const mainWindow = createNewWindow();

if (isSingleInstance) {
Expand Down Expand Up @@ -327,17 +329,14 @@ 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
if (devMode) {
// 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 debugging we want to run everything in the same process to make things easier.
if (noBackendFork) {
process.env[ElectronSecurityToken] = JSON.stringify(electronSecurityToken);
require(mainPath).then(address => {
loadMainWindow(address.port);
Expand All @@ -346,7 +345,10 @@ app.on('ready', () => {
app.exit(1);
});
} else {
const cp = fork(mainPath, [], { env: Object.assign({
// We want to pass flags passed to the Electron app to the backend process.
// Quirk: When developing from sources, we execute Electron as \`electron.exe electron-main.js ...args\`, but when bundled,
// the command looks like \`bundled-application.exe ...args\`.
const cp = fork(mainPath, process.argv.slice(devMode ? 2 : 1), { env: Object.assign({
[ElectronSecurityToken]: JSON.stringify(electronSecurityToken),
}, process.env) });
cp.on('message', (address) => {
Expand Down
4 changes: 1 addition & 3 deletions dev-packages/application-package/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ class ElectronEnv {
* Can be called from both the `main` and the render process. Also works for forked cluster workers.
*/
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
15 changes: 15 additions & 0 deletions dev-packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- [**Inspecting Tests**](#inspecting-tests)
- [**Reporting Test Coverage**](#reporting-test-coverage)
- [**Downloading Plugins**](#downloading-plugins)
- [**Autogenerated Application**](#autogenerated-application)

## Description

Expand Down Expand Up @@ -270,6 +271,20 @@ The property `theiaPlugins` describes the list of plugins to download, for examp
}
```

## Autogenerated Application
paul-marechal marked this conversation as resolved.
Show resolved Hide resolved

This package can auto-generate application code for both the backend and frontend, as well as webpack configuration files.

When targeting Electron, the `electron-main.js` script will spawn the backend process in a Node.js sub-process, where Electron's API won't be available. To prevent the generated application from forking the backend, you can pass a `--no-cluster` flag. This flag is mostly useful/used for debugging.

```sh
# when developing a Theia application with @theia/cli:
yarn theia start --no-cluster

# when starting a bundled application made using @theia/cli:
bundled-application.exe --no-cluster
```

## Additional Information

- [Theia - GitHub](https://github.com/eclipse-theia/theia)
Expand Down