Skip to content

Commit 3f34419

Browse files
committed
fix: update Q profile and customizations on language-servers crash restart
1 parent 276f9ef commit 3f34419

File tree

6 files changed

+500
-18
lines changed

6 files changed

+500
-18
lines changed

packages/amazonq/src/lsp/client.ts

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

257+
// jscpd:ignore-start
258+
async function initializeLanguageServerConfiguration(client: LanguageClient, context: string = 'startup') {
259+
const logger = getLogger('amazonqLsp')
260+
261+
if (AuthUtil.instance.isConnectionValid()) {
262+
logger.info(`[${context}] Initializing language server configuration`)
263+
// jscpd:ignore-end
264+
265+
try {
266+
// Send profile configuration
267+
logger.debug(`[${context}] Sending profile configuration to language server`)
268+
await sendProfileToLsp(client)
269+
logger.debug(`[${context}] Profile configuration sent successfully`)
270+
271+
// Send customization configuration
272+
logger.debug(`[${context}] Sending customization configuration to language server`)
273+
await pushConfigUpdate(client, {
274+
type: 'customization',
275+
customization: getSelectedCustomization(),
276+
})
277+
logger.debug(`[${context}] Customization configuration sent successfully`)
278+
279+
logger.info(`[${context}] Language server configuration completed successfully`)
280+
} catch (error) {
281+
logger.error(`[${context}] Failed to initialize language server configuration: ${error}`)
282+
throw error
283+
}
284+
} else {
285+
logger.warn(
286+
`[${context}] Connection invalid, skipping language server configuration - this will cause authentication failures`
287+
)
288+
const activeConnection = AuthUtil.instance.auth.activeConnection
289+
const connectionState = activeConnection
290+
? AuthUtil.instance.auth.getConnectionState(activeConnection)
291+
: 'no-connection'
292+
logger.warn(`[${context}] Connection state: ${connectionState}`)
293+
}
294+
}
295+
296+
async function sendProfileToLsp(client: LanguageClient) {
297+
const logger = getLogger('amazonqLsp')
298+
const profileArn = AuthUtil.instance.regionProfileManager.activeRegionProfile?.arn
299+
300+
logger.debug(`Sending profile to LSP: ${profileArn || 'undefined'}`)
301+
302+
await pushConfigUpdate(client, {
303+
type: 'profile',
304+
profileArn: profileArn,
305+
})
306+
307+
logger.debug(`Profile sent to LSP successfully`)
308+
}
309+
257310
async function onLanguageServerReady(
258311
extensionContext: vscode.ExtensionContext,
259312
auth: AmazonQLspAuth,
@@ -296,14 +349,7 @@ async function onLanguageServerReady(
296349
// We manually push the cached values the first time since event handlers, which should push, may not have been setup yet.
297350
// Execution order is weird and should be fixed in the flare implementation.
298351
// TODO: Revisit if we need this if we setup the event handlers properly
299-
if (AuthUtil.instance.isConnectionValid()) {
300-
await sendProfileToLsp(client)
301-
302-
await pushConfigUpdate(client, {
303-
type: 'customization',
304-
customization: getSelectedCustomization(),
305-
})
306-
}
352+
await initializeLanguageServerConfiguration(client, 'startup')
307353

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

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

434-
// Need to set the auth token in the again
435-
await auth.refreshConnection(true)
473+
const logger = getLogger('amazonqLsp')
474+
logger.info('[crash-recovery] Language server crash detected, reinitializing authentication')
475+
476+
try {
477+
// Send bearer token
478+
logger.debug('[crash-recovery] Refreshing connection and sending bearer token')
479+
await auth.refreshConnection(true)
480+
logger.debug('[crash-recovery] Bearer token sent successfully')
481+
482+
// Send profile and customization configuration
483+
await initializeLanguageServerConfiguration(client, 'crash-recovery')
484+
logger.info('[crash-recovery] Authentication reinitialized successfully')
485+
} catch (error) {
486+
logger.error(`[crash-recovery] Failed to reinitialize after crash: ${error}`)
487+
}
436488
})
437489
}
438490

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)