fix(@next/env): include dir in cache key for loadEnvConfig#92061
Open
sleitor wants to merge 1 commit intovercel:canaryfrom
Open
fix(@next/env): include dir in cache key for loadEnvConfig#92061sleitor wants to merge 1 commit intovercel:canaryfrom
sleitor wants to merge 1 commit intovercel:canaryfrom
Conversation
When Next.js internally calls loadEnvConfig before user code runs, the result is cached in module-level variables. The cache check: if (combinedEnv && !forceReload) ignored the `dir` argument, so user code calling loadEnvConfig with a different directory (e.g. the monorepo root) would silently receive the cached result for the original directory instead of loading env files from the requested path. Fix: add a `cachedDir` module-level variable and include it in the cache guard so the cache is only reused when the same directory is requested: if (combinedEnv && !forceReload && cachedDir === dir) The existing workaround of passing `forceReload: true` continues to work unchanged. Closes vercel#92040
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.
Problem
loadEnvConfigin@next/envuses module-level variables to cache the result. The cache guard:ignores the
dirargument — so when Next.js internally callsloadEnvConfig(with the app directory) before user code runs, then user code callsloadEnvConfigwith a different directory (e.g. a monorepo root), the cache returns the previous result for the wrong directory with no warning or error.This is especially common in monorepo setups where
.envfiles live in a parent directory outside the Next.js app.Fixes #92040
Fix
Add a
cachedDirmodule-level variable and include it in the cache check:cachedDiris updated whenever a fresh load is performed. The existingforceReload: trueworkaround continues to work.Testing
Added
test/unit/load-env-cache-dir.test.tscovering:.envfilesdirwithoutforceReloadis no longer silently cached from previous dirdirwithoutforceReloadstill correctly uses the cache