Skip to content

Commit 2f39da8

Browse files
authored
fix(toolkit): handle isCn when region is not initialized to fix extension activation failure (#7599)
## Problem With updating Toolkit version to latest in SMUS CodeEditor Spaces, observing Toolkit throws an error as attached in screenshot below with error message ``` Attempted to get compute region without initializing ``` <img width="2039" alt="image" src="https://github.com/user-attachments/assets/a53e8fdc-bf7b-4c96-98fa-78c6db819a72" /> ## Solution - Issue is due to getComputeRegion() invoked first before compute region is initialized - hence solution is to Make `isCn()` resilient to uninitialized state and return a default value - Tested with a local debug artifact in SMUS CodeEditor space, toolkit activation completed and working - <img width="1886" alt="image" src="https://github.com/user-attachments/assets/988c208c-3fe8-4530-a477-3e8704f1f598" /> - Also Tested the changes in standalone VS code application and SMAI compute instance, and toolkit explorer view is seen working as expected - ![image](https://github.com/user-attachments/assets/c8562928-95f9-49e3-bd0a-f1e5dfd92969) - ![image](https://github.com/user-attachments/assets/329f7bed-461c-4833-ba46-7b2fdc068793) - Also tested the vsix in china region cn-north-1, and working as expected - <img width="720" height="393" alt="image" src="https://github.com/user-attachments/assets/b0523567-82ad-4dcb-b218-a935983c3b47" /> --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 9cf9bdd commit 2f39da8

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

packages/core/src/shared/extensionUtilities.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,17 @@ export function isSageMaker(appName: 'SMAI' | 'SMUS' = 'SMAI'): boolean {
194194
}
195195

196196
export function isCn(): boolean {
197-
return getComputeRegion()?.startsWith('cn') ?? false
197+
try {
198+
const region = getComputeRegion()
199+
if (!region || region === 'notInitialized') {
200+
getLogger().debug('isCn called before compute region initialized, defaulting to false')
201+
return false
202+
}
203+
return region.startsWith('cn')
204+
} catch (err) {
205+
getLogger().error(`Error in isCn method: ${err}`)
206+
return false
207+
}
198208
}
199209

200210
/**

packages/core/src/test/shared/extensionUtilities.test.ts

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { AWSError } from 'aws-sdk'
99
import * as sinon from 'sinon'
1010
import { DefaultEc2MetadataClient } from '../../shared/clients/ec2MetadataClient'
1111
import * as vscode from 'vscode'
12-
import { UserActivity, getComputeRegion, initializeComputeRegion } from '../../shared/extensionUtilities'
12+
import { UserActivity, getComputeRegion, initializeComputeRegion, isCn } from '../../shared/extensionUtilities'
1313
import { isDifferentVersion, setMostRecentVersion } from '../../shared/extensionUtilities'
1414
import { InstanceIdentity } from '../../shared/clients/ec2MetadataClient'
1515
import { extensionVersion } from '../../shared/vscode/env'
@@ -135,6 +135,73 @@ describe('initializeComputeRegion, getComputeRegion', async function () {
135135
})
136136
})
137137

138+
describe('isCn', function () {
139+
let sandbox: sinon.SinonSandbox
140+
const metadataService = new DefaultEc2MetadataClient()
141+
142+
beforeEach(function () {
143+
sandbox = sinon.createSandbox()
144+
})
145+
146+
afterEach(function () {
147+
sandbox.restore()
148+
})
149+
150+
it('returns false when compute region is not defined', async function () {
151+
// Reset the compute region to undefined first
152+
const utils = require('../../shared/extensionUtilities')
153+
Object.defineProperty(utils, 'computeRegion', {
154+
value: undefined,
155+
configurable: true,
156+
})
157+
158+
const result = isCn()
159+
160+
assert.strictEqual(result, false, 'isCn() should return false when compute region is undefined')
161+
})
162+
163+
it('returns false when compute region is not initialized', async function () {
164+
// Set the compute region to "notInitialized"
165+
const utils = require('../../shared/extensionUtilities')
166+
Object.defineProperty(utils, 'computeRegion', {
167+
value: 'notInitialized',
168+
configurable: true,
169+
})
170+
171+
const result = isCn()
172+
173+
assert.strictEqual(result, false, 'isCn() should return false when compute region is notInitialized')
174+
})
175+
176+
it('returns true for CN regions', async function () {
177+
sandbox.stub(metadataService, 'getInstanceIdentity').resolves({ region: 'cn-north-1' })
178+
await initializeComputeRegion(metadataService, false, true)
179+
180+
const result = isCn()
181+
182+
assert.strictEqual(result, true, 'isCn() should return true for China regions')
183+
})
184+
185+
it('returns false for non-CN regions', async function () {
186+
sandbox.stub(metadataService, 'getInstanceIdentity').resolves({ region: 'us-east-1' })
187+
await initializeComputeRegion(metadataService, false, true)
188+
189+
const result = isCn()
190+
191+
assert.strictEqual(result, false, 'isCn() should return false for non-China regions')
192+
})
193+
194+
it('returns false when an error occurs', async function () {
195+
const utils = require('../../shared/extensionUtilities')
196+
197+
sandbox.stub(utils, 'getComputeRegion').throws(new Error('Test error'))
198+
199+
const result = isCn()
200+
201+
assert.strictEqual(result, false, 'isCn() should return false when an error occurs')
202+
})
203+
})
204+
138205
describe('UserActivity', function () {
139206
let count: number
140207
let sandbox: sinon.SinonSandbox

0 commit comments

Comments
 (0)