Skip to content

@aws-cdk/aws-lambda-nodejs: bundling fails when adding a workspace or file to nodeModules list #17830

@joshbalfour

Description

@joshbalfour

First off, thanks for all the work you all do on CDK, I have so many good things to say about it, it really is the future of IaC ❤️
This might be an edge case, but I really want to get aws-lambda-nodejs working for us, when it is it'll make our cdk setup super sleak!
I'll figure out a workaround and update the issue when I do, and hopefully be able to actually contribute towards a solution.

What is the problem?

We have a monorepo setup with all our packages in a packages directory.
When I have a dependency listed in the nodeModules attribute of bundling which is in our workspace
The generated package.json looks like this:

{"dependencies":{"espree":"7.3.1","@reapit/app-builder":"workspace:packages/app-builder"}}

and the yarn install --no-immutable which runs in the bundling-temp directory fails with:

➤ YN0000: ┌ Resolution step
➤ YN0001: │ Error: @reapit/app-builder@workspace:packages/app-builder: Workspace not found (@reapit/app-builder@workspace:packages/app-builder)
    at ie.getWorkspaceByDescriptor (/Users/joshbalfour/src/foundations/.yarn/releases/yarn-2.4.2.cjs:2:360510)
    at n.getCandidates (/Users/joshbalfour/src/foundations/.yarn/releases/yarn-2.4.2.cjs:2:408948)
    at n.getCandidates (/Users/joshbalfour/src/foundations/.yarn/releases/yarn-2.4.2.cjs:2:342946)
    at n.getCandidates (/Users/joshbalfour/src/foundations/.yarn/releases/yarn-2.4.2.cjs:2:342946)
    at /Users/joshbalfour/src/foundations/.yarn/releases/yarn-2.4.2.cjs:2:364602
    at Module.w (/Users/joshbalfour/src/foundations/.yarn/releases/yarn-2.4.2.cjs:2:424073)
    at C (/Users/joshbalfour/src/foundations/.yarn/releases/yarn-2.4.2.cjs:2:364565)
➤ YN0000: └ Completed
➤ YN0000: Failed with errors in 0s 63ms

When I try to use the same package with file instead of workspace the generated package.json looks like this:

{"dependencies":{"espree":"7.3.1","@reapit/app-builder":"file:../app-builder"}}

and the yarn install --no-immutable which runs in the bundling-temp directory fails with:

➤ YN0000: ┌ Resolution step
➤ YN0001: │ Error: @reapit/app-builder@file:../app-builder::locator=root-workspace-0b6124%40workspace%3A.: ENOENT: no such file or directory, lstat '/Users/joshbalfour/src/foundations/packages/app-builder-backend/cdk.out/app-builder'
➤ YN0000: └ Completed
➤ YN0000: Failed with errors in 0s 100ms

It works fine when using normal packages from the registry.

Reproduction Steps

I've created a stripped-down monorepo which reproduces this. It's a bit big because of the .yarn/cache folder, sorry.

What did you expect to happen?

The workspace/file packages referenced in my package.json should be installed correctly for my bundled code.

What actually happened?

It errored, as above.

CDK CLI Version

1.134.0

Framework Version

No response

Node.js Version

v14.18.1

OS

macOS Monterey 12.0.1

Language

Typescript

Language Version

4.3.5

Other information

full stack trace:

/Users/joshbalfour/src/foundations/packages/app-builder-backend/node_modules/@aws-cdk/core/lib/asset-staging.ts:398
      throw new Error(`Failed to bundle asset ${this.node.path}, bundle output is located at ${bundleErrorDir}: ${err}`);
            ^
Error: Failed to bundle asset cloud-foundations-app-builder-backend/graphql/Code/Stage, bundle output is located at /Users/joshbalfour/src/foundations/packages/app-builder-backend/cdk.out/bundling-temp-945296717e7e756e9c8781e96618fcf8a2c031e882066df54ce9b0b2c98ee49e-error: Error: bash -c yarn run esbuild --bundle "/Users/joshbalfour/src/foundations/packages/app-builder-backend/src/lambda.js" --target=node14 --platform=node --outfile="/Users/joshbalfour/src/foundations/packages/app-builder-backend/cdk.out/bundling-temp-945296717e7e756e9c8781e96618fcf8a2c031e882066df54ce9b0b2c98ee49e/index.js" --external:aws-sdk --external:espree --external:@reapit/app-builder && echo '{"dependencies":{"espree":"7.3.1","@reapit/app-builder":"file:../app-builder"}}' > /Users/joshbalfour/src/foundations/packages/app-builder-backend/cdk.out/bundling-temp-945296717e7e756e9c8781e96618fcf8a2c031e882066df54ce9b0b2c98ee49e/package.json && cp /Users/joshbalfour/src/foundations/yarn.lock /Users/joshbalfour/src/foundations/packages/app-builder-backend/cdk.out/bundling-temp-945296717e7e756e9c8781e96618fcf8a2c031e882066df54ce9b0b2c98ee49e/yarn.lock && cd /Users/joshbalfour/src/foundations/packages/app-builder-backend/cdk.out/bundling-temp-945296717e7e756e9c8781e96618fcf8a2c031e882066df54ce9b0b2c98ee49e && yarn install --no-immutable run in directory /Users/joshbalfour/src/foundations exited with status 1
    at AssetStaging.bundle (/Users/joshbalfour/src/foundations/packages/app-builder-backend/node_modules/@aws-cdk/core/lib/asset-staging.ts:398:13)
    at AssetStaging.stageByBundling (/Users/joshbalfour/src/foundations/packages/app-builder-backend/node_modules/@aws-cdk/core/lib/asset-staging.ts:246:10)
    at stageThisAsset (/Users/joshbalfour/src/foundations/packages/app-builder-backend/node_modules/@aws-cdk/core/lib/asset-staging.ts:137:35)
    at Cache.obtain (/Users/joshbalfour/src/foundations/packages/app-builder-backend/node_modules/@aws-cdk/core/lib/private/cache.ts:24:13)
    at new AssetStaging (/Users/joshbalfour/src/foundations/packages/app-builder-backend/node_modules/@aws-cdk/core/lib/asset-staging.ts:162:44)
    at new Asset (/Users/joshbalfour/src/foundations/packages/app-builder-backend/node_modules/@aws-cdk/aws-s3-assets/lib/asset.ts:84:21)
    at AssetCode.bind (/Users/joshbalfour/src/foundations/packages/app-builder-backend/node_modules/@aws-cdk/aws-lambda/lib/code.ts:183:20)
    at new Function (/Users/joshbalfour/src/foundations/packages/app-builder-backend/node_modules/@aws-cdk/aws-lambda/lib/function.ts:341:29)
    at new NodejsFunction (/Users/joshbalfour/src/foundations/packages/app-builder-backend/node_modules/@aws-cdk/aws-lambda-nodejs/lib/function.ts:53:5)
    at Object.createFunction (/Users/joshbalfour/src/foundations/packages/app-builder-backend/cdk/components/function.ts:12:10)
Subprocess exited with error 1

Metadata

Metadata

Assignees

Labels

@aws-cdk/aws-workspacesRelated Amazon WorkSpacesbugThis issue is a bug.needs-triageThis issue or PR still needs to be triaged.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions