33 * Licensed under the MIT License. See License.txt in the project root for license information.
44 *--------------------------------------------------------------------------------------------*/
55
6- import 'vs/code/code.main ' ;
6+ import 'vs/platform/update/node/update.config.contribution ' ;
77import { app , dialog } from 'electron' ;
88import { assign } from 'vs/base/common/objects' ;
99import * as platform from 'vs/base/common/platform' ;
@@ -39,6 +39,7 @@ import { uploadLogs } from 'vs/code/electron-main/logUploader';
3939import { setUnexpectedErrorHandler } from 'vs/base/common/errors' ;
4040import { IThemeMainService , ThemeMainService } from 'vs/platform/theme/electron-main/themeMainService' ;
4141import { Client } from 'vs/base/parts/ipc/common/ipc.net' ;
42+ import { once } from 'vs/base/common/functional' ;
4243
4344class ExpectedError extends Error {
4445 readonly isExpected = true ;
@@ -90,17 +91,14 @@ class CodeMain {
9091 // log file access on Windows (https://github.com/Microsoft/vscode/issues/41218)
9192 const bufferLogService = new BufferLogService ( ) ;
9293
93- const instantiationService = this . createServices ( args , bufferLogService ) ;
94+ const [ instantiationService , instanceEnvironment ] = this . createServices ( args , bufferLogService ) ;
9495 try {
96+
97+ // Init services
9598 await instantiationService . invokeFunction ( async accessor => {
9699 const environmentService = accessor . get ( IEnvironmentService ) ;
97100 const stateService = accessor . get ( IStateService ) ;
98- const logService = accessor . get ( ILogService ) ;
99-
100- // Patch `process.env` with the instance's environment
101- const instanceEnvironment = this . patchEnvironment ( environmentService ) ;
102101
103- // Startup
104102 try {
105103 await this . initServices ( environmentService , stateService as StateService ) ;
106104 } catch ( error ) {
@@ -110,9 +108,19 @@ class CodeMain {
110108
111109 throw error ;
112110 }
111+ } ) ;
112+
113+ // Startup
114+ await instantiationService . invokeFunction ( async accessor => {
115+ const environmentService = accessor . get ( IEnvironmentService ) ;
116+ const logService = accessor . get ( ILogService ) ;
117+ const lifecycleService = accessor . get ( ILifecycleService ) ;
118+ const configurationService = accessor . get ( IConfigurationService ) ;
119+
120+ const mainIpcServer = await this . doStartup ( logService , environmentService , lifecycleService , instantiationService , true ) ;
113121
114- const mainIpcServer = await this . doStartup ( logService , environmentService , instantiationService , true ) ;
115122 bufferLogService . logger = new SpdLogService ( 'main' , environmentService . logsPath , bufferLogService . getLevel ( ) ) ;
123+ once ( lifecycleService . onWillShutdown ) ( ( ) => ( configurationService as ConfigurationService ) . dispose ( ) ) ;
116124
117125 return instantiationService . createInstance ( CodeApplication , mainIpcServer , instanceEnvironment ) . startup ( ) ;
118126 } ) ;
@@ -121,29 +129,30 @@ class CodeMain {
121129 }
122130 }
123131
124- private createServices ( args : ParsedArgs , bufferLogService : BufferLogService ) : IInstantiationService {
132+ private createServices ( args : ParsedArgs , bufferLogService : BufferLogService ) : [ IInstantiationService , typeof process . env ] {
125133 const services = new ServiceCollection ( ) ;
126134
127135 const environmentService = new EnvironmentService ( args , process . execPath ) ;
136+ const instanceEnvironment = this . patchEnvironment ( environmentService ) ; // Patch `process.env` with the instance's environment
137+ services . set ( IEnvironmentService , environmentService ) ;
128138
129139 const logService = new MultiplexLogService ( [ new ConsoleLogMainService ( getLogLevel ( environmentService ) ) , bufferLogService ] ) ;
130140 process . once ( 'exit' , ( ) => logService . dispose ( ) ) ;
131-
132- services . set ( IEnvironmentService , environmentService ) ;
133141 services . set ( ILogService , logService ) ;
142+
134143 services . set ( ILifecycleService , new SyncDescriptor ( LifecycleService ) ) ;
135144 services . set ( IStateService , new SyncDescriptor ( StateService ) ) ;
136145 services . set ( IConfigurationService , new SyncDescriptor ( ConfigurationService , [ environmentService . appSettingsPath ] ) ) ;
137146 services . set ( IRequestService , new SyncDescriptor ( RequestService ) ) ;
138147 services . set ( IDiagnosticsService , new SyncDescriptor ( DiagnosticsService ) ) ;
139148 services . set ( IThemeMainService , new SyncDescriptor ( ThemeMainService ) ) ;
140149
141- return new InstantiationService ( services , true ) ;
150+ return [ new InstantiationService ( services , true ) , instanceEnvironment ] ;
142151 }
143152
144153 private initServices ( environmentService : IEnvironmentService , stateService : StateService ) : Promise < unknown > {
145154
146- // Ensure paths for environment service exist
155+ // Environment service (paths)
147156 const environmentServiceInitialization = Promise . all < void | undefined > ( [
148157 environmentService . extensionsPath ,
149158 environmentService . nodeCachedDataDir ,
@@ -175,14 +184,15 @@ class CodeMain {
175184 return instanceEnvironment ;
176185 }
177186
178- private async doStartup ( logService : ILogService , environmentService : IEnvironmentService , instantiationService : IInstantiationService , retry : boolean ) : Promise < Server > {
187+ private async doStartup ( logService : ILogService , environmentService : IEnvironmentService , lifecycleService : ILifecycleService , instantiationService : IInstantiationService , retry : boolean ) : Promise < Server > {
179188
180189 // Try to setup a server for running. If that succeeds it means
181190 // we are the first instance to startup. Otherwise it is likely
182191 // that another instance is already running.
183192 let server : Server ;
184193 try {
185194 server = await serve ( environmentService . mainIPCHandle ) ;
195+ once ( lifecycleService . onWillShutdown ) ( ( ) => server . dispose ( ) ) ;
186196 } catch ( error ) {
187197
188198 // Handle unexpected errors (the only expected error is EADDRINUSE that
@@ -226,10 +236,11 @@ class CodeMain {
226236 fs . unlinkSync ( environmentService . mainIPCHandle ) ;
227237 } catch ( error ) {
228238 logService . warn ( 'Could not delete obsolete instance handle' , error ) ;
239+
229240 throw error ;
230241 }
231242
232- return this . doStartup ( logService , environmentService , instantiationService , false ) ;
243+ return this . doStartup ( logService , environmentService , lifecycleService , instantiationService , false ) ;
233244 }
234245
235246 // Tests from CLI require to be the only instance currently
@@ -261,8 +272,8 @@ class CodeMain {
261272 if ( environmentService . args . status ) {
262273 return instantiationService . invokeFunction ( async accessor => {
263274 const diagnostics = await accessor . get ( IDiagnosticsService ) . getDiagnostics ( launchClient ) ;
264-
265275 console . log ( diagnostics ) ;
276+
266277 throw new ExpectedError ( ) ;
267278 } ) ;
268279 }
@@ -300,12 +311,14 @@ class CodeMain {
300311 // Print --status usage info
301312 if ( environmentService . args . status ) {
302313 logService . warn ( 'Warning: The --status argument can only be used if Code is already running. Please run it again after Code has started.' ) ;
314+
303315 throw new ExpectedError ( 'Terminating...' ) ;
304316 }
305317
306318 // Log uploader usage info
307319 if ( typeof environmentService . args [ 'upload-logs' ] !== 'undefined' ) {
308320 logService . warn ( 'Warning: The --upload-logs argument can only be used if Code is already running. Please run it again after Code has started.' ) ;
321+
309322 throw new ExpectedError ( 'Terminating...' ) ;
310323 }
311324
0 commit comments