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

chore(core): allow the bundler to re-use pre-existing bundler output #8916

Merged
merged 18 commits into from
Jul 13, 2020

Conversation

misterjoshua
Copy link
Contributor

@misterjoshua misterjoshua commented Jul 6, 2020

This PR changes AssetStaging so that the bundler will re-use pre-existing output. Before, the bundler would re-run Docker without considering pre-existing assets, which was slow. Now, when handling a SOURCE hash type, the bundler detects and returns pre-existing asset output without re-running Docker. For all other hash types, the bundler outputs to a temp directory before calculating asset hashes.

Closes #8882


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

@mergify
Copy link
Contributor

mergify bot commented Jul 6, 2020

Title does not follow the guidelines of Conventional Commits. Please adjust title before merge.

@misterjoshua misterjoshua changed the title feature(core): allow the bundler to re-use pre-existing bundler output feat(core): allow the bundler to re-use pre-existing bundler output Jul 6, 2020
@misterjoshua misterjoshua changed the title feat(core): allow the bundler to re-use pre-existing bundler output chore(core): allow the bundler to re-use pre-existing bundler output Jul 6, 2020
Copy link
Contributor

@jogold jogold left a comment

Choose a reason for hiding this comment

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

Looks good

packages/@aws-cdk/core/lib/asset-staging.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/core/lib/asset-staging.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/core/lib/asset-staging.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/core/test/test.staging.ts Outdated Show resolved Hide resolved
misterjoshua and others added 2 commits July 7, 2020 10:18
Co-authored-by: Jonathan Goldwasser <jogold@users.noreply.github.com>
@misterjoshua
Copy link
Contributor Author

misterjoshua commented Jul 7, 2020

@jogold After your review, I was thinking about the failure modes and looking closely at the throws when bundling fails test. I think we need to remove the bundleDir if the bundler fails. Otherwise, subsequent runs may falsely believe that the bundleDir can be re-used. I'll push up a commit that does as much in a few minutes.

@jogold
Copy link
Contributor

jogold commented Jul 8, 2020

One more thing that comes to mind: what if the BundlingOptions are updated? I would expect the asset not be re-used if I change the command for example.

Shouldn't we hash those options together with the custom or source hash and use this final hash in the bundleDir name?

cc @eladb

@eladb
Copy link
Contributor

eladb commented Jul 8, 2020

Shouldn't we hash those options together with the custom or source hash and use this final hash in the bundleDir name?

Makes sense

packages/@aws-cdk/core/lib/asset-staging.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/core/lib/asset-staging.ts Outdated Show resolved Hide resolved
@@ -179,6 +200,7 @@ export class AssetStaging extends Construct {
workingDirectory: options.workingDirectory ?? AssetStaging.BUNDLING_INPUT_DIR,
});
} catch (err) {
fs.removeSync(bundleDir);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is unrelated to this PR, right?
I am also not sure we actually want to delete the bundleDir for diagnosability (I would even include it in the error message).

Copy link
Contributor

Choose a reason for hiding this comment

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

It is, see #8916 (comment)

Otherwise, subsequent runs may falsely believe that the bundleDir can be re-used.

Rename it in case of failure instead (e.g. append -error)?

Copy link
Contributor Author

@misterjoshua misterjoshua Jul 8, 2020

Choose a reason for hiding this comment

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

Rename it in case of failure instead (e.g. append -error)?

This seems good to me. But, if we go this route, we might consider when to remove -error directories if they already exist, such as if docker fails twice for the same bundleDir.

Alternatively, is there a unique number or code I can access for each CDK run? If there were, I could append that number and the error output can be held indefinitely for diagnosability.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For now I've implemented the -error suffix. I'm happy to circle back around.

packages/@aws-cdk/core/lib/asset-staging.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/core/lib/asset-staging.ts Outdated Show resolved Hide resolved
Co-authored-by: Elad Ben-Israel <benisrae@amazon.com>
@misterjoshua
Copy link
Contributor Author

misterjoshua commented Jul 8, 2020

@jogold Any idea why the test throws with assetHash and not CUSTOM hash type expects Docker to have been invoked even though it's an error state? What am I missing? Given that the test seems to be to check for a configuration error, the code I've committed assumes this docker run isn't necessary. However, I can circle around to handle this nuance if needed.

@jogold
Copy link
Contributor

jogold commented Jul 8, 2020

@jogold Any idea why the test throws with assetHash and not CUSTOM hash type expects Docker to have been invoked even though it's an error state? What am I missing? Given that the test seems to be to check for a configuration error, the code I've committed assumes this docker run isn't necessary. However, I can circle around to handle this nuance if needed.

It passed because hash calculation always occurred after bundling, it can be safely removed from the expectations. Looks like it was wrongly introduced here #8481.

Copy link
Contributor

@jogold jogold left a comment

Choose a reason for hiding this comment

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

@misterjoshua 👍

To fix the build error you can run the integ tests in s3-assets and aws-lambda (the failing tests will be integ.assets.bundling.lit.js and integ.bundling.js respectively) or merge from master (see #8969).

@eladb LGTM

We should maybe add a note about this new "behavior" here?

/**
* Specifies the type of hash to calculate for this asset.
*
* If `assetHash` is configured, this option must be `undefined` or
* `AssetHashType.CUSTOM`.
*
* @default - the default is `AssetHashType.SOURCE`, but if `assetHash` is
* explicitly specified this value defaults to `AssetHashType.CUSTOM`.
*/
readonly assetHashType?: AssetHashType;

@misterjoshua
Copy link
Contributor Author

To fix the build error you can run the integ tests in s3-assets and aws-lambda (the failing tests will be integ.assets.bundling.lit.js and integ.bundling.js respectively) or merge from master (see #8969).

@jogold I merged from master but the integration tests still failed. I looked at #8969 and added the new pragma to the top of each offending test file to enable the feature. I butchered my commit message, but in any case, those tests seem to work now. I hope that was the right move.

@mergify
Copy link
Contributor

mergify bot commented Jul 13, 2020

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: 7a5bc94
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

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

@mergify
Copy link
Contributor

mergify bot commented Jul 13, 2020

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@eladb
Copy link
Contributor

eladb commented Jul 13, 2020

Hey @misterjoshua @jogold, we are reverting this PR through #9037 - please revisit.

eladb pushed a commit that referenced this pull request Jul 13, 2020
… output (#8916)" (#9037)

This logic to reuse bundler output is faulty. Once an asset is bundled, the bundle directory is moved into the cloud assembly (cdk.out) and therefore the logic that attempts to reuse the bundle directory only materializes in the case where moveSync decides to copy the directory and not just move it.

This reverts commit 31d6e65.
@jogold
Copy link
Contributor

jogold commented Jul 13, 2020

@misterjoshua see #9037 (comment)

I suggest waiting for #8962 before revisiting this.

Actually I think it will be easier to implement if the assembly dir is know at construction time.

@jogold
Copy link
Contributor

jogold commented Aug 10, 2020

@misterjoshua do you want to give this another try? We can now do everything in cdk.out, no need for .cdk.staging.

Let me know.

@misterjoshua
Copy link
Contributor Author

@misterjoshua do you want to give this another try? We can now do everything in cdk.out, no need for .cdk.staging.

Let me know.

Hey @jogold. Thanks for letting me know. I'll open a new PR and mention you in it after I've made some progress.

curtiseppel pushed a commit to curtiseppel/aws-cdk that referenced this pull request Aug 11, 2020
…ws#8916)

This PR changes `AssetStaging` so that the bundler will re-use pre-existing output. Before, the bundler would re-run Docker without considering pre-existing assets, which was slow. Now, when handling a `SOURCE` hash type, the bundler detects and returns pre-existing asset output without re-running Docker. For all other hash types, the bundler outputs to a temp directory before calculating asset hashes.

Closes aws#8882 

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
curtiseppel pushed a commit to curtiseppel/aws-cdk that referenced this pull request Aug 11, 2020
… output (aws#8916)" (aws#9037)

This logic to reuse bundler output is faulty. Once an asset is bundled, the bundle directory is moved into the cloud assembly (cdk.out) and therefore the logic that attempts to reuse the bundle directory only materializes in the case where moveSync decides to copy the directory and not just move it.

This reverts commit 31d6e65.
mergify bot pushed a commit that referenced this pull request Aug 26, 2020
…(revisited) (#9576)

This PR changes `AssetStaging` so that the bundler will re-use pre-existing output. Before, the bundler would re-run Docker without considering pre-existing assets, which was slow. Now, when handling a `SOURCE` hash type, the bundler detects and returns pre-existing asset output without re-running Docker. For all other hash types, the bundler outputs to an intermediate directory before calculating asset hashes, then renames the intermediate directory into its final location.

This PR revisits #8916 which originally closed #8882. Here are some details from the previous PR which have been addressed in this PR:

- The bundler now outputs directly into the assembly directory
- The bundler's assets can be reused between multiple syntheses 
- The bundler keeps output from failed bundling attempts for diagnosability purposes (renamed with an `-error` suffix)
- Bundler options are hashed together with custom and source hashes
- Removed the check for a docker run from `throws with assetHash and not CUSTOM hash type` as docker is no longer run before the AssetStaging props are validated.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[core] make the asset bundler able to re-use assets
4 participants