Skip to content

Commit bab1a70

Browse files
authored
fix(amazonq): make cross-validating async
## Problem https://quip-amazon.com/MOnPAkOTTGzQ/Amazon-Q-Developer-Bug-Bash-for-FRA#temp:C:aQWe6702ee5b8e6421785dd3e9eb Profile selection page briefly appears on startup before defaulting to a profile then going to chat. Ideally it is not shown at all ## Solution because on project startup, before plugin finish "validating" selected profile, `AuthUtils.requireProfileSeleciton` will return `true` therefore the webview page will briefly appear and be gone when the plugin receives API response.
1 parent 9f1ab71 commit bab1a70

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

packages/amazonq/test/unit/codewhisperer/region/regionProfileManager.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,13 @@ describe('RegionProfileManager', function () {
173173

174174
await sut.persistSelectRegionProfile()
175175

176-
const state = globals.globalState.tryGet<{ [label: string]: string }>(
176+
const state = globals.globalState.tryGet<{ [label: string]: RegionProfile }>(
177177
'aws.amazonq.regionProfiles',
178178
Object,
179179
{}
180180
)
181181

182-
assert.strictEqual(state[conn.id], profileFoo.arn)
182+
assert.strictEqual(state[conn.id], profileFoo)
183183
})
184184

185185
it(`restoreRegionProfile`, async function () {
@@ -191,7 +191,7 @@ describe('RegionProfileManager', function () {
191191
}
192192

193193
const state = {} as any
194-
state[conn.id] = profileFoo.arn
194+
state[conn.id] = profileFoo
195195

196196
await globals.globalState.update('aws.amazonq.regionProfiles', state)
197197

@@ -212,19 +212,19 @@ describe('RegionProfileManager', function () {
212212
fail('connection should not be undefined')
213213
}
214214
await sut.persistSelectRegionProfile()
215-
const state = globals.globalState.tryGet<{ [label: string]: string }>(
215+
const state = globals.globalState.tryGet<{ [label: string]: RegionProfile }>(
216216
'aws.amazonq.regionProfiles',
217217
Object,
218218
{}
219219
)
220-
assert.strictEqual(state[conn.id], profileFoo.arn)
220+
assert.strictEqual(state[conn.id], profileFoo)
221221

222222
// subject to test
223223
await sut.invalidateProfile(profileFoo.arn)
224224

225225
// assertion
226226
assert.strictEqual(sut.activeRegionProfile, undefined)
227-
const actualGlobalState = globals.globalState.tryGet<{ [label: string]: string }>(
227+
const actualGlobalState = globals.globalState.tryGet<{ [label: string]: RegionProfile }>(
228228
'aws.amazonq.regionProfiles',
229229
Object,
230230
{}

packages/core/src/codewhisperer/region/regionProfileManager.ts

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -235,36 +235,37 @@ export class RegionProfileManager {
235235
return
236236
}
237237
// cross-validation
238-
let profiles: RegionProfile[] = []
239-
try {
240-
profiles = await this.listRegionProfile()
241-
} catch (e) {
242-
telemetry.amazonq_profileState.emit({
243-
source: 'reload',
244-
amazonQProfileRegion: 'not-set',
245-
reason: (e as Error).message,
246-
result: 'Failed',
247-
})
248-
249-
return
250-
}
238+
this.listRegionProfile()
239+
.then(async (profiles) => {
240+
const r = profiles.find((it) => it.arn === previousSelected.arn)
241+
if (!r) {
242+
telemetry.amazonq_profileState.emit({
243+
source: 'reload',
244+
amazonQProfileRegion: 'not-set',
245+
reason: 'profile could not be selected',
246+
result: 'Failed',
247+
})
251248

252-
const r = profiles.find((it) => it.arn === previousSelected)
253-
if (!r) {
254-
telemetry.amazonq_profileState.emit({
255-
source: 'reload',
256-
amazonQProfileRegion: 'not-set',
257-
reason: 'profile could not be selected',
258-
result: 'Failed',
249+
await this.invalidateProfile(previousSelected.arn)
250+
RegionProfileManager.logger.warn(
251+
`invlaidating ${previousSelected.name} profile, arn=${previousSelected.arn}`
252+
)
253+
}
254+
})
255+
.catch((e) => {
256+
telemetry.amazonq_profileState.emit({
257+
source: 'reload',
258+
amazonQProfileRegion: 'not-set',
259+
reason: (e as Error).message,
260+
result: 'Failed',
261+
})
259262
})
260-
return
261-
}
262263

263-
await this.switchRegionProfile(r, 'reload')
264+
await this.switchRegionProfile(previousSelected, 'reload')
264265
}
265266

266-
private loadPersistedRegionProfle(): { [label: string]: string } {
267-
const previousPersistedState = globals.globalState.tryGet<{ [label: string]: string }>(
267+
private loadPersistedRegionProfle(): { [label: string]: RegionProfile } {
268+
const previousPersistedState = globals.globalState.tryGet<{ [label: string]: RegionProfile }>(
268269
'aws.amazonq.regionProfiles',
269270
Object,
270271
{}
@@ -282,13 +283,13 @@ export class RegionProfileManager {
282283
}
283284

284285
// persist connectionId to profileArn
285-
const previousPersistedState = globals.globalState.tryGet<{ [label: string]: string }>(
286+
const previousPersistedState = globals.globalState.tryGet<{ [label: string]: RegionProfile }>(
286287
'aws.amazonq.regionProfiles',
287288
Object,
288289
{}
289290
)
290291

291-
previousPersistedState[conn.id] = this.activeRegionProfile.arn
292+
previousPersistedState[conn.id] = this.activeRegionProfile
292293
await globals.globalState.update('aws.amazonq.regionProfiles', previousPersistedState)
293294
}
294295

@@ -337,7 +338,7 @@ export class RegionProfileManager {
337338

338339
const profiles = this.loadPersistedRegionProfle()
339340
const updatedProfiles = Object.fromEntries(
340-
Object.entries(profiles).filter(([connId, profileArn]) => profileArn !== arn)
341+
Object.entries(profiles).filter(([connId, profile]) => profile.arn !== arn)
341342
)
342343
await globals.globalState.update('aws.amazonq.regionProfiles', updatedProfiles)
343344
}

0 commit comments

Comments
 (0)