Unverified Commit 9dd974df authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix dotenv loading with cascading values (#15799)

Adds additional test cases for cascading env values and corrects behavior

Fixes: https://github.com/vercel/next.js/issues/15744
parent 9f928197
......@@ -27,6 +27,9 @@ export function processEnv(loadedEnvFiles: LoadedEnvFiles, dir?: string) {
// function is re-used or we are running in `next start` mode
process.env.__NEXT_PROCESSED_ENV = 'true'
const origEnv = Object.assign({}, process.env)
const parsed: dotenv.DotenvParseOutput = {}
for (const envFile of loadedEnvFiles) {
try {
let result: DotenvConfigOutput = {}
......@@ -38,7 +41,14 @@ export function processEnv(loadedEnvFiles: LoadedEnvFiles, dir?: string) {
log.info(`Loaded env from ${path.join(dir || '', envFile.path)}`)
}
Object.assign(process.env, result.parsed)
for (const key of Object.keys(result.parsed || {})) {
if (
typeof parsed[key] === 'undefined' &&
typeof origEnv[key] === 'undefined'
) {
parsed[key] = result.parsed?.[key]!
}
}
} catch (err) {
log.error(
`Failed to load env from ${path.join(dir || '', envFile.path)}`,
......@@ -47,7 +57,7 @@ export function processEnv(loadedEnvFiles: LoadedEnvFiles, dir?: string) {
}
}
return process.env as Env
return Object.assign(process.env, parsed)
}
export function loadEnvConfig(
......
PROCESS_ENV_KEY="env"
ENV_FILE_KEY=env
ENV_FILE_EMPTY_FIRST=
ENV_FILE_LOCAL_OVERRIDE_TEST=env
ENV_FILE_DEVELOPMENT_OVERRIDE_TEST=env
ENV_FILE_DEVELOPMENT_LOCAL_OVERRIDEOVERRIDE_TEST=env
......@@ -11,4 +12,5 @@ NEXT_PUBLIC_TEST_DEST=another
ENV_FILE_EXPANDED=$ENV_FILE_KEY
ENV_FILE_EXPANDED_CONCAT=hello-${ENV_FILE_KEY}
ENV_FILE_EXPANDED_ESCAPED=\$ENV_FILE_KEY
ENV_FILE_KEY_EXCLAMATION="hello!"
\ No newline at end of file
ENV_FILE_KEY_EXCLAMATION="hello!"
ENV_FILE_PROCESS_ENV="env-file"
\ No newline at end of file
LOCAL_ENV_FILE_KEY=localenv
ENV_FILE_EMPTY_FIRST=\$escaped
ENV_FILE_LOCAL_OVERRIDE_TEST=localenv
\ No newline at end of file
const variables = [
'PROCESS_ENV_KEY',
'ENV_FILE_KEY',
'ENV_FILE_EMPTY_FIRST',
'ENV_FILE_PROCESS_ENV',
'LOCAL_ENV_FILE_KEY',
'ENV_FILE_LOCAL_OVERRIDE_TEST',
'PRODUCTION_ENV_FILE_KEY',
......
const variables = [
'PROCESS_ENV_KEY',
'ENV_FILE_KEY',
'ENV_FILE_EMPTY_FIRST',
'ENV_FILE_PROCESS_ENV',
'LOCAL_ENV_FILE_KEY',
'ENV_FILE_LOCAL_OVERRIDE_TEST',
'PRODUCTION_ENV_FILE_KEY',
......
const variables = [
'PROCESS_ENV_KEY',
'ENV_FILE_KEY',
'ENV_FILE_EMPTY_FIRST',
'ENV_FILE_PROCESS_ENV',
'LOCAL_ENV_FILE_KEY',
'ENV_FILE_LOCAL_OVERRIDE_TEST',
'PRODUCTION_ENV_FILE_KEY',
......@@ -25,7 +27,7 @@ export async function getStaticProps() {
const items = {}
variables.forEach((variable) => {
if (process.env[variable]) {
if (typeof process.env[variable] !== 'undefined') {
items[variable] = process.env[variable]
}
})
......
const variables = [
'PROCESS_ENV_KEY',
'ENV_FILE_KEY',
'ENV_FILE_EMPTY_FIRST',
'ENV_FILE_PROCESS_ENV',
'LOCAL_ENV_FILE_KEY',
'ENV_FILE_LOCAL_OVERRIDE_TEST',
'PRODUCTION_ENV_FILE_KEY',
......@@ -25,7 +27,7 @@ export async function getServerSideProps() {
const items = {}
variables.forEach((variable) => {
if (process.env[variable]) {
if (typeof process.env[variable] !== 'undefined') {
items[variable] = process.env[variable]
}
})
......
......@@ -51,6 +51,8 @@ const runTests = (mode = 'dev') => {
expect(data.ENV_FILE_EXPANDED_CONCAT).toBe('hello-env')
expect(data.ENV_FILE_EXPANDED_ESCAPED).toBe('$ENV_FILE_KEY')
expect(data.ENV_FILE_KEY_EXCLAMATION).toBe('hello!')
expect(data.ENV_FILE_EMPTY_FIRST).toBe(isTestEnv ? '' : '$escaped')
expect(data.ENV_FILE_PROCESS_ENV).toBe('env-cli')
}
it('should have process environment override .env', async () => {
......@@ -135,6 +137,7 @@ describe('Env Config', () => {
app = await launchApp(appDir, appPort, {
env: {
PROCESS_ENV_KEY: 'processenvironment',
ENV_FILE_PROCESS_ENV: 'env-cli',
},
})
})
......@@ -150,6 +153,7 @@ describe('Env Config', () => {
env: {
PROCESS_ENV_KEY: 'processenvironment',
NODE_ENV: 'test',
ENV_FILE_PROCESS_ENV: 'env-cli',
},
})
})
......@@ -163,12 +167,17 @@ describe('Env Config', () => {
const { code } = await nextBuild(appDir, [], {
env: {
PROCESS_ENV_KEY: 'processenvironment',
ENV_FILE_PROCESS_ENV: 'env-cli',
},
})
if (code !== 0) throw new Error(`Build failed with exit code ${code}`)
appPort = await findPort()
app = await nextStart(appDir, appPort)
app = await nextStart(appDir, appPort, {
env: {
ENV_FILE_PROCESS_ENV: 'env-cli',
},
})
})
afterAll(() => killApp(app))
......@@ -201,6 +210,7 @@ describe('Env Config', () => {
const { code } = await nextBuild(appDir, [], {
env: {
PROCESS_ENV_KEY: 'processenvironment',
ENV_FILE_PROCESS_ENV: 'env-cli',
},
})
......@@ -213,7 +223,11 @@ describe('Env Config', () => {
await fs.rename(file, `${file}.bak`)
}
app = await nextStart(appDir, appPort)
app = await nextStart(appDir, appPort, {
env: {
ENV_FILE_PROCESS_ENV: 'env-cli',
},
})
})
afterAll(async () => {
for (const file of envFiles) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment