Skip to content

Commit d4a1e2b

Browse files
authored
fix(env): prevent env expand on process.env (#11213)
1 parent f1b891f commit d4a1e2b

File tree

4 files changed

+44
-18
lines changed

4 files changed

+44
-18
lines changed

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@
127127
"acorn": "*"
128128
}
129129
}
130+
},
131+
"patchedDependencies": {
132+
"dotenv-expand@9.0.0": "patches/dotenv-expand@9.0.0.patch"
130133
}
131134
}
132135
}

packages/vite/src/node/env.ts

+1-15
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,9 @@ export function loadEnv(
3535
}),
3636
)
3737

38-
const expandOptions = {
39-
parsed: {
40-
...(process.env as any),
41-
...parsed,
42-
},
43-
// prevent process.env mutation
44-
ignoreProcessEnv: true,
45-
}
46-
47-
let expandParsed: NonNullable<ReturnType<typeof expand>['parsed']>
4838
try {
4939
// let environment variables use each other
50-
expandParsed = expand(expandOptions).parsed!
40+
expand({ parsed })
5141
} catch (e) {
5242
// custom error handling until https://github.com/motdotla/dotenv-expand/issues/65 is fixed upstream
5343
// check for message "TypeError: Cannot read properties of undefined (reading 'split')"
@@ -59,10 +49,6 @@ export function loadEnv(
5949
throw e
6050
}
6151

62-
Object.keys(parsed).forEach((key) => {
63-
parsed[key] = expandParsed[key]
64-
})
65-
6652
// only keys that start with prefix are exposed to client
6753
for (const [key, value] of Object.entries(parsed)) {
6854
if (prefixes.some((prefix) => key.startsWith(prefix))) {

patches/dotenv-expand@9.0.0.patch

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
diff --git a/lib/main.js b/lib/main.js
2+
index c873cc77229d4cd0cf9de98ae0970b25d89f312f..ddf570558e985760efde52af37a41b56282d30a6 100644
3+
--- a/lib/main.js
4+
+++ b/lib/main.js
5+
@@ -50,9 +50,10 @@ function expand (config) {
6+
config.parsed[configKey] = _interpolate(value, environment, config)
7+
}
8+
9+
- for (const processKey in config.parsed) {
10+
- environment[processKey] = config.parsed[processKey]
11+
- }
12+
+ // PATCH: don't write to process.env
13+
+ // for (const processKey in config.parsed) {
14+
+ // environment[processKey] = config.parsed[processKey]
15+
+ // }
16+
17+
return config
18+
}

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)