-
Notifications
You must be signed in to change notification settings - Fork 3
ENG-813 repair and improvements to dbDotEnv #400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Replace __filename with urlFromPath, as it seems to sometimes be called as ejs. Use SUPABASE variables when defined. (It means that the database _will_ be used if those are defined in a `.env` file.) Give warnings to the developer as appropriate. nextjs should use dbDotEnv.
|
This pull request has been ignored for the connected project Preview Branches by Supabase. |
|
@CodeRabbit review |
✅ Actions performedReview triggered.
|
📝 WalkthroughWalkthroughIntroduces database-driven environment loading. apps/website/next.config.ts imports envContents and copies non-falsy entries into process.env at module load. packages/database/src/dbDotEnv.js is refactored to ESM, expands variant detection, and adds envFilePath, envContents, and config helpers. Type declaration updates envContents return type to Record<string, string>. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor Dev as Node (build/start)
participant NextCfg as apps/website/next.config.ts
participant Env as @repo/database/dbDotEnv
participant Proc as process.env
Dev->>NextCfg: import next.config.ts
NextCfg->>Env: envContents()
Note over Env: Determine variant, load from file or process
Env-->>NextCfg: Map<string,string>
loop For each [k,v]
NextCfg->>Proc: if v truthy, set Proc[k]=v
end
NextCfg-->>Dev: Export NextConfig
sequenceDiagram
autonumber
actor Runtime as Node/CI/Browser
participant DBEnv as packages/database/src/dbDotEnv.js
participant FS as fs
participant Proc as process.env
Runtime->>DBEnv: envContents()
DBEnv->>DBEnv: getVariant()
alt CI (Vercel/GitHub) with SUPABASE vars
DBEnv->>DBEnv: variant = implicit
else Missing required vars in CI
DBEnv->>DBEnv: variant = none
else Local/branch/production flags
DBEnv->>DBEnv: resolve variant
end
DBEnv->>DBEnv: envFilePath(variant)
alt Path exists
DBEnv->>FS: read .env*
DBEnv-->>Runtime: parse to map
else No file
DBEnv->>Proc: read existing vars
DBEnv-->>Runtime: map from Proc
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/database/src/dbDotEnv.js (1)
64-75: Sanitize undefined values before returning to honor the d.ts (or relax the d.ts as proposed).When no env file is present, the fallback can include undefineds. Filter to return only string values.
export const envContents = () => { const path = envFilePath(); - if (!path) - // Fallback to process.env when running in production environments - return { - SUPABASE_URL: process.env.SUPABASE_URL, - SUPABASE_ANON_KEY: process.env.SUPABASE_ANON_KEY, - NEXT_API_ROOT: process.env.NEXT_API_ROOT, - }; - const data = readFileSync(path, "utf8"); - return dotenv.parse(data); + if (!path) { + // Fallback to process.env when running in production environments + const raw = { + SUPABASE_URL: process.env.SUPABASE_URL, + SUPABASE_ANON_KEY: process.env.SUPABASE_ANON_KEY, + NEXT_API_ROOT: process.env.NEXT_API_ROOT, + }; + return Object.fromEntries( + Object.entries(raw).filter(([, v]) => v != null && v !== ""), + ); + } + const data = readFileSync(path, "utf8"); + return dotenv.parse(data); };
🧹 Nitpick comments (4)
packages/database/src/dbDotEnv.js (4)
6-12: Harden root discovery to avoid infinite loop on atypical layouts.If “database” isn’t found in the path, dirname('/') === '/' will spin forever. Guard and error out.
-const findRoot = () => { - let dir = fileURLToPath(import.meta.url); - while (basename(dir) !== "database") { - dir = dirname(dir); - } - return dir; -}; +const findRoot = () => { + // start from the directory containing this file + let dir = dirname(fileURLToPath(import.meta.url)); + while (basename(dir) !== "database") { + const parent = dirname(dir); + if (parent === dir) { + throw new Error('Could not locate "database" root from: ' + dir); + } + dir = parent; + } + return dir; +};
15-23: Avoid ReferenceError in browser contexts when accessing process/argv.The comment says this may run in browser or node. Accessing process or process.argv can crash when process is undefined. Guard it.
- const processHasVars = - !!process.env["SUPABASE_URL"] && !!process.env["SUPABASE_ANON_KEY"]; - const useDbArgPos = (process.argv || []).indexOf("--use-db"); - let variant = - useDbArgPos > 0 - ? process.argv[useDbArgPos + 1] - : process.env["SUPABASE_USE_DB"]; + const env = typeof process !== "undefined" && process.env ? process.env : {}; + const argv = + typeof process !== "undefined" && Array.isArray(process.argv) + ? process.argv + : []; + const processHasVars = !!env["SUPABASE_URL"] && !!env["SUPABASE_ANON_KEY"]; + const useDbArgPos = argv.indexOf("--use-db"); + let variant = + useDbArgPos > -1 ? argv[useDbArgPos + 1] : env["SUPABASE_USE_DB"];
57-63: Treat “all” as a non-file variant (avoid useless FS checks).“all” is tooling-oriented; no corresponding .env file should be loaded.
- if (variant === "implicit" || variant === "none") return null; + if (variant === "implicit" || variant === "none" || variant === "all") + return null;
79-84: Consider delegating to dotenv with explicit override behavior.If you prefer not to clobber existing env, set override: false (explicit) to mirror Next/Dotenv expectations.
- if (path) dotenv.config({ path }); + if (path) dotenv.config({ path, override: false });
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (3)
apps/website/next.config.ts(1 hunks)packages/database/src/dbDotEnv.js(1 hunks)packages/database/types/dbDotEnv.d.ts(1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#0
File: :0-0
Timestamp: 2025-07-22T01:50:20.253Z
Learning: In packages/database/scripts/create_env.ts, the Vercel CLI integration uses both vercel/sdk for deployment queries and vercel CLI for environment variable pulling, with support for different variants (local, branch, production, all) and proper team/project configuration with discourse-graphs defaults.
📚 Learning: 2025-07-22T01:50:20.253Z
Learnt from: maparent
PR: DiscourseGraphs/discourse-graph#0
File: :0-0
Timestamp: 2025-07-22T01:50:20.253Z
Learning: In packages/database/scripts/create_env.ts, the Vercel CLI integration uses both vercel/sdk for deployment queries and vercel CLI for environment variable pulling, with support for different variants (local, branch, production, all) and proper team/project configuration with discourse-graphs defaults.
Applied to files:
packages/database/src/dbDotEnv.js
|
@CodeRabbit this will not be used in a browser environment after all, removing that comment and not implementing the suggested changes. |
|
Tip For best results, initiate chat on the files or code changes. @maparent Understood! Thank you for the clarification. Since this code is intended for Node.js environments only, the browser compatibility considerations are indeed unnecessary. This simplifies the implementation and removes the need for browser-specific handling. ✏️ Learnings added
|
| @@ -1,27 +1,55 @@ | |||
| import { readFileSync, existsSync } from "node:fs"; | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@maparent turbo dev is now failing at this line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ouch. Works for me, and in deployment so really puzzled. Let's review your setup first thing tomorrow.
What "other issues with vercel funcions" did you find out about? Could you give some examples? |
|
Issues with vercel functions: They could not connect to the database when expected to do so. This was just not exercised much, so we did not notice. |

https://linear.app/discourse-graphs/issue/ENG-813/repair-and-improvements-to-dbdotenv
Context: I noticed failure of cucumber-js to load dbDotEnv. That led to the first step below, but when testing that I found out about other issues with vercel functions.
Replace __filename with urlFromPath, as it seems to sometimes be called as ejs.
Use SUPABASE variables when defined.
(It means that the database will be used if those are defined in a
.envfile.)Give warnings to the developer as appropriate.
nextjs should use dbDotEnv.
Summary by CodeRabbit
Bug Fixes
Chores