Skip to content
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

Merged
merged 25 commits into from
Oct 29, 2024

Conversation

sumupitchayan
Copy link
Contributor

@sumupitchayan sumupitchayan commented Oct 23, 2024

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


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
@aws-cdk-automation aws-cdk-automation requested a review from a team October 23, 2024 14:54
@github-actions github-actions bot added bug This issue is a bug. effort/medium Medium work item – several days of effort p1 labels Oct 23, 2024
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Oct 23, 2024
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a 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.

@sumupitchayan sumupitchayan added the pr-linter/exempt-integ-test The PR linter will not require integ test changes label Oct 23, 2024
@aws-cdk-automation aws-cdk-automation dismissed their stale review October 23, 2024 14:58

✅ 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>
@@ -186,6 +186,27 @@ test('CodeBuild: environment variables specified in multiple places are correctl
}),
});

new ModernTestGitHubNpmPipeline(pipelineStack, 'Cdk-2', {
Copy link
Contributor Author

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's perfect!

@sumupitchayan sumupitchayan changed the title fix(assertions): remove assembly caching from synth fix(assertions): throw error or warn when synth is called multiple times on mutated construct tree Oct 28, 2024
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
@sumupitchayan sumupitchayan marked this pull request as ready for review October 28, 2024 16:42
packages/aws-cdk-lib/core/lib/stage.ts Outdated Show resolved Hide resolved
packages/aws-cdk-lib/core/lib/stage.ts Outdated Show resolved Hide resolved
packages/aws-cdk-lib/core/lib/stage.ts Outdated Show resolved Hide resolved
packages/aws-cdk-lib/core/lib/stage.ts Outdated Show resolved Hide resolved
sumupitchayan and others added 5 commits October 28, 2024 14:47
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);
Copy link
Contributor

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);
Copy link
Contributor

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:

Copy link
Contributor

@mrgrain mrgrain left a 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.

sumupitchayan and others added 4 commits October 29, 2024 11:43
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 mrgrain added the pr/do-not-merge This PR should not be merged at this time. label Oct 29, 2024
Copy link
Contributor

@mrgrain mrgrain left a 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 <sumughan@amazon.com>
@sumupitchayan
Copy link
Contributor Author

Conditionally approved until you've checked that addWarningV2() really doesn't work.

Just tested and confirmed that addWarningV2() doesn't work - going with Rico's console.error() suggestion

@sumupitchayan sumupitchayan removed the pr/do-not-merge This PR should not be merged at this time. label Oct 29, 2024
@sumupitchayan sumupitchayan self-assigned this Oct 29, 2024
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 4fec523
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Copy link
Contributor

mergify bot commented Oct 29, 2024

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).

@mergify mergify bot merged commit a261c9d into main Oct 29, 2024
15 checks passed
@mergify mergify bot deleted the sumughan/fix-assertions-unable-to-find-artifact branch October 29, 2024 22:31
Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue is a bug. contribution/core This is a PR that came from AWS. effort/medium Medium work item – several days of effort p1 pr-linter/exempt-integ-test The PR linter will not require integ test changes
Projects
None yet
4 participants