oven-sh / bun

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

Program exists early unless `await` is used #4662

Open Fumaz opened 1 year ago

Fumaz commented 1 year ago

What version of Bun is running?

1.0.0+822a00c4d508b54f650933a73ca5f4a3af9a7983

What platform is your computer?

Darwin 23.0.0 arm64 arm

What steps can reproduce the bug?

  1. Clone this repository: https://github.com/Fumaz/bun-prisma
  2. Start the postgres database using docker compose up -d
  3. Run bunx prisma migrate deploy
  4. Run bunx prisma generate
  5. Start the program with bun run index.ts

What is the expected behavior?

User is created and logged in the console

What do you see instead?

➜  bun-prisma git:(master) bun run index.ts         
[0.30ms] ".env"

As you can see, the user is not printed to the console. It looks like the code is not being executed.

Additional information

No response

divmgl commented 1 year ago

Confirming that a top-level await here fixes the issue in Bun, but in Node.js this code would stay running:

bun-prisma on  master [!] via 🐳 desktop-linux via 🍞 v1.0.0 via  v18.16.0 
❯ bun run index.ts
[0.03ms] ".env"

bun-prisma on  master [!] via 🐳 desktop-linux via 🍞 v1.0.0 via  v18.16.0
❯ npx tsx index.ts
PrismaClientKnownRequestError:
Invalid `database.user.create()` invocation in
/Users/dimgl/dev/playground/bun-prisma/index.ts:6:38

  3 export const database = new PrismaClient({ datasourceUrl: "postgres://postgres:postgres@localhost:5433"});
  4
  5 async function main() {
→ 6     const user = await database.user.create(
The table `public.User` does not exist in the current database.
    at vn.handleRequestError (/Users/dimgl/dev/playground/bun-prisma/node_modules/@prisma/client/runtime/library.js:123:6730)
    at vn.handleAndLogRequestError (/Users/dimgl/dev/playground/bun-prisma/node_modules/@prisma/client/runtime/library.js:123:6119)
    at vn.request (/Users/dimgl/dev/playground/bun-prisma/node_modules/@prisma/client/runtime/library.js:123:5839)
    at l (/Users/dimgl/dev/playground/bun-prisma/node_modules/@prisma/client/runtime/library.js:128:9763)
    at main (/Users/dimgl/dev/playground/bun-prisma/index.ts:6:18) {
  code: 'P2021',
  clientVersion: '5.2.0',
  meta: { table: 'public.User' }
}
Fumaz commented 1 year ago

This happens in express middlewares as well

DoctorGester commented 1 year ago

Same issue in my project. An async function called on the top level without an await doesn't prevent the process from exiting. My project has some scripts written before top-level await was available in nodejs.