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.5k stars 1.17k forks source link

`BuildMethod: esbuild` does not include bundled files #7143

Open jghaines opened 3 months ago

jghaines commented 3 months ago

Description:

Some npm packages, or lambda source modules, include bundled files.

When using Metadata.BuildMethod: esbuild these bundled files are not included in the deployment package

When using default javascript (without esbuild) these bundled files are correctly included.

Steps to reproduce:

The bug is revealed when using the following nodejs20.x sam-app-templates:

To each templated-app I add the npm module errorhandler which depends upon bundled ./public/* files to initialise.

uname -a
# Darwin Macbook2022.local 23.5.0 Darwin Kernel Version 23.5.0: Wed May  1 20:19:05 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T8112 arm64
sam --version
# SAM CLI, version 1.118.0

APP_TEMPLATE=hello-world-typescript
APP_SOURCE=app.ts

# Deploy and invoke - default template
sam init --app-template "${APP_TEMPLATE}" --name sam-app --package-type Zip --runtime nodejs20.x --no-tracing --no-application-insights --no-structured-logging
cd sam-app
sam build
sam deploy --no-confirm-changeset
curl $(aws cloudformation describe-stacks --stack-name sam-app --query 'Stacks[0].Outputs[?OutputKey==`HelloWorldApi`].OutputValue' --output text)
# {"message":"hello world"}%
sam logs
# no error messages

# Install errorhandler library - an npm library with included ./public/* files
cd ./hello-world
npm install
npm i errorhandler
npm i --save-dev @types/errorhandler
echo "import errorhandler from 'errorhandler';" > temp.app
echo "errorhandler();" >> temp.app
cat "${APP_SOURCE}" >> temp.app
mv -f temp.app "${APP_SOURCE}"
npm run test
cd ..

# Deploy and invoke - with errorhandler library installed
sam build
find .aws-sam -name public
# .aws-sam/deps/7cd4d57c-fe7c-49ed-ac9f-838c83e8e788/node_modules/errorhandler/public
sam deploy --no-confirm-changeset
curl $(aws cloudformation describe-stacks --stack-name sam-app --query 'Stacks[0].Outputs[?OutputKey==` HelloWorldApi`].OutputValue' --output text)
# {"message": "Internal server error"}%
sam logs
# see error below

sam delete --no-prompts
cd ..
rm -rf sam-app

Observed result:

# Deploy and invoke - default template
% curl ....
{"message":"hello world"}%

% sam logs
# no error messages

# Deploy and invoke - with errorhandler library installed
% curl
{"message": "Internal server error"}%
% sam logs
...
2024/06/07/[$LATEST]28ff1ed338e644a8a83d55aaf2d8ae91 2024-06-07T02:09:14.194000 INIT_START Runtime Version: nodejs:20.v23       Runtime Version ARN: arn:aws:lambda:ap-southeast-2::runtime:92c5bcb1529200756eb64a0d90d4ab606fdaf21421321da6c202187b88833f52
2024/06/07/[$LATEST]28ff1ed338e644a8a83d55aaf2d8ae91 2024-06-07T02:09:14.313000 2024-06-07T02:09:14.313Z        undefined       ERROR   Uncaught Exception      {"errorType":"Error","errorMessage":"ENOENT: no such file or directory, open '/var/task/public/style.css'","code":"ENOENT","errno":-2,"syscall":"open","path":"/var/task/public/style.css","stack":["Error: ENOENT: no such file or directory, open '/var/task/public/style.css'","    at Object.readFileSync (node:fs:448:20)","    at <anonymous> (/private/var/folders/78/lz3qk5tn1cb7vc1b2l77nvy80000gn/T/tmpo3qdql_y/node_modules/errorhandler/index.js:30:21)","    at /var/task/app.js:1:221","    at Object.<anonymous> (/private/var/folders/78/lz3qk5tn1cb7vc1b2l77nvy80000gn/T/tmpo3qdql_y/app.ts:1:26)","    at Module._compile (node:internal/modules/cjs/loader:1358:14)","    at Module._extensions..js (node:internal/modules/cjs/loader:1416:10)","    at Module.load (node:internal/modules/cjs/loader:1208:32)","    at Module._load (node:internal/modules/cjs/loader:1024:12)","    at Module.require (node:internal/modules/cjs/loader:1233:19)","    at require (node:internal/modules/helpers:179:18)"]}
2024/06/07/[$LATEST]28ff1ed338e644a8a83d55aaf2d8ae91 2024-06-07T02:09:14.339000 INIT_REPORT Init Duration: 145.47 ms    Phase: init     Status: error   Error Type: Runtime.Unknown

Expected result:

Repeating the above steps with the default javascript app-template (no typescript/esbuild) works as expected:

APP_TEMPLATE=hello-world
APP_SOURCE=app.mjs

# Deploy and invoke - default template
sam init --app-template "${APP_TEMPLATE}" --name sam-app --package-type Zip --runtime nodejs20.x --no-tracing --no-application-insights --no-structured-logging
...
% curl ...
{"message":"hello world"}%
% sam logs
# no error messages

# Deploy and invoke - with errorhandler library installed
...
% curl ...
{"message":"hello world"}%
% sam logs
# no error messages

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: uname -a : Darwin Macbook2022.local 23.5.0 Darwin Kernel Version 23.5.0: Wed May 1 20:19:05 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T8112 arm64
  2. sam --version: SAM CLI, version 1.118.0
  3. AWS region: ap-southeast-2
{
  "version": "1.118.0",
  "system": {
    "python": "3.8.13",
    "os": "macOS-14.5-x86_64-i386-64bit"
  },
  "additional_dependencies": {
    "docker_engine": "24.0.7",
    "aws_cdk": "Not available",
    "terraform": "Not available"
  },
  "available_beta_feature_env_vars": [
    "SAM_CLI_BETA_FEATURES",
    "SAM_CLI_BETA_BUILD_PERFORMANCE",
    "SAM_CLI_BETA_TERRAFORM_SUPPORT",
    "SAM_CLI_BETA_RUST_CARGO_LAMBDA"
  ]
}
sidhujus commented 3 months ago

Hi @jghaines Thanks for the detailed reproduction steps! I was able to reproduce the issue and will mark it as a bug for the team to prioritize a fix

sidhujus commented 3 months ago

https://github.com/aws/aws-lambda-builders/issues/516 Looking into it a bit further this seems like it falls under this feature request.