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: allow expandable env vars to reference any non-expandable env vars #18982

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
11 changes: 11 additions & 0 deletions packages/vite/src/node/__tests__/env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ describe('loadEnv', () => {
`)
})

test('override 2', () => {
expect(loadEnv('development2', join(__dirname, './env')))
.toMatchInlineSnapshot(`
{
"VITE_APP_BASE_ROUTE": "source",
"VITE_APP_BASE_URL": "source",
"VITE_SOURCE": "source",
}
`)
})

test('VITE_USER_NODE_ENV', () => {
loadEnv('development', join(__dirname, './env'))
expect(process.env.VITE_USER_NODE_ENV).toEqual(undefined)
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
28 changes: 27 additions & 1 deletion packages/vite/src/node/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function loadEnv(
const env: Record<string, string> = {}
const envFiles = getEnvFilesForMode(mode, envDir)

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

Expand All @@ -49,6 +49,8 @@ export function loadEnv(
process.env.BROWSER_ARGS = parsed.BROWSER_ARGS
}

parsed = reorderEnvs(parsed)

// 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 Down Expand Up @@ -83,3 +85,27 @@ export function resolveEnvPrefix({
}
return envPrefix
}

// https://github.com/motdotla/dotenv-expand/blob/6af96d46a28db30779bec90b6e9f40a98028a735/lib/main.js#L10C17-L10C73
const dotEnvExpandRE = /(?<!\\)\$\{[^{}]+\}|(?<!\\)\$[A-Za-z_]\w*/
const isExpandable = (value: string) => dotEnvExpandRE.test(value)

/**
* Reorder env keys so that env vars that are expandable are last
*
* This is to make all expandable variable possible to reference non-expandable variables
*/
function reorderEnvs(input: Record<string, string>) {
return Object.fromEntries(
Object.entries(input).sort(([, a], [, b]) => {
const aIsExpandable = isExpandable(a)
const bIsExpandable = isExpandable(b)
if (
(aIsExpandable && bIsExpandable) ||
(!aIsExpandable && !bIsExpandable)
)
return 0
return aIsExpandable ? 1 : -1
}),
)
}
Loading