Skip to content

Commit

Permalink
fix env reloading for turbopack (#52194)
Browse files Browse the repository at this point in the history
### What?

fixes WEB-1258
  • Loading branch information
sokra authored and shuding committed Jul 8, 2023
1 parent 32d7795 commit a3d95e0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
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

0 comments on commit a3d95e0

Please sign in to comment.