Unverified Commit 907fee27 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Keep Git-free Turbopack HMR regression without workaround

Co-authored-by: 's avatarNiklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
parent 0720326b
import type { Socket } from 'net' import type { Socket } from 'net'
import { mkdir, writeFile } from 'fs/promises' import { mkdir, writeFile } from 'fs/promises'
import { readFileSync, realpathSync } from 'fs' import { realpathSync } from 'fs'
import { execFileSync } from 'node:child_process'
import * as inspector from 'inspector' import * as inspector from 'inspector'
import { import { join, extname, relative, isAbsolute, sep } from 'path'
join,
extname,
relative,
isAbsolute,
resolve as resolvePath,
sep,
} from 'path'
import { fileURLToPath, pathToFileURL } from 'url' import { fileURLToPath, pathToFileURL } from 'url'
import ws from 'next/dist/compiled/ws' import ws from 'next/dist/compiled/ws'
...@@ -336,12 +328,6 @@ function setupServerHmr( ...@@ -336,12 +328,6 @@ function setupServerHmr(
return applyPromise return applyPromise
} }
function reEvaluate(): Promise<void> {
const reEvaluation = pending.then(recover, recover)
pending = reEvaluation
return reEvaluation
}
function reset(): Promise<void> { function reset(): Promise<void> {
const resetState = () => { const resetState = () => {
versions.clear() versions.clear()
...@@ -352,7 +338,7 @@ function setupServerHmr( ...@@ -352,7 +338,7 @@ function setupServerHmr(
return resetPromise return resetPromise
} }
return { apply, reset, reEvaluate } return { apply, reset }
} }
function getSourceMapFromTurbopack( function getSourceMapFromTurbopack(
...@@ -426,44 +412,6 @@ export async function createHotReloaderTurbopack( ...@@ -426,44 +412,6 @@ export async function createHotReloaderTurbopack(
const { nextConfig, dir: projectPath } = opts const { nextConfig, dir: projectPath } = opts
const lazyDynamicImports = nextConfig.experimental.turbopackLazyDynamicImports const lazyDynamicImports = nextConfig.experimental.turbopackLazyDynamicImports
// A branch checkout can replace an entire module graph before the file watcher
// has emitted all of its changes. Server HMR diffs are not safe to apply across
// that boundary, so fall back to loading the newly written chunks from disk.
let headPath: string | undefined
let previousHead: string | undefined
if (serverFastRefresh) {
try {
headPath = resolvePath(
projectPath,
execFileSync('git', ['rev-parse', '--git-path', 'HEAD'], {
cwd: projectPath,
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore'],
timeout: 2000,
}).trim()
)
previousHead = readFileSync(headPath, 'utf8')
} catch {
headPath = undefined
}
}
let branchReEvaluation: Promise<void> | undefined
async function reEvaluateOnBranchChange() {
if (!headPath) return
let head: string
try {
head = readFileSync(headPath, 'utf8')
} catch {
return
}
if (head !== previousHead) {
previousHead = head
branchReEvaluation = serverHmr?.reEvaluate()
}
await branchReEvaluation
}
const bindings = getBindingsSync() const bindings = getBindingsSync()
// Turbopack requires native bindings and cannot run with WASM bindings. // Turbopack requires native bindings and cannot run with WASM bindings.
...@@ -2036,7 +1984,6 @@ export async function createHotReloaderTurbopack( ...@@ -2036,7 +1984,6 @@ export async function createHotReloaderTurbopack(
} }
await currentEntriesHandling await currentEntriesHandling
await reEvaluateOnBranchChange()
// TODO We shouldn't look into the filesystem again. This should use the information from entrypoints // TODO We shouldn't look into the filesystem again. This should use the information from entrypoints
let routeDef: Pick< let routeDef: Pick<
......
import { execFileSync } from 'node:child_process' import { rmSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils' import { retry } from 'next-test-utils'
// This test switches the disposable app's Git branches, not the Next.js checkout.
// @force-gate dev && turbopack // @force-gate dev && turbopack
describe('client manifest after a branch switch', () => { describe('client manifest after replacing a client component', () => {
const { next } = nextTestSetup({ files: __dirname, skipStart: true }) const { next } = nextTestSetup({ files: __dirname, skipStart: true })
function git(...args: string[]) { function replaceClient(useReplacement: boolean) {
execFileSync('git', args, { cwd: next.testDir, stdio: 'pipe' }) const component = useReplacement ? 'ReplacementClient' : 'LegacyClient'
const file = useReplacement ? 'replacement-client' : 'legacy-client'
const removedFile = useReplacement ? 'legacy-client' : 'replacement-client'
const text = useReplacement ? 'NEW CLIENT' : 'OLD CLIENT'
const appDir = join(next.testDir, 'app')
// Write the new module before changing both routes, then remove the unused
// module. No Git checkout is needed to trigger the module-update race.
writeFileSync(
join(appDir, `${file}.tsx`),
`'use client'\nexport default function ${component}() {\n return <button id="client-result">${text}</button>\n}`
)
writeFileSync(
join(appDir, 'page.tsx'),
`import ${component} from './${file}'\nexport default function Page() {\n return <main><h1>First route</h1><${component} /></main>\n}`
)
writeFileSync(
join(appDir, 'second', 'page.tsx'),
`import ${component} from '../${file}'\nexport default function SecondPage() {\n return <main><h1>Second route</h1><${component} /></main>\n}`
)
rmSync(join(appDir, `${removedFile}.tsx`))
} }
async function assertSettled(branch: 'old-client' | 'replacement') { async function assertSettled(useReplacement: boolean) {
const current = branch === 'old-client' ? 'OLD CLIENT' : 'NEW CLIENT' const current = useReplacement ? 'NEW CLIENT' : 'OLD CLIENT'
const currentFile = const currentFile = useReplacement
branch === 'old-client' ? 'legacy-client.tsx' : 'replacement-client.tsx' ? 'replacement-client.tsx'
const removedFile = : 'legacy-client.tsx'
branch === 'old-client' ? 'replacement-client.tsx' : 'legacy-client.tsx' const removedFile = useReplacement
? 'legacy-client.tsx'
: 'replacement-client.tsx'
await retry(async () => { await retry(async () => {
for (const route of ['/', '/second']) { for (const route of ['/', '/second']) {
...@@ -35,74 +57,25 @@ describe('client manifest after a branch switch', () => { ...@@ -35,74 +57,25 @@ describe('client manifest after a branch switch', () => {
}, 15_000) }, 15_000)
} }
it("does not mix a route module with the other branch's Client Manifest", async () => { it('does not mix a route module with a stale Client Manifest', async () => {
git('init', '-q', '-b', 'old-client')
git('add', 'app')
git(
'-c',
'user.name=Test',
'-c',
'user.email=test@example.invalid',
'commit',
'-qm',
'old client'
)
git('switch', '-qc', 'replacement')
await next.patchFile(
'app/replacement-client.tsx',
`'use client'
export default function ReplacementClient() {
return <button id="client-result">NEW CLIENT</button>
}`
)
await next.deleteFile('app/legacy-client.tsx')
await next.patchFile(
'app/page.tsx',
`import ReplacementClient from './replacement-client'
export default function Page() {
return <main><h1>First route</h1><ReplacementClient /></main>
}`
)
await next.patchFile(
'app/second/page.tsx',
`import ReplacementClient from '../replacement-client'
export default function SecondPage() {
return <main><h1>Second route</h1><ReplacementClient /></main>
}`
)
git('add', 'app')
git(
'-c',
'user.name=Test',
'-c',
'user.email=test@example.invalid',
'commit',
'-qm',
'replacement client'
)
git('switch', '-q', 'old-client')
await next.start() await next.start()
await assertSettled('old-client') await assertSettled(false)
// Check the first requests too: a stale successful page is acceptable // The watcher can briefly serve the old page, but a mixed graph must not
// while the watcher catches up, but a mixed manifest must not throw. // throw. Once it settles, both routes and manifests must use the new graph.
for (let cycle = 0; cycle < 20; cycle++) { for (const useReplacement of [true, false]) {
for (const branch of ['replacement', 'old-client'] as const) { replaceClient(useReplacement)
git('switch', '-q', branch) for (const route of ['/second', '/']) {
for (const route of ['/second', '/']) { const response = await next.fetch(route)
const response = await next.fetch(route) const html = await response.text()
const html = await response.text() if (response.status !== 200) {
if (response.status !== 200) { throw new Error(
throw new Error( `${useReplacement ? 'replacement' : 'old client'} ${route}: HTTP ${response.status}; ${html.slice(0, 500)}`
`cycle ${cycle}, ${branch} ${route}: HTTP ${response.status}; ${html.slice(0, 500)}` )
)
}
expect(html).not.toContain('Could not find the module')
} }
await assertSettled(branch) expect(html).not.toContain('Could not find the module')
} }
await assertSettled(useReplacement)
} }
}) })
}) })
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