aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.52k stars 1.17k forks source link

Issue with sam build --build-in-source #7516

Open aleguern-azivko opened 1 month ago

aleguern-azivko commented 1 month ago

When running sam build --build-in-source, the build process fails for the lambda function with an esbuild error. However, sam build without the --build-in-source flag works successfully.

Steps to reproduce: Set up a SAM project with multiple lambda functions using esbuild. Run sam build --build-in-source

Expected behavior: The build process should complete successfully for all lambda functions.

Actual behavior:

The build fails with the following error:
CopyNodejsNpmEsbuildBuilder:EsbuildBundle failed
EsbuildExecutionError: Esbuild Failed: ✘ [ERROR] Could not resolve "@sendgrid/client"

node_modules/@sendgrid/mail/src/classes/mail-service.js:6:25:
  6 │ const {Client} = require('@sendgrid/client');
    ╵                          ~~~~~~~~~~~~~~~~~~

You can mark the path "@sendgrid/client" as external to exclude it from the bundle, which will remove this error. You can also surround this "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.

Environment: npm version: 10.5.0 SAM CLI version: 1.124.0 Operating System: macOS

Relevant configuration: SAM template snippet for the Contact function:

  Type: AWS::Serverless::Function
  Properties:
    Environment:
      Variables:
        SENDGRID_API_KEY: !Ref SendGridApiKey
    Events:
      Contact:
        Type: Api
        Properties:
          RestApiId: !Ref Api
          Path: /contact
          Method: post
          Auth:
            Authorizer: NONE
  Metadata:
    BuildMethod: esbuild
    BuildProperties:
      Minify: true
      Target: 'es2022'
      Sourcemap: false
      EntryPoints:
        - src/public/contact/index.ts

Relevant dependencies from package.json:

"dependencies": {
  "@sendgrid/mail": "^7.7.0",
  "esbuild": "^0.17.17"
},
"devDependencies": {
  "@types/aws-lambda": "^8.10.145",
  "typescript": "^4.5.5"
}
aleguern-azivko commented 1 month ago

Nevermind I found the issue, I had a samconfig.toml file in the project with parallel:true, and npm failed to build with this parameter. You can close the issue if it not considered a bug.

hawflau commented 1 month ago

@aleguern-azivko thanks for raising issue and for finding the root cause. If build-in-source doesn't work well with parallel, I believe we should check and not allow them to be active at the same time. Marking it as a bug

aleguern-azivko commented 1 month ago

To add a little bit of context, the bug occurred using typescript and npm, didn’t try other languages.

aleguern-azivko commented 1 week ago

any progress on this @hawflau ?