oven-sh / bun

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

Bun doesn't run prisma generate or prisma migrate inside docker containers or WSL #5320

Closed sum117 closed 4 months ago

sum117 commented 10 months ago

What version of Bun is running?

1.0.1+31aec4ebe325982fc0ef27498984b0ad9969162b

What platform is your computer?

Microsoft Windows NT 10.0.22621.0 x64

What steps can reproduce the bug?

Docker:

FROM oven/bun AS builder

WORKDIR /tmp/app

COPY package.json .
COPY bun.lockb .

RUN bun install

COPY src ./src
COPY tsconfig.json .

# This would be the optimal solution but it doesn't work, so I'm gonna use a hack here until I figure out how to fix it
#   RUN bunx prisma:generate
#   COPY prisma/schema.prisma ./prisma/schema.prisma
COPY node_modules/.prisma ./node_modules/.prisma

RUN bun run build

FROM oven/bun AS runner

ARG DEFAULT_DATABASE_URL= "file:./deploy.db"
ENV DATABASE_URL=$DEFAULT_DATABASE_URL

WORKDIR /app

COPY --from=builder /tmp/app/package.json /app/package.json
COPY --from=builder /tmp/app/bun.lockb /app/bun.lockb
RUN bun install -p

# Refer to the comment above
#   COPY --from=builder /tmp/app/prisma/schema.prisma ./prisma/schema.prisma
#   RUN bun run prisma:generate
COPY --from=builder /tmp/app/node_modules/.prisma /app/node_modules/.prisma

#   These commands aren't working either. The prisma:migrate has to be done in the external machine
#   RUN bun add prisma
#   RUN bun run prisma:migrate

COPY --from=builder /tmp/app/build /app/build

CMD ["bun", "start"]

WSL:

Install WSL normally...

wsl --install

Make bun installation inside Ubuntu.

Try to follow this exact guide: https://bun.sh/guides/ecosystem/prisma

What is the expected behavior?

Prisma installs normally

What do you see instead?

This, download never finishes:

root@7fa00e64baa1:/app# bun run prisma:deploy
$ bunx prisma migrate deploy
Downloading Prisma engines for Node-API for debian-openssl-1.1.x [===                 ] 15%root@7fa00e64baa1:/app#

Additional information

No response

sum117 commented 9 months ago

Issue with Prisma CLI commands seems to be fixed in v1.0.2. Run bun upgrade and try again. Reopen this issue if it doesn't work for you with additional OS details.

DeepDoge commented 9 months ago

I use devcontainer with the docker image mcr.microsoft.com/devcontainers/base:jammy. I have the same problem with bun version 1.0.2.

When i only run:

bunx prisma

It runs gives me the --help response. But nothing else works:

Sometimes randomly, it says this:

Downloading Prisma engines for Node-API for debian-openssl-3.0.x [================    ] 81%
rileychh commented 9 months ago

1.0.2 with Docker also has non-working prisma generate.

OS Info: oven/bun:1.0.2 Docker version 24.0.5, build ced0996600 Arch Linux 6.5.3-zen1-1-zen

veloxy commented 9 months ago

Same issue here, in the oven/bun docker container:

root@69248dd04828:/app# bun -v 
1.0.2
root@69248dd04828:/app# bunx prisma migrate deploy   
> Downloading Prisma engines for Node-API for debian-openssl-1.1.x [=========           ] 47%root@69248dd04828:/app# 

Stops are 47%, no errors or anything.

I'm also running docker on wsl, but I don't think that matters?

sum117 commented 9 months ago

Thanks for all the feedback guys. I have reopened this issue because of it. I'm sorry this is still a problem for your use case and I ask you pardon for my oversight.

The core team will hopefully come back to us, I'm sure of it!

kylemclaren commented 9 months ago

Same issue here, bunx prisma generate completes but client is not generated

lentg commented 9 months ago

+1

sorenhansendk commented 9 months ago

First you need to include nodejs in your Docker image. That is required for Prisma to work with Bun:

# Dockerfile
COPY --from=node:18 /usr/local/bin/node /usr/local/bin/node

After that you need to trust the Prisma client package, so it can execute postinstall scripts:

# package.json
"trustedDependencies": [
  "@prisma/client"
],

With this, I'm able to run bun install and it generates my schema without any problems.

I can also run bunx prisma generate without any problems. Here is my full Dockerfile:

FROM oven/bun:1.0.2
WORKDIR /opt/app

COPY --from=node:18 /usr/local/bin/node /usr/local/bin/node
COPY ./packages ./packages
COPY ./package.json ./
COPY ./bun.lockb ./

# Install packages
RUN bun install

# Run generation
RUN bunx prisma generate

It still dosen't work with the ARM image. I think Prisma missing a ARM build of the Debian engine.

veloxy commented 9 months ago

First you need to include nodejs in your Docker image. That is required for Prisma to work with Bun:

Kinda defeats the purpose of using Bun as it is a Node.js replacement 😕

It would also make the image larger, I don't want to ship Node.js and Bun to run migrations.

sorenhansendk commented 9 months ago

Kinda defeats the purpose of using Bun as it is a Node.js replacement 😕

I agree. This is not a super optimal solution, but for now, I think it's more clean than copying node_modules around.

It would also make the image larger, I don't want to ship Node.js and Bun to run migrations.

You can use a multi-stage build, if you don't want to include Node.js in your final image, or delete it after prisma commandas has ran. Something like this: RUN rm -rf /usr/local/bin/node

wedelgaard commented 9 months ago

I'm experiencing this issue as well. I tried a different approach which is not optimal as well, but dont seem to work. I ran bun run prisma generate locally and copy the generated files to a new folder prisma-client/client. Then when I build my Docker Image i copy that folder into the /node_modules/@prisma/client folder.

This is what my Dockerfile looks like:

FROM oven/bun:1.0.2
ENV NODE_ENV=production
WORKDIR /usr/src/app
COPY package.json bun.lockb ./
RUN bun install
COPY prisma-client/client /node_modules/@prisma/client
COPY . .
RUN rm -rf prisma-client
EXPOSE 3000
CMD [ "bun", "run", "start" ]

Unfortunately I'm still seing the same error:

2023-09-22 11:58:58 $ bun run src/index.ts
2023-09-22 11:58:58 
2023-09-22 11:58:58 
2023-09-22 11:58:58 error: Cannot find module ".prisma/client/index" from "/usr/src/app/node_modules/@prisma/client/index.js"
2023-09-22 11:58:58 error: script "start" exited with code 1 (SIGHUP)
wedelgaard commented 9 months ago

By following @sorenhansendk example I was able to get it working, but I had to define a custom output path for the prisma generated files:

https://www.prisma.io/docs/concepts/components/prisma-client/working-with-prismaclient/generating-prisma-client#using-a-custom-output-path

veloxy commented 9 months ago

You can use a multi-stage build, if you don't want to include Node.js in your final image, or delete it after prisma commandas has ran. Something like this: RUN rm -rf /usr/local/bin/node

True, but for my usecase I need to run migrations when the container starts, which is only possible with Prisma using npx (afaik). So it's unfortunately not a solution for me, I think? Or can you run bunx without node after bun install with node? I'll try it later. Thank you for providing a work-around at least!

sorenhansendk commented 9 months ago

So it's unfortunately not a solution for me, I think? Or can you run bunx without node after bun install with node?

I have not tested migrations without Node.js included. I think you will run into problems - but let us please know the result 👍

sacummings91 commented 9 months ago

Just to add on to this. The current state of bun install using Bun v1.0.3 with prisma for us is this.

Locally bun install does not execute the postinstall script to generate the client in node_modules/.prisma/. But if you run bun run prisma generate it works as expected and generates the client.

In our Gitlab CI/CD runners using docker, bun install also does not execute the postinstall script. But here even running bun run prisma generate does nothing as well. We can confirm this by running tsc afterwards and it fails every place we import the Prisma Client or the types it generates.

antn9x commented 9 months ago

This still not working with bun 1.0.3 My docker file

FROM oven/bun:1.0.3 as base
WORKDIR /app/
COPY package.json bun.lockb ./
COPY ./src ./src
COPY ./prisma ./prisma
RUN bun install --production
RUN bunx prisma generate
CMD bun run deploy-one
DeepDoge commented 9 months ago

Alright, I was able to run it by also having node installed in the container. I don't know but, this might be a prisma problem, looking specifically for node, not sure.

Here's my devcontainer.json:

{
    "name": "Bun & Node.js & TypeScript",
    "image": "mcr.microsoft.com/devcontainers/typescript-node:1-20-bullseye",
    "features": {
        "ghcr.io/shyim/devcontainers-features/bun:0": {}
    },
    "postCreateCommand": "bun i",
}
mausworks commented 9 months ago

Ran into the same issue today. Adding with some speculations on my side:

It's super weird, bun prisma generate prints nothing to the console on build/start (on railway.app), but works perfectly locally.

I guess bun (at some point) runs Node (instead of itself), but then fails silently if Node is not installed. Locally, this works, since I have both installed.

There is provably something in Prisma that has a hard dependency on Node somewhere e.g. with a shebang #!/usr/bin/node, or a spawn/exec somewhere.

It's just so odd that it's entirely silent. I guess I'll include Node18 with the container for now, but will spend some time this week to investigate.

QuentinDutot commented 9 months ago

In Docker it fails silently and in local it throws this error:

✘ [ERROR] Could not resolve ".prisma/client/index-browser"

    node_modules/@prisma/client/index-browser.js:1:23:
      1 │ const prisma = require('.prisma/client/index-browser')
        ╵                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  You can mark the path ".prisma/client/index-browser" as external to
  exclude it from the bundle, which will remove this error. You can also
  surround this "require" call with a try/catch block to handle this
  failure at run-time instead of bundle-time.
judocode commented 9 months ago

I have created a repo where Prisma and Bun do not play well in Docker nor non-Docker: https://github.com/judocode/prisma-bun. I currently see the following problem in Docker:

 > [6/6] RUN bun run prisma generate:
16.82 error: "prisma" exited with code 9 (SIGKILL)
------
Dockerfile:11
--------------------
   9 |     COPY . .
  10 |
  11 | >>> RUN bun run prisma generate
  12 |
  13 |     CMD ["bun", "./src/index.ts"]
--------------------
ERROR: failed to solve: process "/bin/sh -c bun run prisma generate" did not complete successfully: exit code: 9
rmblockcode commented 8 months ago

Hi people.

Does somebody have this problem when using docker with prisma and bun?:

 error TS2305: Module '"@prisma/client"' has no exported member 'PrismaClient'.

If a run it locally, this problem doesn't show up! but running docker build, it fails!

JinYuSha0 commented 8 months ago

I made a stupid mistake calling cli generate without schema.prisma file this dockerfile works for me

FROM oven/bun:latest
WORKDIR /usr/src/app

# Install nodejs using n
RUN apt-get -y update; apt-get -y install curl
ARG NODE_VERSION=18
RUN curl -L https://raw.githubusercontent.com/tj/n/master/bin/n -o n \
    && bash n $NODE_VERSION \
    && rm n \
    && npm install -g n

COPY bun.lockb package.json ./
RUN bun install
# You must ensure that schema.prisma is already in the directory
COPY . .
RUN bunx prisma generate

RUN bun run build
ENV NODE_ENV production
EXPOSE 3000
ENTRYPOINT [ "bun", "run", "start:prod" ]
xcaeser commented 8 months ago

This is the dockerfile generated by fly.io. and guess what? it doesn't work :( exactly at the bun run build because it can't find prisma client types HELP!

# syntax = docker/dockerfile:1

# Adjust BUN_VERSION as desired
ARG BUN_VERSION=1.0.7
FROM oven/bun:${BUN_VERSION} as base

LABEL fly_launch_runtime="Next.js/Prisma"

# Next.js/Prisma app lives here
WORKDIR /app

# Set production environment
ENV NODE_ENV="production"

# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && apt-get install -y build-essential openssl pkg-config python-is-python3

# Install node modules
COPY --link bun.lockb package.json ./
RUN bun install

# Generate Prisma Client
COPY --link prisma .
RUN bun prisma generate

# Copy application code
COPY --link . .

# Build application
RUN bun run build

# Remove development dependencies
RUN rm -rf node_modules && bun install --ci

# Final stage for app image
FROM base

# Install packages needed for deployment
RUN apt-get update -qq && apt-get install --no-install-recommends -y openssl && rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Copy built application
COPY --from=build /app /app

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD [ "bun", "run", "start" ]
JinYuSha0 commented 8 months ago

In fact, you also need to install node. Prisma generate needs to generate a node addons.

xcaeser commented 8 months ago

In fact, you also need to install node. Prisma generate needs to generate a node addons.

Yes tried it, it works. But there is another bug where bun leaks memory and VM shuts down due to that.

DeepDoge commented 8 months ago

even though having also node in the container works on my pc, same container doesn't work on the server: https://github.com/oven-sh/bun/issues/3315#issuecomment-1768976067

had to switch to node. there's certainly a memory leak.

vutran-tvg commented 8 months ago

I faced a similar issue when using Prisma with Bun. The prisma generate runs ok when I use bun but it's unable to create a Prisma client when running with docker. Tried to search around but didn't find any solution yet.

My workaround is to generate it locally with bun (in a non-docker environment) and then ship it with a docker image. Note that I work to change the output of the generator to a different location rather than the default inside node_module. It's help simplify the Dockerfile setup.

Here's my setup prisma.schema

  provider      = "prisma-client-js"
  output        = "../generated/prisma-client" //Change output to custom path
  binaryTargets = ["native", "debian-openssl-1.1.x"]
}

Setup in Dockerfile


WORKDIR /usr/src/app

COPY package*.json bun.lockb ./
RUN bun install
COPY . .

# If your `generated/prisma-client` is not in the Docker build context (the directory containing your Dockerfile), 
# you'll need to place it within the build context or adjust your project structure accordingly. E.g

ENV NODE_ENV production

CMD [ "bun", "start" ]

This solution works for me but it violates the docker concept to keep all the environment consistent and brings an additional step in the build process to generate the Prisma client. Anw, it's good enough to go with the current situation until I find a better alternative or receive updates from Prisma. Note that the Prisma client runs differently on different OS so the deployment needs to be tested carefully

craigcosmo commented 8 months ago

yes bunx doesn't work with prisma, is it a prisma bug or a bun bug ?

flysand7 commented 8 months ago

Wasted a few days on this until finally found this thread.. guess I'll have to switch back to using node

cholasimmons commented 8 months ago

Hi people. Does somebody have this problem when using docker with prisma and bun?:

 error TS2305: Module '"@prisma/client"' has no exported member 'PrismaClient'.

If a run it locally, this problem doesn't show up! but running docker build, it fails!

This is exactly what I have! I'm running ReviOS 11 so I thought it's due to some of it's optimizations, I can never get PrismaClient to be detected and neither can i run any bunx prisma codes.... only --help works


--- EDIT ---

All is perfect now, turns out I also needed an installation of nodeJS itself (not just bunJS)., I'm guessing this would be required in a docker container too, Prisma definitely runs some node-specific code somewhere

gausie commented 7 months ago

Weird, mine is failing to parse prisma itself -

 > [build  7/10] RUN bunx prisma generate:
#0 0.367 /app/node_modules/prisma/build/index.js:867
#0 0.367     if (typeof process !== "undefined" && typeof process.stderr?.write !== "function") {
#0 0.367                                                                 ^
#0 0.367 
#0 0.367 SyntaxError: Unexpected token '.'
#0 0.367     at wrapSafe (internal/modules/cjs/loader.js:915:16)
#0 0.367     at Module._compile (internal/modules/cjs/loader.js:963:27)
#0 0.367     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
#0 0.367     at Module.load (internal/modules/cjs/loader.js:863:32)
#0 0.367     at Function.Module._load (internal/modules/cjs/loader.js:708:14)
#0 0.367     at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
#0 0.367     at internal/main/run_main_module.js:17:47
------

Optional chaining has been around for a while and this is node v20 so I don't know why it's happening here

Xu-pixel commented 7 months ago

@gausie me too, in wsl image

Xu-pixel commented 7 months ago

@gausie suggest using prisma by installing node --> npx prisma , then use @prisma/client in bun

sathwik77 commented 6 months ago

Has anyone found a solution that does not involve installing Prisma with node?

heyfirst commented 5 months ago

Hei, here is my simpmle solution, same ideas with @Xu-pixel, Not ideal to use right away but probably you can get some ideas.

So since bunx prisma is not working with oven/bun then what I do is to use node docker image instead. Then I install bun with npm. My dockerfile will be just like this below.

FROM node:20-slim

WORKDIR /app
ENV NODE_ENV="production"

# Install needed libs for prisma, e.g. openssl, etc..
RUN apt-get update -qq && apt-get install -y build-essential openssl pkg-config python-is-python3

# Install bun
RUN npm install -g bun

# Install node modules
COPY bun.lockb package.json ./

# Clean install
RUN bun install --ci

# Generate Prisma Client
COPY . .

# FIXME: `bunx` has issue with prisma cli, so we use `npx` instead.
RUN npx prisma generate

# Start the server by default, this can be split to another build step to make the size much smaller
EXPOSE 3000
CMD [ "bun", "./src/index.ts" ]

There are some improvements of this that you can do

  1. Use this dockerfile directly and your image size will end up around 800mb+, So the way to reduce is to use docker multi-build, We need only generated prisma client, So we can easily use split the bun run part to a smaller one.
  2. You probably don't need node:20-slim and all libs stuffs. you can use anything as well, the point here is you need npx for run prisma generate.
  3. This won't work if you need to run bunx primsa migrate deploy because bunx prisma won't work anyway.
jsphbtst commented 5 months ago

Followed @JinYuSha0's approach of installing Node and running bunx prisma generate. It was pretty straightfoward from there. Here's the very rough Dockerfile I wrote:

FROM oven/bun:1 AS base

# Install nodejs using n
# Apparently bunx needs node to run `prisma generate` lol
RUN apt-get -y update; apt-get -y install curl
ARG NODE_VERSION=18
RUN curl -L https://raw.githubusercontent.com/tj/n/master/bin/n -o n \
    && bash n $NODE_VERSION \
    && rm n \
    && npm install -g n

# Hacky way of copying over stuff from NX
WORKDIR /usr/src/app
COPY tsconfig.base.json .
COPY .eslintrc.json .
COPY prisma prisma
COPY apps/our-app apps/our-app
COPY apps/our-app/custom-package.json package.json
RUN bun install
RUN bunx prisma generate

CMD ["bun", "apps/our-app/src/index.ts"]

Haven't explore multi-stage builds yet as I wanted to just make it work. The image produced here ballooned to 750MB probably because of Node lol. I'll iterate more on this and will message here if I discover anything significant.

Timic3 commented 4 months ago

Just reiterating, this issue still persists on 1.0.27. When running any prisma command with Bun instead of Node.js, it fails silently (exit code 0).

Can be replicated with: bunx --bun prisma --help - fails bunx prisma --help - works (but you need Node.js installed and available - node -v)

As others have said, current workaround is having Node installed. My workaround is:

FROM oven/bun:1-debian

COPY --from=node:18 /usr/local/bin/node /usr/local/bin/node
...
services:
  app:
    # image: oven/bun:1-debian
    build:
      context: ./docker/bun
...
cholasimmons commented 4 months ago

I think i may have found a solution with pure Bun - no node installation. Send plenty US dollars to my paypal and i'll share 🤣 simmonsfrank gmail com 😊 Let me just recreate the issue to double-check

cholasimmons commented 4 months ago

Has anyone found a solution that does not involve installing Prisma with node?

Check this article, scroll down to the very last paragraph. Note that the versions used are Bun 1.0.28, Prisma 5.10.2

https://medium.com/@simmonsfrank/beautiful-elysia-imprismad-in-a-jail-6518dd2af586

zackify commented 4 months ago

oh nice, ill have to try it again, must be the fix from the blog post on the release:

image
zackify commented 4 months ago

That solution does not work, i tried it. The person who made the blog post must have copied the generated folder into their docker context, so it was already there.

prisma generate does not work without node still.

cholasimmons commented 4 months ago

Try bumping up your Bun and Prisma versions to the latest. 1.0.28 and 5.10.2 are out, respectively. I'm running ElysiaJS in a docker container as we speak. Not a single stitch of node here.

zackify commented 4 months ago

Yes that works, if you're building your docker container and the generated prisma client is already in the build context.

I attempted with the latest versions.

In your docker container you can run bun prisma migrate and see there is still no output.

cholasimmons commented 4 months ago

bunx prisma migrate you mean ? All is running perfectly for me bro. Did you relink all mentions of @prisma/client to generated?

camero2734 commented 4 months ago

That solution does not work, i tried it. The person who made the blog post must have copied the generated folder into their docker context, so it was already there.

prisma generate does not work without node still.

Yeah, this fix was for a different error that occurred once prisma was actually running, the generator part is still broken unfortunately

marvinruder commented 4 months ago

I have tried using Prisma commands both in the latest canary container and with a locally built version of Bun directly from the main branch some hours ago and I still see Prisma silently failing:

root@4262fb6e72c4:~# bun x prisma init
> Downloading Prisma engines for Node-API for linux-arm64-openssl-3.0.x [====================] 99%root@4262fb6e72c4:~# bun x prisma init
> Downloading Prisma engines for Node-API for linux-arm64-openssl-3.0.x [==========          ] 49%root@4262fb6e72c4:~# bun x prisma init
> Downloading Prisma engines for Node-API for linux-arm64-openssl-3.0.x [==========          ] 49%root@4262fb6e72c4:~#

Sometimes Prisma commands succeed, but only lead to the discovery of other issues. For example, the output of prisma init always ends with

error: Script not found "/tmp/bunx-0-prisma@latest/node_modules/prisma/build/child"

I cannot pinpoint this issue to an exact location in Prisma’s source code, but there is a line in the minified bundle that reads

childPath=path__default.default.join(eval("__dirname"),"child");

Changing child to child.js there removes the error message.

Also, prisma generate fails for me when working with a larger schema. The error shows

Generator "/root/rating-tracker/node_modules/@prisma/client/generator-build/index.js" failed:

13849 | 
13850 | // ../generator-helper/src/generatorHandler.ts
13851 | function generatorHandler(handler) {
13852 |   byline(process.stdin).on("data", async (line) => {
13853 |     console.log(String(line)); // I added this line
13854 |     const json = JSON.parse(String(line));
                         ^
SyntaxError: JSON Parse error: Unterminated string
      at /root/rating-tracker/node_modules/@prisma/client/generator-build/index.js:13854:18
      at /root/rating-tracker/node_modules/@prisma/client/generator-build/index.js:13852:43
      at addChunk (node:stream:1941:43)
      at readableAddChunk (node:stream:1895:59)
      at /root/rating-tracker/node_modules/@prisma/client/generator-build/index.js:13826:12
      at /root/rating-tracker/node_modules/@prisma/client/generator-build/index.js:13838:3
      at final (node:stream:3247:37)
      at callFinal (node:stream:2778:45)

The output from my console.log indeed shows a huge JSON output beginning with {"jsonrpc":"2.0","method":"generate","params":… and ending in the middle of nowhere:

…se,"isNullable":true,"inputTypes":[{"type":"Float","location":"scalar","isList":false},{"type":"Null","location":"scalar","isList":false}]},{"name":"description","isRequired":false,"isNullable":true,"inputTypes":[{"type":"String","location":"scalar","isList":false},{"type":"Null","location":"scalar","isList":false}]},{"name":"marketCap","isRequired":false,"isNullable":true,"inputTypes":[{"type":"Float","location":"scalar","isList":false},{"type":"Null","location":"scalar","isList":false}]}]},{"name":"StockUpdateManyMutationInput","constraints":{"maxNumFields

This is as far as I get. Not sure what to make of all this, I just thought to leave some more information here that might help track down remaining issues. Let me know if you need anything else.

PS: The example above is easily reproducable by using Prisma’s quickstart walkthrough and a non-trivial Schema:

mkdir hello-prisma
cd hello-prisma
bun init -y
bun install typescript ts-node @types/node
bun install prisma
bun x prisma init --datasource-provider sqlite
# now copy some schema to the prisma folder, I used mine from https://github.com/marvinruder/rating-tracker/blob/main/packages/backend/prisma/schema.prisma
bun x prisma generate
# you can add `console.log(String(line));` to node_modules/@prisma/client/generator-build/index.js:13853 to see the json-rpc messages of which parsing fails
marvinruder commented 4 months ago

Still seeing this issue in today’s canary:

docker run --rm -it --entrypoint /bin/sh oven/bun:canary-alpine # also tried with Debian-based oven/bun:canary, same result
/home/bun/app # bunx prisma init
> Downloading Prisma engines for Node-API for linux-musl-arm64-openssl-3.0.x [=================   ] 87%/home/bun/app # 
/home/bun/app # bun --revision
1.0.29+2fb6733ee
/home/bun/app # 
Jarred-Sumner commented 4 months ago

Confirming this issue is still not fixed. The previous PRs made good progress, but prisma generate continues to not work when used in Bun.

"Downloading" hangs or exits prematurely, before it finishes:

❯ bun --bun prisma generate
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
> Downloading Prisma engines for Node-API for darwin-arm64 [==============      ] 69%
In a debug build, those logs look like this ```js [MEM] free(6824153) = 10240862 [fetch] onData 177937 [fetch] progressUpdate true [fetch] releaseSocket(0x00006000010184D0) [fetch] Keep-Alive release binaries.prisma.sh:443 (0x105553133143248) [fetch] onAsyncHTTPCallback: 182.198ms [FetchTasklet] callback success true has_more false bytes 177937 [SYS] write(45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], 2599) = 2599 [FileSink] Wrote 2599 bytes (fd: 45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], head: 0, 0/180224) [SYS] write(45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], 16384) = 16384 [FileSink] Wrote 16384 bytes (fd: 45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], head: 0, 0/196608) [SYS] write(45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], 16384) = 16384 [FileSink] Wrote 16384 bytes (fd: 45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], head: 0, 0/212992) [SYS] write(45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], 12778) = 12778 [FileSink] Wrote 12778 bytes (fd: 45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], head: 0, 0/225770) [SYS] write(1, 4) = 4 [FileSink] Wrote 4 bytes (fd: 1, head: 0, 0/105) > Downloading Prisma engines for Node-API for darwin-arm64 [ ] 1%[SYS] write(1, 93) = 93 [FileSink] Wrote 93 bytes (fd: 1, head: 0, 0/198) [SYS] write(1, 4) = 4 [FileSink] Wrote 4 bytes (fd: 1, head: 0, 0/202) [SYS] write(45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], 3606) = 3606 [FileSink] Wrote 3606 bytes (fd: 45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], head: 0, 0/229376) [SYS] write(45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], 16384) = 16384 [FileSink] Wrote 16384 bytes (fd: 45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], head: 0, 0/245760) [SYS] write(45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], 16384) = 16384 [FileSink] Wrote 16384 bytes (fd: 45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], head: 0, 0/262144) [SYS] write(45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], 5189) = 5189 [FileSink] Wrote 5189 bytes (fd: 45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], head: 0, 0/267333) [SYS] write(45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], 11195) = 11195 [FileSink] Wrote 11195 bytes (fd: 45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], head: 0, 0/278528) [SYS] write(45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], 16384) = 16384 [FileSink] Wrote 16384 bytes (fd: 45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], head: 0, 0/294912) [SYS] write(45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], 1242) = 1242 [FileSink] Wrote 1242 bytes (fd: 45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], head: 0, 0/296154) [SYS] write(45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], 15142) = 15142 [FileSink] Wrote 15142 bytes (fd: 45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], head: 0, 0/311296) [SYS] write(45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], 16384) = 16384 [FileSink] Wrote 16384 bytes (fd: 45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], head: 0, 0/327680) [SYS] write(45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], 1430) = 1430 [FileSink] Wrote 1430 bytes (fd: 45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], head: 0, 0/329110) [FetchTasklet] onProgressUpdate [Loop] sub 1 - 1 = 0 [FetchTasklet] clearData [MEM] free(122) = 10240740 [MEM] free(128) = 10240612 [MEM] free(73) = 10240539 [MEM] free(430) = 10240109 [MEM] free(384) = 10239725 [MEM] free(10236237) = 3488 [FetchTasklet] deinit [MEM] free(2688) = 800 [MEM] free(800) = 0 [SYS] write(1, 4) = 4 [FileSink] Wrote 4 bytes (fd: 1, head: 0, 0/206) > Downloading Prisma engines for Node-API for darwin-arm64 [ ] 2%[SYS] write(1, 93) = 93 [FileSink] Wrote 93 bytes (fd: 1, head: 0, 0/299) [SYS] write(1, 4) = 4 [FileSink] Wrote 4 bytes (fd: 1, head: 0, 0/303) [SYS] write(45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], 14954) = 14954 [FileSink] Wrote 14954 bytes (fd: 45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], head: 0, 0/344064) [SYS] write(45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], 16384) = 16384 [FileSink] Wrote 16384 bytes (fd: 45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], head: 0, 0/360448) [SYS] write(45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], 7141) = 7141 [FileSink] Wrote 7141 bytes (fd: 45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], head: 0, 0/367589) [SYS] write(45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], 9243) = 9243 [FileSink] Wrote 9243 bytes (fd: 45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], head: 0, 0/376832) [SYS] write(45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], 16384) = 16384 [FileSink] Wrote 16384 bytes (fd: 45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], head: 0, 0/393216) [SYS] write(45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], 5046) = 5046 [FileSink] Wrote 5046 bytes (fd: 45[/private/var/folders/hd/k_cxl9f55475q62m8_f_9k_c0000gn/T/3179ff6fa0b860d374338cd1441ca7e3/partial], head: 0, 0/398262) ```

This issue will only reproduce when prisma engines have not yet been downloaded on the machine, like when using Docker and the Prisma engine is not cached on-disk.

There is also an issue when prisma calls child_process.fork but I'm not able to isolate it yet.

camero2734 commented 4 months ago

The issue now seems to be with node-fetch. Here's a minified repro:

import hasha from "hasha"; // bun add hasha@5.2.2
import zlib from "zlib";
// import fetch from "node-fetch";
import fetch from "./node_modules/node-fetch/lib/index.mjs"; // bun add node-fetch@2.7.0

async function downloadZip(url2) {
  const response = await fetch(url2, { compress: false });
  if (!response.ok) throw new Error('fail');

  return await new Promise(async (resolve3, reject2) => {
    let bytesRead = 0;
    response.body
      .on("error", reject2)
      .on("data", (chunk) => { bytesRead += chunk.length });
    const gunzip = zlib.createGunzip();
    gunzip.on("error", reject2);
    const zipStream = response.body.pipe(gunzip);
    const zippedHashPromise = hasha.fromStream(response.body, { algorithm: "sha256" });
    const hashPromise = hasha.fromStream(zipStream, { algorithm: "sha256" });
    console.log("will await");
    await hashPromise;
    await zippedHashPromise;
    console.log("all okay");
    resolve3();
  });
}

const url = "https://binaries.prisma.sh/all_commits/5a9203d0590c951969e85a7d07215503f4672eb9/debian-openssl-1.1.x/libquery_engine.so.node.gz";
const array = new Array(10).fill(url);

const promises = array.map((url) => downloadZip(url));

await Promise.all(promises);
console.log("DONE");

This will never log DONE, it will hang after 4 or so all okay logs.

Note that I use the explicit node-fetch import because Prisma bundles the OG node-fetch with it, which means it isn't replaced with Bun's version (if you swap it out for the commented-out import, it will work and reach DONE).

Haven't dug any deeper than this atm, but this narrows it down quite a bit

xcaeser commented 4 months ago

switched to drizzle after all.