diff --git a/packages/core/src/shared/extensionUtilities.ts b/packages/core/src/shared/extensionUtilities.ts index 9675f060951..bb29ed45238 100644 --- a/packages/core/src/shared/extensionUtilities.ts +++ b/packages/core/src/shared/extensionUtilities.ts @@ -370,9 +370,15 @@ export async function initializeComputeRegion( export function getComputeRegion(): string | undefined { if (computeRegion === notInitialized) { - throw new Error('Attempted to get compute region without initializing.') + // Set a default value immediately to prevent future calls from throwing error + const isC9 = isCloud9() + const isSM = isSageMaker() + computeRegion = isC9 || isSM ? 'unknown' : undefined + // Start async initialization in the background + initializeComputeRegion(undefined, isC9, isSM).catch((e) => { + getLogger().error('Failed to initialize compute region: %s and using a default value: %s', e, computeRegion) + }) } - return computeRegion } diff --git a/packages/core/src/test/shared/extensionUtilities.test.ts b/packages/core/src/test/shared/extensionUtilities.test.ts index 0fc846f54ab..8ca4f23a3c0 100644 --- a/packages/core/src/test/shared/extensionUtilities.test.ts +++ b/packages/core/src/test/shared/extensionUtilities.test.ts @@ -72,13 +72,10 @@ describe('initializeComputeRegion, getComputeRegion', async function () { sandbox.restore() }) - it('throws if the region has not been set', async function () { - // not quite a pure test: we call activate during the test load so this value will always be set - // manually hack in the notInitialized value to trigger the error - sandbox.stub(metadataService, 'getInstanceIdentity').resolves({ region: 'notInitialized' }) - - await initializeComputeRegion(metadataService, true) - assert.throws(getComputeRegion) + it('returns default value when region is not initialized', function () { + // Test the auto-initialization behavior when getComputeRegion is called before initialization + const result = getComputeRegion() + assert.ok(result === 'unknown' || result === undefined) }) it('returns a compute region', async function () {