fix(assertions): throw error or warn when synth is called multiple times on mutated construct tree#31865
Conversation
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
aws-cdk-automation
left a comment
There was a problem hiding this comment.
The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.
A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed add Clarification Request to a comment.
✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
…s are correctly merged) - move Template.stack calls after stack mutations Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
| }), | ||
| }); | ||
|
|
||
| new ModernTestGitHubNpmPipeline(pipelineStack, 'Cdk-2', { |
There was a problem hiding this comment.
This existing pipelines test had to be changed to not throw an error - all I did was move the stack mutations before the Template.fromStack() calls
synthsynth is called multiple times on mutated construct tree
Signed-off-by: Sumu <sumughan@amazon.com>
Co-authored-by: Rico Hermans <rix0rrr@gmail.com>
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
| throw new Error(errorMessage); | ||
| } else { | ||
| // eslint-disable-next-line no-console | ||
| console.error(errorMessage); |
There was a problem hiding this comment.
So we print this as a warning. And now what? The message text implies that we are in the warning situation and is trying to say the following:
- Hey we have detected that you called synth() multiple and changed the construct tree after the first synth()
- We are going to ignoring all changes, but are not failing the execution.
- You should stop doing this, unless you really know what you do.
| if (!this.constructPathSetsAreEqual(this.constructPathsCache, newConstructPaths)) { | ||
| const errorMessage = 'The construct tree has been modified after synthesis. Only the results of the first synth() call are written to disk, and modifications done after it are ignored. Avoid construct tree mutations after synth() has been called.'; | ||
| if (options.errorOnDuplicateSynth ?? true) { | ||
| throw new Error(errorMessage); |
There was a problem hiding this comment.
Not sure we can really use the exact same message. the call to action is different when there is an error vs a warning. In my head, the error scenario is this:
- Hey we have detected that you called synth() multiple times and changed the construct tree after the first synth()
- You cannot do this, we are failing the execution.
- If you really know what you are doing, you can set this flag to keep going:
mrgrain
left a comment
There was a problem hiding this comment.
Much improvement on the error messages. Still not 100% happy though.
Co-authored-by: Momo Kornher <kornherm@amazon.co.uk>
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
mrgrain
left a comment
There was a problem hiding this comment.
Conditionally approved until you've checked that addWarningV2() really doesn't work.
Signed-off-by: Sumu <sumughan@amazon.com>
Just tested and confirmed that |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
|
Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
|
Comments on closed issues and PRs are hard for our team to see. |
Closes #24689
Reason for this change
Calling
Template.fromStack(stack)twice on the same stack object will throw the errorUnable to find artifact with idif the stack is mutated after the firstTemplate.fromStack(stack)call. This is because synth should only be called once - from the comment on thesynthfunction:Second call of
Template.fromStack(stack)tries to find the mutated stack sinceapp.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 fromprocess.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
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license