Skip to content

Commit 9365583

Browse files
committed
fix: use bunx --bun for drizzle-kit to ensure .env.local loading
1 parent b41a92d commit 9365583

File tree

5 files changed

+33
-47
lines changed

5 files changed

+33
-47
lines changed

.bin/bun

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -202,46 +202,10 @@ check_env_setup() {
202202
echo "" >&2
203203
}
204204

205-
# Clear inherited env vars that would conflict with .env file values
206-
# Bun loads .env files natively, but inherited shell variables take precedence.
207-
# We unset variables defined in our env files so Bun can set fresh values.
208-
clear_inherited_env_vars() {
209-
# Skip clearing if CODEBUFF_SKIP_ENV_CLEAR is set
210-
# Used by TypeScript scripts that already have env vars loaded via Bun
211-
if [ -n "$CODEBUFF_SKIP_ENV_CLEAR" ]; then
212-
return
213-
fi
214-
215-
# Unset variables from .env.local so Bun can load fresh values
216-
if [ -f "$ENV_LOCAL_FILE" ]; then
217-
while IFS='=' read -r key _; do
218-
# Skip comments and empty lines
219-
case "$key" in
220-
'#'*|'') continue ;;
221-
esac
222-
# Remove any 'export ' prefix
223-
key="${key#export }"
224-
unset "$key" 2>/dev/null || true
225-
done < "$ENV_LOCAL_FILE"
226-
fi
227-
228-
# Unset variables from .env.development.local
229-
if [ -f "$ENV_DEVELOPMENT_LOCAL_FILE" ]; then
230-
while IFS='=' read -r key _; do
231-
case "$key" in
232-
'#'*|'') continue ;;
233-
esac
234-
key="${key#export }"
235-
unset "$key" 2>/dev/null || true
236-
done < "$ENV_DEVELOPMENT_LOCAL_FILE"
237-
fi
238-
}
239-
240205
run_bun() {
241206
create_env_symlinks
242207
check_env_setup
243-
clear_inherited_env_vars
244-
# Bun natively loads .env files in the correct precedence order
208+
# Bun natively loads .env files and gives them precedence over inherited shell vars
245209
exec "$REAL_BUN" "$@"
246210
}
247211

packages/internal/knowledge.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# @codebuff/internal
2+
3+
This package contains internal utilities, database schema, and shared server-side code.
4+
5+
## Database Commands
6+
7+
### Why `bunx --bun` for Drizzle Commands
8+
9+
All drizzle-kit scripts in `package.json` use `bunx --bun` instead of calling `drizzle-kit` directly:
10+
11+
```json
12+
"db:generate": "bunx --bun drizzle-kit generate --config=./src/db/drizzle.config.ts",
13+
"db:migrate": "bunx --bun drizzle-kit push --config=./src/db/drizzle.config.ts",
14+
"db:studio": "bunx --bun drizzle-kit studio --config=./src/db/drizzle.config.ts"
15+
```
16+
17+
**Why this is necessary:**
18+
19+
1. `drizzle-kit` runs via Node.js (not Bun) by default - it has `#!/usr/bin/env node` in its shebang
20+
2. Node.js does NOT auto-load `.env.local` files like Bun does
21+
3. Without `--bun`, drizzle-kit won't have access to environment variables like `DATABASE_URL`
22+
4. The `--bun` flag forces the command to run via Bun's runtime, which properly loads `.env.local`
23+
24+
**If you add new drizzle-kit scripts**, always use `bunx --bun drizzle-kit ...` to ensure environment variables are available.

packages/internal/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
"scripts": {
3939
"typecheck": "tsc --noEmit -p .",
4040
"test": "bun test",
41-
"db:generate": "drizzle-kit generate --config=./src/db/drizzle.config.ts",
42-
"db:migrate": "drizzle-kit push --config=./src/db/drizzle.config.ts",
41+
"db:generate": "bunx --bun drizzle-kit generate --config=./src/db/drizzle.config.ts",
42+
"db:migrate": "bunx --bun drizzle-kit push --config=./src/db/drizzle.config.ts",
4343
"db:start": "docker compose -f ./src/db/docker-compose.yml up --wait && bun run db:generate && (timeout 1 || sleep 1) && bun run db:migrate",
44-
"db:studio": "drizzle-kit studio --config=./src/db/drizzle.config.ts"
44+
"db:studio": "bunx --bun drizzle-kit studio --config=./src/db/drizzle.config.ts"
4545
},
4646
"sideEffects": false,
4747
"engines": {

scripts/dev.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import { join, resolve } from 'path'
2323
const PROJECT_ROOT = resolve(import.meta.dir, '..')
2424
const LOG_DIR = join(PROJECT_ROOT, 'debug', 'console')
2525
const PID_FILE = join(LOG_DIR, 'services.json')
26-
// Use the .bin/bun wrapper with CODEBUFF_SKIP_ENV_CLEAR to preserve inherited env vars
2726
const BUN_PATH = join(PROJECT_ROOT, '.bin', 'bun')
2827

2928
// Get config from environment (Bun loads .env files automatically)
@@ -156,7 +155,7 @@ function startDb(): boolean {
156155
const result = spawnSync(BUN_PATH, ['--cwd', 'packages/internal', 'db:start'], {
157156
cwd: PROJECT_ROOT,
158157
stdio: ['ignore', logFile, logFile],
159-
env: { ...process.env, CODEBUFF_SKIP_ENV_CLEAR: 'true' },
158+
env: process.env,
160159
})
161160

162161
if (result.status !== 0) {
@@ -180,7 +179,7 @@ function spawnBackgroundProcess(
180179
cwd: PROJECT_ROOT,
181180
detached: true,
182181
stdio: ['ignore', logFile, logFile],
183-
env: { ...process.env, CODEBUFF_SKIP_ENV_CLEAR: 'true' },
182+
env: process.env,
184183
})
185184

186185
child.unref()
@@ -253,7 +252,7 @@ function startCli(args: string[]): Promise<number> {
253252
cliProcess = spawn(BUN_PATH, ['--cwd', 'cli', 'dev', ...args], {
254253
cwd: PROJECT_ROOT,
255254
stdio: 'inherit',
256-
env: { ...process.env, CODEBUFF_SKIP_ENV_CLEAR: 'true' },
255+
env: process.env,
257256
})
258257

259258
cliProcess.on('close', (code) => {

scripts/start-services.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import { join, resolve } from 'path'
2525
const PROJECT_ROOT = resolve(import.meta.dir, '..')
2626
const LOG_DIR = join(PROJECT_ROOT, 'debug', 'console')
2727
const PID_FILE = join(LOG_DIR, 'services.json')
28-
// Use the .bin/bun wrapper with CODEBUFF_SKIP_ENV_CLEAR to preserve inherited env vars
2928
const BUN_PATH = join(PROJECT_ROOT, '.bin', 'bun')
3029

3130
// Get config from environment (Bun loads .env files automatically)
@@ -127,7 +126,7 @@ function startDb(): boolean {
127126
const result = spawnSync(BUN_PATH, ['--cwd', 'packages/internal', 'db:start'], {
128127
cwd: PROJECT_ROOT,
129128
stdio: ['ignore', logFile, logFile],
130-
env: { ...process.env, CODEBUFF_SKIP_ENV_CLEAR: 'true' },
129+
env: process.env,
131130
})
132131

133132
if (result.status !== 0) {
@@ -152,7 +151,7 @@ function spawnBackgroundProcess(
152151
cwd: PROJECT_ROOT,
153152
detached: true,
154153
stdio: ['ignore', logFile, logFile],
155-
env: { ...process.env, CODEBUFF_SKIP_ENV_CLEAR: 'true' },
154+
env: process.env,
156155
})
157156

158157
child.unref()

0 commit comments

Comments
 (0)