Skip to content

Commit 825c793

Browse files
Dunqingbenmccannsapphi-red
authoredDec 6, 2022
feat: improve the error message of expand (#11141)
* feat: improve the error message of `expand` * Update packages/vite/src/node/env.ts Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> Co-authored-by: sapphi-red <green@sapphi.red>
1 parent 51ceb4e commit 825c793

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed
 

‎packages/vite/src/node/env.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,29 @@ export function loadEnv(
3535
}),
3636
)
3737

38-
// let environment variables use each other
39-
const expandParsed = expand({
38+
const expandOptions = {
4039
parsed: {
4140
...(process.env as any),
4241
...parsed,
4342
},
4443
// prevent process.env mutation
4544
ignoreProcessEnv: true,
46-
}).parsed!
45+
}
46+
47+
let expandParsed: NonNullable<ReturnType<typeof expand>['parsed']>
48+
try {
49+
// let environment variables use each other
50+
expandParsed = expand(expandOptions).parsed!
51+
} catch (e) {
52+
// custom error handling until https://github.com/motdotla/dotenv-expand/issues/65 is fixed upstream
53+
// check for message "TypeError: Cannot read properties of undefined (reading 'split')"
54+
if (e.message.includes('split')) {
55+
throw new Error(
56+
'dotenv-expand failed to expand env vars. Maybe you need to escape `$`?',
57+
)
58+
}
59+
throw e
60+
}
4761

4862
Object.keys(parsed).forEach((key) => {
4963
parsed[key] = expandParsed[key]

0 commit comments

Comments
 (0)
Please sign in to comment.