aws-amplify / amplify-backend

Home to all tools related to Amplify's code-first DX (Gen 2) for building fullstack apps on AWS
Apache License 2.0
152 stars 51 forks source link

unable to use dependencies like `sharp` that depend on OS-specific addons #1432

Open josefaidt opened 3 months ago

josefaidt commented 3 months ago

Environment information

System:
  OS: macOS 14.3.1
  CPU: (10) arm64 Apple M1 Pro
  Memory: 253.05 MB / 32.00 GB
  Shell: /opt/homebrew/bin/fish
Binaries:
  Node: 20.12.2 - ~/Library/Caches/fnm_multishells/88476_1714755397954/bin/node
  Yarn: 1.22.19 - ~/Library/Caches/fnm_multishells/88476_1714755397954/bin/yarn
  npm: 10.5.0 - ~/Library/Caches/fnm_multishells/88476_1714755397954/bin/npm
  pnpm: 9.0.5 - ~/Library/Caches/fnm_multishells/88476_1714755397954/bin/pnpm
NPM Packages:
  @aws-amplify/backend: 1.0.0
  @aws-amplify/backend-cli: 1.0.1
  aws-amplify: 6.2.0
  aws-cdk: 2.140.0
  aws-cdk-lib: 2.140.0
  typescript: 5.4.5
AWS environment variables:
  AWS_PROFILE = josef-gen2
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Description

When using sharp with a bundler, it is recommended to externalize the sharp dependency to mitigate the issue described here where the bundle is created with the macos-specific addon instead of the linux-specific addon. https://sharp.pixelplumbing.com/install#esbuild

If we simply install the sharp dependency and import it to our function, we will encounter the following error at runtime:

{
    "errorType": "Error",
    "errorMessage": "Could not load the \"sharp\" module using the linux-x64 runtime\nPossible solutions:\n- Ensure optional dependencies can be installed:\n    npm install --include=optional sharp\n    yarn add sharp --ignore-engines\n- Ensure your package manager supports multi-platform installation:\n    See https://sharp.pixelplumbing.com/install#cross-platform\n- Add platform-specific dependencies:\n    npm install --os=linux --cpu=x64 sharp\n- Consult the installation documentation:\n    See https://sharp.pixelplumbing.com/install",
    "stack": [
        "Error: Could not load the \"sharp\" module using the linux-x64 runtime",
        "Possible solutions:",
        "- Ensure optional dependencies can be installed:",
        "    npm install --include=optional sharp",
        "    yarn add sharp --ignore-engines",
        "- Ensure your package manager supports multi-platform installation:",
        "    See https://sharp.pixelplumbing.com/install#cross-platform",
        "- Add platform-specific dependencies:",
        "    npm install --os=linux --cpu=x64 sharp",
        "- Consult the installation documentation:",
        "    See https://sharp.pixelplumbing.com/install",
        "    at node_modules/.pnpm/sharp@0.33.3/node_modules/sharp/lib/sharp.js (file:///var/task/index.mjs:2642:13)",
        "    at __require2 (file:///var/task/index.mjs:19:50)",
        "    at node_modules/.pnpm/sharp@0.33.3/node_modules/sharp/lib/constructor.js (file:///var/task/index.mjs:2655:5)",
        "    at __require2 (file:///var/task/index.mjs:19:50)",
        "    at node_modules/.pnpm/sharp@0.33.3/node_modules/sharp/lib/index.js (file:///var/task/index.mjs:6821:17)",
        "    at __require2 (file:///var/task/index.mjs:19:50)",
        "    at file:///var/task/index.mjs:6836:28",
        "    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"
    ]
}

This will impact any non-AL2023 deployments (i.e. sandbox deployments from macos, windows, and custom deployments from non-linux runners). From some other issues, this seems to be where it is recommended to force a build with docker instead of esbuild from the project.

ykethan commented 3 months ago

Marking this as feature request for improvements.