Skip to content

Commit ffe91a2

Browse files
fix(amazonq): Wrong customization being pushed to LSP on start
Problem On start of the extension when we called getSelectedCustomization() it would return the wrong customization. This caused the wrong customization to be pushed to the LSP. The reason was due to notifyNewCustomizations not being called before getSelectedCustomization() was used. Solution Ensure the call that uses notifyNewCustomizations() runs before getSelectedCustomization is used. I also refactored to compartmentalize the relevant code. Signed-off-by: nkomonen-amazon <[email protected]>
1 parent 4e1d059 commit ffe91a2

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

packages/amazonq/src/lsp/chat/activation.ts

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { registerLanguageServerEventListener, registerMessageListeners } from '.
1111
import { Commands, getLogger, globals, undefinedIfEmpty } from 'aws-core-vscode/shared'
1212
import { activate as registerLegacyChatListeners } from '../../app/chat/activation'
1313
import { DefaultAmazonQAppInitContext } from 'aws-core-vscode/amazonq'
14-
import { AuthUtil, getSelectedCustomization } from 'aws-core-vscode/codewhisperer'
14+
import { AuthUtil, getSelectedCustomization, notifyNewCustomizations } from 'aws-core-vscode/codewhisperer'
1515
import {
1616
DidChangeConfigurationNotification,
1717
updateConfigurationRequestType,
@@ -25,11 +25,8 @@ export async function activate(languageClient: LanguageClient, encryptionKey: Bu
2525
type: 'profile',
2626
profileArn: AuthUtil.instance.regionProfileManager.activeRegionProfile?.arn,
2727
})
28-
// We need to push the cached customization on startup explicitly
29-
await pushConfigUpdate(languageClient, {
30-
type: 'customization',
31-
customization: getSelectedCustomization(),
32-
})
28+
29+
await initializeCustomizations()
3330

3431
const provider = new AmazonQChatViewProvider(mynahUIPath)
3532

@@ -86,10 +83,7 @@ export async function activate(languageClient: LanguageClient, encryptionKey: Bu
8683
await provider.refreshWebview()
8784
}),
8885
Commands.register('aws.amazonq.updateCustomizations', () => {
89-
void pushConfigUpdate(languageClient, {
90-
type: 'customization',
91-
customization: undefinedIfEmpty(getSelectedCustomization().arn),
92-
})
86+
pushCustomizationToServer(languageClient)
9387
}),
9488
globals.logOutputChannel.onDidChangeLogLevel((logLevel) => {
9589
getLogger('amazonqLsp').info(`Local log level changed to ${logLevel}, notifying LSP`)
@@ -98,15 +92,48 @@ export async function activate(languageClient: LanguageClient, encryptionKey: Bu
9892
})
9993
})
10094
)
95+
96+
/**
97+
* Initialize customizations on extension startup
98+
*/
99+
async function initializeCustomizations() {
100+
/**
101+
* Even though this function is called "notify", it has a side effect that first restores the
102+
* cached customization. So for {@link getSelectedCustomization()} to work as expected, we must
103+
* call {@link notifyNewCustomizations} first.
104+
*
105+
* TODO: Separate restoring and notifying, or just rename the function to something better
106+
*/
107+
if (AuthUtil.instance.isIdcConnection() && AuthUtil.instance.isConnected()) {
108+
await notifyNewCustomizations()
109+
}
110+
111+
/**
112+
* HACK: We must explicitly push the customization here since restoring the customization from cache
113+
* does not currently trigger a push to server.
114+
*
115+
* TODO: Always push to server whenever restoring from cache.
116+
*/
117+
pushCustomizationToServer(languageClient)
118+
}
119+
120+
function pushCustomizationToServer(languageClient: LanguageClient) {
121+
void pushConfigUpdate(languageClient, {
122+
type: 'customization',
123+
customization: undefinedIfEmpty(getSelectedCustomization().arn),
124+
})
125+
}
101126
}
102127

103128
/**
104-
* Push a config value to the language server, effectively updating it with the
129+
* Request/Notify a config value to the language server, effectively updating it with the
105130
* latest configuration from the client.
106131
*
107132
* The issue is we need to push certain configs to different places, since there are
108133
* different handlers for specific configs. So this determines the correct place to
109134
* push the given config.
135+
*
136+
* TODO: Move this to somewhere more appropriate
110137
*/
111138
async function pushConfigUpdate(client: LanguageClient, config: QConfigs) {
112139
switch (config.type) {

packages/core/src/codewhisperer/activation.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ import { AuthUtil } from './util/authUtil'
7272
import { ImportAdderProvider } from './service/importAdderProvider'
7373
import { TelemetryHelper } from './util/telemetryHelper'
7474
import { openUrl } from '../shared/utilities/vsCodeUtils'
75-
import { notifyNewCustomizations, onProfileChangedListener } from './util/customizationUtil'
75+
import { onProfileChangedListener } from './util/customizationUtil'
7676
import { CodeWhispererCommandBackend, CodeWhispererCommandDeclarations } from './commands/gettingStartedPageCommands'
7777
import { SecurityIssueHoverProvider } from './service/securityIssueHoverProvider'
7878
import { SecurityIssueCodeActionProvider } from './service/securityIssueCodeActionProvider'
@@ -354,9 +354,6 @@ export async function activate(context: ExtContext): Promise<void> {
354354
{ emit: false, functionId: { name: 'activateCwCore' } }
355355
)
356356

357-
if (AuthUtil.instance.isIdcConnection() && AuthUtil.instance.isConnected()) {
358-
await notifyNewCustomizations()
359-
}
360357
if (AuthUtil.instance.isBuilderIdConnection()) {
361358
await CodeScansState.instance.setScansEnabled(false)
362359
}

packages/core/src/codewhisperer/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export {
107107
baseCustomization,
108108
onProfileChangedListener,
109109
CustomizationProvider,
110+
notifyNewCustomizations,
110111
} from './util/customizationUtil'
111112
export { Container } from './service/serviceContainer'
112113
export * from './util/gitUtil'

0 commit comments

Comments
 (0)