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

Fix remote support in packaged apps #13584

Merged
merged 2 commits into from
Apr 17, 2024
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 @@ -52,10 +52,12 @@ if (process.env.LC_ALL) {
}
process.env.LC_NUMERIC = 'C';

const { resolve } = require('path');
const theiaAppProjectPath = resolve(__dirname, '..', '..');
process.env.THEIA_APP_PROJECT_PATH = theiaAppProjectPath;
const { default: electronMainApplicationModule } = require('@theia/core/lib/electron-main/electron-main-application-module');
const { ElectronMainApplication, ElectronMainApplicationGlobals } = require('@theia/core/lib/electron-main/electron-main-application');
const { Container } = require('inversify');
const { resolve } = require('path');
const { app } = require('electron');

const config = ${this.prettyStringify(this.pck.props.frontend.config)};
Expand All @@ -71,7 +73,7 @@ const isSingleInstance = ${this.pck.props.backend.config.singleInstance === true
const container = new Container();
container.load(electronMainApplicationModule);
container.bind(ElectronMainApplicationGlobals).toConstantValue({
THEIA_APP_PROJECT_PATH: resolve(__dirname, '..', '..'),
THEIA_APP_PROJECT_PATH: theiaAppProjectPath,
THEIA_BACKEND_MAIN_PATH: resolve(__dirname, 'main.js'),
THEIA_FRONTEND_HTML_PATH: resolve(__dirname, '..', '..', 'lib', 'frontend', 'index.html'),
});
Expand Down Expand Up @@ -119,6 +121,7 @@ if ('ELECTRON_RUN_AS_NODE' in process.env) {
}

const path = require('path');
process.env.THEIA_APP_PROJECT_PATH = path.resolve(__dirname, '..', '..')
const express = require('express');
const { Container } = require('inversify');
const { BackendApplication, BackendApplicationServer, CliManager } = require('@theia/core/lib/node');
Expand Down
4 changes: 0 additions & 4 deletions packages/core/src/electron-main/electron-main-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,10 +548,6 @@ export class ElectronMainApplication {
protected async startBackend(): Promise<number> {
// Check if we should run everything as one process.
const noBackendFork = process.argv.indexOf('--no-cluster') !== -1;
// We cannot use the `process.cwd()` as the application project path (the location of the `package.json` in other words)
// in a bundled electron application because it depends on the way we start it. For instance, on OS X, these are a differences:
// https://github.com/eclipse-theia/theia/issues/3297#issuecomment-439172274
process.env.THEIA_APP_PROJECT_PATH = this.globals.THEIA_APP_PROJECT_PATH;
// 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.
process.env.THEIA_ELECTRON_VERSION = process.versions.electron;
Expand Down
7 changes: 2 additions & 5 deletions packages/core/src/node/backend-application-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
bindContributionProvider, MessageService, MessageClient, ConnectionHandler, RpcConnectionHandler,
CommandService, commandServicePath, messageServicePath, OSBackendProvider, OSBackendProviderPath
} from '../common';
import { BackendApplication, BackendApplicationContribution, BackendApplicationCliContribution, BackendApplicationServer } from './backend-application';
import { BackendApplication, BackendApplicationContribution, BackendApplicationCliContribution, BackendApplicationServer, BackendApplicationPath } from './backend-application';
import { CliManager, CliContribution } from './cli';
import { IPCConnectionProvider } from './messaging';
import { ApplicationServerImpl } from './application-server';
Expand Down Expand Up @@ -101,10 +101,7 @@ export const backendApplicationModule = new ContainerModule(bind => {
})
).inSingletonScope();

bind(ApplicationPackage).toDynamicValue(({ container }) => {
const { projectPath } = container.get(BackendApplicationCliContribution);
return new ApplicationPackage({ projectPath });
}).inSingletonScope();
bind(ApplicationPackage).toConstantValue(new ApplicationPackage({ projectPath: BackendApplicationPath }));

bind(WsRequestValidator).toSelf().inSingletonScope();
bindContributionProvider(bind, WsRequestValidatorContribution);
Expand Down
28 changes: 10 additions & 18 deletions packages/core/src/node/backend-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ import { CliContribution } from './cli';
import { Deferred } from '../common/promise-util';
import { environment } from '../common/index';
import { AddressInfo } from 'net';
import { ApplicationPackage } from '@theia/application-package';
import { ProcessUtils } from './process-utils';

/**
* The path to the application project directory. This is the directory where the application code is located.
* Mostly contains the `package.json` file and the `lib` directory.
*/
export const BackendApplicationPath = process.env.THEIA_APP_PROJECT_PATH || process.cwd();

export type DnsResultOrder = 'ipv4first' | 'verbatim' | 'nodeDefault';

const APP_PROJECT_PATH = 'app-project-path';
Expand Down Expand Up @@ -115,15 +120,16 @@ export class BackendApplicationCliContribution implements CliContribution {
ssl: boolean | undefined;
cert: string | undefined;
certkey: string | undefined;
projectPath: string;
/** @deprecated Use the `BackendApplicationPath` constant or `process.env.THEIA_APP_PROJECT_PATH` environment variable instead */
projectPath = BackendApplicationPath;

configure(conf: yargs.Argv): void {
conf.option('port', { alias: 'p', description: 'The port the backend server listens on.', type: 'number', default: DEFAULT_PORT });
conf.option('hostname', { alias: 'h', description: 'The allowed hostname for connections.', type: 'string', default: DEFAULT_HOST });
conf.option('ssl', { description: 'Use SSL (HTTPS), cert and certkey must also be set', type: 'boolean', default: DEFAULT_SSL });
conf.option('cert', { description: 'Path to SSL certificate.', type: 'string' });
conf.option('certkey', { description: 'Path to SSL certificate key.', type: 'string' });
conf.option(APP_PROJECT_PATH, { description: 'Sets the application project directory', default: this.appProjectPath() });
conf.option(APP_PROJECT_PATH, { description: 'Sets the application project directory', deprecated: true });
conf.option('dnsDefaultResultOrder', {
type: 'string',
description: 'Configure Node\'s DNS resolver default behavior, see https://nodejs.org/docs/latest-v18.x/api/dns.html#dnssetdefaultresultorderorder',
Expand All @@ -138,19 +144,8 @@ export class BackendApplicationCliContribution implements CliContribution {
this.ssl = args.ssl as boolean;
this.cert = args.cert as string;
this.certkey = args.certkey as string;
this.projectPath = args[APP_PROJECT_PATH] as string;
this.dnsDefaultResultOrder = args.dnsDefaultResultOrder as DnsResultOrder;
}

protected appProjectPath(): string {
if (environment.electron.is()) {
if (process.env.THEIA_APP_PROJECT_PATH) {
return process.env.THEIA_APP_PROJECT_PATH;
}
throw new Error('The \'THEIA_APP_PROJECT_PATH\' environment variable must be set when running in electron.');
}
return process.cwd();
}
}

/**
Expand All @@ -161,9 +156,6 @@ export class BackendApplication {

protected readonly app: express.Application = express();

@inject(ApplicationPackage)
protected readonly applicationPackage: ApplicationPackage;

@inject(ProcessUtils)
protected readonly processUtils: ProcessUtils;

Expand Down Expand Up @@ -352,7 +344,7 @@ export class BackendApplication {
const acceptedEncodings = req.acceptsEncodings();

const gzUrl = `${req.url}.gz`;
const gzPath = path.join(this.applicationPackage.projectPath, 'lib', 'frontend', gzUrl);
const gzPath = path.join(BackendApplicationPath, 'lib', 'frontend', gzUrl);
if (acceptedEncodings.indexOf('gzip') === -1 || !(await fs.pathExists(gzPath))) {
next();
return;
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/node/env-variables/env-variables-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { pathExists, mkdir } from 'fs-extra';
import { EnvVariable, EnvVariablesServer } from '../../common/env-variables';
import { isWindows } from '../../common/os';
import { FileUri } from '../../common/file-uri';
import { BackendApplicationPath } from '../backend-application';

@injectable()
export class EnvVariablesServerImpl implements EnvVariablesServer {
Expand All @@ -45,10 +46,7 @@ export class EnvVariablesServerImpl implements EnvVariablesServer {
}

protected async createConfigDirUri(): Promise<string> {
let dataFolderPath: string = '';
if (process.env.THEIA_APP_PROJECT_PATH) {
dataFolderPath = join(process.env.THEIA_APP_PROJECT_PATH, 'data');
}
const dataFolderPath = join(BackendApplicationPath, 'data');
const userDataPath = join(dataFolderPath, 'user-data');
const dataFolderExists = this.pathExistenceCache[dataFolderPath] ??= await pathExists(dataFolderPath);
if (dataFolderExists) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ export class RemoteCopyRegistry {
const globResult = await promiseGlob(pattern, {
cwd: projectPath
});
const relativeFiles = globResult.map(file => path.relative(projectPath, file));
for (const file of relativeFiles) {
for (const file of globResult) {
const targetFile = this.withTarget(file, target);
this.files.push({
path: file,
Expand Down
Loading