nx-plugins icon indicating copy to clipboard operation
nx-plugins copied to clipboard

Serverless package doesn't work with serverless-plugin-warmup

Open JulioC opened this issue 2 years ago • 2 comments

When running @ns3/nx-serverless:sls executor with package command, it fails if the serverless.yml is using serverless-plugin-warmup.

To reproduce this issue, generate an app using npx nx generate @ns3/nx-serverless:app my-app-name --plugin @ns3/nx-serverless/plugin, install the dependency serverless-plugin-warmup@^8.2.1 and modify the serverless.yml follow as described below:

service: my-app-name

plugins:
  - '@ns3/nx-serverless/plugin'
  - serverless-offline
  # Declare the plugin
  - serverless-plugin-warmup

# ...

# Add the custom properties
custom:
  warmup:
    default:
      enabled: true
      prewarm: true
      events:
        - schedule: rate(4 minutes)

# ...

The error thrown is described below.

Error: Unexpected exit code from native zip: 12
 executed command 'zip --quiet --recurse-paths /my-project/packages/my-app-name/.serverless/warmUpPluginDefault.zip'
 executed in directory '/my-project/dist/packages/my-app-name'

It appears @ns3/nx-serverless ignores the warmup handler path (which is not in the dist folder), so it tried to create a zip without contents.

JulioC avatar Oct 23 '23 22:10 JulioC

Hmm, I do not know that plugin. I would have to look into it. I make no promises when, sorry, life's busy :(

I would welcome a PR though if you are willing to look into it yourself.

Plus, maybe a silly question. Have you tried changing the order (placing it before @ns3/nx-serverless/plugin)?

Bielik20 avatar Oct 24 '23 08:10 Bielik20

It appears the issue happens because warmup plugin creates a function handler in the project root folder. findFunctionFiles in package-manager in @ns3/nx-serverless expects all function handler files to be in the dist folder. When creating the list of files for the zip, it doesn't find anything so it tries to create an empty ZIP.

I believe the plugin should try to find files in the dist folder and if none is find, it falls back to searching in the original path. This could be done in the findFunctionFiles.

Let me know if you think the proposed solution is fine so I can open a PR.


I found a workaround, by including the generated file in the assets configuration it gets copied to the dist folder, and findFunctionFiles lists it to be zipped.

{
  "targets": {
    "build": {
      "executor": "@nx/webpack:webpack",
      "outputs": ["{options.outputPath}"],
      "options": {
        "main": "noop",
        "outputPath": "dist/packages/my-app",
        "tsConfig": "packages/my-app/tsconfig.app.json",
        "target": "node",
        "compiler": "tsc",
        "assets": [
          "packages/my-app/assets",
          "packages/my-app/.warmup",
          "packages/my-app/package.json"
        ],
        "externalDependencies": [
          "@nestjs/microservices",
          "@nestjs/microservices/microservices-module",
          "@nestjs/websockets/socket-module"
        ],
        "isolatedConfig": true,
        "webpackConfig": "packages/my-app/webpack.config.js"
      },
      "configurations": {
        "production": {
          "optimization": true,
          "extractLicenses": true,
          "inspect": false
        }
      }
    }
  }
}

I'm confortable in keeping this workaround on my side, so no rush on shipping the proper solution.

JulioC avatar Oct 24 '23 17:10 JulioC