Skip to content

bun auto-loads project .env from CWD — DATABASE_URL silently overrides gbrain config with no warning #427

@zek01svg

Description

@zek01svg

Summary

If you run gbrain from a project directory that has DATABASE_URL in its .env file (common in Next.js, Hono, Supabase, and most Node/bun web apps), bun automatically loads that .env and DATABASE_URL silently overrides the URL in ~/.gbrain/config.json. gbrain connects to the wrong database with no indication that a project .env is involved.

The error message points the user at the wrong place:

Cannot connect to database: Tenant or user not found.
Fix: Check your connection URL in ~/.gbrain/config.json

The URL in config.json is correct. The .env file is the culprit — but nothing says so.

Reproduction

  1. Have a working gbrain install with ~/.gbrain/config.json pointing at your brain (e.g. Supabase project A).
  2. Create a web project with a .env containing DATABASE_URL=<some other postgres URL> (e.g. the app's own Supabase project B).
  3. cd into that project and run gbrain stats.
  4. gbrain connects to project B (or fails with "Tenant or user not found" if project B does not exist), with no warning.

Environment

  • OS: Windows 11 (also affects any OS where the project .env has DATABASE_URL)
  • Bun: 1.x (bun auto-loads .env from CWD by default)
  • gbrain: 0.18.2
  • Brain backend: Supabase postgres pooler (port 6543)

Root cause

loadConfig() in src/core/config.ts:

const dbUrl = process.env.GBRAIN_DATABASE_URL || process.env.DATABASE_URL;

DATABASE_URL is used by virtually every web framework that talks to Postgres (Prisma, Drizzle, Supabase client, etc.). When bun loads the project .env, it injects DATABASE_URL into process.env before gbrain reads it, so it wins over config.json.

Why this is hard to debug

  • The error says "Check your connection URL in ~/.gbrain/config.json" — config.json is fine.
  • No indication that an env var is active or where it came from.
  • Took ~2 hours to isolate by adding console.log to loadConfig().

Suggested fix

When DATABASE_URL (not GBRAIN_DATABASE_URL) is the active source, emit a one-time warning:

[gbrain] Warning: DATABASE_URL env var is overriding ~/.gbrain/config.json.
         This may be from a project .env file. Use GBRAIN_DATABASE_URL to
         avoid conflicts with application databases.

Or drop the DATABASE_URL fallback entirely — too collision-prone for a tool running inside arbitrary project directories.

Workaround

Set GBRAIN_DATABASE_URL as a persistent env var (takes precedence over DATABASE_URL).

macOS/Linux:

export GBRAIN_DATABASE_URL="<your gbrain connection string>"

Windows:

[System.Environment]::SetEnvironmentVariable("GBRAIN_DATABASE_URL", "<url>", "User")

Also add to the gbrain MCP server env block in ~/.claude.json so it inherits correctly regardless of CWD.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions