@@ -264,6 +264,59 @@ async function initializeAuth(client: LanguageClient): Promise<AmazonQLspAuth> {
264
264
return auth
265
265
}
266
266
267
+ // jscpd:ignore-start
268
+ async function initializeLanguageServerConfiguration ( client : LanguageClient , context : string = 'startup' ) {
269
+ const logger = getLogger ( 'amazonqLsp' )
270
+
271
+ if ( AuthUtil . instance . isConnectionValid ( ) ) {
272
+ logger . info ( `[${ context } ] Initializing language server configuration` )
273
+ // jscpd:ignore-end
274
+
275
+ try {
276
+ // Send profile configuration
277
+ logger . debug ( `[${ context } ] Sending profile configuration to language server` )
278
+ await sendProfileToLsp ( client )
279
+ logger . debug ( `[${ context } ] Profile configuration sent successfully` )
280
+
281
+ // Send customization configuration
282
+ logger . debug ( `[${ context } ] Sending customization configuration to language server` )
283
+ await pushConfigUpdate ( client , {
284
+ type : 'customization' ,
285
+ customization : getSelectedCustomization ( ) ,
286
+ } )
287
+ logger . debug ( `[${ context } ] Customization configuration sent successfully` )
288
+
289
+ logger . info ( `[${ context } ] Language server configuration completed successfully` )
290
+ } catch ( error ) {
291
+ logger . error ( `[${ context } ] Failed to initialize language server configuration: ${ error } ` )
292
+ throw error
293
+ }
294
+ } else {
295
+ logger . warn (
296
+ `[${ context } ] Connection invalid, skipping language server configuration - this will cause authentication failures`
297
+ )
298
+ const activeConnection = AuthUtil . instance . auth . activeConnection
299
+ const connectionState = activeConnection
300
+ ? AuthUtil . instance . auth . getConnectionState ( activeConnection )
301
+ : 'no-connection'
302
+ logger . warn ( `[${ context } ] Connection state: ${ connectionState } ` )
303
+ }
304
+ }
305
+
306
+ async function sendProfileToLsp ( client : LanguageClient ) {
307
+ const logger = getLogger ( 'amazonqLsp' )
308
+ const profileArn = AuthUtil . instance . regionProfileManager . activeRegionProfile ?. arn
309
+
310
+ logger . debug ( `Sending profile to LSP: ${ profileArn || 'undefined' } ` )
311
+
312
+ await pushConfigUpdate ( client , {
313
+ type : 'profile' ,
314
+ profileArn : profileArn ,
315
+ } )
316
+
317
+ logger . debug ( `Profile sent to LSP successfully` )
318
+ }
319
+
267
320
async function onLanguageServerReady (
268
321
extensionContext : vscode . ExtensionContext ,
269
322
auth : AmazonQLspAuth ,
@@ -295,14 +348,7 @@ async function onLanguageServerReady(
295
348
// We manually push the cached values the first time since event handlers, which should push, may not have been setup yet.
296
349
// Execution order is weird and should be fixed in the flare implementation.
297
350
// TODO: Revisit if we need this if we setup the event handlers properly
298
- if ( AuthUtil . instance . isConnectionValid ( ) ) {
299
- await sendProfileToLsp ( client )
300
-
301
- await pushConfigUpdate ( client , {
302
- type : 'customization' ,
303
- customization : getSelectedCustomization ( ) ,
304
- } )
305
- }
351
+ await initializeLanguageServerConfiguration ( client , 'startup' )
306
352
307
353
toDispose . push (
308
354
inlineManager ,
@@ -404,13 +450,6 @@ async function onLanguageServerReady(
404
450
// Set this inside onReady so that it only triggers on subsequent language server starts (not the first)
405
451
onServerRestartHandler ( client , auth )
406
452
)
407
-
408
- async function sendProfileToLsp ( client : LanguageClient ) {
409
- await pushConfigUpdate ( client , {
410
- type : 'profile' ,
411
- profileArn : AuthUtil . instance . regionProfileManager . activeRegionProfile ?. arn ,
412
- } )
413
- }
414
453
}
415
454
416
455
/**
@@ -430,8 +469,21 @@ function onServerRestartHandler(client: LanguageClient, auth: AmazonQLspAuth) {
430
469
// TODO: Port this metric override to common definitions
431
470
telemetry . languageServer_crash . emit ( { id : 'AmazonQ' } )
432
471
433
- // Need to set the auth token in the again
434
- await auth . refreshConnection ( true )
472
+ const logger = getLogger ( 'amazonqLsp' )
473
+ logger . info ( '[crash-recovery] Language server crash detected, reinitializing authentication' )
474
+
475
+ try {
476
+ // Send bearer token
477
+ logger . debug ( '[crash-recovery] Refreshing connection and sending bearer token' )
478
+ await auth . refreshConnection ( true )
479
+ logger . debug ( '[crash-recovery] Bearer token sent successfully' )
480
+
481
+ // Send profile and customization configuration
482
+ await initializeLanguageServerConfiguration ( client , 'crash-recovery' )
483
+ logger . info ( '[crash-recovery] Authentication reinitialized successfully' )
484
+ } catch ( error ) {
485
+ logger . error ( `[crash-recovery] Failed to reinitialize after crash: ${ error } ` )
486
+ }
435
487
} )
436
488
}
437
489
0 commit comments