Skip to content

Commit

Permalink
feat: improve the error message of expand (#11141)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people committed Dec 6, 2022
1 parent 51ceb4e commit 825c793
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions packages/vite/src/node/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,29 @@ export function loadEnv(
}),
)

// let environment variables use each other
const expandParsed = expand({
const expandOptions = {
parsed: {
...(process.env as any),
...parsed,
},
// prevent process.env mutation
ignoreProcessEnv: true,
}).parsed!
}

let expandParsed: NonNullable<ReturnType<typeof expand>['parsed']>
try {
// let environment variables use each other
expandParsed = expand(expandOptions).parsed!
} catch (e) {
// custom error handling until https://github.com/motdotla/dotenv-expand/issues/65 is fixed upstream
// check for message "TypeError: Cannot read properties of undefined (reading 'split')"
if (e.message.includes('split')) {
throw new Error(
'dotenv-expand failed to expand env vars. Maybe you need to escape `$`?',
)
}
throw e
}

Object.keys(parsed).forEach((key) => {
parsed[key] = expandParsed[key]
Expand Down

0 comments on commit 825c793

Please sign in to comment.