diff --git a/packages/@aws-cdk/cloudformation-diff/lib/diff-template.ts b/packages/@aws-cdk/cloudformation-diff/lib/diff-template.ts index b93365840..6284a834b 100644 --- a/packages/@aws-cdk/cloudformation-diff/lib/diff-template.ts +++ b/packages/@aws-cdk/cloudformation-diff/lib/diff-template.ts @@ -51,8 +51,12 @@ export function fullDiff( changeSet?: DescribeChangeSetOutput, isImport?: boolean, ): types.TemplateDiff { - normalize(currentTemplate); - normalize(newTemplate); + // AFTER — work on clones, originals stay untouched + const currentCopy = structuredClone(currentTemplate); + const newCopy = structuredClone(newTemplate); + normalize(currentCopy); + normalize(newCopy); + // use currentCopy / newCopy everywhere below instead const theDiff = diffTemplate(currentTemplate, newTemplate); if (changeSet) { // These methods mutate the state of theDiff, using the changeSet. diff --git a/packages/@aws-cdk/toolkit-lib/lib/api/resource-import/importer.ts b/packages/@aws-cdk/toolkit-lib/lib/api/resource-import/importer.ts index e59f5b9cf..d2e500ce2 100644 --- a/packages/@aws-cdk/toolkit-lib/lib/api/resource-import/importer.ts +++ b/packages/@aws-cdk/toolkit-lib/lib/api/resource-import/importer.ts @@ -296,27 +296,23 @@ export class ResourceImporter { */ private async currentTemplate(): Promise { if (!this._currentTemplate) { - this._currentTemplate = await this.cfn.readCurrentTemplate(this.stack); + this._currentTemplate = Object.freeze(structuredClone(await this.cfn.readCurrentTemplate(this.stack))); } return this._currentTemplate; } - /** * Return the current template, with the given resources added to it */ private async currentTemplateWithAdditions(additions: ImportableResource[]): Promise { - const template = await this.currentTemplate(); + const template = structuredClone(await this.currentTemplate()); if (!template.Resources) { template.Resources = {}; } - for (const add of additions) { template.Resources[add.logicalId] = add.resourceDefinition; } - return template; } - /** * Get a list of import identifiers for all resource types used in the given * template that do support the import operation (SINGLETON)