66import * as semver from 'semver'
77import * as vscode from 'vscode'
88import * as packageJson from '../../../package.json'
9- import * as os from 'os'
109import { getLogger } from '../logger/logger'
1110import { onceChanged } from '../utilities/functionUtils'
1211import { ChildProcess } from '../utilities/processUtils'
@@ -175,33 +174,31 @@ export function hasSageMakerEnvVars(): boolean {
175174/**
176175 * Checks if the current environment is running on Amazon Linux 2.
177176 *
178- * This function detects if we're actually running on AL2 , not just if the host is AL2 .
179- * In containerized environments, we check the container's OS, not the host's .
177+ * This function detects the container/runtime OS , not the host OS .
178+ * In containerized environments, we check the container's OS identity .
180179 *
181180 * Detection Process (in order):
182181 * 1. Returns false for web environments (browser-based)
183- * 2. Returns false for SageMaker environments (even if host is AL2)
182+ * 2. Returns false for SageMaker environments (even if container is AL2)
184183 * 3. Checks `/etc/os-release` with fallback to `/usr/lib/os-release`
185- * - Standard Linux OS identification files
186- * - Explicitly checks for and rejects Amazon Linux 2023
184+ * - Standard Linux OS identification files per freedesktop.org spec
187185 * - Looks for `ID="amzn"` and `VERSION_ID="2"` for AL2
188- * 4. Falls back to kernel version check as last resort
189- * - Checks for `.amzn2.` or `.amzn2int.` in kernel release
190- * - Only used if file-based detection fails or confirms AL2
186+ * - This correctly identifies AL2 containers regardless of host OS
191187 *
192188 * This approach ensures correct detection in:
193189 * - Containerized environments (detects container OS, not host)
190+ * - AL2 containers on any host OS (Ubuntu, AL2023, etc.)
194191 * - Web/browser environments (returns false)
195- * - Amazon Linux 2023 systems (properly distinguished from AL2)
196192 * - SageMaker environments (returns false)
197193 *
194+ * Note: We intentionally do NOT check kernel version as it reflects the host OS,
195+ * not the container OS. AL2 containers should be treated as AL2 environments
196+ * regardless of whether they run on AL2, Ubuntu, or other host kernels.
197+ *
198198 * References:
199199 * - https://docs.aws.amazon.com/linux/al2/ug/ident-amazon-linux-specific.html
200200 * - https://docs.aws.amazon.com/linux/al2/ug/ident-os-release.html
201- *
202- * Example kernel versions:
203- * - `5.10.220-188.869.amzn2int.x86_64` (internal AL2)
204- * - `5.10.236-227.928.amzn2.x86_64` (Cloud Dev Machine)
201+ * - https://www.freedesktop.org/software/systemd/man/latest/os-release.html
205202 */
206203export function isAmazonLinux2 ( ) {
207204 // Skip AL2 detection for web environments
@@ -211,7 +208,7 @@ export function isAmazonLinux2() {
211208 }
212209
213210 // First check if we're in a SageMaker environment, which should not be treated as AL2
214- // even if the underlying host is AL2
211+ // even if the underlying container is AL2
215212 if ( hasSageMakerEnvVars ( ) ) {
216213 return false
217214 }
@@ -221,54 +218,36 @@ export function isAmazonLinux2() {
221218 return false
222219 }
223220
224- // For containerized environments, check the actual container OS
225- // not the host kernel version
221+ // Check the container/runtime OS identity via os-release files
222+ // This correctly identifies AL2 containers regardless of host OS
226223 try {
227224 const fs = require ( 'fs' )
228- // Check /etc/os-release with fallback to /usr/lib/os-release as per https://docs.aws.amazon.com/linux/al2/ug/ident-os-release.html
225+ // Check /etc/os-release with fallback to /usr/lib/os-release as per freedesktop.org spec
229226 const osReleasePaths = [ '/etc/os-release' , '/usr/lib/os-release' ]
227+
230228 for ( const osReleasePath of osReleasePaths ) {
231229 if ( fs . existsSync ( osReleasePath ) ) {
232230 try {
233231 const osReleaseContent = fs . readFileSync ( osReleasePath , 'utf8' )
234232 const osRelease = parseOsRelease ( osReleaseContent )
235233
236- // Check if this is Amazon Linux 2023 (not AL2)
237- if ( osRelease . VERSION_ID === '2023' || osRelease . PLATFORM_ID === 'platform:al2023' ) {
238- // This is Amazon Linux 2023, not AL2
239- return false
240- }
241-
242- // Check if this is actually Amazon Linux 2
243- // Must be specifically version 2, not 2023 or other versions
244- const isAL2 = osRelease . ID === 'amzn' && osRelease . VERSION_ID === '2'
245-
246- // If we found os-release file, trust its content over kernel version
247- if ( ! isAL2 ) {
248- // Explicitly not AL2 based on os-release
249- return false
250- }
251- // If it is AL2 according to os-release, continue to kernel check for confirmation
252- break // Found and processed os-release, no need to check fallback
234+ // Check if this is Amazon Linux 2
235+ // We trust os-release as the authoritative source for container OS identity
236+ return osRelease . VERSION_ID === '2' && osRelease . ID === 'amzn'
253237 } catch ( e ) {
254- // Continue to next path or fallback check
255- getLogger ( ) . error ( `Parsing os-release file failed with error : ${ e } ` )
238+ // Continue to next path if parsing fails
239+ getLogger ( ) . error ( `Parsing os-release file ${ osReleasePath } failed : ${ e } ` )
256240 }
257241 }
258242 }
259243 } catch ( e ) {
260- // If we can't read the files, fall back to the os.release() check
261- // This might happen in some restricted environments
262- getLogger ( ) . error ( `Checking the current environment failed with error: ${ e } ` )
244+ // If we can't read the files, we cannot determine AL2 status
245+ getLogger ( ) . error ( `Checking os-release files failed: ${ e } ` )
263246 }
264247
265- // Check kernel version as a fallback or confirmation
266- // This should only be trusted if we couldn't determine from files above
267- // or if files confirmed it's AL2
268- const kernelRelease = os . release ( )
269- const hasAL2Kernel = kernelRelease . includes ( '.amzn2int.' ) || kernelRelease . includes ( '.amzn2.' )
270-
271- return hasAL2Kernel
248+ // If no os-release files found or all failed to parse, assume not AL2
249+ // We do NOT fall back to kernel version as it reflects host OS, not container OS
250+ return false
272251}
273252
274253/**
0 commit comments