Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix env reloading for turbopack #52194

Merged
merged 3 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/next-env/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ export function processEnv(
return Object.assign(process.env, parsed)
}

export function resetEnv() {
if (initialEnv) {
replaceProcessEnv(initialEnv)
}
}

export function loadEnvConfig(
dir: string,
dev?: boolean,
Expand Down
4 changes: 4 additions & 0 deletions packages/next/src/cli/next-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { getNpxCommand } from '../lib/helpers/get-npx-command'
import Watchpack from 'watchpack'
import stripAnsi from 'next/dist/compiled/strip-ansi'
import { getPossibleInstrumentationHookFilenames } from '../build/worker'
import { resetEnv } from '@next/env'

let dir: string
let isTurboSession = false
Expand Down Expand Up @@ -293,6 +294,9 @@ const nextDev: CliCommand = async (argv) => {
}
}

// Turbopack need to be in control over reading the .env files and watching them.
// So we need to start with a initial env to know which env vars are coming from the user.
resetEnv()
let bindings: any = await loadBindings()
let server = bindings.turbo.startDev({
...devServerOptions,
Expand Down
60 changes: 29 additions & 31 deletions test/development/app-hmr/hmr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ createNextDescribe(
{
files: __dirname,
},
({ next, isTurbopack }) => {
({ next }) => {
describe('filesystem changes', () => {
it('should not break when renaming a folder', async () => {
const browser = await next.browser('/folder')
Expand All @@ -35,39 +35,37 @@ createNextDescribe(
}
})

if (!isTurbopack) {
it('should update server components pages when env files is changed (nodejs)', async () => {
const envContent = await next.readFile(envFile)
const browser = await next.browser('/env/node')
expect(await browser.elementByCss('p').text()).toBe('mac')
await next.patchFile(envFile, 'MY_DEVICE="ipad"')
it('should update server components pages when env files is changed (nodejs)', async () => {
const envContent = await next.readFile(envFile)
const browser = await next.browser('/env/node')
expect(await browser.elementByCss('p').text()).toBe('mac')
await next.patchFile(envFile, 'MY_DEVICE="ipad"')

try {
await check(async () => {
expect(await browser.elementByCss('p').text()).toBe('ipad')
return 'success'
}, /success/)
} finally {
await next.patchFile(envFile, envContent)
}
})
try {
await check(async () => {
expect(await browser.elementByCss('p').text()).toBe('ipad')
return 'success'
}, /success/)
} finally {
await next.patchFile(envFile, envContent)
}
})

it('should update server components pages when env files is changed (edge)', async () => {
const envContent = await next.readFile(envFile)
const browser = await next.browser('/env/edge')
expect(await browser.elementByCss('p').text()).toBe('mac')
await next.patchFile(envFile, 'MY_DEVICE="ipad"')
it('should update server components pages when env files is changed (edge)', async () => {
const envContent = await next.readFile(envFile)
const browser = await next.browser('/env/edge')
expect(await browser.elementByCss('p').text()).toBe('mac')
await next.patchFile(envFile, 'MY_DEVICE="ipad"')

try {
await check(async () => {
expect(await browser.elementByCss('p').text()).toBe('ipad')
return 'success'
}, /success/)
} finally {
await next.patchFile(envFile, envContent)
}
})
}
try {
await check(async () => {
expect(await browser.elementByCss('p').text()).toBe('ipad')
return 'success'
}, /success/)
} finally {
await next.patchFile(envFile, envContent)
}
})

it('should have no unexpected action error for hmr', async () => {
expect(next.cliOutput).not.toContain('Unexpected action')
Expand Down