Failed to generate with Bun, Drizzle and PostgreSQL #1047

Closed
opened 2026-03-13 08:20:22 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @Marius-brt on GitHub (Apr 14, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

My project is a monorepo with turborepo. I use Bun, Drizzle and PostgreSQL. For the SQL engine I use the Bun SQL API. The bug appear when I try to generate the schema with the following command :

bunx --bun @better-auth/cli@latest generate --config ./src/auth.ts --output ../database/schemas/base.ts

I reproduced the bug here https://codesandbox.io/p/sandbox/staging-cherry-hpwrfh.
I don't really know if it's a bug of Bun or Better Auth.

Current vs. Expected behavior

When I try to generate schema with the CLI, I have the following error :

auth git:(master) ✗ bunx --bun @better-auth/cli@latest generate --config ./src/auth.ts --output ../database/schemas/base.ts
  🔍 Resolving... 
  TOUCH ba23eeee118cd63e16015df367567cb043fed872.intermediate
  ACTION deps_sqlite3_gyp_locate_sqlite3_target_copy_builtin_sqlite3 ba23eeee118cd63e16015df367567cb043fed872.intermediate
  TOUCH Release/obj.target/deps/locate_sqlite3.stamp
  CC(target) Release/obj.target/sqlite3/gen/sqlite3/sqlite3.o
  LIBTOOL-STATIC Release/sqlite3.a
  CXX(target) Release/obj.target/better_sqlite3/src/better_sqlite3.o
rm ba23eeee118cd63e16015df367567cb043fed872.intermediate

============================================================
Bun v1.2.9 (9a329c04) macOS Silicon
macOS v15.4
CPU: fp aes crc32 atomics
Args: "node" "/private/tmp/bunx-501-@better-auth/cli@latest/node_modules/.bin/prebuild-install"
Features: Bun.stderr(2) Bun.stdout(2) jsc 
Builtins: "bun:main" "node:assert" "node:buffer" "node:child_process" "node:crypto" "node:events" "node:fs" "node:http" "node:https" "node:net" "node:os" "node:path" "node:querystring" "node:stream" "node:string_decoder" "node:tls" "node:url" "node:util" "node:zlib" 
Elapsed: 310ms | User: 65ms | Sys: 33ms
RSS: 57.43MB | Peak: 57.43MB | Commit: 1.07GB | Faults: 230

panic(main thread): Segmentation fault at address 0x0
Crashed while while loading native module: /private/tmp/bunx-501-@better-auth/cli@latest/node_modules/better-sqlite3/build/Release/better_sqlite3.node
oh no: Bun has crashed. This indicates a bug in Bun, not your code.

To send a redacted crash report to Bun's team,
please file a GitHub issue using the link below:

 https://bun.report/1.2.9/Mn19a329c0AqggQ___++04iB______A2AA

/bin/bash: line 1: 46111 Trace/BPT trap: 5       prebuild-install
gyp info it worked if it ends with ok
gyp info using node-gyp@11.2.0
gyp info using node@22.6.0 | darwin | arm64
gyp info find Python using Python version 3.12.7 found at "/opt/anaconda3/bin/python3"
gyp info spawn /opt/anaconda3/bin/python3
gyp info spawn args [
gyp info spawn args '/private/tmp/bunx-501-node-gyp@latest/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args 'binding.gyp',
gyp info spawn args '-f',
gyp info spawn args 'make',
gyp info spawn args '-I',
gyp info spawn args '/private/tmp/bunx-501-@better-auth/cli@latest/node_modules/better-sqlite3/build/config.gypi',
gyp info spawn args '-I',
gyp info spawn args '/private/tmp/bunx-501-node-gyp@latest/node_modules/node-gyp/addon.gypi',
gyp info spawn args '-I',
gyp info spawn args '/Users/marius/Library/Caches/node-gyp/22.6.0/include/node/common.gypi',
gyp info spawn args '-Dlibrary=shared_library',
gyp info spawn args '-Dvisibility=default',
gyp info spawn args '-Dnode_root_dir=/Users/marius/Library/Caches/node-gyp/22.6.0',
gyp info spawn args '-Dnode_gyp_dir=/private/tmp/bunx-501-node-gyp@latest/node_modules/node-gyp',
gyp info spawn args '-Dnode_lib_file=/Users/marius/Library/Caches/node-gyp/22.6.0/<(target_arch)/node.lib',
gyp info spawn args '-Dmodule_root_dir=/private/tmp/bunx-501-@better-auth/cli@latest/node_modules/better-sqlite3',
gyp info spawn args '-Dnode_engine=v8',
gyp info spawn args '--depth=.',
gyp info spawn args '--no-parallel',
gyp info spawn args '--generator-output',
gyp info spawn args 'build',
gyp info spawn args '-Goutput_dir=.'
gyp info spawn args ]
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
In file included from ../src/better_sqlite3.cpp:4:
./src/better_sqlite3.lzz:2:10: fatal error: 'climits' file not found
    2 | #include <climits>
      |          ^~~~~~~~~
1 error generated.
make: *** [Release/obj.target/better_sqlite3/src/better_sqlite3.o] Error 1
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack at <anonymous> (/private/tmp/bunx-501-node-gyp@latest/node_modules/node-gyp/lib/build.js:219:27)
gyp ERR! stack at processTicksAndRejections (native:7:39)
gyp ERR! System Darwin 24.4.0
gyp ERR! command "/opt/homebrew/Cellar/bun/1.0.31/bin/bun" "/private/tmp/bunx-501-node-gyp@latest/node_modules/.bin/node-gyp" "rebuild" "--release"
gyp ERR! cwd /private/tmp/bunx-501-@better-auth/cli@latest/node_modules/better-sqlite3
gyp ERR! node -v v22.6.0
gyp ERR! node-gyp -v v11.2.0
gyp ERR! not ok 

error: install script from "better-sqlite3" exited with 1

It's seem to be similare to #2155 but the problem here is that I don't use Sqlite but PostgreSQL. So it's weird that it failed with better-sqlite3. Here is my auth.ts :

import { db } from '@repo/database';
import { betterAuth } from 'better-auth';
import { drizzleAdapter } from 'better-auth/adapters/drizzle';

export const auth = betterAuth({
  database: drizzleAdapter(db, {
    provider: 'pg'
  })
});

And this is my database client :

import { SQL } from 'bun';
import { drizzle } from 'drizzle-orm/bun-sql';
import * as baseSchemas from './schemas/base';

const client = new SQL(process.env.DATABASE_URL || '');

export const db = drizzle({
  client,
  schema: {
    ...baseSchemas
    // Add other schemas here
  }
});

What version of Better Auth are you using?

1.2.7

Provide environment information

- MacOS 15.4
- Bun 1.2.9
- Drizzle 0.41.0

Which area(s) are affected? (Select all that apply)

Other

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

No response

Originally created by @Marius-brt on GitHub (Apr 14, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce My project is a monorepo with turborepo. I use Bun, Drizzle and PostgreSQL. For the SQL engine I use the Bun SQL API. The bug appear when I try to generate the schema with the following command : ```bash bunx --bun @better-auth/cli@latest generate --config ./src/auth.ts --output ../database/schemas/base.ts ``` I reproduced the bug here [https://codesandbox.io/p/sandbox/staging-cherry-hpwrfh](https://codesandbox.io/p/sandbox/staging-cherry-hpwrfh). I don't really know if it's a bug of Bun or Better Auth. ### Current vs. Expected behavior When I try to generate schema with the CLI, I have the following error : ```bash auth git:(master) ✗ bunx --bun @better-auth/cli@latest generate --config ./src/auth.ts --output ../database/schemas/base.ts 🔍 Resolving... TOUCH ba23eeee118cd63e16015df367567cb043fed872.intermediate ACTION deps_sqlite3_gyp_locate_sqlite3_target_copy_builtin_sqlite3 ba23eeee118cd63e16015df367567cb043fed872.intermediate TOUCH Release/obj.target/deps/locate_sqlite3.stamp CC(target) Release/obj.target/sqlite3/gen/sqlite3/sqlite3.o LIBTOOL-STATIC Release/sqlite3.a CXX(target) Release/obj.target/better_sqlite3/src/better_sqlite3.o rm ba23eeee118cd63e16015df367567cb043fed872.intermediate ============================================================ Bun v1.2.9 (9a329c04) macOS Silicon macOS v15.4 CPU: fp aes crc32 atomics Args: "node" "/private/tmp/bunx-501-@better-auth/cli@latest/node_modules/.bin/prebuild-install" Features: Bun.stderr(2) Bun.stdout(2) jsc Builtins: "bun:main" "node:assert" "node:buffer" "node:child_process" "node:crypto" "node:events" "node:fs" "node:http" "node:https" "node:net" "node:os" "node:path" "node:querystring" "node:stream" "node:string_decoder" "node:tls" "node:url" "node:util" "node:zlib" Elapsed: 310ms | User: 65ms | Sys: 33ms RSS: 57.43MB | Peak: 57.43MB | Commit: 1.07GB | Faults: 230 panic(main thread): Segmentation fault at address 0x0 Crashed while while loading native module: /private/tmp/bunx-501-@better-auth/cli@latest/node_modules/better-sqlite3/build/Release/better_sqlite3.node oh no: Bun has crashed. This indicates a bug in Bun, not your code. To send a redacted crash report to Bun's team, please file a GitHub issue using the link below: https://bun.report/1.2.9/Mn19a329c0AqggQ___++04iB______A2AA /bin/bash: line 1: 46111 Trace/BPT trap: 5 prebuild-install gyp info it worked if it ends with ok gyp info using node-gyp@11.2.0 gyp info using node@22.6.0 | darwin | arm64 gyp info find Python using Python version 3.12.7 found at "/opt/anaconda3/bin/python3" gyp info spawn /opt/anaconda3/bin/python3 gyp info spawn args [ gyp info spawn args '/private/tmp/bunx-501-node-gyp@latest/node_modules/node-gyp/gyp/gyp_main.py', gyp info spawn args 'binding.gyp', gyp info spawn args '-f', gyp info spawn args 'make', gyp info spawn args '-I', gyp info spawn args '/private/tmp/bunx-501-@better-auth/cli@latest/node_modules/better-sqlite3/build/config.gypi', gyp info spawn args '-I', gyp info spawn args '/private/tmp/bunx-501-node-gyp@latest/node_modules/node-gyp/addon.gypi', gyp info spawn args '-I', gyp info spawn args '/Users/marius/Library/Caches/node-gyp/22.6.0/include/node/common.gypi', gyp info spawn args '-Dlibrary=shared_library', gyp info spawn args '-Dvisibility=default', gyp info spawn args '-Dnode_root_dir=/Users/marius/Library/Caches/node-gyp/22.6.0', gyp info spawn args '-Dnode_gyp_dir=/private/tmp/bunx-501-node-gyp@latest/node_modules/node-gyp', gyp info spawn args '-Dnode_lib_file=/Users/marius/Library/Caches/node-gyp/22.6.0/<(target_arch)/node.lib', gyp info spawn args '-Dmodule_root_dir=/private/tmp/bunx-501-@better-auth/cli@latest/node_modules/better-sqlite3', gyp info spawn args '-Dnode_engine=v8', gyp info spawn args '--depth=.', gyp info spawn args '--no-parallel', gyp info spawn args '--generator-output', gyp info spawn args 'build', gyp info spawn args '-Goutput_dir=.' gyp info spawn args ] gyp info spawn make gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ] In file included from ../src/better_sqlite3.cpp:4: ./src/better_sqlite3.lzz:2:10: fatal error: 'climits' file not found 2 | #include <climits> | ^~~~~~~~~ 1 error generated. make: *** [Release/obj.target/better_sqlite3/src/better_sqlite3.o] Error 1 gyp ERR! build error gyp ERR! stack Error: `make` failed with exit code: 2 gyp ERR! stack at <anonymous> (/private/tmp/bunx-501-node-gyp@latest/node_modules/node-gyp/lib/build.js:219:27) gyp ERR! stack at processTicksAndRejections (native:7:39) gyp ERR! System Darwin 24.4.0 gyp ERR! command "/opt/homebrew/Cellar/bun/1.0.31/bin/bun" "/private/tmp/bunx-501-node-gyp@latest/node_modules/.bin/node-gyp" "rebuild" "--release" gyp ERR! cwd /private/tmp/bunx-501-@better-auth/cli@latest/node_modules/better-sqlite3 gyp ERR! node -v v22.6.0 gyp ERR! node-gyp -v v11.2.0 gyp ERR! not ok error: install script from "better-sqlite3" exited with 1 ``` It's seem to be similare to #2155 but the problem here is that I don't use Sqlite but PostgreSQL. So it's weird that it failed with `better-sqlite3`. Here is my `auth.ts` : ```ts import { db } from '@repo/database'; import { betterAuth } from 'better-auth'; import { drizzleAdapter } from 'better-auth/adapters/drizzle'; export const auth = betterAuth({ database: drizzleAdapter(db, { provider: 'pg' }) }); ```` And this is my database client : ```ts import { SQL } from 'bun'; import { drizzle } from 'drizzle-orm/bun-sql'; import * as baseSchemas from './schemas/base'; const client = new SQL(process.env.DATABASE_URL || ''); export const db = drizzle({ client, schema: { ...baseSchemas // Add other schemas here } }); ``` ### What version of Better Auth are you using? 1.2.7 ### Provide environment information ```bash - MacOS 15.4 - Bun 1.2.9 - Drizzle 0.41.0 ``` ### Which area(s) are affected? (Select all that apply) Other ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context _No response_
GiteaMirror added the bugwontfix labels 2026-03-13 08:20:22 -05:00
Author
Owner

@LuggaPugga commented on GitHub (Apr 21, 2025):

Im having the same issue on ubuntu

@LuggaPugga commented on GitHub (Apr 21, 2025): Im having the same issue on ubuntu
Author
Owner

@ping-maxwell commented on GitHub (Apr 23, 2025):

Use another package manager such as pnpm. @Marius-brt

@ping-maxwell commented on GitHub (Apr 23, 2025): Use another package manager such as pnpm. @Marius-brt
Author
Owner

@ping-maxwell commented on GitHub (Apr 23, 2025):

I personally run into issues running schema generating using bunx as well, but other package managers work fine.

I'm going to close this for now.

@ping-maxwell commented on GitHub (Apr 23, 2025): I personally run into issues running schema generating using bunx as well, but other package managers work fine. I'm going to close this for now.
Author
Owner

@elitan commented on GitHub (Aug 8, 2025):

Docker Build Timeout Solution

I encountered a very similar issue with Docker build timeouts when was being compiled during Docker builds, even though I'm using PostgreSQL. Here's how I solved it:

The Problem

  • Docker builds would timeout during bun install v1.2.13 (64ed68c9)

Checked 750 installs across 809 packages (no changes) [191.00ms] because (pulled in by ) needed to compile native binaries

  • The compilation required Python and build tools that weren't available in the Alpine containers
  • Even with build tools installed, the compilation was taking too long for deployment timeouts

The Solution

Multi-stage Docker build with proper environment setup:

FROM oven/bun:1-debian AS builder

# Install Python and build dependencies
RUN apt-get update && apt-get install -y python3 python3-pip build-essential && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Copy package files and install ALL dependencies (including dev) for building
COPY package.json bun.lock* ./
RUN bun install --frozen-lockfile

# Copy source code and build
COPY . .
ENV SKIP_ENV_VALIDATION=1
ENV NODE_ENV=production
RUN bun run build

# Production stage
FROM oven/bun:1-debian AS runner

WORKDIR /app

# Add non-root user
RUN groupadd --system --gid 1001 nodejs
RUN useradd --system --uid 1001 nextjs

# Copy built application
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/package.json ./package.json

# Install only production dependencies
RUN bun install --production --frozen-lockfile

# Set permissions
RUN chown -R nextjs:nodejs /app
USER nextjs

EXPOSE 3000

ENV NODE_ENV=production
ENV PORT=3000

CMD ["bun", "start"]

Key points:

  1. Use Debian instead of Alpine - Better build tool support
  2. Two-stage build - Install dev dependencies (including with ) for building, then create clean production image
  3. Production dependencies only in final stage - Avoids rebuilding SQLite in production
  4. Skip environment validation during build - For Next.js apps using t3-env

Environment Validation Skip

If using , add this to your env config:

export const env = createEnv({
  skipValidation: \!\!process.env.SKIP_ENV_VALIDATION,
  // ... rest of config
});

This reduced my build time from timeout (>80s) to ~21 seconds and eliminated the SQLite compilation issues entirely.

Alternative simpler solution: If you don't need the CLI tools in production, consider moving to a separate development-only package.json or using a different package manager as suggested.

@elitan commented on GitHub (Aug 8, 2025): ## Docker Build Timeout Solution I encountered a very similar issue with Docker build timeouts when was being compiled during Docker builds, even though I'm using PostgreSQL. Here's how I solved it: ### The Problem - Docker builds would timeout during bun install v1.2.13 (64ed68c9) Checked 750 installs across 809 packages (no changes) [191.00ms] because (pulled in by ) needed to compile native binaries - The compilation required Python and build tools that weren't available in the Alpine containers - Even with build tools installed, the compilation was taking too long for deployment timeouts ### The Solution **Multi-stage Docker build with proper environment setup:** ```dockerfile FROM oven/bun:1-debian AS builder # Install Python and build dependencies RUN apt-get update && apt-get install -y python3 python3-pip build-essential && rm -rf /var/lib/apt/lists/* WORKDIR /app # Copy package files and install ALL dependencies (including dev) for building COPY package.json bun.lock* ./ RUN bun install --frozen-lockfile # Copy source code and build COPY . . ENV SKIP_ENV_VALIDATION=1 ENV NODE_ENV=production RUN bun run build # Production stage FROM oven/bun:1-debian AS runner WORKDIR /app # Add non-root user RUN groupadd --system --gid 1001 nodejs RUN useradd --system --uid 1001 nextjs # Copy built application COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next COPY --from=builder --chown=nextjs:nodejs /app/public ./public COPY --from=builder --chown=nextjs:nodejs /app/package.json ./package.json # Install only production dependencies RUN bun install --production --frozen-lockfile # Set permissions RUN chown -R nextjs:nodejs /app USER nextjs EXPOSE 3000 ENV NODE_ENV=production ENV PORT=3000 CMD ["bun", "start"] ``` **Key points:** 1. **Use Debian instead of Alpine** - Better build tool support 2. **Two-stage build** - Install dev dependencies (including with ) for building, then create clean production image 3. **Production dependencies only in final stage** - Avoids rebuilding SQLite in production 4. **Skip environment validation during build** - For Next.js apps using t3-env ### Environment Validation Skip If using , add this to your env config: ```typescript export const env = createEnv({ skipValidation: \!\!process.env.SKIP_ENV_VALIDATION, // ... rest of config }); ``` This reduced my build time from timeout (>80s) to ~21 seconds and eliminated the SQLite compilation issues entirely. **Alternative simpler solution:** If you don't need the CLI tools in production, consider moving to a separate development-only package.json or using a different package manager as suggested.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1047