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

BuildMethod: esbuild does not include bundled files #7143

Open
jghaines opened this issue Jun 7, 2024 · 2 comments
Open

BuildMethod: esbuild does not include bundled files #7143

jghaines opened this issue Jun 7, 2024 · 2 comments
Labels
area/build sam build command area/esbuild type/feature Feature request

Comments

@jghaines
Copy link

jghaines commented Jun 7, 2024

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:

  • hello-world - javascript, works
  • hello-world-typescript - typescript/esbuild, error

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"
  ]
}
@jghaines jghaines added the stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. label Jun 7, 2024
@sidhujus
Copy link
Contributor

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 sidhujus added type/bug area/build sam build command area/esbuild and removed stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. labels Jun 12, 2024
@sidhujus
Copy link
Contributor

aws/aws-lambda-builders#516 Looking into it a bit further this seems like it falls under this feature request.

@sidhujus sidhujus added type/feature Feature request and removed type/bug labels Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/build sam build command area/esbuild type/feature Feature request
Projects
None yet
Development

No branches or pull requests

2 participants