Skip to content

Commit 30516e3

Browse files
committed
fix: isAmazonLinux2 incorrectly identifies regular linux/ubuntu systems in web as AL2
1 parent 42e38c6 commit 30516e3

File tree

2 files changed

+56
-19
lines changed

2 files changed

+56
-19
lines changed

packages/core/src/shared/vscode/env.ts

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -146,36 +146,56 @@ export function hasSageMakerEnvVars(): boolean {
146146
/**
147147
* Checks if the current environment is running on Amazon Linux 2.
148148
*
149-
* This function attempts to detect if we're running in a container on an AL2 host
150-
* by checking both the OS release and container-specific indicators.
149+
* This function detects if we're actually running on AL2, not just if the host is AL2.
150+
* In containerized environments, we check the container's OS, not the host's.
151151
*
152152
* Example: `5.10.220-188.869.amzn2int.x86_64` or `5.10.236-227.928.amzn2.x86_64` (Cloud Dev Machine)
153153
*/
154154
export function isAmazonLinux2() {
155+
// Skip AL2 detection for web environments
156+
// In web mode, we're running in a browser, not on AL2
157+
if (isWeb()) {
158+
return false
159+
}
160+
155161
// First check if we're in a SageMaker environment, which should not be treated as AL2
156162
// even if the underlying host is AL2
157163
if (hasSageMakerEnvVars()) {
158164
return false
159165
}
160166

161-
// Check if we're in a container environment that's not AL2
162-
if (process.env.container === 'docker' || process.env.DOCKER_HOST || process.env.DOCKER_BUILDKIT) {
163-
// Additional check for container OS - if we can determine it's not AL2
164-
try {
165-
const fs = require('fs')
166-
if (fs.existsSync('/etc/os-release')) {
167-
const osRelease = fs.readFileSync('/etc/os-release', 'utf8')
168-
if (!osRelease.includes('Amazon Linux 2') && !osRelease.includes('amzn2')) {
169-
return false
170-
}
167+
// For containerized environments, check the actual container OS
168+
// not the host kernel version
169+
try {
170+
const fs = require('fs')
171+
if (fs.existsSync('/etc/os-release')) {
172+
const osRelease = fs.readFileSync('/etc/os-release', 'utf8')
173+
// Check if this is actually Amazon Linux 2
174+
const isAL2 =
175+
osRelease.includes('Amazon Linux 2') ||
176+
(osRelease.includes('ID="amzn"') && osRelease.includes('VERSION_ID="2"'))
177+
178+
// If we found os-release file, trust its content over kernel version
179+
if (!isAL2) {
180+
// Explicitly not AL2 based on os-release
181+
return false
171182
}
172-
} catch (e) {
173-
// If we can't read the file, fall back to the os.release() check
183+
// If it is AL2 according to os-release, continue to kernel check for confirmation
174184
}
185+
} catch (e) {
186+
// If we can't read the file, fall back to the os.release() check
187+
// This might happen in some restricted environments
188+
getLogger().error(`Checking the current environment failed with error: ${e}`)
175189
}
176190

177-
// Standard check for AL2 in the OS release string
178-
return (os.release().includes('.amzn2int.') || os.release().includes('.amzn2.')) && process.platform === 'linux'
191+
// Check kernel version as a fallback or confirmation
192+
// This should only be trusted if we couldn't determine from /etc/os-release
193+
// or if /etc/os-release confirmed it's AL2
194+
const kernelRelease = os.release()
195+
const hasAL2Kernel =
196+
(kernelRelease.includes('.amzn2int.') || kernelRelease.includes('.amzn2.')) && process.platform === 'linux'
197+
198+
return hasAL2Kernel
179199
}
180200

181201
/**
@@ -217,9 +237,9 @@ export function getExtRuntimeContext(): {
217237
extensionHost: ExtensionHostLocation
218238
} {
219239
const extensionHost =
220-
// taken from https://github.com/microsoft/vscode/blob/7c9e4bb23992c63f20cd86bbe7a52a3aa4bed89d/extensions/github-authentication/src/githubServer.ts#L121 to help determine which auth flows
221-
// should be used
222-
typeof navigator === 'undefined'
240+
// Check if we're in a Node.js environment (desktop/remote) vs web worker
241+
// Updated to be compatible with Node.js v22 which includes navigator global
242+
typeof process === 'object' && process.versions?.node
223243
? globals.context.extension.extensionKind === vscode.ExtensionKind.UI
224244
? 'local'
225245
: 'remote'

packages/core/src/test/shared/vscode/env.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import path from 'path'
88
import { isCloudDesktop, getEnvVars, getServiceEnvVarConfig, isAmazonLinux2, isBeta } from '../../../shared/vscode/env'
99
import { ChildProcess } from '../../../shared/utilities/processUtils'
1010
import * as sinon from 'sinon'
11+
import * as nodeFs from 'fs' // eslint-disable-line no-restricted-imports
1112
import os from 'os'
1213
import fs from '../../../shared/fs/fs'
1314
import vscode from 'vscode'
@@ -100,13 +101,29 @@ describe('env', function () {
100101
it('isAmazonLinux2', function () {
101102
sandbox.stub(process, 'platform').value('linux')
102103
const versionStub = stubOsVersion('5.10.220-188.869.amzn2int.x86_64')
104+
105+
// Test with actual AL2 kernel
103106
assert.strictEqual(isAmazonLinux2(), true)
104107

105108
versionStub.returns('5.10.236-227.928.amzn2.x86_64')
106109
assert.strictEqual(isAmazonLinux2(), true)
107110

108111
versionStub.returns('5.10.220-188.869.NOT_INTERNAL.x86_64')
109112
assert.strictEqual(isAmazonLinux2(), false)
113+
114+
// Test with container environment (Ubuntu container on AL2 host)
115+
versionStub.returns('5.10.236-227.928.amzn2.x86_64')
116+
const existsStub = sandbox.stub(nodeFs, 'existsSync').returns(true)
117+
const readFileStub = sandbox.stub(nodeFs, 'readFileSync').returns('ID="ubuntu"\nVERSION_ID="20.04"')
118+
assert.strictEqual(isAmazonLinux2(), false, 'Should return false for Ubuntu container on AL2 host')
119+
120+
// Test with actual AL2 in /etc/os-release
121+
readFileStub.returns('ID="amzn"\nVERSION_ID="2"')
122+
assert.strictEqual(isAmazonLinux2(), true, 'Should return true for actual AL2')
123+
124+
// Clean up stubs
125+
existsStub.restore()
126+
readFileStub.restore()
110127
})
111128

112129
it('isCloudDesktop', async function () {

0 commit comments

Comments
 (0)