oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.95k stars 2.75k forks source link

Optional dependency imports (`import("zlib-sync")`) are required #4760

Closed Fyko closed 1 year ago

Fyko commented 1 year ago

What version of Bun is running?

1.0.0+822a00c4d508b54f650933a73ca5f4a3af9a7983

What platform is your computer?

Darwin 22.6.0 arm64 arm

What steps can reproduce the bug?

  1. git clone https://github.com/Fyko/playground
  2. cd dev-commands
  3. bun install
  4. bun run build

What is the expected behavior?

The project will build.

What do you see instead?

$ bun build --target=bun ./src/index.ts --outfile=dist/server.js
[0.02ms] ".env"
error: Could not resolve: "zlib-sync". Maybe you need to "bun install"?

var getZlibSync = lazy2(async () => import("zlib-sync").then((mod) => mod.default).catch(() => null));
                                           ^
/Users/carter/code/fyko/playground/dev-commands/node_modules/@discordjs/ws/dist/index.mjs:524:44 17661
error: script "build" exited with code 1 (SIGHUP)

(some similar errors were removed for readability)

Even after installing zlib-sync, Bun still can't seem to find it.

error: Could not resolve: "./build/Release/zlib_sync.node"

module.exports = require("./build/Release/zlib_sync.node");
                         ^
/Users/carter/code/fyko/playground/dev-commands/node_modules/zlib-sync/index.js:3:26 40

Additional information

Some libraries have optional dependencies that are imported with import('dep') and caught. https://github.com/discordjs/discord.js/blob/c50809e20648cacea99f5450e8073d960ff8aa39/packages/ws/src/ws/WebSocketShard.ts#L30

Bun doesn't allow the import to fail.

PatrickMatthiesen commented 1 year ago

I'm having the same issue. Running the app from index.ts works fine, but the build fails.

Changing the code in @discordjs/ws/dist/index.js:524 and uninstalling zlib-sync fixed it for me.

var getZlibSync = null;

This isn't a viable solution though.

JoeyEamigh commented 1 year ago

Now that #5777 is merged, this is a large issue for bundling NestJS apps. While they now run fine, attempting to use bun build will instantly start asking for optional dependencies such as @nestjs/microservices and @nestjs/websockets. Looking forward to this being resolved!

danielgwilson commented 1 year ago

I'm having the same issue. Running the app from index.ts works fine, but the build fails.

Changing the code in @discordjs/ws/dist/index.js:524 and uninstalling zlib-sync fixed it for me.

var getZlibSync = null;

This isn't a viable solution though.

+1 to having this issue with discord.js

Electroid commented 1 year ago

We recently fixed optionalDependencies and optional peerDependencies using bun install, so this issue should be fixed. I tested your reproduction with Bun v1.0.7, and it worked.

If you are still experiencing this issue after upgrading, let us know and we'll re-open this issue and investigate further.

mattapperson commented 11 months ago

This is still an issue, for example, react-devtools-core is an optional dependency in a react-ink app, yet running bun build ./src/cli.tsx --compile --outfile ./dist/cli results in Could not resolve: "react-devtools-core". Maybe you need to "bun install"?

This can be reproduced in a new pastel app for easy repro.

Jarred-Sumner commented 11 months ago

You can pass --external=react-devtools-core, though it is supposed to automatically detect its optional by the try / catch handler.

Wamy-Dev commented 6 months ago

I am still having this issue with Discord JS Here is my package.json

{
  "name": "discord-bot",
  "module": "index.ts",
  "type": "module",
  "devDependencies": {
    "bun-types": "latest"
  },
  "peerDependencies": {
    "typescript": "^5.0.0"
  },
  "dependencies": {
    "@sentry/bun": "^7.108.0",
    "@supabase/supabase-js": "^2.39.7",
    "bufferutil": "^4.0.8",
    "discord-api-types": "^0.37.71",
    "discord.js": "^14.14.1",
    "sugar": "^2.0.6",
    "utf-8-validate": "^6.0.3",
    "zlib-sync": "^0.1.9"
  }
}

And my dockerfile below. Still getting this error when building.

FROM oven/bun:debian as base
WORKDIR /usr/src/app

# install dependencies into temp directory
# this will cache them and speed up future builds
FROM base AS install
RUN mkdir -p /temp/dev
COPY package.json bun.lockb /temp/dev/
RUN apt-get update && apt-get install -y curl python3 python3-pip python3-dev build-essential
RUN cd /temp/dev && bun install --frozen-lockfile

# install with --production (exclude devDependencies)
RUN mkdir -p /temp/prod
COPY package.json bun.lockb /temp/prod/
RUN cd /temp/prod && bun install --frozen-lockfile --production

# copy node_modules from temp directory
# then copy all (non-ignored) project files into the image
FROM base AS prerelease
COPY --from=install /temp/dev/node_modules node_modules
COPY . .

# copy production dependencies and source code into final image
FROM base AS release
COPY --from=install /temp/prod/node_modules node_modules
COPY --from=prerelease /usr/src/app/index.ts .
COPY --from=prerelease /usr/src/app/package.json .
COPY . .

# run the app
USER bun
EXPOSE 3000/tcp
ENV DISCORD_BOT_TOKEN=""
ENV DISCORD_BOT_CLIENT_ID=""
ENV DISCORD_BOT_CLIENT_SECRET=""

ENTRYPOINT [ "bun", "run", "index.ts" ]

Here is the actual error when building on docker hub.

2024-04-22T21:05:42Z #12 [install 4/7] RUN cd /temp/dev && bun install --frozen-lockfile
2024-04-22T21:05:42Z #12 0.549 bun install v1.1.4 (fbe2fe0c)
2024-04-22T21:05:43Z #12 0.553 Resolving dependencies
2024-04-22T21:05:43Z #12 1.270 Resolved, downloaded and extracted [2]
2024-04-22T21:05:49Z #12 7.297
2024-04-22T21:05:49Z #12 7.298
2024-04-22T21:05:49Z #12 7.298 gyp info it worked if it ends with ok
2024-04-22T21:05:49Z #12 7.298 gyp info using node-gyp@10.1.0
2024-04-22T21:05:49Z #12 7.298 gyp info using node@21.6.0 | linux | x64
2024-04-22T21:05:49Z #12 7.298 gyp info find Python using Python version 3.9.2 found at "/usr/bin/python3"
2024-04-22T21:05:49Z #12 7.298 gyp http GET https://nodejs.org/dist/v21.6.0/node-v21.6.0-headers.tar.gz
2024-04-22T21:05:49Z #12 7.298 gyp http 200 https://nodejs.org/dist/v21.6.0/node-v21.6.0-headers.tar.gz
2024-04-22T21:05:49Z #12 7.298 gyp http GET https://nodejs.org/dist/v21.6.0/SHASUMS256.txt
2024-04-22T21:05:49Z #12 7.298 gyp http 200 https://nodejs.org/dist/v21.6.0/SHASUMS256.txt
2024-04-22T21:05:49Z #12 7.298 gyp info spawn /usr/bin/python3
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args [
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args '/tmp/bunx-0-node-gyp@latest/node_modules/node-gyp/gyp/gyp_main.py',
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args 'binding.gyp',
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args '-f',
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args 'make',
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args '-I',
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args '/temp/dev/node_modules/zlib-sync/build/config.gypi',
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args '-I',
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args '/tmp/bunx-0-node-gyp@latest/node_modules/node-gyp/addon.gypi',
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args '-I',
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args '/root/.cache/node-gyp/21.6.0/include/node/common.gypi',
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args '-Dlibrary=shared_library',
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args '-Dvisibility=default',
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args '-Dnode_root_dir=/root/.cache/node-gyp/21.6.0',
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args '-Dnode_gyp_dir=/tmp/bunx-0-node-gyp@latest/node_modules/node-gyp',
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args '-Dnode_lib_file=/root/.cache/node-gyp/21.6.0/<(target_arch)/node.lib',
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args '-Dmodule_root_dir=/temp/dev/node_modules/zlib-sync',
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args '-Dnode_engine=v8',
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args '--depth=.',
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args '--no-parallel',
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args '--generator-output',
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args 'build',
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args '-Goutput_dir=.'
2024-04-22T21:05:49Z #12 7.298 gyp info spawn args ]
2024-04-22T21:05:49Z #12 7.298 gyp: name 'llvm_version' is not defined while evaluating condition 'llvm_version=="0.0"' in binding.gyp while trying to load binding.gyp
2024-04-22T21:05:49Z #12 7.298 gyp ERR! configure error
2024-04-22T21:05:49Z #12 7.298 gyp ERR! stack Error: `gyp` failed with exit code: 1
2024-04-22T21:05:49Z #12 7.298 gyp ERR! stack at <anonymous> (/tmp/bunx-0-node-gyp@latest/node_modules/node-gyp/lib/configure.js:296:66)
2024-04-22T21:05:49Z #12 7.298 gyp ERR! stack at emit (node:events:158:48)
2024-04-22T21:05:49Z #12 7.298 gyp ERR! stack at #handleOnExit (node:child_process:546:16)
2024-04-22T21:05:49Z #12 7.298 gyp ERR! stack at processTicksAndRejections (:12:39)
2024-04-22T21:05:49Z #12 7.298 gyp ERR! System Linux 5.4.0-1068-aws
2024-04-22T21:05:49Z #12 7.298 gyp ERR! command "/usr/local/bin/bun" "/tmp/bunx-0-node-gyp@latest/node_modules/.bin/node-gyp" "rebuild"
2024-04-22T21:05:49Z #12 7.298 gyp ERR! cwd /temp/dev/node_modules/zlib-sync
2024-04-22T21:05:49Z #12 7.298 gyp ERR! node -v v21.6.0
2024-04-22T21:05:49Z #12 7.298 gyp ERR! node-gyp -v v10.1.0
2024-04-22T21:05:49Z #12 7.298 gyp ERR! not ok
2024-04-22T21:05:49Z #12 7.299
2024-04-22T21:05:49Z #12 7.299 error: install script from "zlib-sync" exited with 1
2024-04-22T21:05:50Z #12 ERROR: executor failed running [/bin/sh -c cd /temp/dev && bun install --frozen-lockfile]: exit code: 1
2024-04-22T21:05:50Z ------
2024-04-22T21:05:50Z > [install 4/7] RUN cd /temp/dev && bun install --frozen-lockfile:
2024-04-22T21:05:50Z #12 7.298 gyp ERR! stack at #handleOnExit (node:child_process:546:16)
2024-04-22T21:05:50Z #12 7.298 gyp ERR! stack at processTicksAndRejections (:12:39)
2024-04-22T21:05:50Z #12 7.298 gyp ERR! System Linux 5.4.0-1068-aws
2024-04-22T21:05:50Z #12 7.298 gyp ERR! command "/usr/local/bin/bun" "/tmp/bunx-0-node-gyp@latest/node_modules/.bin/node-gyp" "rebuild"
2024-04-22T21:05:50Z #12 7.298 gyp ERR! cwd /temp/dev/node_modules/zlib-sync
2024-04-22T21:05:50Z #12 7.298 gyp ERR! node -v v21.6.0
2024-04-22T21:05:50Z #12 7.298 gyp ERR! node-gyp -v v10.1.0
2024-04-22T21:05:50Z #12 7.298 gyp ERR! not ok
2024-04-22T21:05:50Z #12 7.299
2024-04-22T21:05:50Z #12 7.299 error: install script from "zlib-sync" exited with 1
2024-04-22T21:05:50Z ------
2024-04-22T21:05:50Z ERROR: failed to solve: executor failed running [/bin/sh -c cd /temp/dev && bun install --frozen-lockfile]: exit code: 1
2024-04-22T21:05:50Z Build failed using Buildkit (1)