-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(assertions): throw error or warn when synth
is called multiple times on mutated construct tree
#31865
fix(assertions): throw error or warn when synth
is called multiple times on mutated construct tree
#31865
Conversation
Signed-off-by: Sumu <[email protected]>
Signed-off-by: Sumu <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 <[email protected]>
Signed-off-by: Sumu <[email protected]>
Signed-off-by: Sumu <[email protected]>
Signed-off-by: Sumu <[email protected]>
…s are correctly merged) - move Template.stack calls after stack mutations Signed-off-by: Sumu <[email protected]>
Signed-off-by: Sumu <[email protected]>
@@ -186,6 +186,27 @@ test('CodeBuild: environment variables specified in multiple places are correctl | |||
}), | |||
}); | |||
|
|||
new ModernTestGitHubNpmPipeline(pipelineStack, 'Cdk-2', { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's perfect!
synth
synth
is called multiple times on mutated construct tree
Signed-off-by: Sumu <[email protected]>
Signed-off-by: Sumu <[email protected]>
Co-authored-by: Rico Hermans <[email protected]>
Signed-off-by: Sumu <[email protected]>
Signed-off-by: Sumu <[email protected]>
Signed-off-by: Sumu <[email protected]>
Signed-off-by: Sumu <[email protected]>
throw new Error(errorMessage); | ||
} else { | ||
// eslint-disable-next-line no-console | ||
console.error(errorMessage); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much improvement on the error messages. Still not 100% happy though.
Co-authored-by: Momo Kornher <[email protected]>
Signed-off-by: Sumu <[email protected]>
Signed-off-by: Sumu <[email protected]>
Signed-off-by: Sumu <[email protected]>
Signed-off-by: Sumu <[email protected]>
Signed-off-by: Sumu <[email protected]>
Signed-off-by: Sumu <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Conditionally approved until you've checked that addWarningV2()
really doesn't work.
Signed-off-by: Sumu <[email protected]>
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 id
if the stack is mutated after the firstTemplate.fromStack(stack)
call. This is because synth should only be called once - from the comment on thesynth
function: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