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

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

Open
aleguern-azivko opened this issue Sep 26, 2024 · 4 comments
Open

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

aleguern-azivko opened this issue Sep 26, 2024 · 4 comments
Labels
area/build sam build command type/bug

Comments

@aleguern-azivko
Copy link

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 aleguern-azivko added the stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. label Sep 26, 2024
@aleguern-azivko
Copy link
Author

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
Copy link
Contributor

hawflau commented Oct 3, 2024

@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

@hawflau hawflau added area/build sam build command type/bug and removed stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. labels Oct 3, 2024
@aleguern-azivko
Copy link
Author

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

@aleguern-azivko
Copy link
Author

any progress on this @hawflau ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/build sam build command type/bug
Projects
None yet
Development

No branches or pull requests

2 participants