diff --git a/packages/amazonq/.changes/next-release/Feature-d46a67ff-b237-46cc-b6e7-8de8f2e87f45.json b/packages/amazonq/.changes/next-release/Feature-d46a67ff-b237-46cc-b6e7-8de8f2e87f45.json new file mode 100644 index 00000000000..8028e402f9f --- /dev/null +++ b/packages/amazonq/.changes/next-release/Feature-d46a67ff-b237-46cc-b6e7-8de8f2e87f45.json @@ -0,0 +1,4 @@ +{ + "type": "Feature", + "description": "/transform: run all builds client-side" +} diff --git a/packages/amazonq/test/e2e/amazonq/transformByQ.test.ts b/packages/amazonq/test/e2e/amazonq/transformByQ.test.ts index 4493a7c2387..7a9273a1e84 100644 --- a/packages/amazonq/test/e2e/amazonq/transformByQ.test.ts +++ b/packages/amazonq/test/e2e/amazonq/transformByQ.test.ts @@ -129,8 +129,6 @@ describe('Amazon Q Code Transformation', function () { waitIntervalInMs: 1000, }) - // TO-DO: add this back when releasing CSB - /* const customDependencyVersionPrompt = tab.getChatItems().pop() assert.strictEqual( customDependencyVersionPrompt?.body?.includes('You can optionally upload a YAML file'), @@ -139,11 +137,10 @@ describe('Amazon Q Code Transformation', function () { tab.clickCustomFormButton({ id: 'gumbyTransformFormContinue' }) // 2 additional chat messages get sent after Continue button clicked; wait for both of them - await tab.waitForEvent(() => tab.getChatItems().length > 13, { + await tab.waitForEvent(() => tab.getChatItems().length > 10, { waitTimeoutInMs: 5000, waitIntervalInMs: 1000, }) - */ const sourceJdkPathPrompt = tab.getChatItems().pop() assert.strictEqual(sourceJdkPathPrompt?.body?.includes('Enter the path to JDK 8'), true) @@ -151,7 +148,7 @@ describe('Amazon Q Code Transformation', function () { tab.addChatMessage({ prompt: '/dummy/path/to/jdk8' }) // 2 additional chat messages get sent after JDK path submitted; wait for both of them - await tab.waitForEvent(() => tab.getChatItems().length > 10, { + await tab.waitForEvent(() => tab.getChatItems().length > 12, { waitTimeoutInMs: 5000, waitIntervalInMs: 1000, }) @@ -173,7 +170,7 @@ describe('Amazon Q Code Transformation', function () { text: 'View summary', }) - await tab.waitForEvent(() => tab.getChatItems().length > 11, { + await tab.waitForEvent(() => tab.getChatItems().length > 13, { waitTimeoutInMs: 5000, waitIntervalInMs: 1000, }) diff --git a/packages/core/resources/amazonQCT/QCT-Maven-6-16.jar b/packages/core/resources/amazonQCT/QCT-Maven-6-16.jar new file mode 100644 index 00000000000..bdc734b4d7b Binary files /dev/null and b/packages/core/resources/amazonQCT/QCT-Maven-6-16.jar differ diff --git a/packages/core/src/amazonqGumby/chat/controller/controller.ts b/packages/core/src/amazonqGumby/chat/controller/controller.ts index 57367143cd4..3b40ad9882f 100644 --- a/packages/core/src/amazonqGumby/chat/controller/controller.ts +++ b/packages/core/src/amazonqGumby/chat/controller/controller.ts @@ -41,13 +41,9 @@ import { } from '../../errors' import * as CodeWhispererConstants from '../../../codewhisperer/models/constants' import MessengerUtils, { ButtonActions, GumbyCommands } from './messenger/messengerUtils' -import { CancelActionPositions, JDKToTelemetryValue, telemetryUndefined } from '../../telemetry/codeTransformTelemetry' +import { CancelActionPositions } from '../../telemetry/codeTransformTelemetry' import { openUrl } from '../../../shared/utilities/vsCodeUtils' -import { - telemetry, - CodeTransformJavaTargetVersionsAllowed, - CodeTransformJavaSourceVersionsAllowed, -} from '../../../shared/telemetry/telemetry' +import { telemetry } from '../../../shared/telemetry/telemetry' import { CodeTransformTelemetryState } from '../../telemetry/codeTransformTelemetryState' import DependencyVersions from '../../models/dependencies' import { getStringHash } from '../../../shared/utilities/textUtilities' @@ -308,7 +304,6 @@ export class GumbyController { } private async validateLanguageUpgradeProjects(message: any) { - let telemetryJavaVersion = JDKToTelemetryValue(JDKVersion.UNSUPPORTED) as CodeTransformJavaSourceVersionsAllowed try { const validProjects = await telemetry.codeTransform_validateProject.run(async () => { telemetry.record({ @@ -317,12 +312,6 @@ export class GumbyController { }) const validProjects = await getValidLanguageUpgradeCandidateProjects() - if (validProjects.length > 0) { - // validProjects[0].JDKVersion will be undefined if javap errors out or no .class files found, so call it UNSUPPORTED - const javaVersion = validProjects[0].JDKVersion ?? JDKVersion.UNSUPPORTED - telemetryJavaVersion = JDKToTelemetryValue(javaVersion) as CodeTransformJavaSourceVersionsAllowed - } - telemetry.record({ codeTransformLocalJavaVersion: telemetryJavaVersion }) return validProjects }) return validProjects @@ -384,7 +373,7 @@ export class GumbyController { break case ButtonActions.CONTINUE_TRANSFORMATION_FORM: this.messenger.sendMessage( - CodeWhispererConstants.continueWithoutYamlMessage, + CodeWhispererConstants.continueWithoutConfigFileMessage, message.tabID, 'ai-prompt' ) @@ -437,9 +426,7 @@ export class GumbyController { userChoice: skipTestsSelection, }) this.messenger.sendSkipTestsSelectionMessage(skipTestsSelection, message.tabID) - this.promptJavaHome('source', message.tabID) - // TO-DO: delete line above and uncomment line below when releasing CSB - // await this.messenger.sendCustomDependencyVersionMessage(message.tabID) + await this.messenger.sendCustomDependencyVersionMessage(message.tabID) }) } @@ -465,16 +452,9 @@ export class GumbyController { const fromJDKVersion: JDKVersion = message.formSelectedValues['GumbyTransformJdkFromForm'] telemetry.record({ - // TODO: remove JavaSource/TargetVersionsAllowed when BI is updated to use source/target - codeTransformJavaSourceVersionsAllowed: JDKToTelemetryValue( - fromJDKVersion - ) as CodeTransformJavaSourceVersionsAllowed, - codeTransformJavaTargetVersionsAllowed: JDKToTelemetryValue( - toJDKVersion - ) as CodeTransformJavaTargetVersionsAllowed, source: fromJDKVersion, target: toJDKVersion, - codeTransformProjectId: pathToProject === undefined ? telemetryUndefined : getStringHash(pathToProject), + codeTransformProjectId: pathToProject === undefined ? undefined : getStringHash(pathToProject), userChoice: 'Confirm-Java', }) @@ -503,7 +483,7 @@ export class GumbyController { const schema: string = message.formSelectedValues['GumbyTransformSQLSchemaForm'] telemetry.record({ - codeTransformProjectId: pathToProject === undefined ? telemetryUndefined : getStringHash(pathToProject), + codeTransformProjectId: pathToProject === undefined ? undefined : getStringHash(pathToProject), source: transformByQState.getSourceDB(), target: transformByQState.getTargetDB(), userChoice: 'Confirm-SQL', @@ -563,7 +543,7 @@ export class GumbyController { canSelectMany: false, openLabel: 'Select', filters: { - 'YAML file': ['yaml'], // restrict user to only pick a .yaml file + File: ['yaml', 'yml'], // restrict user to only pick a .yaml file }, }) if (!fileUri || fileUri.length === 0) { @@ -576,7 +556,7 @@ export class GumbyController { this.messenger.sendUnrecoverableErrorResponse('invalid-custom-versions-file', message.tabID) return } - this.messenger.sendMessage('Received custom dependency version YAML file.', message.tabID, 'ai-prompt') + this.messenger.sendMessage(CodeWhispererConstants.receivedValidConfigFileMessage, message.tabID, 'ai-prompt') transformByQState.setCustomDependencyVersionFilePath(fileUri[0].fsPath) this.promptJavaHome('source', message.tabID) } @@ -660,17 +640,13 @@ export class GumbyController { const pathToJavaHome = extractPath(data.message) if (pathToJavaHome) { transformByQState.setSourceJavaHome(pathToJavaHome) - // TO-DO: delete line below and uncomment the block below when releasing CSB - await this.prepareLanguageUpgradeProject(data.tabID) // if source and target JDK versions are the same, just re-use the source JAVA_HOME and start the build - /* if (transformByQState.getTargetJDKVersion() === transformByQState.getSourceJDKVersion()) { transformByQState.setTargetJavaHome(pathToJavaHome) await this.prepareLanguageUpgradeProject(data.tabID) } else { this.promptJavaHome('target', data.tabID) } - */ } else { this.messenger.sendUnrecoverableErrorResponse('invalid-java-home', data.tabID) } diff --git a/packages/core/src/amazonqGumby/chat/controller/messenger/messenger.ts b/packages/core/src/amazonqGumby/chat/controller/messenger/messenger.ts index 5265cb5b888..0880e2556d9 100644 --- a/packages/core/src/amazonqGumby/chat/controller/messenger/messenger.ts +++ b/packages/core/src/amazonqGumby/chat/controller/messenger/messenger.ts @@ -410,7 +410,7 @@ export class Messenger { message = CodeWhispererConstants.noPomXmlFoundChatMessage break case 'could-not-compile-project': - message = CodeWhispererConstants.cleanInstallErrorChatMessage + message = CodeWhispererConstants.cleanTestCompileErrorChatMessage break case 'invalid-java-home': message = CodeWhispererConstants.noJavaHomeFoundChatMessage @@ -704,7 +704,7 @@ ${codeSnippet} } public async sendCustomDependencyVersionMessage(tabID: string) { - const message = CodeWhispererConstants.chooseYamlMessage + const message = CodeWhispererConstants.chooseConfigFileMessage const buttons: ChatItemButton[] = [] buttons.push({ @@ -731,7 +731,7 @@ ${codeSnippet} tabID ) ) - const sampleYAML = `name: "custom-dependency-management" + const sampleYAML = `name: "dependency-upgrade" description: "Custom dependency version management for Java migration from JDK 8/11/17 to JDK 17/21" dependencyManagement: @@ -744,7 +744,7 @@ dependencyManagement: targetVersion: "3.0.0" originType: "THIRD_PARTY" plugins: - - identifier: "com.example.plugin" + - identifier: "com.example:plugin" targetVersion: "1.2.0" versionProperty: "plugin.version" # Optional` diff --git a/packages/core/src/amazonqGumby/errors.ts b/packages/core/src/amazonqGumby/errors.ts index d6805159569..c77bbcfc4bd 100644 --- a/packages/core/src/amazonqGumby/errors.ts +++ b/packages/core/src/amazonqGumby/errors.ts @@ -30,12 +30,6 @@ export class NoMavenJavaProjectsFoundError extends ToolkitError { } } -export class ZipExceedsSizeLimitError extends ToolkitError { - constructor() { - super('Zip file exceeds size limit', { code: 'ZipFileExceedsSizeLimit' }) - } -} - export class AlternateDependencyVersionsNotFoundError extends Error { constructor() { super('No available versions for dependency update') diff --git a/packages/core/src/codewhisperer/client/codewhisperer.ts b/packages/core/src/codewhisperer/client/codewhisperer.ts index 35f699b24c2..051254d1873 100644 --- a/packages/core/src/codewhisperer/client/codewhisperer.ts +++ b/packages/core/src/codewhisperer/client/codewhisperer.ts @@ -262,7 +262,7 @@ export class DefaultCodeWhispererClient { /** * @description Use this function to get the status of the code transformation. We should * be polling this function periodically to get updated results. When this function - * returns COMPLETED we know the transformation is done. + * returns PARTIALLY_COMPLETED or COMPLETED we know the transformation is done. */ public async codeModernizerGetCodeTransformation( request: CodeWhispererUserClient.GetTransformationRequest @@ -272,15 +272,15 @@ export class DefaultCodeWhispererClient { } /** - * @description After the job has been PAUSED we need to get user intervention. Once that user - * intervention has been handled we can resume the transformation job. + * @description During client-side build, or after the job has been PAUSED we need to get user intervention. + * Once that user action has been handled we can resume the transformation job. * @params transformationJobId - String id returned from StartCodeTransformationResponse * @params userActionStatus - String to determine what action the user took, if any. */ public async codeModernizerResumeTransformation( request: CodeWhispererUserClient.ResumeTransformationRequest ): Promise> { - return (await this.createUserSdkClient()).resumeTransformation(request).promise() + return (await this.createUserSdkClient(8)).resumeTransformation(request).promise() } /** diff --git a/packages/core/src/codewhisperer/commands/startTransformByQ.ts b/packages/core/src/codewhisperer/commands/startTransformByQ.ts index 74e50f0890e..56e54a97a8a 100644 --- a/packages/core/src/codewhisperer/commands/startTransformByQ.ts +++ b/packages/core/src/codewhisperer/commands/startTransformByQ.ts @@ -5,6 +5,7 @@ import * as vscode from 'vscode' import * as fs from 'fs' // eslint-disable-line no-restricted-imports +import os from 'os' import path from 'path' import { getLogger } from '../../shared/logger/logger' import * as CodeWhispererConstants from '../models/constants' @@ -16,7 +17,6 @@ import { jobPlanProgress, FolderInfo, ZipManifest, - TransformByQStatus, TransformationType, TransformationCandidateProject, RegionProfile, @@ -43,7 +43,6 @@ import { validateOpenProjects, } from '../service/transformByQ/transformProjectValidationHandler' import { - getVersionData, prepareProjectDependencies, runMavenDependencyUpdateCommands, } from '../service/transformByQ/transformMavenHandler' @@ -82,7 +81,7 @@ import { AuthUtil } from '../util/authUtil' export function getFeedbackCommentData() { const jobId = transformByQState.getJobId() - const s = `Q CodeTransform jobId: ${jobId ? jobId : 'none'}` + const s = `Q CodeTransformation jobId: ${jobId ? jobId : 'none'}` return s } @@ -110,10 +109,10 @@ export async function processSQLConversionTransformFormInput(pathToProject: stri export async function compileProject() { try { - const dependenciesFolder: FolderInfo = getDependenciesFolderInfo() + const dependenciesFolder: FolderInfo = await getDependenciesFolderInfo() transformByQState.setDependencyFolderInfo(dependenciesFolder) - const modulePath = transformByQState.getProjectPath() - await prepareProjectDependencies(dependenciesFolder, modulePath) + const projectPath = transformByQState.getProjectPath() + await prepareProjectDependencies(dependenciesFolder.path, projectPath) } catch (err) { // open build-logs.txt file to show user error logs await writeAndShowBuildLogs(true) @@ -175,8 +174,7 @@ export async function humanInTheLoopRetryLogic(jobId: string, profile: RegionPro if (status === 'PAUSED') { const hilStatusFailure = await initiateHumanInTheLoopPrompt(jobId) if (hilStatusFailure) { - // We rejected the changes and resumed the job and should - // try to resume normal polling asynchronously + // resume polling void humanInTheLoopRetryLogic(jobId, profile) } } else { @@ -184,9 +182,7 @@ export async function humanInTheLoopRetryLogic(jobId: string, profile: RegionPro } } catch (error) { status = 'FAILED' - // TODO if we encounter error in HIL, do we stop job? await finalizeTransformByQ(status) - // bubble up error to callee function throw error } } @@ -225,11 +221,9 @@ export async function preTransformationUploadCode() { const payloadFilePath = zipCodeResult.tempFilePath const zipSize = zipCodeResult.fileSize - const dependenciesCopied = zipCodeResult.dependenciesCopied telemetry.record({ codeTransformTotalByteSize: zipSize, - codeTransformDependenciesCopied: dependenciesCopied, }) transformByQState.setPayloadFilePath(payloadFilePath) @@ -408,7 +402,7 @@ export async function finishHumanInTheLoop(selectedDependency?: string) { // 7) We need to take that output of maven and use CreateUploadUrl const uploadFolderInfo = humanInTheLoopManager.getUploadFolderInfo() - await prepareProjectDependencies(uploadFolderInfo, uploadFolderInfo.path) + await prepareProjectDependencies(uploadFolderInfo.path, uploadFolderInfo.path) // zipCode side effects deletes the uploadFolderInfo right away const uploadResult = await zipCode({ dependenciesFolder: uploadFolderInfo, @@ -449,13 +443,11 @@ export async function finishHumanInTheLoop(selectedDependency?: string) { await terminateHILEarly(jobId) void humanInTheLoopRetryLogic(jobId, profile) } finally { - // Always delete the dependency directories telemetry.codeTransform_humanInTheLoop.emit({ codeTransformSessionId: CodeTransformTelemetryState.instance.getSessionId(), codeTransformJobId: jobId, codeTransformMetadata: CodeTransformTelemetryState.instance.getCodeTransformMetaDataString(), result: hilResult, - // TODO: make a generic reason field for telemetry logging so we don't log sensitive PII data reason: hilResult === MetadataResult.Fail ? 'Runtime error occurred' : undefined, }) await HumanInTheLoopManager.instance.cleanUpArtifacts() @@ -504,7 +496,7 @@ export async function startTransformationJob( throw new JobStartError() } - await sleep(2000) // sleep before polling job to prevent ThrottlingException + await sleep(5000) // sleep before polling job status to prevent ThrottlingException throwIfCancelled() return jobId @@ -523,9 +515,7 @@ export async function pollTransformationStatusUntilPlanReady(jobId: string, prof transformByQState.setJobFailureErrorChatMessage(CodeWhispererConstants.failedToCompleteJobChatMessage) } - // Since we don't yet have a good way of knowing what the error was, - // we try to fetch any build failure artifacts that may exist so that we can optionally - // show them to the user if they exist. + // try to download pre-build error logs if available let pathToLog = '' try { const tempToolkitFolder = await makeTemporaryToolkitFolder() @@ -651,6 +641,7 @@ export async function setTransformationToRunningState() { transformByQState.resetSessionJobHistory() transformByQState.setJobId('') // so that details for last job are not overwritten when running one job after another transformByQState.setPolledJobStatus('') // so that previous job's status does not display at very beginning of this job + transformByQState.setHasSeenTransforming(false) CodeTransformTelemetryState.instance.setStartTime() transformByQState.setStartTime( @@ -693,23 +684,17 @@ export async function postTransformationJob() { const durationInMs = calculateTotalLatency(CodeTransformTelemetryState.instance.getStartTime()) const resultStatusMessage = transformByQState.getStatus() - if (transformByQState.getTransformationType() !== TransformationType.SQL_CONVERSION) { - // the below is only applicable when user is doing a Java 8/11 language upgrade - const versionInfo = await getVersionData() - const mavenVersionInfoMessage = `${versionInfo[0]} (${transformByQState.getMavenName()})` - const javaVersionInfoMessage = `${versionInfo[1]} (${transformByQState.getMavenName()})` - - telemetry.codeTransform_totalRunTime.emit({ - buildSystemVersion: mavenVersionInfoMessage, - codeTransformSessionId: CodeTransformTelemetryState.instance.getSessionId(), - codeTransformJobId: transformByQState.getJobId(), - codeTransformResultStatusMessage: resultStatusMessage, - codeTransformRunTimeLatency: durationInMs, - codeTransformLocalJavaVersion: javaVersionInfoMessage, - result: resultStatusMessage === TransformByQStatus.Succeeded ? MetadataResult.Pass : MetadataResult.Fail, - reason: `${resultStatusMessage}-${chatMessage}`, - }) - } + telemetry.codeTransform_totalRunTime.emit({ + codeTransformSessionId: CodeTransformTelemetryState.instance.getSessionId(), + codeTransformJobId: transformByQState.getJobId(), + codeTransformResultStatusMessage: resultStatusMessage, + codeTransformRunTimeLatency: durationInMs, + reason: transformByQState.getPolledJobStatus(), + result: + transformByQState.isSucceeded() || transformByQState.isPartiallySucceeded() + ? MetadataResult.Pass + : MetadataResult.Fail, + }) let notificationMessage = '' @@ -739,9 +724,14 @@ export async function postTransformationJob() { }) } - if (transformByQState.getPayloadFilePath() !== '') { + if (transformByQState.getPayloadFilePath()) { // delete original upload ZIP at very end of transformation - fs.rmSync(transformByQState.getPayloadFilePath(), { recursive: true, force: true }) + fs.rmSync(transformByQState.getPayloadFilePath(), { force: true }) + } + // delete temporary build logs file + const logFilePath = path.join(os.tmpdir(), 'build-logs.txt') + if (fs.existsSync(logFilePath)) { + fs.rmSync(logFilePath, { force: true }) } // attempt download for user @@ -754,17 +744,15 @@ export async function postTransformationJob() { export async function transformationJobErrorHandler(error: any) { if (!transformByQState.isCancelled()) { // means some other error occurred; cancellation already handled by now with stopTransformByQ + await stopJob(transformByQState.getJobId()) transformByQState.setToFailed() transformByQState.setPolledJobStatus('FAILED') // jobFailureErrorNotification should always be defined here - let displayedErrorMessage = + const displayedErrorMessage = transformByQState.getJobFailureErrorNotification() ?? CodeWhispererConstants.failedToCompleteJobNotification - if (transformByQState.getJobFailureMetadata() !== '') { - displayedErrorMessage += ` ${transformByQState.getJobFailureMetadata()}` - transformByQState.setJobFailureErrorChatMessage( - `${transformByQState.getJobFailureErrorChatMessage()} ${transformByQState.getJobFailureMetadata()}` - ) - } + transformByQState.setJobFailureErrorChatMessage( + transformByQState.getJobFailureErrorChatMessage() ?? CodeWhispererConstants.failedToCompleteJobChatMessage + ) void vscode.window .showErrorMessage(displayedErrorMessage, CodeWhispererConstants.amazonQFeedbackText) .then((choice) => { diff --git a/packages/core/src/codewhisperer/models/constants.ts b/packages/core/src/codewhisperer/models/constants.ts index 319127cba20..5691d154625 100644 --- a/packages/core/src/codewhisperer/models/constants.ts +++ b/packages/core/src/codewhisperer/models/constants.ts @@ -580,7 +580,7 @@ export const invalidMetadataFileUnsupportedTargetDB = 'I can only convert SQL for migrations to Aurora PostgreSQL or Amazon RDS for PostgreSQL target databases. The provided .sct file indicates another target database for this migration.' export const invalidCustomVersionsFileMessage = - 'Your .YAML file is not formatted correctly. Make sure that the .YAML file you upload follows the format of the sample file provided.' + "I wasn't able to parse the dependency upgrade file. Check that it's configured properly and try again. For an example of the required dependency upgrade file format, see the [documentation](https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/code-transformation.html#dependency-upgrade-file)." export const invalidMetadataFileErrorParsing = "It looks like the .sct file you provided isn't valid. Make sure that you've uploaded the .zip file you retrieved from your schema conversion in AWS DMS." @@ -640,10 +640,14 @@ export const jobCancelledNotification = 'You cancelled the transformation.' export const continueWithoutHilMessage = 'I will continue transforming your code without upgrading this dependency.' -export const continueWithoutYamlMessage = 'Ok, I will continue without this information.' +export const continueWithoutConfigFileMessage = + 'Ok, I will continue the transformation without additional dependency upgrade information.' -export const chooseYamlMessage = - 'You can optionally upload a YAML file to specify which dependency versions to upgrade to.' +export const receivedValidConfigFileMessage = + 'The dependency upgrade file looks good. I will use this information to upgrade the dependencies you specified.' + +export const chooseConfigFileMessage = + 'Would you like to provide a custom dependency upgrade file? You can specify first-party dependencies to upgrade in a YAML file, and I will upgrade them during the JDK upgrade (for example, Java 8 to 17). You can initiate a separate transformation (17 to 17 or 21 to 21) after the initial JDK upgrade to transform third-party dependencies.\n\nWithout a YAML file, I can perform a minimum JDK upgrade, and then you can initiate a separate transformation to upgrade all third-party dependencies as part of a maximum transformation. For an example dependency upgrade file, see the [documentation](https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/code-transformation.html#dependency-upgrade-file).' export const enterJavaHomePlaceholder = 'Enter the path to your Java installation' @@ -656,7 +660,7 @@ export const jobCompletedNotification = 'Amazon Q transformed your code. You can review the diff to see my proposed changes and accept or reject them. The transformation summary has details about the changes. ' export const upgradeLibrariesMessage = - 'After successfully building in Java 17 or 21, an additional transformation is required to upgrade your libraries and dependencies. Choose the same source code version and target code version (for example, 17 to 17) to do this.' + 'After successfully transforming to Java 17 or 21, an additional transformation is required to upgrade your libraries and dependencies. Choose the same source code version and target code version (for example, 17 to 17) to do this.' export const jobPartiallyCompletedChatMessage = `I transformed part of your code. You can review the diff to see my proposed changes and accept or reject them. The transformation summary has details about the files I updated and the errors that prevented a complete transformation. ` @@ -720,14 +724,14 @@ export const linkToBillingInfo = 'https://aws.amazon.com/q/developer/pricing/' export const dependencyFolderName = 'transformation_dependencies_temp_' -export const cleanInstallErrorChatMessage = `Sorry, I couldn\'t run the Maven clean install command to build your project. For more information, see the [Amazon Q documentation](${codeTransformTroubleshootMvnFailure}).` +export const cleanTestCompileErrorChatMessage = `I could not run \`mvn clean test-compile\` to build your project. For more information, see the [Amazon Q documentation](${codeTransformTroubleshootMvnFailure}).` -export const cleanInstallErrorNotification = `Amazon Q could not run the Maven clean install command to build your project. For more information, see the [Amazon Q documentation](${codeTransformTroubleshootMvnFailure}).` +export const cleanTestCompileErrorNotification = `Amazon Q could not run \`mvn clean test-compile\` to build your project. For more information, see the [Amazon Q documentation](${codeTransformTroubleshootMvnFailure}).` export const enterJavaHomeChatMessage = 'Enter the path to JDK' export const projectPromptChatMessage = - 'I can upgrade your Java project. To start the transformation, I need some information from you. Choose the project you want to upgrade and the target code version to upgrade to. Then, choose Confirm.\n\nAfter successfully building in Java 17 or 21, an additional transformation is required to upgrade your libraries and dependencies. Choose the same source code version and target code version (for example, 17 to 17) to do this.' + "I can upgrade your Java project. To start the transformation, I need some information from you. Choose the project you want to upgrade and the target code version to upgrade to. Then, choose Confirm.\n\nAfter successfully transforming to Java 17 or 21, an additional transformation is required to upgrade your libraries and dependencies. Choose the same source code version and target code version (for example, 17 to 17) to do this.\n\nI will perform the transformation based on your project's requests, descriptions, and content. To maintain security, avoid including external, unvetted artifacts in your project repository prior to starting the transformation and always validate transformed code for both functionality and security." export const windowsJavaHomeHelpChatMessage = 'To find the JDK path, run the following commands in a new terminal: `cd "C:/Program Files/Java"` and then `dir`. If you see your JDK version, run `cd ` and then `cd` to show the path.' @@ -738,10 +742,6 @@ export const macJavaVersionHomeHelpChatMessage = (version: number) => export const linuxJavaHomeHelpChatMessage = 'To find the JDK path, run the following command in a new terminal: `update-java-alternatives --list`' -export const projectSizeTooLargeChatMessage = `Sorry, your project size exceeds the Amazon Q Code Transformation upload limit of 2GB. For more information, see the [Amazon Q documentation](${codeTransformTroubleshootProjectSize}).` - -export const projectSizeTooLargeNotification = `Your project size exceeds the Amazon Q Code Transformation upload limit of 2GB. For more information, see the [Amazon Q documentation](${codeTransformTroubleshootProjectSize}).` - export const JDK8VersionNumber = '52' export const JDK11VersionNumber = '55' @@ -759,7 +759,7 @@ export const chooseProjectSchemaFormMessage = 'To continue, choose the project a export const skipUnitTestsFormTitle = 'Choose to skip unit tests' export const skipUnitTestsFormMessage = - 'I will build your project using `mvn clean test` by default. If you would like me to build your project without running unit tests, I will use `mvn clean test-compile`.' + 'I will build generated code in your local environment, not on the server side. For information on how I scan code to reduce security risks associated with building the code in your local environment, see the [documentation](https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/code-transformation.html#java-local-builds).\n\nI will build your project using `mvn clean test` by default. If you would like me to build your project without running unit tests, I will use `mvn clean test-compile`.' export const runUnitTestsMessage = 'Run unit tests' diff --git a/packages/core/src/codewhisperer/models/model.ts b/packages/core/src/codewhisperer/models/model.ts index 279469353fb..72483feec51 100644 --- a/packages/core/src/codewhisperer/models/model.ts +++ b/packages/core/src/codewhisperer/models/model.ts @@ -675,16 +675,15 @@ export enum BuildSystem { Unknown = 'Unknown', } -// TO-DO: include the custom YAML file path here somewhere? export class ZipManifest { sourcesRoot: string = 'sources/' dependenciesRoot: string = 'dependencies/' - buildLogs: string = 'build-logs.txt' version: string = '1.0' hilCapabilities: string[] = ['HIL_1pDependency_VersionUpgrade'] - // TO-DO: add 'CLIENT_SIDE_BUILD' here when releasing - transformCapabilities: string[] = ['EXPLAINABILITY_V1', 'SELECTIVE_TRANSFORMATION_V2'] + transformCapabilities: string[] = ['EXPLAINABILITY_V1', 'SELECTIVE_TRANSFORMATION_V2', 'CLIENT_SIDE_BUILD'] noInteractiveMode: boolean = true + dependencyUpgradeConfigFile?: string = undefined + compilationsJsonFile: string = 'compilations.json' customBuildCommand: string = 'clean test' requestedConversions?: { sqlConversion?: { @@ -782,7 +781,7 @@ export class TransformByQState { private polledJobStatus: string = '' - private jobFailureMetadata: string = '' + private hasSeenTransforming: boolean = false private payloadFilePath: string = '' @@ -831,6 +830,10 @@ export class TransformByQState { return this.transformByQState === TransformByQStatus.PartiallySucceeded } + public getHasSeenTransforming() { + return this.hasSeenTransforming + } + public getTransformationType() { return this.transformationType } @@ -923,10 +926,6 @@ export class TransformByQState { return this.projectCopyFilePath } - public getJobFailureMetadata() { - return this.jobFailureMetadata - } - public getPayloadFilePath() { return this.payloadFilePath } @@ -1007,6 +1006,10 @@ export class TransformByQState { this.transformByQState = TransformByQStatus.PartiallySucceeded } + public setHasSeenTransforming(hasSeen: boolean) { + this.hasSeenTransforming = hasSeen + } + public setTransformationType(type: TransformationType) { this.transformationType = type } @@ -1091,10 +1094,6 @@ export class TransformByQState { this.projectCopyFilePath = filePath } - public setJobFailureMetadata(data: string) { - this.jobFailureMetadata = data - } - public setPayloadFilePath(payloadFilePath: string) { this.payloadFilePath = payloadFilePath } @@ -1153,9 +1152,9 @@ export class TransformByQState { public setJobDefaults() { this.setToNotStarted() + this.hasSeenTransforming = false this.jobFailureErrorNotification = undefined this.jobFailureErrorChatMessage = undefined - this.jobFailureMetadata = '' this.payloadFilePath = '' this.metadataPathSQL = '' this.customVersionPath = '' diff --git a/packages/core/src/codewhisperer/service/transformByQ/transformApiHandler.ts b/packages/core/src/codewhisperer/service/transformByQ/transformApiHandler.ts index e284207540d..20ef306f7ab 100644 --- a/packages/core/src/codewhisperer/service/transformByQ/transformApiHandler.ts +++ b/packages/core/src/codewhisperer/service/transformByQ/transformApiHandler.ts @@ -40,13 +40,12 @@ import { CodeTransformTelemetryState } from '../../../amazonqGumby/telemetry/cod import { calculateTotalLatency } from '../../../amazonqGumby/telemetry/codeTransformTelemetry' import { MetadataResult } from '../../../shared/telemetry/telemetryClient' import request from '../../../shared/request' -import { JobStoppedError, ZipExceedsSizeLimitError } from '../../../amazonqGumby/errors' +import { JobStoppedError } from '../../../amazonqGumby/errors' import { createLocalBuildUploadZip, extractOriginalProjectSources, writeAndShowBuildLogs } from './transformFileHandler' import { createCodeWhispererChatStreamingClient } from '../../../shared/clients/codewhispererChatClient' import { downloadExportResultArchive } from '../../../shared/utilities/download' import { ExportContext, ExportIntent, TransformationDownloadArtifactType } from '@amzn/codewhisperer-streaming' import fs from '../../../shared/fs/fs' -import { ChatSessionManager } from '../../../amazonqGumby/chat/storages/chatSession' import { encodeHTML } from '../../../shared/utilities/textUtilities' import { convertToTimeString } from '../../../shared/datetime' import { getAuthType } from '../../../auth/utils' @@ -55,7 +54,6 @@ import { setContext } from '../../../shared/vscode/setContext' import { AuthUtil } from '../../util/authUtil' import { DiffModel } from './transformationResultsViewProvider' import { spawnSync } from 'child_process' // eslint-disable-line no-restricted-imports -import { isClientSideBuildEnabled } from '../../../dev/config' export function getSha256(buffer: Buffer) { const hasher = crypto.createHash('sha256') @@ -187,12 +185,13 @@ export async function stopJob(jobId: string) { return } + getLogger().info(`CodeTransformation: Stopping transformation job with ID: ${jobId}`) + try { await codeWhisperer.codeWhispererClient.codeModernizerStopCodeTransformation({ transformationJobId: jobId, }) } catch (e: any) { - transformByQState.setJobFailureMetadata(` (request ID: ${e.requestId ?? 'unavailable'})`) getLogger().error(`CodeTransformation: StopTransformation error = %O`, e) throw new Error('Stop job failed') } @@ -218,7 +217,6 @@ export async function uploadPayload( }) } catch (e: any) { const errorMessage = `Creating the upload URL failed due to: ${(e as Error).message}` - transformByQState.setJobFailureMetadata(` (request ID: ${e.requestId ?? 'unavailable'})`) getLogger().error(`CodeTransformation: CreateUploadUrl error: = %O`, e) throw new Error(errorMessage) } @@ -309,24 +307,20 @@ export function createZipManifest({ hilZipParams }: IZipManifestParams) { interface IZipCodeParams { dependenciesFolder?: FolderInfo - humanInTheLoopFlag?: boolean projectPath?: string zipManifest: ZipManifest | HilZipManifest } interface ZipCodeResult { - dependenciesCopied: boolean tempFilePath: string fileSize: number } export async function zipCode( - { dependenciesFolder, humanInTheLoopFlag, projectPath, zipManifest }: IZipCodeParams, + { dependenciesFolder, projectPath, zipManifest }: IZipCodeParams, zip: AdmZip = new AdmZip() ) { let tempFilePath = undefined - let logFilePath = undefined - let dependenciesCopied = false try { throwIfCancelled() @@ -384,65 +378,48 @@ export async function zipCode( continue } const relativePath = path.relative(dependenciesFolder.path, file) - // const paddedPath = path.join(`dependencies/${dependenciesFolder.name}`, relativePath) - const paddedPath = path.join(`dependencies/`, relativePath) - zip.addLocalFile(file, path.dirname(paddedPath)) + if (relativePath.includes('compilations.json')) { + let fileContents = await nodefs.promises.readFile(file, 'utf-8') + if (os.platform() === 'win32') { + fileContents = fileContents.replace(/\\\\/g, '/') + } + zip.addFile('compilations.json', Buffer.from(fileContents, 'utf-8')) + } else { + zip.addLocalFile(file, path.dirname(relativePath)) + } dependencyFilesSize += (await nodefs.promises.stat(file)).size } getLogger().info(`CodeTransformation: dependency files size = ${dependencyFilesSize}`) - dependenciesCopied = true } - // TO-DO: decide where exactly to put the YAML file / what to name it if (transformByQState.getCustomDependencyVersionFilePath() && zipManifest instanceof ZipManifest) { zip.addLocalFile( transformByQState.getCustomDependencyVersionFilePath(), - 'custom-upgrades', - 'dependency-versions.yaml' + 'sources', + 'dependency_upgrade.yml' ) + zipManifest.dependencyUpgradeConfigFile = 'dependency_upgrade.yml' } zip.addFile('manifest.json', Buffer.from(JSON.stringify(zipManifest)), 'utf-8') throwIfCancelled() - // add text file with logs from mvn clean install and mvn copy-dependencies - logFilePath = await writeAndShowBuildLogs() - // We don't add build-logs.txt file to the manifest if we are - // uploading HIL artifacts - if (!humanInTheLoopFlag) { - zip.addLocalFile(logFilePath) - } - tempFilePath = path.join(os.tmpdir(), 'zipped-code.zip') await fs.writeFile(tempFilePath, zip.toBuffer()) - if (dependenciesFolder && (await fs.exists(dependenciesFolder.path))) { + if (dependenciesFolder?.path) { await fs.delete(dependenciesFolder.path, { recursive: true, force: true }) } } catch (e: any) { getLogger().error(`CodeTransformation: zipCode error = ${e}`) throw Error('Failed to zip project') - } finally { - if (logFilePath) { - await fs.delete(logFilePath) - } } - const zipSize = (await nodefs.promises.stat(tempFilePath)).size + const fileSize = (await nodefs.promises.stat(tempFilePath)).size - const exceedsLimit = zipSize > CodeWhispererConstants.uploadZipSizeLimitInBytes + getLogger().info(`CodeTransformation: created ZIP of size ${fileSize} at ${tempFilePath}`) - getLogger().info(`CodeTransformation: created ZIP of size ${zipSize} at ${tempFilePath}`) - - if (exceedsLimit) { - void vscode.window.showErrorMessage(CodeWhispererConstants.projectSizeTooLargeNotification) - transformByQState.getChatControllers()?.transformationFinished.fire({ - message: CodeWhispererConstants.projectSizeTooLargeChatMessage, - tabID: ChatSessionManager.Instance.getSession().tabID, - }) - throw new ZipExceedsSizeLimitError() - } - return { dependenciesCopied: dependenciesCopied, tempFilePath: tempFilePath, fileSize: zipSize } as ZipCodeResult + return { tempFilePath: tempFilePath, fileSize: fileSize } as ZipCodeResult } export async function startJob(uploadId: string, profile: RegionProfile | undefined) { @@ -465,7 +442,6 @@ export async function startJob(uploadId: string, profile: RegionProfile | undefi return response.transformationJobId } catch (e: any) { const errorMessage = `Starting the job failed due to: ${(e as Error).message}` - transformByQState.setJobFailureMetadata(` (request ID: ${e.requestId ?? 'unavailable'})`) getLogger().error(`CodeTransformation: StartTransformation error = %O`, e) throw new Error(errorMessage) } @@ -652,12 +628,9 @@ export async function getTransformationPlan(jobId: string, profile: RegionProfil return plan } catch (e: any) { const errorMessage = (e as Error).message - transformByQState.setJobFailureMetadata(` (request ID: ${e.requestId ?? 'unavailable'})`) getLogger().error(`CodeTransformation: GetTransformationPlan error = %O`, e) - /* Means API call failed - * If response is defined, means a display/parsing error occurred, so continue transformation - */ + // GetTransformationPlan API call failed, but if response is defined, a display/parsing error occurred, so continue transformation if (response === undefined) { throw new Error(errorMessage) } @@ -672,7 +645,6 @@ export async function getTransformationSteps(jobId: string, profile: RegionProfi }) return response.transformationPlan.transformationSteps.slice(1) // skip step 0 (contains supplemental info) } catch (e: any) { - transformByQState.setJobFailureMetadata(` (request ID: ${e.requestId ?? 'unavailable'})`) getLogger().error(`CodeTransformation: GetTransformationPlan error = %O`, e) throw e } @@ -692,6 +664,9 @@ export async function pollTransformationJob(jobId: string, validStates: string[] if (CodeWhispererConstants.validStatesForBuildSucceeded.includes(status)) { jobPlanProgress['buildCode'] = StepProgress.Succeeded } + if (status === 'TRANSFORMING') { + transformByQState.setHasSeenTransforming(true) + } // emit metric when job status changes if (status !== transformByQState.getPolledJobStatus()) { telemetry.codeTransform_jobStatusChanged.emit({ @@ -728,16 +703,23 @@ export async function pollTransformationJob(jobId: string, validStates: string[] // final plan is complete; show to user isPlanComplete = true } + // for JDK upgrades without a YAML file, we show a static plan so no need to keep refreshing it + if ( + plan && + transformByQState.getSourceJDKVersion() !== transformByQState.getTargetJDKVersion() && + !transformByQState.getCustomDependencyVersionFilePath() + ) { + isPlanComplete = true + } } if (validStates.includes(status)) { break } - // TO-DO: remove isClientSideBuildEnabled when releasing CSB + // TO-DO: later, handle case where PlannerAgent needs to run mvn dependency:tree during PLANNING stage; not needed for now if ( - isClientSideBuildEnabled && - status === 'TRANSFORMING' && + transformByQState.getHasSeenTransforming() && transformByQState.getTransformationType() === TransformationType.LANGUAGE_UPGRADE ) { // client-side build is N/A for SQL conversions @@ -762,7 +744,6 @@ export async function pollTransformationJob(jobId: string, validStates: string[] await sleep(CodeWhispererConstants.transformationJobPollingIntervalSeconds * 1000) } catch (e: any) { getLogger().error(`CodeTransformation: GetTransformation error = %O`, e) - transformByQState.setJobFailureMetadata(` (request ID: ${e.requestId ?? 'unavailable'})`) throw e } } @@ -844,17 +825,24 @@ async function processClientInstructions(jobId: string, clientInstructionsPath: const destinationPath = path.join(os.tmpdir(), `originalCopy_${jobId}_${artifactId}`) await extractOriginalProjectSources(destinationPath) getLogger().info(`CodeTransformation: copied project to ${destinationPath}`) - const diffModel = new DiffModel() - diffModel.parseDiff(clientInstructionsPath, path.join(destinationPath, 'sources'), true) - // show user the diff.patch - const doc = await vscode.workspace.openTextDocument(clientInstructionsPath) - await vscode.window.showTextDocument(doc, { viewColumn: vscode.ViewColumn.One }) + const diffContents = await fs.readFileText(clientInstructionsPath) + if (diffContents.trim()) { + const diffModel = new DiffModel() + diffModel.parseDiff(clientInstructionsPath, path.join(destinationPath, 'sources'), true) + // show user the diff.patch + const doc = await vscode.workspace.openTextDocument(clientInstructionsPath) + await vscode.window.showTextDocument(doc, { viewColumn: vscode.ViewColumn.One }) + } else { + // still need to set the project copy so that we can use it below + transformByQState.setProjectCopyFilePath(path.join(destinationPath, 'sources')) + getLogger().info(`CodeTransformation: diff.patch is empty`) + } await runClientSideBuild(transformByQState.getProjectCopyFilePath(), artifactId) } -export async function runClientSideBuild(projectCopyPath: string, clientInstructionArtifactId: string) { +export async function runClientSideBuild(projectCopyDir: string, clientInstructionArtifactId: string) { const baseCommand = transformByQState.getMavenName() - const args = [] + const args = ['clean'] if (transformByQState.getCustomBuildCommand() === CodeWhispererConstants.skipUnitTestsBuildCommand) { args.push('test-compile') } else { @@ -864,22 +852,22 @@ export async function runClientSideBuild(projectCopyPath: string, clientInstruct const argString = args.join(' ') const spawnResult = spawnSync(baseCommand, args, { - cwd: projectCopyPath, + cwd: projectCopyDir, shell: true, encoding: 'utf-8', env: environment, }) - const buildLogs = `Intermediate build result from running ${baseCommand} ${argString}:\n\n${spawnResult.stdout}` + const buildLogs = `Intermediate build result from running mvn ${argString}:\n\n${spawnResult.stdout}` transformByQState.clearBuildLog() transformByQState.appendToBuildLog(buildLogs) await writeAndShowBuildLogs() - const uploadZipBaseDir = path.join( + const uploadZipDir = path.join( os.tmpdir(), `clientInstructionsResult_${transformByQState.getJobId()}_${clientInstructionArtifactId}` ) - const uploadZipPath = await createLocalBuildUploadZip(uploadZipBaseDir, spawnResult.status, spawnResult.stdout) + const uploadZipPath = await createLocalBuildUploadZip(uploadZipDir, spawnResult.status, spawnResult.stdout) // upload build results const uploadContext: UploadContext = { @@ -892,10 +880,33 @@ export async function runClientSideBuild(projectCopyPath: string, clientInstruct try { await uploadPayload(uploadZipPath, AuthUtil.instance.regionProfileManager.activeRegionProfile, uploadContext) await resumeTransformationJob(transformByQState.getJobId(), 'COMPLETED') + } catch (err: any) { + getLogger().error(`CodeTransformation: upload client build results / resumeTransformation error = %O`, err) + transformByQState.setJobFailureErrorChatMessage( + `${CodeWhispererConstants.failedToCompleteJobGenericChatMessage} ${err.message}` + ) + transformByQState.setJobFailureErrorNotification( + `${CodeWhispererConstants.failedToCompleteJobGenericNotification} ${err.message}` + ) + // in case server-side execution times out, still call resumeTransformationJob + if (err.message.includes('find a step in desired state:AWAITING_CLIENT_ACTION')) { + getLogger().info('CodeTransformation: resuming job after server-side execution timeout') + await resumeTransformationJob(transformByQState.getJobId(), 'COMPLETED') + } else { + throw err + } } finally { - await fs.delete(projectCopyPath, { recursive: true }) - await fs.delete(uploadZipBaseDir, { recursive: true }) - getLogger().info(`CodeTransformation: Just deleted project copy and uploadZipBaseDir after client-side build`) + await fs.delete(projectCopyDir, { recursive: true }) + await fs.delete(uploadZipDir, { recursive: true }) + await fs.delete(uploadZipPath, { force: true }) + const exportZipDir = path.join( + os.tmpdir(), + `downloadClientInstructions_${transformByQState.getJobId()}_${clientInstructionArtifactId}` + ) + await fs.delete(exportZipDir, { recursive: true }) + getLogger().info( + `CodeTransformation: deleted projectCopy, clientInstructionsResult, and downloadClientInstructions directories/files` + ) } } diff --git a/packages/core/src/codewhisperer/service/transformByQ/transformFileHandler.ts b/packages/core/src/codewhisperer/service/transformByQ/transformFileHandler.ts index fd74ca7b147..88f34a799d1 100644 --- a/packages/core/src/codewhisperer/service/transformByQ/transformFileHandler.ts +++ b/packages/core/src/codewhisperer/service/transformByQ/transformFileHandler.ts @@ -9,7 +9,7 @@ import * as os from 'os' import xml2js = require('xml2js') import * as CodeWhispererConstants from '../../models/constants' import { existsSync, readFileSync, writeFileSync } from 'fs' // eslint-disable-line no-restricted-imports -import { BuildSystem, DB, FolderInfo, TransformationType, transformByQState } from '../../models/model' +import { BuildSystem, DB, FolderInfo, transformByQState } from '../../models/model' import { IManifestFile } from '../../../amazonqFeatureDev/models' import fs from '../../../shared/fs/fs' import globals from '../../../shared/extensionGlobals' @@ -18,9 +18,10 @@ import { AbsolutePathDetectedError } from '../../../amazonqGumby/errors' import { getLogger } from '../../../shared/logger/logger' import AdmZip from 'adm-zip' -export function getDependenciesFolderInfo(): FolderInfo { +export async function getDependenciesFolderInfo(): Promise { const dependencyFolderName = `${CodeWhispererConstants.dependencyFolderName}${globals.clock.Date.now()}` const dependencyFolderPath = path.join(os.tmpdir(), dependencyFolderName) + await fs.mkdir(dependencyFolderPath) return { name: dependencyFolderName, path: dependencyFolderPath, @@ -31,15 +32,12 @@ export async function writeAndShowBuildLogs(isLocalInstall: boolean = false) { const logFilePath = path.join(os.tmpdir(), 'build-logs.txt') writeFileSync(logFilePath, transformByQState.getBuildLog()) const doc = await vscode.workspace.openTextDocument(logFilePath) - if ( - !transformByQState.getBuildLog().includes('clean install succeeded') && - transformByQState.getTransformationType() !== TransformationType.SQL_CONVERSION - ) { + const logs = transformByQState.getBuildLog().toLowerCase() + if (logs.includes('intermediate build result') || logs.includes('maven jar failed')) { // only show the log if the build failed; show it in second column for intermediate builds only const options = isLocalInstall ? undefined : { viewColumn: vscode.ViewColumn.Two } await vscode.window.showTextDocument(doc, options) } - return logFilePath } export async function createLocalBuildUploadZip(baseDir: string, exitCode: number | null, stdout: string) { @@ -174,8 +172,7 @@ export async function validateSQLMetadataFile(fileContents: string, message: any } export function setMaven() { - // for now, just use regular Maven since the Maven executables can - // cause permissions issues when building if user has not ran 'chmod' + // avoid using maven wrapper since we can run into permissions issues transformByQState.setMavenName('mvn') } @@ -214,7 +211,6 @@ export async function getJsonValuesFromManifestFile( return { hilCapability: jsonValues?.hilType, pomFolderName: jsonValues?.pomFolderName, - // TODO remove this forced version sourcePomVersion: jsonValues?.sourcePomVersion || '1.0', pomArtifactId: jsonValues?.pomArtifactId, pomGroupId: jsonValues?.pomGroupId, diff --git a/packages/core/src/codewhisperer/service/transformByQ/transformMavenHandler.ts b/packages/core/src/codewhisperer/service/transformByQ/transformMavenHandler.ts index ebcbfec8970..b38a6ef1da8 100644 --- a/packages/core/src/codewhisperer/service/transformByQ/transformMavenHandler.ts +++ b/packages/core/src/codewhisperer/service/transformByQ/transformMavenHandler.ts @@ -8,153 +8,72 @@ import { getLogger } from '../../../shared/logger/logger' import * as CodeWhispererConstants from '../../models/constants' // Consider using ChildProcess once we finalize all spawnSync calls import { spawnSync } from 'child_process' // eslint-disable-line no-restricted-imports -import { CodeTransformBuildCommand, telemetry } from '../../../shared/telemetry/telemetry' -import { CodeTransformTelemetryState } from '../../../amazonqGumby/telemetry/codeTransformTelemetryState' -import { ToolkitError } from '../../../shared/errors' import { setMaven } from './transformFileHandler' import { throwIfCancelled } from './transformApiHandler' import { sleep } from '../../../shared/utilities/timeoutUtils' +import path from 'path' +import globals from '../../../shared/extensionGlobals' -function installProjectDependencies(dependenciesFolder: FolderInfo, modulePath: string) { - telemetry.codeTransform_localBuildProject.run(() => { - telemetry.record({ codeTransformSessionId: CodeTransformTelemetryState.instance.getSessionId() }) +function collectDependenciesAndMetadata(dependenciesFolderPath: string, workingDirPath: string) { + getLogger().info('CodeTransformation: running mvn clean test-compile with maven JAR') - // will always be 'mvn' - const baseCommand = transformByQState.getMavenName() - - const args = [`-Dmaven.repo.local=${dependenciesFolder.path}`, 'clean', 'install', '-q'] - - transformByQState.appendToBuildLog(`Running ${baseCommand} ${args.join(' ')}`) - - if (transformByQState.getCustomBuildCommand() === CodeWhispererConstants.skipUnitTestsBuildCommand) { - args.push('-DskipTests') - } - - let environment = process.env - - if (transformByQState.getSourceJavaHome()) { - environment = { ...process.env, JAVA_HOME: transformByQState.getSourceJavaHome() } - } - - const argString = args.join(' ') - const spawnResult = spawnSync(baseCommand, args, { - cwd: modulePath, - shell: true, - encoding: 'utf-8', - env: environment, - maxBuffer: CodeWhispererConstants.maxBufferSize, - }) - - const mavenBuildCommand = transformByQState.getMavenName() - telemetry.record({ codeTransformBuildCommand: mavenBuildCommand as CodeTransformBuildCommand }) - - if (spawnResult.status !== 0) { - let errorLog = '' - errorLog += spawnResult.error ? JSON.stringify(spawnResult.error) : '' - errorLog += `${spawnResult.stderr}\n${spawnResult.stdout}` - transformByQState.appendToBuildLog(`${baseCommand} ${argString} failed: \n ${errorLog}`) - getLogger().error( - `CodeTransformation: Error in running Maven command ${baseCommand} ${argString} = ${errorLog}` - ) - throw new ToolkitError(`Maven ${argString} error`, { code: 'MavenExecutionError' }) - } else { - transformByQState.appendToBuildLog(`mvn clean install succeeded`) - } - }) -} - -function copyProjectDependencies(dependenciesFolder: FolderInfo, modulePath: string) { const baseCommand = transformByQState.getMavenName() + const jarPath = globals.context.asAbsolutePath(path.join('resources', 'amazonQCT', 'QCT-Maven-6-16.jar')) + + getLogger().info('CodeTransformation: running Maven extension with JAR') const args = [ - 'dependency:copy-dependencies', - `-DoutputDirectory=${dependenciesFolder.path}`, - '-Dmdep.useRepositoryLayout=true', - '-Dmdep.copyPom=true', - '-Dmdep.addParentPoms=true', - '-q', + `-Dmaven.ext.class.path=${jarPath}`, + `-Dcom.amazon.aws.developer.transform.jobDirectory=${dependenciesFolderPath}`, + 'clean', + 'test-compile', ] let environment = process.env - if (transformByQState.getSourceJavaHome()) { + if (transformByQState.getSourceJavaHome() !== undefined) { environment = { ...process.env, JAVA_HOME: transformByQState.getSourceJavaHome() } } const spawnResult = spawnSync(baseCommand, args, { - cwd: modulePath, + cwd: workingDirPath, shell: true, encoding: 'utf-8', env: environment, - maxBuffer: CodeWhispererConstants.maxBufferSize, }) + + getLogger().info( + `CodeTransformation: Ran mvn clean test-compile with maven JAR; status code = ${spawnResult.status}}` + ) + if (spawnResult.status !== 0) { let errorLog = '' errorLog += spawnResult.error ? JSON.stringify(spawnResult.error) : '' errorLog += `${spawnResult.stderr}\n${spawnResult.stdout}` - getLogger().info( - `CodeTransformation: Maven command ${baseCommand} ${args} failed, but still continuing with transformation: ${errorLog}` - ) - throw new Error('Maven copy-deps error') + errorLog = errorLog.toLowerCase().replace('elasticgumby', 'QCT') + transformByQState.appendToBuildLog(`mvn clean test-compile with maven JAR failed:\n${errorLog}`) + getLogger().error(`CodeTransformation: Error in running mvn clean test-compile with maven JAR = ${errorLog}`) + throw new Error('mvn clean test-compile with maven JAR failed') } + getLogger().info( + `CodeTransformation: mvn clean test-compile with maven JAR succeeded; dependencies copied to ${dependenciesFolderPath}` + ) } -export async function prepareProjectDependencies(dependenciesFolder: FolderInfo, rootPomPath: string) { +export async function prepareProjectDependencies(dependenciesFolderPath: string, workingDirPath: string) { setMaven() - getLogger().info('CodeTransformation: running Maven copy-dependencies') // pause to give chat time to update await sleep(100) try { - copyProjectDependencies(dependenciesFolder, rootPomPath) - } catch (err) { - // continue in case of errors - getLogger().info( - `CodeTransformation: Maven copy-dependencies failed, but transformation will continue and may succeed` - ) - } - - getLogger().info('CodeTransformation: running Maven install') - try { - installProjectDependencies(dependenciesFolder, rootPomPath) + collectDependenciesAndMetadata(dependenciesFolderPath, workingDirPath) } catch (err) { - void vscode.window.showErrorMessage(CodeWhispererConstants.cleanInstallErrorNotification) + getLogger().error('CodeTransformation: collectDependenciesAndMetadata failed') + void vscode.window.showErrorMessage(CodeWhispererConstants.cleanTestCompileErrorNotification) throw err } - throwIfCancelled() void vscode.window.showInformationMessage(CodeWhispererConstants.buildSucceededNotification) } -export async function getVersionData() { - const baseCommand = transformByQState.getMavenName() - const projectPath = transformByQState.getProjectPath() - const args = ['-v'] - const spawnResult = spawnSync(baseCommand, args, { cwd: projectPath, shell: true, encoding: 'utf-8' }) - - let localMavenVersion: string | undefined = '' - let localJavaVersion: string | undefined = '' - - try { - const localMavenVersionIndex = spawnResult.stdout.indexOf('Apache Maven') - const localMavenVersionString = spawnResult.stdout.slice(localMavenVersionIndex + 13).trim() - localMavenVersion = localMavenVersionString.slice(0, localMavenVersionString.indexOf(' ')).trim() - } catch (e: any) { - localMavenVersion = undefined // if this happens here or below, user most likely has JAVA_HOME incorrectly defined - } - - try { - const localJavaVersionIndex = spawnResult.stdout.indexOf('Java version: ') - const localJavaVersionString = spawnResult.stdout.slice(localJavaVersionIndex + 14).trim() - localJavaVersion = localJavaVersionString.slice(0, localJavaVersionString.indexOf(',')).trim() // will match value of JAVA_HOME - } catch (e: any) { - localJavaVersion = undefined - } - - getLogger().info( - `CodeTransformation: Ran ${baseCommand} to get Maven version = ${localMavenVersion} and Java version = ${localJavaVersion} with project JDK = ${transformByQState.getSourceJDKVersion()}` - ) - return [localMavenVersion, localJavaVersion] -} - export function runMavenDependencyUpdateCommands(dependenciesFolder: FolderInfo) { const baseCommand = transformByQState.getMavenName() diff --git a/packages/core/src/codewhisperer/service/transformByQ/transformationResultsViewProvider.ts b/packages/core/src/codewhisperer/service/transformByQ/transformationResultsViewProvider.ts index e5de2099753..0b678f8120d 100644 --- a/packages/core/src/codewhisperer/service/transformByQ/transformationResultsViewProvider.ts +++ b/packages/core/src/codewhisperer/service/transformByQ/transformationResultsViewProvider.ts @@ -165,7 +165,9 @@ export class DiffModel { throw new Error(CodeWhispererConstants.noChangesMadeMessage) } - const changedFiles = parsePatch(diffContents) + let changedFiles = parsePatch(diffContents) + // exclude dependency_upgrade.yml from patch application + changedFiles = changedFiles.filter((file) => !file.oldFileName?.includes('dependency_upgrade')) getLogger().info('CodeTransformation: parsed patch file successfully') // if doing intermediate client-side build, pathToWorkspace is the path to the unzipped project's 'sources' directory (re-using upload ZIP) // otherwise, we are at the very end of the transformation and need to copy the changed files in the project to show the diff(s) diff --git a/packages/core/src/dev/config.ts b/packages/core/src/dev/config.ts index b4df78f64b0..d5fa49b2426 100644 --- a/packages/core/src/dev/config.ts +++ b/packages/core/src/dev/config.ts @@ -10,6 +10,3 @@ export const betaUrl = { amazonq: '', toolkit: '', } - -// TO-DO: remove when releasing CSB -export const isClientSideBuildEnabled = false diff --git a/packages/core/src/test/codewhisperer/commands/transformByQ.test.ts b/packages/core/src/test/codewhisperer/commands/transformByQ.test.ts index edb2524ee68..554d24c855a 100644 --- a/packages/core/src/test/codewhisperer/commands/transformByQ.test.ts +++ b/packages/core/src/test/codewhisperer/commands/transformByQ.test.ts @@ -53,18 +53,21 @@ import * as nodefs from 'fs' // eslint-disable-line no-restricted-imports describe('transformByQ', function () { let fetchStub: sinon.SinonStub let tempDir: string - const validCustomVersionsFile = `name: "custom-dependency-management" + const validCustomVersionsFile = `name: "dependency-upgrade" description: "Custom dependency version management for Java migration from JDK 8/11/17 to JDK 17/21" dependencyManagement: dependencies: - identifier: "com.example:library1" - targetVersion: "2.1.0" - versionProperty: "library1.version" - originType: "FIRST_PARTY" + targetVersion: "2.1.0" + versionProperty: "library1.version" # Optional + originType: "FIRST_PARTY" # or "THIRD_PARTY" + - identifier: "com.example:library2" + targetVersion: "3.0.0" + originType: "THIRD_PARTY" plugins: - - identifier: "com.example.plugin" - targetVersion: "1.2.0" - versionProperty: "plugin.version"` + - identifier: "com.example:plugin" + targetVersion: "1.2.0" + versionProperty: "plugin.version" # Optional` const validSctFile = ` @@ -119,6 +122,7 @@ dependencyManagement: }) afterEach(async function () { + fetchStub.restore() sinon.restore() await fs.delete(tempDir, { recursive: true }) }) @@ -405,7 +409,6 @@ dependencyManagement: path: tempDir, name: tempFileName, }, - humanInTheLoopFlag: false, projectPath: tempDir, zipManifest: transformManifest, }).then((zipCodeResult) => { @@ -462,7 +465,7 @@ dependencyManagement: ] for (const folder of m2Folders) { - const folderPath = path.join(tempDir, folder) + const folderPath = path.join(tempDir, 'dependencies', folder) await fs.mkdir(folderPath) for (const file of filesToAdd) { await fs.writeFile(path.join(folderPath, file), 'sample content for the test file') @@ -476,7 +479,6 @@ dependencyManagement: path: tempDir, name: tempFileName, }, - humanInTheLoopFlag: false, projectPath: tempDir, zipManifest: new ZipManifest(), }).then((zipCodeResult) => { @@ -662,7 +664,6 @@ dependencyManagement: message: expectedMessage, } ) - sinon.assert.callCount(fetchStub, 4) }) it('should not retry upload on non-retriable error', async () => { diff --git a/packages/core/src/testInteg/perf/zipcode.test.ts b/packages/core/src/testInteg/perf/zipcode.test.ts index f5e81086152..71303e493c9 100644 --- a/packages/core/src/testInteg/perf/zipcode.test.ts +++ b/packages/core/src/testInteg/perf/zipcode.test.ts @@ -54,7 +54,6 @@ function performanceTestWrapper(numberOfFiles: number, fileSize: number) { path: setup.tempDir, name: setup.tempFileName, }, - humanInTheLoopFlag: false, projectPath: setup.tempDir, zipManifest: setup.transformQManifest, })