Unverified Commit 0915a508 authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

fix(turbopack): Always console.log the "done in" message after receiving a BUILT event (#75730)

Previously, when making a change that doesn't actually cause any modules to update, you could see a:

```
[Fast Refresh] rebuilding
```

that is never followed by a

```
[Fast Refresh] done in 90ms
```

message. This PR attempts to more closely emulate webpack's behavior.

I've seen people observe this before and wrongly believe that Turbopack is hanging.

Doing this right is a little tricky: Turbopack emits the `BUILT` event separately using a debounce (currently 30ms) after the actual HMR updates.

I don't think the Turbopack HMR APIs are particularly great overall, but the way Turbopack is handing this sort-of makes sense. A file could be updated multiple times in quick succession (i.e. as the editor is in the middle of writing it), or somebody could save changes to multiple files at once, and there's no way to atomically synchronize all of the potential filesystem/editor interactions.
parent 026f00e2
...@@ -63,6 +63,8 @@ let mostRecentCompilationHash: any = null ...@@ -63,6 +63,8 @@ let mostRecentCompilationHash: any = null
let __nextDevClientId = Math.round(Math.random() * 100 + Date.now()) let __nextDevClientId = Math.round(Math.random() * 100 + Date.now())
let reloading = false let reloading = false
let startLatency: number | null = null let startLatency: number | null = null
let turbopackLastUpdateLatency: number | null = null
let turbopackUpdatedModules: Set<string> = new Set()
let pendingHotUpdateWebpack = Promise.resolve() let pendingHotUpdateWebpack = Promise.resolve()
let resolvePendingHotUpdateWebpack: () => void = () => {} let resolvePendingHotUpdateWebpack: () => void = () => {}
...@@ -104,7 +106,9 @@ function reportHmrLatency( ...@@ -104,7 +106,9 @@ function reportHmrLatency(
updatedModules: ReadonlyArray<string> updatedModules: ReadonlyArray<string>
) { ) {
if (!startLatency) return if (!startLatency) return
let endLatency = Date.now() // turbopack has a debounce for the "built" event which we don't want to
// incorrectly show in this number, use the last TURBOPACK_MESSAGE time
let endLatency = turbopackLastUpdateLatency ?? Date.now()
const latency = endLatency - startLatency const latency = endLatency - startLatency
console.log(`[Fast Refresh] done in ${latency}ms`) console.log(`[Fast Refresh] done in ${latency}ms`)
sendMessage( sendMessage(
...@@ -314,6 +318,7 @@ function processMessage( ...@@ -314,6 +318,7 @@ function processMessage(
function handleHotUpdate() { function handleHotUpdate() {
if (process.env.TURBOPACK) { if (process.env.TURBOPACK) {
dispatcher.onBuildOk() dispatcher.onBuildOk()
reportHmrLatency(sendMessage, [...turbopackUpdatedModules])
} else { } else {
tryApplyUpdates( tryApplyUpdates(
function onBeforeHotUpdate(hasUpdates: boolean) { function onBeforeHotUpdate(hasUpdates: boolean) {
...@@ -375,6 +380,8 @@ function processMessage( ...@@ -375,6 +380,8 @@ function processMessage(
} }
case HMR_ACTIONS_SENT_TO_BROWSER.BUILDING: { case HMR_ACTIONS_SENT_TO_BROWSER.BUILDING: {
startLatency = Date.now() startLatency = Date.now()
turbopackLastUpdateLatency = null
turbopackUpdatedModules.clear()
if (!process.env.TURBOPACK) { if (!process.env.TURBOPACK) {
setPendingHotUpdateWebpack() setPendingHotUpdateWebpack()
} }
...@@ -461,7 +468,6 @@ function processMessage( ...@@ -461,7 +468,6 @@ function processMessage(
break break
} }
case HMR_ACTIONS_SENT_TO_BROWSER.TURBOPACK_MESSAGE: { case HMR_ACTIONS_SENT_TO_BROWSER.TURBOPACK_MESSAGE: {
const updatedModules = extractModulesFromTurbopackMessage(obj.data)
dispatcher.onBeforeRefresh() dispatcher.onBeforeRefresh()
processTurbopackMessage({ processTurbopackMessage({
type: HMR_ACTIONS_SENT_TO_BROWSER.TURBOPACK_MESSAGE, type: HMR_ACTIONS_SENT_TO_BROWSER.TURBOPACK_MESSAGE,
...@@ -472,7 +478,10 @@ function processMessage( ...@@ -472,7 +478,10 @@ function processMessage(
console.warn(REACT_REFRESH_FULL_RELOAD_FROM_ERROR) console.warn(REACT_REFRESH_FULL_RELOAD_FROM_ERROR)
performFullReload(null, sendMessage) performFullReload(null, sendMessage)
} }
reportHmrLatency(sendMessage, updatedModules) for (const module of extractModulesFromTurbopackMessage(obj.data)) {
turbopackUpdatedModules.add(module)
}
turbopackLastUpdateLatency = Date.now()
break break
} }
// TODO-APP: make server component change more granular // TODO-APP: make server component change more granular
......
...@@ -139,6 +139,7 @@ function handleSuccess() { ...@@ -139,6 +139,7 @@ function handleSuccess() {
tryApplyUpdates(onBeforeFastRefresh, onFastRefresh) tryApplyUpdates(onBeforeFastRefresh, onFastRefresh)
} }
} else { } else {
reportHmrLatency([...turbopackUpdatedModules])
onBuildOk() onBuildOk()
} }
} }
...@@ -213,7 +214,9 @@ function handleErrors(errors: any) { ...@@ -213,7 +214,9 @@ function handleErrors(errors: any) {
} }
} }
let startLatency: number | undefined = undefined let startLatency: number | null = null
let turbopackLastUpdateLatency: number | null = null
let turbopackUpdatedModules: Set<string> = new Set()
function onBeforeFastRefresh(updatedModules: string[]) { function onBeforeFastRefresh(updatedModules: string[]) {
if (updatedModules.length > 0) { if (updatedModules.length > 0) {
...@@ -235,26 +238,27 @@ function onFastRefresh(updatedModules: ReadonlyArray<string> = []) { ...@@ -235,26 +238,27 @@ function onFastRefresh(updatedModules: ReadonlyArray<string> = []) {
} }
function reportHmrLatency(updatedModules: ReadonlyArray<string> = []) { function reportHmrLatency(updatedModules: ReadonlyArray<string> = []) {
if (startLatency) { if (!startLatency) return
const endLatency = Date.now() // turbopack has a debounce for the BUILT event which we don't want to
const latency = endLatency - startLatency // incorrectly show in this number, use the last TURBOPACK_MESSAGE time
console.log(`[Fast Refresh] done in ${latency}ms`) let endLatency = turbopackLastUpdateLatency ?? Date.now()
sendMessage( const latency = endLatency - startLatency
JSON.stringify({ console.log(`[Fast Refresh] done in ${latency}ms`)
event: 'client-hmr-latency', sendMessage(
id: window.__nextDevClientId, JSON.stringify({
startTime: startLatency, event: 'client-hmr-latency',
endTime: endLatency, id: window.__nextDevClientId,
page: window.location.pathname, startTime: startLatency,
updatedModules, endTime: endLatency,
// Whether the page (tab) was hidden at the time the event occurred. page: window.location.pathname,
// This can impact the accuracy of the event's timing. updatedModules,
isPageHidden: document.visibilityState === 'hidden', // Whether the page (tab) was hidden at the time the event occurred.
}) // This can impact the accuracy of the event's timing.
) isPageHidden: document.visibilityState === 'hidden',
if (self.__NEXT_HMR_LATENCY_CB) { })
self.__NEXT_HMR_LATENCY_CB(latency) )
} if (self.__NEXT_HMR_LATENCY_CB) {
self.__NEXT_HMR_LATENCY_CB(latency)
} }
} }
...@@ -274,6 +278,8 @@ function processMessage(obj: HMR_ACTION_TYPES) { ...@@ -274,6 +278,8 @@ function processMessage(obj: HMR_ACTION_TYPES) {
switch (obj.action) { switch (obj.action) {
case HMR_ACTIONS_SENT_TO_BROWSER.BUILDING: { case HMR_ACTIONS_SENT_TO_BROWSER.BUILDING: {
startLatency = Date.now() startLatency = Date.now()
turbopackLastUpdateLatency = null
turbopackUpdatedModules.clear()
console.log('[Fast Refresh] rebuilding') console.log('[Fast Refresh] rebuilding')
break break
} }
...@@ -345,7 +351,7 @@ function processMessage(obj: HMR_ACTION_TYPES) { ...@@ -345,7 +351,7 @@ function processMessage(obj: HMR_ACTION_TYPES) {
} }
case HMR_ACTIONS_SENT_TO_BROWSER.TURBOPACK_MESSAGE: { case HMR_ACTIONS_SENT_TO_BROWSER.TURBOPACK_MESSAGE: {
const updatedModules = extractModulesFromTurbopackMessage(obj.data) const updatedModules = extractModulesFromTurbopackMessage(obj.data)
onBeforeFastRefresh(updatedModules) onBeforeFastRefresh([...updatedModules])
for (const listener of turbopackMessageListeners) { for (const listener of turbopackMessageListeners) {
listener({ listener({
type: HMR_ACTIONS_SENT_TO_BROWSER.TURBOPACK_MESSAGE, type: HMR_ACTIONS_SENT_TO_BROWSER.TURBOPACK_MESSAGE,
...@@ -357,7 +363,10 @@ function processMessage(obj: HMR_ACTION_TYPES) { ...@@ -357,7 +363,10 @@ function processMessage(obj: HMR_ACTION_TYPES) {
performFullReload(null) performFullReload(null)
} }
onRefresh() onRefresh()
reportHmrLatency(updatedModules) for (const module of updatedModules) {
turbopackUpdatedModules.add(module)
}
turbopackLastUpdateLatency = Date.now()
break break
} }
default: { default: {
......
...@@ -2,7 +2,7 @@ import type { Update as TurbopackUpdate } from '../../build/swc/types' ...@@ -2,7 +2,7 @@ import type { Update as TurbopackUpdate } from '../../build/swc/types'
export function extractModulesFromTurbopackMessage( export function extractModulesFromTurbopackMessage(
data: TurbopackUpdate | TurbopackUpdate[] data: TurbopackUpdate | TurbopackUpdate[]
) { ): Set<string> {
const updatedModules: Set<string> = new Set() const updatedModules: Set<string> = new Set()
const updates = Array.isArray(data) ? data : [data] const updates = Array.isArray(data) ? data : [data]
...@@ -31,5 +31,5 @@ export function extractModulesFromTurbopackMessage( ...@@ -31,5 +31,5 @@ export function extractModulesFromTurbopackMessage(
} }
} }
return [...updatedModules] return updatedModules
} }
...@@ -78,24 +78,13 @@ describe(`app-dir-hmr`, () => { ...@@ -78,24 +78,13 @@ describe(`app-dir-hmr`, () => {
const fastRefreshLogs = logs.filter((log) => { const fastRefreshLogs = logs.filter((log) => {
return log.message.startsWith('[Fast Refresh]') return log.message.startsWith('[Fast Refresh]')
}) })
// FIXME: 3+ "rebuilding" but no "done" is confusing.
// There may actually be more "rebuilding" but not reliably.
// To ignore this flakiness, we just assert on subset matches.
// Once the bug is fixed, each "rebuilding" should be paired with a "done in" exactly.
expect(fastRefreshLogs).toEqual( expect(fastRefreshLogs).toEqual(
expect.arrayContaining([ expect.arrayContaining([
{ source: 'log', message: '[Fast Refresh] rebuilding' }, { source: 'log', message: '[Fast Refresh] rebuilding' },
{ source: 'log', message: '[Fast Refresh] rebuilding' }, {
{ source: 'log', message: '[Fast Refresh] rebuilding' },
])
)
// FIXME: Turbopack should have matching "done in" for each "rebuilding"
expect(logs).not.toEqual(
expect.arrayContaining([
expect.objectContaining({
message: expect.stringContaining('[Fast Refresh] done in'),
source: 'log', source: 'log',
}), message: expect.stringContaining('[Fast Refresh] done in'),
},
]) ])
) )
}) })
...@@ -172,26 +161,14 @@ describe(`app-dir-hmr`, () => { ...@@ -172,26 +161,14 @@ describe(`app-dir-hmr`, () => {
expect(await browser.elementByCss('p').text()).toBe('ipad') expect(await browser.elementByCss('p').text()).toBe('ipad')
}) })
if (process.env.TURBOPACK) { expect(logs).toEqual(
// FIXME: Turbopack should have matching "done in" for each "rebuilding" expect.arrayContaining([
expect(logs).not.toEqual( expect.objectContaining({
expect.arrayContaining([ message: expect.stringContaining('[Fast Refresh] done in'),
expect.objectContaining({ source: 'log',
message: expect.stringContaining('[Fast Refresh] done in'), }),
source: 'log', ])
}), )
])
)
} else {
expect(logs).toEqual(
expect.arrayContaining([
expect.objectContaining({
message: expect.stringContaining('[Fast Refresh] done in'),
source: 'log',
}),
])
)
}
}) })
// ensure it's restored back to "mac" before the next test // ensure it's restored back to "mac" before the next test
...@@ -221,54 +198,25 @@ describe(`app-dir-hmr`, () => { ...@@ -221,54 +198,25 @@ describe(`app-dir-hmr`, () => {
const logs = await browser.log() const logs = await browser.log()
// TODO: Should assert on all logs but these are cluttered with logs from our test utils (e.g. playwright tracing or webdriver) // TODO: Should assert on all logs but these are cluttered with logs from our test utils (e.g. playwright tracing or webdriver)
if (process.env.TURBOPACK) { expect(logs).toEqual(
// FIXME: logging "rebuilding" multiple times instead of closing it of with "done in" expect.arrayContaining([
// Should just not branch here and have the same logs as Webpack. {
expect(logs).toEqual( message: '[Fast Refresh] rebuilding',
expect.arrayContaining([ source: 'log',
{ },
message: '[Fast Refresh] rebuilding', {
source: 'log', message: expect.stringContaining('[Fast Refresh] done in'),
}, source: 'log',
{ },
message: '[Fast Refresh] rebuilding', ])
source: 'log', )
}, expect(logs).not.toEqual(
{ expect.arrayContaining([
message: '[Fast Refresh] rebuilding', expect.objectContaining({
source: 'log', source: 'error',
}, }),
]) ])
) )
expect(logs).not.toEqual(
expect.arrayContaining([
{
message: expect.stringContaining('[Fast Refresh] done in'),
source: 'log',
},
])
)
} else {
expect(logs).toEqual(
expect.arrayContaining([
{
message: '[Fast Refresh] rebuilding',
source: 'log',
},
{
message: expect.stringContaining('[Fast Refresh] done in'),
source: 'log',
},
])
)
expect(logs).not.toEqual(
expect.arrayContaining([
expect.objectContaining({
source: 'error',
}),
])
)
}
// No MPA navigation triggered // No MPA navigation triggered
expect(await browser.eval('window.__TEST_NO_RELOAD')).toEqual(true) expect(await browser.eval('window.__TEST_NO_RELOAD')).toEqual(true)
}) })
......
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