From 825c7939efc4bd7fad05cc767d76c65d152d680d Mon Sep 17 00:00:00 2001 From: Dunqing Date: Tue, 6 Dec 2022 14:28:31 +0800 Subject: [PATCH] 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 --- packages/vite/src/node/env.ts | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/env.ts b/packages/vite/src/node/env.ts index 10e570713..431409c2d 100644 --- a/packages/vite/src/node/env.ts +++ b/packages/vite/src/node/env.ts @@ -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['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]