Skip to content

Commit 88caf6b

Browse files
authored
fix(amazonq): update Q profile and customizations on language-servers crash restart (#7671)
fix(amazonq): update Q profile and customizations on language-servers crash restart
2 parents d6fc897 + 85f5045 commit 88caf6b

File tree

6 files changed

+521
-18
lines changed

6 files changed

+521
-18
lines changed

packages/amazonq/src/lsp/client.ts

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,59 @@ async function initializeAuth(client: LanguageClient): Promise<AmazonQLspAuth> {
264264
return auth
265265
}
266266

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+
267320
async function onLanguageServerReady(
268321
extensionContext: vscode.ExtensionContext,
269322
auth: AmazonQLspAuth,
@@ -295,14 +348,7 @@ async function onLanguageServerReady(
295348
// We manually push the cached values the first time since event handlers, which should push, may not have been setup yet.
296349
// Execution order is weird and should be fixed in the flare implementation.
297350
// 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')
306352

307353
toDispose.push(
308354
inlineManager,
@@ -404,13 +450,6 @@ async function onLanguageServerReady(
404450
// Set this inside onReady so that it only triggers on subsequent language server starts (not the first)
405451
onServerRestartHandler(client, auth)
406452
)
407-
408-
async function sendProfileToLsp(client: LanguageClient) {
409-
await pushConfigUpdate(client, {
410-
type: 'profile',
411-
profileArn: AuthUtil.instance.regionProfileManager.activeRegionProfile?.arn,
412-
})
413-
}
414453
}
415454

416455
/**
@@ -430,8 +469,21 @@ function onServerRestartHandler(client: LanguageClient, auth: AmazonQLspAuth) {
430469
// TODO: Port this metric override to common definitions
431470
telemetry.languageServer_crash.emit({ id: 'AmazonQ' })
432471

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+
}
435487
})
436488
}
437489

packages/amazonq/src/lsp/config.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55
import * as vscode from 'vscode'
6-
import { DevSettings, getServiceEnvVarConfig, BaseLspInstaller } from 'aws-core-vscode/shared'
6+
import { DevSettings, getServiceEnvVarConfig, BaseLspInstaller, getLogger } from 'aws-core-vscode/shared'
77
import { LanguageClient } from 'vscode-languageclient'
88
import {
99
DidChangeConfigurationNotification,
@@ -68,23 +68,31 @@ export function toAmazonQLSPLogLevel(logLevel: vscode.LogLevel): LspLogLevel {
6868
* push the given config.
6969
*/
7070
export async function pushConfigUpdate(client: LanguageClient, config: QConfigs) {
71+
const logger = getLogger('amazonqLsp')
72+
7173
switch (config.type) {
7274
case 'profile':
75+
logger.debug(`Pushing profile configuration: ${config.profileArn || 'undefined'}`)
7376
await client.sendRequest(updateConfigurationRequestType.method, {
7477
section: 'aws.q',
7578
settings: { profileArn: config.profileArn },
7679
})
80+
logger.debug(`Profile configuration pushed successfully`)
7781
break
7882
case 'customization':
83+
logger.debug(`Pushing customization configuration: ${config.customization || 'undefined'}`)
7984
client.sendNotification(DidChangeConfigurationNotification.type.method, {
8085
section: 'aws.q',
8186
settings: { customization: config.customization },
8287
})
88+
logger.debug(`Customization configuration pushed successfully`)
8389
break
8490
case 'logLevel':
91+
logger.debug(`Pushing log level configuration`)
8592
client.sendNotification(DidChangeConfigurationNotification.type.method, {
8693
section: 'aws.logLevel',
8794
})
95+
logger.debug(`Log level configuration pushed successfully`)
8896
break
8997
}
9098
}

0 commit comments

Comments
 (0)