Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/core/src/shared/extensionUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By removing this, you are hiding a problem.

The error exists to ensure that order-of-operations is correct. After your change, that guarantee will no longer be possible.

It seems convenient now, but is a short-term solution with long-term consequences.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the question, though the Error is removed in terms of order-of-operations the above implementation ensures initializeComputeRegion is being called within getComputeRegion to handle case where user may not set it correctly. Also default region being set follows same logic from initializeComputeRegion

  • curious why not handle initialize region within get call above?

// 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
}

Expand Down
11 changes: 4 additions & 7 deletions packages/core/src/test/shared/extensionUtilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
Loading