Skip to content

Commit 05b3df0

Browse files
authored
fix(env): compatible with env variables ended with unescaped $ (#12031)
1 parent 54d511e commit 05b3df0

File tree

6 files changed

+43
-24
lines changed

6 files changed

+43
-24
lines changed

packages/vite/src/node/env.ts

+3-13
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,9 @@ export function loadEnv(
4747
process.env.BROWSER_ARGS = parsed.BROWSER_ARGS
4848
}
4949

50-
try {
51-
// let environment variables use each other
52-
expand({ parsed })
53-
} catch (e) {
54-
// custom error handling until https://github.com/motdotla/dotenv-expand/issues/65 is fixed upstream
55-
// check for message "TypeError: Cannot read properties of undefined (reading 'split')"
56-
if (e.message.includes('split')) {
57-
throw new Error(
58-
'dotenv-expand failed to expand env vars. Maybe you need to escape `$`?',
59-
)
60-
}
61-
throw e
62-
}
50+
// let environment variables use each other
51+
// `expand` patched in patches/dotenv-expand@9.0.0.patch
52+
expand({ parsed })
6353

6454
// only keys that start with prefix are exposed to client
6555
for (const [key, value] of Object.entries(parsed)) {

patches/dotenv-expand@9.0.0.patch

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
diff --git a/lib/main.js b/lib/main.js
2-
index c873cc77229d4cd0cf9de98ae0970b25d89f312f..ddf570558e985760efde52af37a41b56282d30a6 100644
2+
index c873cc77229d4cd0cf9de98ae0970b25d89f312f..901758c6b665d2935501404fc09c6abd94b7eb1e 100644
33
--- a/lib/main.js
44
+++ b/lib/main.js
5-
@@ -50,9 +50,10 @@ function expand (config) {
5+
@@ -17,6 +17,10 @@ function _interpolate (envValue, environment, config) {
6+
replacePart = parts[0]
7+
value = replacePart.replace('\\$', '$')
8+
} else {
9+
+ // PATCH: compatible with env variables ended with unescaped $
10+
+ if(!parts[2]) {
11+
+ return newEnv
12+
+ }
13+
const keyParts = parts[2].split(':-')
14+
const key = keyParts[0]
15+
replacePart = parts[0].substring(prefix.length)
16+
@@ -50,9 +54,10 @@ function expand (config) {
617
config.parsed[configKey] = _interpolate(value, environment, config)
718
}
819

playground/env/.env

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@ VITE_CUSTOM_ENV_VARIABLE=1
22
CUSTOM_PREFIX_ENV_VARIABLE=1
33
VITE_EFFECTIVE_MODE_FILE_NAME=.env
44
VITE_BOOL=true
5-
VITE_EXPAND=$EXPAND
5+
VITE_EXPAND_A=$EXPAND
6+
VITE_EXPAND_B=$DEPEND_ENV
7+
VITE_ESCAPE_A=escape\$
8+
VITE_ESCAPE_B=escape$
9+
IRRELEVANT_ENV=$DEPEND_ENV
10+
IRRELEVANT_ESCAPE_ENV=irrelevant$
11+
DEPEND_ENV=depend

playground/env/__tests__/env.spec.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,29 @@ test('NODE_ENV', async () => {
4646
})
4747

4848
test('expand', async () => {
49-
expect(await page.textContent('.expand')).toBe('expand')
49+
expect(await page.textContent('.expand-a')).toBe('expand')
50+
expect(await page.textContent('.expand-b')).toBe('depend')
5051
})
5152

5253
test('ssr', async () => {
5354
expect(await page.textContent('.ssr')).toBe('false')
5455
})
5556

5657
test('env object', async () => {
57-
const envText = await page.textContent('.env-object')
58-
expect(JSON.parse(envText)).toMatchObject({
58+
const env = JSON.parse(await page.textContent('.env-object'))
59+
expect(env).not.toHaveProperty([
60+
'DEPEND_ENV',
61+
'IRRELEVANT_ENV',
62+
'IRRELEVANT_ESCAPE_ENV',
63+
])
64+
expect(env).toMatchObject({
5965
VITE_EFFECTIVE_MODE_FILE_NAME: `.env.${mode}`,
6066
CUSTOM_PREFIX_ENV_VARIABLE: '1',
6167
VITE_CUSTOM_ENV_VARIABLE: '1',
68+
VITE_EXPAND_A: 'expand',
69+
VITE_EXPAND_B: 'depend',
70+
VITE_ESCAPE_A: 'escape$',
71+
VITE_ESCAPE_B: 'escape$',
6272
BASE_URL: '/env/',
6373
VITE_BOOL: true,
6474
SSR: false,

playground/env/index.html

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ <h1>Environment Variables</h1>
1414
<p>import.meta.env.VITE_INLINE: <code class="inline"></code></p>
1515
<p>typeof import.meta.env.VITE_BOOL: <code class="bool"></code></p>
1616
<p>process.env.NODE_ENV: <code class="node-env"></code></p>
17-
<p>import.meta.env.VITE_EXPAND: <code class="expand"></code></p>
17+
<p>import.meta.env.VITE_EXPAND_A: <code class="expand-a"></code></p>
18+
<p>import.meta.env.VITE_EXPAND_B: <code class="expand-b"></code></p>
1819
<p>import.meta.env.SSR: <code class="ssr"></code></p>
1920
<p>import.meta.env: <span class="pre env-object"></span></p>
2021
<p>import.meta.url: <span class="pre url"></span></p>
@@ -32,7 +33,8 @@ <h1>Environment Variables</h1>
3233
text('.ssr', import.meta.env.SSR)
3334
text('.node-env', process.env.NODE_ENV)
3435
text('.env-object', JSON.stringify(import.meta.env, null, 2))
35-
text('.expand', import.meta.env.VITE_EXPAND)
36+
text('.expand-a', import.meta.env.VITE_EXPAND_A)
37+
text('.expand-b', import.meta.env.VITE_EXPAND_B)
3638

3739
function text(el, text) {
3840
document.querySelector(el).textContent = text

pnpm-lock.yaml

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)