aws-actions / setup-sam

Action to set up AWS SAM CLI and add it to the PATH
Apache License 2.0
151 stars 23 forks source link

Error: NodejsNpmEsbuildBuilder:EsbuildBundle - Esbuild Failed in GitHub Actions #78

Closed aleguern closed 1 year ago

aleguern commented 1 year ago

Hello,

I am encountering an error while using Sam build on GitHub Actions with the AWS-actions/setup-sam. Strangely, the build process works perfectly fine on my local machine using Sam build. However, when the same build process is executed on GitHub Actions, I receive the following error:

"Error: NodejsNpmEsbuildBuilder:EsbuildBundle - Esbuild Failed: Cannot find esbuild. esbuild must be installed on the host machine to use this feature. It is recommended to be installed on the PATH, but can also be included as a project dependency."

I have double-checked that esbuild is installed on my local machine and even included it as a project dependency, which allows me to perform successful builds locally. However, the error only occurs within the GitHub Actions environment.

I kindly request your assistance and guidance in troubleshooting this perplexing issue. If anyone has encountered a similar situation or possesses insights into potential solutions, I would greatly appreciate your input.

Thank you for your support.

hoffa commented 1 year ago

Per the error message:

esbuild must be installed on the host machine to use this feature.

Do you have eslint installed on the machine running the workflow?

aleguern commented 1 year ago

Yes I did install it with npm i -g esbuild, is there a different way to do this ?

hoffa commented 1 year ago

So to be clear, esbuild is installed in the GitHub Actions environment (not only on development machine), and is in the PATH (i.e. you can use esbuild and it'll call the executable correctly)?

aleguern commented 1 year ago

I run npm i -g esbuild and then run esbuild --version it displays the correct version 0.18.6

hoffa commented 1 year ago

Can you include the relevant parts of your GitHub workflow? Specifically, where eslint and SAM CLI is installed, and where SAM CLI is called.

aleguern commented 1 year ago
jobs:
  build-and-package:
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - run: npm install
      - run: npm i -g esbuild
      - run: esbuild --version
      - uses: aws-actions/setup-sam@v2
        with:
          use-installer: true
      - name: Build resources
        run: sam build --template ${SAM_TEMPLATE} --use-container
hoffa commented 1 year ago

With --use-container the project will be built in a container, so whatever is installed on the host won't be available in it. It should work if you make esbuild a project dependency, so that it's picked up in the container as well.

aleguern commented 1 year ago

Thanks for your advice, I’m not sure what I’m supposed to do, could you tell me how to do this ?

hoffa commented 1 year ago

Try adding it as a dependency to your package.json.

aleguern commented 1 year ago

That’s already what I did and not working, I tried adding it to dependencies and devDependencies.

hoffa commented 1 year ago

Interesting; can you share your package.json? And project structure as well if possible (to ensure it's picking up the correct package.json). We haven't been able to reproduce the issue.

mildaniel commented 1 year ago

Adding some extra context here. I was unable to reproduce this issue in GitHub actions. My setup looks like this and runs successfully

├── README.md
├── events
│   └── event.json
├── hello-world
│   ├── app.ts
│   ├── jest.config.ts
│   ├── package.json
│   ├── tests
│   │   └── unit
│   │       └── test-handler.test.ts
│   └── tsconfig.json
├── samconfig.toml
└── template.yaml

package.json

{
  "name": "hello_world",
  "version": "1.0.0",
  "description": "hello world sample for NodeJS",
  "main": "app.js",
 ...
  },
  "dependencies": {
    "esbuild": "^0.14.14"
  },
  "devDependencies": {
...
  }
}

and the workflow

name: Build

on: push

jobs:
  build:
    name: Build esbuild project
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 18
      - uses: aws-actions/setup-sam@v2
        with:
          use-installer: true
      - run: sam build -u

I noticed you also specify the template. Can you confirm the template flag is pointing to the correct file?

aleguern commented 1 year ago

I found the bug I'm sorry, it seems I forgot to install esbuild dependency at the lambda level, it was only installed at the root lebel.

hoffa commented 1 year ago

No worries! Thanks for the update; good you found the issue.