fix(@next/env): include dir in loadEnvConfig cache key#92054
Open
Shailesh93602 wants to merge 1 commit intovercel:canaryfrom
Open
fix(@next/env): include dir in loadEnvConfig cache key#92054Shailesh93602 wants to merge 1 commit intovercel:canaryfrom
Shailesh93602 wants to merge 1 commit intovercel:canaryfrom
Conversation
The cache guard only checked whether a previous load had occurred — it never compared the `dir` argument. In a monorepo where `.env` lives at the workspace root, Next.js internally calls `loadEnvConfig` with the app directory first (no `.env` found there). When user code then calls it with the monorepo root, it silently returns the stale cached result with `loadedEnvFiles: []` and all env vars `undefined` — no error, no warning. Fix by adding a `cachedDir` variable so the cache is only returned when the directory matches. A changed `dir` triggers a fresh load instead of returning the wrong cached result. Workaround before this fix: pass `forceReload: true` as the 4th argument, which is undocumented and requires two `undefined` placeholders to reach.
Collaborator
|
Allow CI Workflow Run
Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
Include
diras part of theloadEnvConfigcache key in@next/env.Why?
loadEnvConfigcaches its result in module-level state, but the cache check only tested whether any load had happened — it never compared thedirargument:In a monorepo where
.envlives at the workspace root, Next.js internally callsloadEnvConfigwith the app directory first (no.envthere, soloadedEnvFiles: []). When user code then calls it with the monorepo root — a common pattern to load shared env vars — it silently hits the stale cache and returns empty results.DATABASE_URL(and all other vars) areundefinedwith no error and no warning.The only workaround was the undocumented
forceReload: true4th argument, which also requires passingundefinedtwice to reach it.Reproduction: https://github.com/aynaitlamine/loadenvconfig-nextjs-issue-reproduction
How?
Added a module-level
cachedDirvariable. The cache is now only returned whencachedDir === dir, andcachedDiris updated whenever a fresh load runs. TheforceReloadpath is unchanged.fixes #92040test/unit/load-env-config-dir-cache.test.tsfixes #92040