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(lambda-nodejs): improve spawnSync error message #16986

Merged

Conversation

blimmer
Copy link
Contributor

@blimmer blimmer commented Oct 14, 2021

This PR improves the error message when a lambda-nodejs function fails to bundle.

I'm working on moving a client's repository from a "makeshift monorepo" to a real yarn/lerna driven monorepo. They make heavy use of NodejsFunction, so I'm moving them to the newer reference project architecture with a single lockfile at the root of the repository.

While working through this issue, I kept running into this error message:

> yarn cdk diff
Bundling asset SomeLambdaHandler/SomeSubLambda/Lambda/Code/Stage...
Usage Error: Couldn't find a script named "esbuild".

$ yarn run [--inspect] [--inspect-brk] <scriptName> ...
Failed to bundle asset SomeLambdaHandler/SomeSubLambda/Lambda/Code/Stage, bundle output is located at /Users/blimmer/code/client/project/packages/some-workspace/cdk.out/bundling-temp-2f3ffba54d828547eb851ebe672a601943e153ec31fdc5e45f8e80ed976da6d3-error: Error: bash exited with status 1
Subprocess exited with error 1

This was confusing to me because I have esbuild installed in both sub-packages that require it. The error message just tells me that bash failed to run, which isn't very helpful.

However, when I set an interactive breakpoint, I got a lot more information about the failure. By digging into these arguments

export function exec(cmd: string, args: string[], options?: SpawnSyncOptions) {
const proc = spawnSync(cmd, args, options);
I can actually see what's being run and which directory it's run in.

argument contents comments
cmd bash this doesn't really tell me anything about what's happening
args [ -c, "yarn run esbuild --bundle \"/Users/blimmer/code/client/project/packages/some-workspace/lib/some-sub-lambda/lambda/index.ts\" --target=node14 --platform=node --outfile=\"/Users/blimmer/code/client/project/packages/some-workspace/cdk.out/bundling-temp-2f3ffba54d828547eb851ebe672a601943e153ec31fdc5e45f8e80ed976da6d3/index.js\" --external:aws-sdk" ] the second argument of this array actually represents the command being run - this is way more useful than just bash
options { ...lotsOfOtherStuff, cwd: '/Users/blimmer/code/client/project' } cwd was actually crucial for me to fix this problem. because I see this is running in the root of the monorepo, it shows that I need to install esbuild there, instead of in the workspaces.

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

@gitpod-io
Copy link

gitpod-io bot commented Oct 14, 2021

@blimmer blimmer force-pushed the blimmer/improve-spawn-sync-error-messages-node branch from 507769a to 58856fa Compare October 15, 2021 00:26
@peterwoodworth peterwoodworth changed the title chore(lambda-nodejs): improve spawnSync error message chore(lambda-nodejs): improve spawnSync error message Oct 21, 2021
@mergify
Copy link
Contributor

mergify bot commented Nov 5, 2021

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: AutoBuildProject89A8053A-LhjRyN9kxr8o
  • Commit ID: c61c3ee
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

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

@mergify mergify bot merged commit 8c7eab5 into aws:master Nov 5, 2021
@mergify
Copy link
Contributor

mergify bot commented Nov 5, 2021

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

iliapolo pushed a commit that referenced this pull request Nov 7, 2021
This PR improves the error message when a lambda-nodejs function fails to bundle.

I'm working on moving a client's repository from a "makeshift monorepo" to a real `yarn`/`lerna` driven monorepo. They make heavy use of `NodejsFunction`, so I'm moving them to the newer [reference project architecture](https://docs.aws.amazon.com/cdk/api/latest/docs/aws-lambda-nodejs-readme.html#reference-project-architecture) with a single lockfile at the root of the repository.

While working through this issue, I kept running into this error message:

```
> yarn cdk diff
Bundling asset SomeLambdaHandler/SomeSubLambda/Lambda/Code/Stage...
Usage Error: Couldn't find a script named "esbuild".

$ yarn run [--inspect] [--inspect-brk] <scriptName> ...
Failed to bundle asset SomeLambdaHandler/SomeSubLambda/Lambda/Code/Stage, bundle output is located at /Users/blimmer/code/client/project/packages/some-workspace/cdk.out/bundling-temp-2f3ffba54d828547eb851ebe672a601943e153ec31fdc5e45f8e80ed976da6d3-error: Error: bash exited with status 1
Subprocess exited with error 1
```

This was confusing to me because I have `esbuild` installed in both sub-packages that require it. The error message just tells me that `bash` failed to run, which isn't very helpful.

However, when I set an interactive breakpoint, I got a lot more information about the failure. By digging into these arguments https://github.com/aws/aws-cdk/blob/507769aa034ba3d8daa497953be629408072baed/packages/%40aws-cdk/aws-lambda-nodejs/lib/util.ts#L56-L57 I can actually see what's being run and which directory it's run in.

| argument | contents | comments |
| -------- | -------- | -------- |
| `cmd`    | `bash`   | this doesn't really tell me anything about what's happening |
| `args`   | [ `-c`, `"yarn run esbuild --bundle \"/Users/blimmer/code/client/project/packages/some-workspace/lib/some-sub-lambda/lambda/index.ts\" --target=node14 --platform=node --outfile=\"/Users/blimmer/code/client/project/packages/some-workspace/cdk.out/bundling-temp-2f3ffba54d828547eb851ebe672a601943e153ec31fdc5e45f8e80ed976da6d3/index.js\" --external:aws-sdk"` ] | the second argument of this array actually represents the command being run - this is way more useful than just `bash` |
| `options` | `{ ...lotsOfOtherStuff, cwd: '/Users/blimmer/code/client/project' }` | `cwd` was actually crucial for me to fix this problem. because I see this is running in the root of the monorepo, it shows that I need to install `esbuild` there, instead of in the workspaces. |

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this pull request Feb 21, 2022
This PR improves the error message when a lambda-nodejs function fails to bundle.

I'm working on moving a client's repository from a "makeshift monorepo" to a real `yarn`/`lerna` driven monorepo. They make heavy use of `NodejsFunction`, so I'm moving them to the newer [reference project architecture](https://docs.aws.amazon.com/cdk/api/latest/docs/aws-lambda-nodejs-readme.html#reference-project-architecture) with a single lockfile at the root of the repository.

While working through this issue, I kept running into this error message:

```
> yarn cdk diff
Bundling asset SomeLambdaHandler/SomeSubLambda/Lambda/Code/Stage...
Usage Error: Couldn't find a script named "esbuild".

$ yarn run [--inspect] [--inspect-brk] <scriptName> ...
Failed to bundle asset SomeLambdaHandler/SomeSubLambda/Lambda/Code/Stage, bundle output is located at /Users/blimmer/code/client/project/packages/some-workspace/cdk.out/bundling-temp-2f3ffba54d828547eb851ebe672a601943e153ec31fdc5e45f8e80ed976da6d3-error: Error: bash exited with status 1
Subprocess exited with error 1
```

This was confusing to me because I have `esbuild` installed in both sub-packages that require it. The error message just tells me that `bash` failed to run, which isn't very helpful.

However, when I set an interactive breakpoint, I got a lot more information about the failure. By digging into these arguments https://github.com/aws/aws-cdk/blob/507769aa034ba3d8daa497953be629408072baed/packages/%40aws-cdk/aws-lambda-nodejs/lib/util.ts#L56-L57 I can actually see what's being run and which directory it's run in.

| argument | contents | comments |
| -------- | -------- | -------- |
| `cmd`    | `bash`   | this doesn't really tell me anything about what's happening |
| `args`   | [ `-c`, `"yarn run esbuild --bundle \"/Users/blimmer/code/client/project/packages/some-workspace/lib/some-sub-lambda/lambda/index.ts\" --target=node14 --platform=node --outfile=\"/Users/blimmer/code/client/project/packages/some-workspace/cdk.out/bundling-temp-2f3ffba54d828547eb851ebe672a601943e153ec31fdc5e45f8e80ed976da6d3/index.js\" --external:aws-sdk"` ] | the second argument of this array actually represents the command being run - this is way more useful than just `bash` |
| `options` | `{ ...lotsOfOtherStuff, cwd: '/Users/blimmer/code/client/project' }` | `cwd` was actually crucial for me to fix this problem. because I see this is running in the root of the monorepo, it shows that I need to install `esbuild` there, instead of in the workspaces. |

----

*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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants