Skip to content
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

fix: run dotenv-expand for each file separately #18981

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/vite/src/node/__tests__/env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@ describe('loadEnv', () => {
.toMatchInlineSnapshot(`
{
"VITE_APP_BASE_ROUTE": "/app/",
"VITE_APP_BASE_URL": "/app/",
"VITE_APP_BASE_URL": "/",
}
`)
})

test('override 2', () => {
expect(loadEnv('development2', join(__dirname, './env')))
.toMatchInlineSnapshot(`
{
"VITE_APP_BASE_ROUTE": "source",
"VITE_APP_BASE_URL": "/",
"VITE_SOURCE": "source",
}
`)
})
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/src/node/__tests__/env/.env.development2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
VITE_SOURCE=source
VITE_APP_BASE_ROUTE=$VITE_SOURCE
33 changes: 20 additions & 13 deletions packages/vite/src/node/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,30 @@ export function loadEnv(
const env: Record<string, string> = {}
const envFiles = getEnvFilesForMode(mode, envDir)

const parsed = Object.fromEntries(
envFiles.flatMap((filePath) => {
if (!tryStatSync(filePath)?.isFile()) return []
const parsedList = envFiles.map((filePath) => {
if (!tryStatSync(filePath)?.isFile()) return {}

return Object.entries(parse(fs.readFileSync(filePath)))
}),
)
return parse(fs.readFileSync(filePath))
})

const findValue = (name: string) =>
parsedList.findLast((obj) => obj[name])?.[name]
// test NODE_ENV override before expand as otherwise process.env.NODE_ENV would override this
if (parsed.NODE_ENV && process.env.VITE_USER_NODE_ENV === undefined) {
process.env.VITE_USER_NODE_ENV = parsed.NODE_ENV
if (findValue('NODE_ENV') && process.env.VITE_USER_NODE_ENV === undefined) {
process.env.VITE_USER_NODE_ENV = findValue('NODE_ENV')!
}

for (const parsed of parsedList) {
// let environment variables use each other. make a copy of `process.env` so that `dotenv-expand`
// doesn't re-assign the expanded values to the global `process.env`.
const processEnv = { ...process.env } as DotenvPopulateInput
expand({ parsed, processEnv })
}

const parsed = Object.fromEntries(
parsedList.flatMap((parsed) => Object.entries(parsed)),
)

// support BROWSER and BROWSER_ARGS env variables
if (parsed.BROWSER && process.env.BROWSER === undefined) {
process.env.BROWSER = parsed.BROWSER
Expand All @@ -49,11 +61,6 @@ export function loadEnv(
process.env.BROWSER_ARGS = parsed.BROWSER_ARGS
}

// let environment variables use each other. make a copy of `process.env` so that `dotenv-expand`
// doesn't re-assign the expanded values to the global `process.env`.
const processEnv = { ...process.env } as DotenvPopulateInput
expand({ parsed, processEnv })

// only keys that start with prefix are exposed to client
for (const [key, value] of Object.entries(parsed)) {
if (prefixes.some((prefix) => key.startsWith(prefix))) {
Expand Down
Loading