You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(assertions): throw error or warn when synth is called multiple times on mutated construct tree (#31865)
Closes#24689
### Reason for this change
Calling `Template.fromStack(stack)` twice on the same stack object will throw the error `Unable to find artifact with id` if the stack is mutated after the first `Template.fromStack(stack)` call. This is because synth should only be called once - from the comment on the `synth` function:
> _Once an assembly has been synthesized, it cannot be modified. Subsequent calls will return the same assembly._
Second call of `Template.fromStack(stack)` tries to find the mutated stack since `app.synth()` caches and returns the assembly from the first synth call.
### Description of changes
This PR checks to see whether or not the construct tree has been each time `synth()` is called - if it has, then we either throw an error or a warning. We will only throw a warning if synth is being called from `process.once('beforeExit')`.
### Description of how you validated changes
Unit test with the customer's same example case, asserting than an error is thrown when synth is called twice after a stack has been mutated after the first synth call.
### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)
----
*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
consterrorMessage='Synthesis has been called multiple times and the construct tree was modified after the first synthesis.';
235
+
if(options.errorOnDuplicateSynth??true){
236
+
thrownewError(errorMessage+' This is not allowed. Remove multple synth() calls and do not modify the construct tree after the first synth().');
237
+
}else{
238
+
// eslint-disable-next-line no-console
239
+
console.error(errorMessage+' Only the results of the first synth() call are used, and modifications done after it are ignored. Avoid construct tree mutations after synth() has been called unless this is intentional.');
240
+
}
241
+
}
242
+
243
+
// Reset construct paths cache
244
+
this.constructPathsCache=newConstructPaths;
245
+
220
246
returnthis.assembly;
221
247
}
222
248
249
+
// Function that lists all construct paths and returns them as a set
0 commit comments