Skip to content

Commit 779501f

Browse files
fix: Show "Must select Developer Profile" message
Problem: THe show developer profile message was appearing at the wrong time. It was due to the order that the code executed. This was because when we called to check if the profiles were restored from cache, the logic that restored them had not actually run yet. Solution: Move the logic that decides to show the Needs Profile message, to after the code that restores the Profile. It is also only called once since we only want to show the message on startup. Signed-off-by: nkomonen-amazon <[email protected]>
1 parent 6958c25 commit 779501f

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
import * as nls from 'vscode-nls'
6+
const localize = nls.loadMessageBundle()
7+
import vscode from 'vscode'
8+
import { AmazonQPromptSettings, placeholder } from 'aws-core-vscode/shared'
9+
import { telemetry } from 'aws-core-vscode/telemetry'
10+
import { selectRegionProfileCommand, toastMessage } from 'aws-core-vscode/codewhisperer'
11+
import { once } from 'aws-core-vscode/utils'
12+
13+
/**
14+
* Creates a toast message telling the user they need to select a Developer Profile
15+
*/
16+
export const notifySelectDeveloperProfile = once(_notifySelectDeveloperProfile)
17+
async function _notifySelectDeveloperProfile() {
18+
const suppressId = 'amazonQSelectDeveloperProfile'
19+
const settings = AmazonQPromptSettings.instance
20+
const shouldShow = settings.isPromptEnabled(suppressId)
21+
if (!shouldShow) {
22+
return
23+
}
24+
25+
const message = localize(
26+
'aws.amazonq.profile.mustSelectMessage',
27+
'You must select a Q Developer Profile for Amazon Q features to work.'
28+
)
29+
const selectProfile = 'Select Profile'
30+
const dontShowAgain = 'Dont Show Again'
31+
32+
await telemetry.toolkit_showNotification.run(async () => {
33+
telemetry.record({ id: 'mustSelectDeveloperProfileMessage' })
34+
void vscode.window.showWarningMessage(message, selectProfile, dontShowAgain).then(async (resp) => {
35+
await telemetry.toolkit_invokeAction.run(async () => {
36+
if (resp === selectProfile) {
37+
// Show Profile
38+
telemetry.record({ action: 'select' })
39+
void selectRegionProfileCommand.execute(placeholder, toastMessage)
40+
} else if (resp === dontShowAgain) {
41+
telemetry.record({ action: 'dontShowAgain' })
42+
await settings.disablePrompt(suppressId)
43+
} else {
44+
telemetry.record({ action: 'ignore' })
45+
}
46+
})
47+
})
48+
})
49+
}

packages/core/src/codewhisperer/activation.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ import { setContext } from '../shared/vscode/setContext'
9191
import { syncSecurityIssueWebview } from './views/securityIssue/securityIssueWebview'
9292
import { detectCommentAboveLine } from '../shared/utilities/commentUtils'
9393
import { activateEditTracking } from './nextEditPrediction/activation'
94-
import { notifySelectDeveloperProfile } from './region/utils'
9594

9695
let localize: nls.LocalizeFunc
9796

@@ -351,10 +350,6 @@ export async function activate(context: ExtContext): Promise<void> {
351350
await AuthUtil.instance.notifySessionConfiguration()
352351
}
353352
}
354-
355-
if (AuthUtil.instance.regionProfileManager.requireProfileSelection()) {
356-
await notifySelectDeveloperProfile()
357-
}
358353
},
359354
{ emit: false, functionId: { name: 'activateCwCore' } }
360355
)

packages/core/src/codewhisperer/util/authUtil.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import { builderIdStartUrl, internalStartUrl } from '../../auth/sso/constants'
3030
import { VSCODE_EXTENSION_ID } from '../../shared/extensions'
3131
import { RegionProfileManager } from '../region/regionProfileManager'
3232
import { AuthFormId } from '../../login/webview/vue/types'
33+
import { notifySelectDeveloperProfile } from '../region/utils'
34+
import { once } from '../../shared/utilities/functionUtils'
3335

3436
const localize = nls.loadMessageBundle()
3537

@@ -91,8 +93,20 @@ export class AuthUtil implements IAuthProvider {
9193
return this.session.loginType === LoginTypes.SSO
9294
}
9395

96+
/**
97+
* HACK: Ideally we'd put {@link notifySelectDeveloperProfile} in to {@link restore}.
98+
* But because {@link refreshState} is only called if !isConnected, we cannot do it since
99+
* {@link notifySelectDeveloperProfile} needs {@link refreshState} to run so it can set
100+
* the Bearer Token in the LSP first.
101+
*/
102+
startedConnected = false
103+
94104
async restore() {
95105
await this.session.restore()
106+
this.startedConnected = this.isConnected()
107+
108+
// HACK: The LSP should allow us to call refreshState multiple times, but for some reason it breaks.
109+
// isConnected somehow allows it to work. Assumption is that the LSP does not handle redundant calls nicely.
96110
if (!this.isConnected()) {
97111
await this.refreshState()
98112
}
@@ -258,7 +272,6 @@ export class AuthUtil implements IAuthProvider {
258272
private async refreshState(state = this.getAuthState()) {
259273
if (state === 'expired' || state === 'notConnected') {
260274
this.lspAuth.deleteBearerToken()
261-
262275
if (this.isIdcConnection()) {
263276
await this.regionProfileManager.invalidateProfile(this.regionProfileManager.activeRegionProfile?.arn)
264277
await this.regionProfileManager.clearCache()
@@ -273,6 +286,9 @@ export class AuthUtil implements IAuthProvider {
273286
}
274287
}
275288

289+
// regardless of state, send message at startup if user needs to select a Developer Profile
290+
void this.tryNotifySelectDeveloperProfile()
291+
276292
vsCodeState.isFreeTierLimitReached = false
277293
await this.setVscodeContextProps(state)
278294
await Promise.all([
@@ -285,6 +301,12 @@ export class AuthUtil implements IAuthProvider {
285301
}
286302
}
287303

304+
private tryNotifySelectDeveloperProfile = once(async () => {
305+
if (this.regionProfileManager.requireProfileSelection() && this.startedConnected) {
306+
await notifySelectDeveloperProfile()
307+
}
308+
})
309+
288310
async getTelemetryMetadata(): Promise<TelemetryMetadata> {
289311
if (!this.isConnected()) {
290312
return {

0 commit comments

Comments
 (0)