Skip to content

Commit d2c6f7f

Browse files
author
Akos Kitta
committed
fix: can open IDE2 from an ino file
With the previous electron version update, the app start lifecycle has changed, and when the application is launched with args (such as when starting IDE2 from a terminal `#launchFromArgs`), the `start` method is not called, and the frontend application config is not injected into the electron app from the generated electron main code. This PR makes the frontend application config access resilient. Closes #2209 Signed-off-by: Akos Kitta <[email protected]>
1 parent 22a69f7 commit d2c6f7f

File tree

1 file changed

+47
-13
lines changed

1 file changed

+47
-13
lines changed

arduino-ide-extension/src/electron-main/theia/electron-main-application.ts

+47-13
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { log as logToFile, setup as setupFileLog } from 'node-log-rotate';
2727
import { fork } from 'node:child_process';
2828
import { promises as fs, rm, rmSync } from 'node:fs';
2929
import type { AddressInfo } from 'node:net';
30-
import { isAbsolute, join, resolve } from 'node:path';
30+
import path from 'node:path';
3131
import { Sketch } from '../../common/protocol';
3232
import {
3333
AppInfo,
@@ -226,9 +226,12 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
226226
const appPath = app.getAppPath();
227227
let traceFile: string | undefined;
228228
if (appPath) {
229-
const tracesPath = join(appPath, 'traces');
229+
const tracesPath = path.join(appPath, 'traces');
230230
await fs.mkdir(tracesPath, { recursive: true });
231-
traceFile = join(tracesPath, `trace-${new Date().toISOString()}.trace`);
231+
traceFile = path.join(
232+
tracesPath,
233+
`trace-${new Date().toISOString()}.trace`
234+
);
232235
}
233236
console.log('>>> Content tracing has started...');
234237
await contentTracing.startRecording(traceOptions);
@@ -270,11 +273,11 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
270273
maybePath: string,
271274
cwd: string
272275
): Promise<string | undefined> {
273-
if (isAbsolute(maybePath)) {
276+
if (path.isAbsolute(maybePath)) {
274277
return maybePath;
275278
}
276279
try {
277-
const resolved = await fs.realpath(resolve(cwd, maybePath));
280+
const resolved = await fs.realpath(path.resolve(cwd, maybePath));
278281
return resolved;
279282
} catch (err) {
280283
if (ErrnoException.isENOENT(err)) {
@@ -505,13 +508,38 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
505508
}
506509

507510
protected override getDefaultOptions(): TheiaBrowserWindowOptions {
508-
const options = super.getDefaultOptions();
509-
if (!options.webPreferences) {
510-
options.webPreferences = {};
511-
}
512-
options.webPreferences.v8CacheOptions = 'bypassHeatCheck'; // TODO: verify this. VS Code use this V8 option.
513-
options.minWidth = 680;
514-
options.minHeight = 593;
511+
// When opening IDE2 by double clicking on the ino file (for example, from Finder or Explorer), the frontend app config is not yet set.
512+
// This customized default options method ensures that getting the frontend config is failsafe.
513+
// https://github.com/arduino/arduino-ide/pull/2287#issuecomment-1818828569
514+
const config = this._config ?? {
515+
applicationName: '',
516+
electron: { windowOptions: {} },
517+
};
518+
const options: TheiaBrowserWindowOptions = {
519+
show: false,
520+
title: config.applicationName,
521+
minWidth: 680,
522+
minHeight: 593,
523+
webPreferences: {
524+
// `global` is undefined when `true`.
525+
contextIsolation: true,
526+
sandbox: false,
527+
nodeIntegration: false,
528+
// Setting the following option to `true` causes some features to break, somehow.
529+
// Issue: https://github.com/eclipse-theia/theia/issues/8577
530+
nodeIntegrationInWorker: false,
531+
preload: path
532+
.resolve(
533+
this.globals.THEIA_APP_PROJECT_PATH,
534+
'lib',
535+
'frontend',
536+
'preload.js'
537+
)
538+
.toString(),
539+
v8CacheOptions: 'bypassHeatCheck', // TODO: verify this. VS Code use this V8 option.
540+
},
541+
...(config.electron?.windowOptions || {}),
542+
};
515543
return options;
516544
}
517545

@@ -777,7 +805,13 @@ async function updateFrontendApplicationConfigFromPackageJson(
777805
try {
778806
const modulePath = __filename;
779807
// must go from `./lib/backend/electron-main.js` to `./package.json` when the app is webpacked.
780-
const packageJsonPath = join(modulePath, '..', '..', '..', 'package.json');
808+
const packageJsonPath = path.join(
809+
modulePath,
810+
'..',
811+
'..',
812+
'..',
813+
'package.json'
814+
);
781815
console.debug(
782816
`Checking for frontend application configuration customizations. Module path: ${modulePath}, destination 'package.json': ${packageJsonPath}`
783817
);

0 commit comments

Comments
 (0)