Skip to content

(cli): watch command not working properly with NodeJsFunction #17391

@tmokmss

Description

@tmokmss

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:

  1. When synthesizing assets, bundling code is skipped because skip = true is passed. When skipped, the resulting asset would become entire CDK root directory in my case.

    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,
    };
    }

  2. the skip flag becomes true because this.node.tryGetContext(cxapi.BUNDLING_STACKS) is []

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

  3. this.node.tryGetContext(cxapi.BUNDLING_STACKS) is [] because BUNDLING_COMMANDS does not include watch command.

    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 = [];
    }

    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!

Metadata

Metadata

Assignees

Labels

bugThis issue is a bug.effort/smallSmall work item – less than a day of effortp2package/toolsRelated to AWS CDK Tools or CLI

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions