From f5cf3bde1d47dac4c18c405a872385c0a6530fef Mon Sep 17 00:00:00 2001 From: Ashish Reddy Podduturi Date: Tue, 24 Jun 2025 17:15:43 -0700 Subject: [PATCH 1/2] fix(amazonq): fix for amazon q app initialization failure on sagemaker --- .../Bug Fix-sagemaker-al2-support.json | 4 ++ packages/amazonq/src/lsp/client.ts | 43 ++++++++++++++++--- .../core/src/shared/extensionUtilities.ts | 19 ++++++++ packages/core/src/shared/vscode/env.ts | 32 +++++++++++++- 4 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 packages/amazonq/.changes/next-release/Bug Fix-sagemaker-al2-support.json diff --git a/packages/amazonq/.changes/next-release/Bug Fix-sagemaker-al2-support.json b/packages/amazonq/.changes/next-release/Bug Fix-sagemaker-al2-support.json new file mode 100644 index 00000000000..9f15457f59e --- /dev/null +++ b/packages/amazonq/.changes/next-release/Bug Fix-sagemaker-al2-support.json @@ -0,0 +1,4 @@ +{ + "type": "Bug Fix", + "description": "Improved Amazon Linux 2 support with better SageMaker environment detection" +} diff --git a/packages/amazonq/src/lsp/client.ts b/packages/amazonq/src/lsp/client.ts index c359ac73ded..6cbb05dd582 100644 --- a/packages/amazonq/src/lsp/client.ts +++ b/packages/amazonq/src/lsp/client.ts @@ -38,6 +38,7 @@ import { isAmazonLinux2, getClientId, extensionVersion, + isSageMaker, } from 'aws-core-vscode/shared' import { processUtils } from 'aws-core-vscode/shared' import { activate } from './chat/activation' @@ -53,11 +54,24 @@ import { InlineChatTutorialAnnotation } from '../app/inline/tutorials/inlineChat const localize = nls.loadMessageBundle() const logger = getLogger('amazonqLsp.lspClient') -export const glibcLinker: string = process.env.VSCODE_SERVER_CUSTOM_GLIBC_LINKER || '' -export const glibcPath: string = process.env.VSCODE_SERVER_CUSTOM_GLIBC_PATH || '' - export function hasGlibcPatch(): boolean { - return glibcLinker.length > 0 && glibcPath.length > 0 + // Skip GLIBC patching for SageMaker environments + if (isSageMaker()) { + getLogger('amazonqLsp').info('SageMaker environment detected in hasGlibcPatch, skipping GLIBC patching') + return false // Return false to ensure SageMaker doesn't try to use GLIBC patching + } + + // Check for environment variables (for CDM) + const glibcLinker = process.env.VSCODE_SERVER_CUSTOM_GLIBC_LINKER || '' + const glibcPath = process.env.VSCODE_SERVER_CUSTOM_GLIBC_PATH || '' + + if (glibcLinker.length > 0 && glibcPath.length > 0) { + getLogger('amazonqLsp').info('GLIBC patching environment variables detected') + return true + } + + // No environment variables, no patching needed + return false } export async function startLanguageServer( @@ -82,9 +96,24 @@ export async function startLanguageServer( const traceServerEnabled = Settings.instance.isSet(`${clientId}.trace.server`) let executable: string[] = [] // apply the GLIBC 2.28 path to node js runtime binary - if (isAmazonLinux2() && hasGlibcPatch()) { - executable = [glibcLinker, '--library-path', glibcPath, resourcePaths.node] - getLogger('amazonqLsp').info(`Patched node runtime with GLIBC to ${executable}`) + if (isSageMaker()) { + // SageMaker doesn't need GLIBC patching + getLogger('amazonqLsp').info('SageMaker environment detected, skipping GLIBC patching') + executable = [resourcePaths.node] + } else if (isAmazonLinux2() && hasGlibcPatch()) { + // Use environment variables if available (for CDM) + if (process.env.VSCODE_SERVER_CUSTOM_GLIBC_LINKER && process.env.VSCODE_SERVER_CUSTOM_GLIBC_PATH) { + executable = [ + process.env.VSCODE_SERVER_CUSTOM_GLIBC_LINKER, + '--library-path', + process.env.VSCODE_SERVER_CUSTOM_GLIBC_PATH, + resourcePaths.node, + ] + getLogger('amazonqLsp').info(`Patched node runtime with GLIBC using env vars to ${executable}`) + } else { + // No environment variables, use the node executable directly + executable = [resourcePaths.node] + } } else { executable = [resourcePaths.node] } diff --git a/packages/core/src/shared/extensionUtilities.ts b/packages/core/src/shared/extensionUtilities.ts index 9675f060951..d498d64ea16 100644 --- a/packages/core/src/shared/extensionUtilities.ts +++ b/packages/core/src/shared/extensionUtilities.ts @@ -170,12 +170,31 @@ export function isCloud9(flavor: 'classic' | 'codecatalyst' | 'any' = 'any'): bo return (flavor === 'classic' && !codecat) || (flavor === 'codecatalyst' && codecat) } +/** + * Checks if the current environment has SageMaker-specific environment variables + * @returns true if SageMaker environment variables are detected + */ +function hasSageMakerEnvVars(): boolean { + return ( + process.env.SAGEMAKER_APP_TYPE !== undefined || + process.env.SAGEMAKER_INTERNAL_IMAGE_URI !== undefined || + process.env.STUDIO_LOGGING_DIR?.includes('/var/log/studio') === true + ) +} + /** * * @param appName to identify the proper SM instance * @returns true if the current system is SageMaker(SMAI or SMUS) */ export function isSageMaker(appName: 'SMAI' | 'SMUS' = 'SMAI'): boolean { + // Check for SageMaker-specific environment variables first + if (hasSageMakerEnvVars() || process.env.SERVICE_NAME === sageMakerUnifiedStudio) { + getLogger().debug('SageMaker environment detected via environment variables') + return true + } + + // Fall back to app name checks switch (appName) { case 'SMAI': return vscode.env.appName === sageMakerAppname diff --git a/packages/core/src/shared/vscode/env.ts b/packages/core/src/shared/vscode/env.ts index 02d46ae6695..8f53465c6b5 100644 --- a/packages/core/src/shared/vscode/env.ts +++ b/packages/core/src/shared/vscode/env.ts @@ -125,13 +125,41 @@ export function isRemoteWorkspace(): boolean { } /** - * There is Amazon Linux 2. + * Checks if the current environment is running on Amazon Linux 2. * - * Use {@link isCloudDesktop()} to know if we are specifically using internal Amazon Linux 2. + * This function attempts to detect if we're running in a container on an AL2 host + * by checking both the OS release and container-specific indicators. * * Example: `5.10.220-188.869.amzn2int.x86_64` or `5.10.236-227.928.amzn2.x86_64` (Cloud Dev Machine) */ export function isAmazonLinux2() { + // First check if we're in a SageMaker environment, which should not be treated as AL2 + // even if the underlying host is AL2 + if ( + process.env.SAGEMAKER_APP_TYPE || + process.env.SERVICE_NAME === 'SageMakerUnifiedStudio' || + process.env.SAGEMAKER_INTERNAL_IMAGE_URI + ) { + return false + } + + // Check if we're in a container environment that's not AL2 + if (process.env.container === 'docker' || process.env.DOCKER_HOST || process.env.DOCKER_BUILDKIT) { + // Additional check for container OS - if we can determine it's not AL2 + try { + const fs = require('fs') + if (fs.existsSync('/etc/os-release')) { + const osRelease = fs.readFileSync('/etc/os-release', 'utf8') + if (!osRelease.includes('Amazon Linux 2') && !osRelease.includes('amzn2')) { + return false + } + } + } catch (e) { + // If we can't read the file, fall back to the os.release() check + } + } + + // Standard check for AL2 in the OS release string return (os.release().includes('.amzn2int.') || os.release().includes('.amzn2.')) && process.platform === 'linux' } From 053a5bd440ed3709c2df5d772fba247d0bb781db Mon Sep 17 00:00:00 2001 From: Ashish Reddy Podduturi Date: Wed, 25 Jun 2025 12:13:17 -0700 Subject: [PATCH 2/2] fix(amazonq): fix to move isSagemaker to env --- .../core/src/shared/extensionUtilities.ts | 16 ++---------- packages/core/src/shared/vscode/env.ts | 25 +++++++++++++++---- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/packages/core/src/shared/extensionUtilities.ts b/packages/core/src/shared/extensionUtilities.ts index d498d64ea16..8037dfa0381 100644 --- a/packages/core/src/shared/extensionUtilities.ts +++ b/packages/core/src/shared/extensionUtilities.ts @@ -11,7 +11,7 @@ import { getLogger } from './logger/logger' import { VSCODE_EXTENSION_ID, extensionAlphaVersion } from './extensions' import { Ec2MetadataClient } from './clients/ec2MetadataClient' import { DefaultEc2MetadataClient } from './clients/ec2MetadataClient' -import { extensionVersion, getCodeCatalystDevEnvId } from './vscode/env' +import { extensionVersion, getCodeCatalystDevEnvId, hasSageMakerEnvVars } from './vscode/env' import globals from './extensionGlobals' import { once } from './utilities/functionUtils' import { @@ -170,18 +170,6 @@ export function isCloud9(flavor: 'classic' | 'codecatalyst' | 'any' = 'any'): bo return (flavor === 'classic' && !codecat) || (flavor === 'codecatalyst' && codecat) } -/** - * Checks if the current environment has SageMaker-specific environment variables - * @returns true if SageMaker environment variables are detected - */ -function hasSageMakerEnvVars(): boolean { - return ( - process.env.SAGEMAKER_APP_TYPE !== undefined || - process.env.SAGEMAKER_INTERNAL_IMAGE_URI !== undefined || - process.env.STUDIO_LOGGING_DIR?.includes('/var/log/studio') === true - ) -} - /** * * @param appName to identify the proper SM instance @@ -189,7 +177,7 @@ function hasSageMakerEnvVars(): boolean { */ export function isSageMaker(appName: 'SMAI' | 'SMUS' = 'SMAI'): boolean { // Check for SageMaker-specific environment variables first - if (hasSageMakerEnvVars() || process.env.SERVICE_NAME === sageMakerUnifiedStudio) { + if (hasSageMakerEnvVars()) { getLogger().debug('SageMaker environment detected via environment variables') return true } diff --git a/packages/core/src/shared/vscode/env.ts b/packages/core/src/shared/vscode/env.ts index 8f53465c6b5..5ee891cc7d3 100644 --- a/packages/core/src/shared/vscode/env.ts +++ b/packages/core/src/shared/vscode/env.ts @@ -124,6 +124,25 @@ export function isRemoteWorkspace(): boolean { return vscode.env.remoteName === 'ssh-remote' } +/** + * Checks if the current environment has SageMaker-specific environment variables + * @returns true if SageMaker environment variables are detected + */ +export function hasSageMakerEnvVars(): boolean { + // Check both old and new environment variable names + // SageMaker is renaming their environment variables in their Docker images + return ( + // Original environment variables + process.env.SAGEMAKER_APP_TYPE !== undefined || + process.env.SAGEMAKER_INTERNAL_IMAGE_URI !== undefined || + process.env.STUDIO_LOGGING_DIR?.includes('/var/log/studio') === true || + // New environment variables (update these with the actual new names) + process.env.SM_APP_TYPE !== undefined || + process.env.SM_INTERNAL_IMAGE_URI !== undefined || + process.env.SERVICE_NAME === 'SageMakerUnifiedStudio' + ) +} + /** * Checks if the current environment is running on Amazon Linux 2. * @@ -135,11 +154,7 @@ export function isRemoteWorkspace(): boolean { export function isAmazonLinux2() { // First check if we're in a SageMaker environment, which should not be treated as AL2 // even if the underlying host is AL2 - if ( - process.env.SAGEMAKER_APP_TYPE || - process.env.SERVICE_NAME === 'SageMakerUnifiedStudio' || - process.env.SAGEMAKER_INTERNAL_IMAGE_URI - ) { + if (hasSageMakerEnvVars()) { return false }