Skip to content

Commit dbd0902

Browse files
committed
fix(dev): keep the same line from two requests apart
1 parent d2bda15 commit dbd0902

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎packages/nuxt-cli/src/dev/tui/events.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,23 @@ export class DevEventLog {
231231
* The report is the fullest account of a log, carrying the request it was
232232
* written for, then the reporter's. Whatever was printed is kept throughout,
233233
* being what the log view shows.
234+
*
235+
* Two requests can log the same line at once, so an arrival that knows its
236+
* request only joins an entry that could be for the same one. Printed output
237+
* is left out of that: it is attributed when the capture is flushed, which
238+
* can be on another request's call stack.
234239
*/
235240
#join(event: DevLogEvent, route: DevLogRoute): DevLogEvent | undefined {
236-
return this.#merge(event, candidate => !!candidate.routes && !candidate.routes.has(route), (candidate) => {
241+
const sameRequest = (candidate: DevLogEvent) => {
242+
if (route === 'output' || (candidate.routes!.size === 1 && candidate.routes!.has('output'))) {
243+
return true
244+
}
245+
if (candidate.requestId !== undefined && event.requestId !== undefined) {
246+
return candidate.requestId === event.requestId
247+
}
248+
return candidate.request === undefined || event.request === undefined || candidate.request === event.request
249+
}
250+
return this.#merge(event, candidate => !!candidate.routes && !candidate.routes.has(route) && sameRequest(candidate), (candidate) => {
237251
const routes = candidate.routes!
238252
const rendered = candidate.rendered ?? event.rendered
239253
if (route === 'report') {

‎packages/nuxt-cli/test/unit/dev-tui.spec.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,24 @@ describe('dev event log', () => {
894894
expect(events.recent(10)[0]!.raw).toBeFalsy()
895895
})
896896

897+
it.each([
898+
['reports first', ['report 1', 'report 2', 'reporter 1', 'reporter 2']],
899+
['reporters first', ['reporter 1', 'reporter 2', 'report 1', 'report 2']],
900+
['a reporter with no report of its own', ['report 2', 'reporter 1']],
901+
])('does not join the same line from two requests (%s)', (_name, order) => {
902+
const events = new DevEventLog()
903+
for (const step of order) {
904+
const [route, id] = step.split(' ') as ['report' | 'reporter', string]
905+
events.push({ time: Date.now(), level: 3, type: 'log', message: 'same line', raw: route === 'reporter', source: 'runtime', request: 'GET /', requestId: Number(id) }, { route })
906+
}
907+
const entries = events.recent(10)
908+
expect(entries).toHaveLength(2)
909+
expect(new Set(entries.map(entry => entry.requestId)).size).toBe(2)
910+
for (const entry of entries) {
911+
expect(entry.routes!.size).toBe(order.length / 2)
912+
}
913+
})
914+
897915
it('does not pair printed output with a log nothing reported', () => {
898916
const events = new DevEventLog()
899917
events.push({ time: Date.now(), level: 3, type: 'info', message: 'same line', source: 'cli' })

0 commit comments

Comments
 (0)