@@ -16,16 +16,16 @@ import { Deferred } from '@theia/core/lib/common/promise-util';
16
16
import { isObject , MaybePromise , Mutable } from '@theia/core/lib/common/types' ;
17
17
import { ElectronSecurityToken } from '@theia/core/lib/electron-common/electron-token' ;
18
18
import {
19
- ElectronMainApplication as TheiaElectronMainApplication ,
20
19
ElectronMainExecutionParams ,
20
+ ElectronMainApplication as TheiaElectronMainApplication ,
21
21
} from '@theia/core/lib/electron-main/electron-main-application' ;
22
22
import type { TheiaBrowserWindowOptions } from '@theia/core/lib/electron-main/theia-electron-window' ;
23
23
import { FileUri } from '@theia/core/lib/node/file-uri' ;
24
24
import { inject , injectable } from '@theia/core/shared/inversify' ;
25
25
import { URI } from '@theia/core/shared/vscode-uri' ;
26
26
import { log as logToFile , setup as setupFileLog } from 'node-log-rotate' ;
27
27
import { fork } from 'node:child_process' ;
28
- import { promises as fs , rm , rmSync } from 'node:fs' ;
28
+ import { promises as fs , readFileSync , rm , rmSync } from 'node:fs' ;
29
29
import type { AddressInfo } from 'node:net' ;
30
30
import { isAbsolute , join , resolve } from 'node:path' ;
31
31
import { Sketch } from '../../common/protocol' ;
@@ -745,6 +745,19 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
745
745
}
746
746
}
747
747
}
748
+
749
+ // Fallback app config when starting IDE2 from an ino file (from Explorer, Finder, etc.) and the app config is not yet set.
750
+ // https://github.com/arduino/arduino-ide/issues/2209
751
+ private _fallbackConfig : FrontendApplicationConfig | undefined ;
752
+ override get config ( ) : FrontendApplicationConfig {
753
+ if ( ! this . _config ) {
754
+ if ( ! this . _fallbackConfig ) {
755
+ this . _fallbackConfig = readFrontendAppConfigSync ( ) ;
756
+ }
757
+ return this . _fallbackConfig ;
758
+ }
759
+ return super . config ;
760
+ }
748
761
}
749
762
750
763
class InterruptWorkspaceRestoreError extends Error {
@@ -775,11 +788,8 @@ async function updateFrontendApplicationConfigFromPackageJson(
775
788
return config ;
776
789
}
777
790
try {
778
- const modulePath = __filename ;
779
- // must go from `./lib/backend/electron-main.js` to `./package.json` when the app is webpacked.
780
- const packageJsonPath = join ( modulePath , '..' , '..' , '..' , 'package.json' ) ;
781
791
console . debug (
782
- `Checking for frontend application configuration customizations. Module path: ${ modulePath } , destination 'package.json': ${ packageJsonPath } `
792
+ `Checking for frontend application configuration customizations. Module path: ${ __filename } , destination 'package.json': ${ packageJsonPath } `
783
793
) ;
784
794
const rawPackageJson = await fs . readFile ( packageJsonPath , {
785
795
encoding : 'utf8' ,
@@ -827,6 +837,46 @@ async function updateFrontendApplicationConfigFromPackageJson(
827
837
return config ;
828
838
}
829
839
840
+ const fallbackFrontendAppConfig : FrontendApplicationConfig = {
841
+ applicationName : 'Arduino IDE' ,
842
+ defaultTheme : {
843
+ light : 'arduino-theme' ,
844
+ dark : 'arduino-theme-dark' ,
845
+ } ,
846
+ defaultIconTheme : 'none' ,
847
+ validatePreferencesSchema : false ,
848
+ defaultLocale : '' ,
849
+ electron : { } ,
850
+ } ;
851
+
852
+ // When the package.json must go from `./lib/backend/electron-main.js` to `./package.json` when the app is webpacked.
853
+ // Only for production mode!
854
+ const packageJsonPath = join ( __filename , '..' , '..' , '..' , 'package.json' ) ;
855
+
856
+ function readFrontendAppConfigSync ( ) : FrontendApplicationConfig {
857
+ if ( environment . electron . isDevMode ( ) ) {
858
+ console . debug (
859
+ 'Running in dev mode. Using the fallback fronted application config.'
860
+ ) ;
861
+ return fallbackFrontendAppConfig ;
862
+ }
863
+ try {
864
+ const raw = readFileSync ( packageJsonPath , { encoding : 'utf8' } ) ;
865
+ const packageJson = JSON . parse ( raw ) ;
866
+ const config = packageJson ?. theia ?. frontend ?. config ;
867
+ if ( config ) {
868
+ return config ;
869
+ }
870
+ throw new Error ( `Frontend application config not found. ${ packageJson } ` ) ;
871
+ } catch ( err ) {
872
+ console . error (
873
+ `Could not read package.json content from ${ packageJsonPath } .` ,
874
+ err
875
+ ) ;
876
+ return fallbackFrontendAppConfig ;
877
+ }
878
+ }
879
+
830
880
/**
831
881
* Mutates the `toUpdate` argument and returns with it.
832
882
*/
0 commit comments