-
Notifications
You must be signed in to change notification settings - Fork 4.4k
(cli): watch command not working properly with NodeJsFunction #17391
Description
What is the problem?
Hi, I tried new cdk watch command but it's not working as expected. Here's the detail:
When we cdk watch a stack with a NodeJsFunction, the resulting asset of Lambda code becomes broken.
Note that cdk deploy --watch seems working fine, so it isn't a big issue.
Reproduction Steps
cdk watch a stack with NodeJsFunction, and then modify lambdas/handler/index.ts to trigger a new deploy.
import * as cdk from "@aws-cdk/core";
import * as lambda from "@aws-cdk/aws-lambda-nodejs";
export class TestStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const handler = new lambda.NodejsFunction(this, "Handler", {
entry: "lambdas/handler/index.ts"
});
}
}in lambdas/handler/ directory, there is only a file index.ts
export const handler = async () => {
return ""
}What did you expect to happen?
The resulting asset contains only files related to the Lambda function such as index.js.
What actually happened?
The resulting asset contains all the files in the CDK root directory.
CDK CLI Version
1.131.0
Framework Version
1.131.0
Node.js Version
v14.15.0
OS
macOS
Language
Typescript
Language Version
No response
Other information
I found why it's happening as below:
-
When synthesizing assets, bundling code is skipped because
skip = trueis passed. When skipped, the resulting asset would become entire CDK root directory in my case.
aws-cdk/packages/@aws-cdk/core/lib/asset-staging.ts
Lines 305 to 320 in d231b9e
if (skip) { // We should have bundled, but didn't to save time. Still pretend to have a hash. // If the asset uses OUTPUT or BUNDLE, we use a CUSTOM hash to avoid fingerprinting // a potentially very large source directory. Other hash types are kept the same. let hashType = this.hashType; if (hashType === AssetHashType.OUTPUT || hashType === AssetHashType.BUNDLE) { this.customSourceFingerprint = Names.uniqueId(this); hashType = AssetHashType.CUSTOM; } return { assetHash: this.calculateHash(hashType, bundling), stagedPath: this.sourcePath, packaging: FileAssetPackaging.ZIP_DIRECTORY, isArchive: true, }; } -
the
skipflag becomestruebecausethis.node.tryGetContext(cxapi.BUNDLING_STACKS)is[]
aws-cdk/packages/@aws-cdk/core/lib/asset-staging.ts
Lines 189 to 194 in d231b9e
if (props.bundling) { // Check if we actually have to bundle for this stack const bundlingStacks: string[] = this.node.tryGetContext(cxapi.BUNDLING_STACKS) ?? ['*']; skip = !bundlingStacks.find(pattern => minimatch(Stack.of(this).stackName, pattern)); const bundling = props.bundling; stageThisAsset = () => this.stageByBundling(bundling, skip); -
this.node.tryGetContext(cxapi.BUNDLING_STACKS)is[]becauseBUNDLING_COMMANDSdoes not includewatchcommand.
aws-cdk/packages/aws-cdk/lib/settings.ts
Lines 253 to 261 in d231b9e
if (BUNDLING_COMMANDS.includes(argv._[0])) { // If we deploy, diff or synth a list of stacks exclusively we skip // bundling for all other stacks. bundlingStacks = argv.exclusively ? argv.STACKS ?? ['*'] : ['*']; } else { // Skip bundling for all stacks bundlingStacks = []; }
aws-cdk/packages/aws-cdk/lib/settings.ts
Lines 35 to 40 in d231b9e
const BUNDLING_COMMANDS = [ Command.DEPLOY, Command.DIFF, Command.SYNTH, Command.SYNTHESIZE, ];
So I guess we can solve this issue by something like adding watch to BUNDLING_COMMANDS array. (I don't know if there'd be some side effects though...)
btw it worked well with cdk deploy --watch command, thanks for a great new feature!