-
Notifications
You must be signed in to change notification settings - Fork 46
fix: dedupe Next captureOutput logs and map dev stacks to source #381
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
db2aec7
fix: dedupe Next captureOutput logs and map dev stacks to source
HugoRCD 200d532
refactor: simplify PR #381 and keep production stacks intact
claude 3ee0d7e
Merge remote-tracking branch 'origin/claude/exciting-ptolemy-8xz84l' …
HugoRCD 6124f81
Merge remote-tracking branch 'origin/main' into fix/next-capture-outp…
HugoRCD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "evlog": patch | ||
| --- | ||
|
|
||
| Fix duplicate terminal output when Next.js `captureOutput` is enabled: pretty-print writes use the native stdout handle registered at patch time and passthrough is skipped unless `silent: true`. Next.js dev stacks are source-mapped to original TypeScript (like Nitro) via a Next-only enricher that does not bundle nitropack/youch; stored stacks are compacted in dev (production stacks are kept intact) and useless `.next`/`node:` snippet previews are skipped. The primary `at` line now points at your route/handler file instead of Next `route-modules` internals. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /** | ||
| * Source-map stack enrichment for Next.js dev — isolated from Nitro to avoid bundling nitropack/youch. | ||
| */ | ||
| export async function enrichNextErrorStackForDev( | ||
| error: Error, | ||
| options: { pretty?: boolean } = {}, | ||
| ): Promise<void> { | ||
| if (process.env.NODE_ENV === 'production') return | ||
| if (options.pretty === false) return | ||
|
|
||
| const { enrichErrorStackFromNextDev } = await import('../shared/enrich-error-stack-next.node') | ||
| enrichErrorStackFromNextDev(error) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
packages/evlog/src/shared/enrich-error-stack-next.node.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| import { existsSync, readFileSync } from 'node:fs' | ||
| import { createRequire } from 'node:module' | ||
| import { isAbsolute, relative } from 'node:path' | ||
| import { pathToFileURL, fileURLToPath } from 'node:url' | ||
| import { isFrameworkRuntimePath } from './pretty-error' | ||
|
|
||
| /** Parsed stack frame from Next.js `parseStack`. */ | ||
| interface NextParsedFrame { | ||
| file: string | null | ||
| line1: number | null | ||
| column1: number | null | ||
| methodName: string | null | ||
| arguments: string[] | ||
| } | ||
|
|
||
| type SourceMapConsumerInstance = { | ||
| originalPositionFor: (pos: { line: number, column: number }) => { | ||
| source: string | null | ||
| line: number | null | ||
| column: number | null | ||
| name: string | null | ||
| } | ||
| } | ||
|
|
||
| const require = createRequire(import.meta.url) | ||
|
|
||
| function formatMappedFrame( | ||
| methodName: string | null, | ||
| sourceURL: string | null, | ||
| line1: number | null, | ||
| column1: number | null, | ||
| ): string { | ||
| let sourceLocation = line1 !== null ? `:${line1}` : '' | ||
| if (column1 !== null && sourceLocation !== '') { | ||
| sourceLocation += `:${column1}` | ||
| } | ||
|
|
||
| let fileLocation: string | ||
| if (sourceURL !== null && sourceURL.startsWith('file://') && URL.canParse(sourceURL)) { | ||
| fileLocation = relative(process.cwd(), fileURLToPath(sourceURL)) | ||
| } else if (sourceURL !== null && sourceURL.startsWith('/')) { | ||
| fileLocation = relative(process.cwd(), sourceURL) | ||
| } else { | ||
| fileLocation = sourceURL ?? 'unknown' | ||
| } | ||
|
|
||
| return methodName | ||
| ? ` at ${methodName} (${fileLocation}${sourceLocation})` | ||
| : ` at ${fileLocation}${sourceLocation}` | ||
| } | ||
|
|
||
| function shouldSkipMappedSource(source: string): boolean { | ||
| const normalized = source.replace(/\\/g, '/') | ||
| return normalized.includes('node_modules') | ||
| || normalized.includes('/packages/evlog/') | ||
| || isFrameworkRuntimePath(normalized) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| function resolveFrameFile(frame: NextParsedFrame): string | null { | ||
| if (!frame.file) return null | ||
| if (frame.file.startsWith('file://')) { | ||
| try { | ||
| return fileURLToPath(frame.file) | ||
| } catch { | ||
| return frame.file | ||
| } | ||
| } | ||
| if (isAbsolute(frame.file)) return frame.file | ||
| return null | ||
| } | ||
|
|
||
| function getSourceMapConsumer( | ||
| frameFile: string, | ||
| cache: Map<string, SourceMapConsumerInstance | null>, | ||
| ): SourceMapConsumerInstance | null { | ||
| const cached = cache.get(frameFile) | ||
| if (cached !== undefined) return cached | ||
|
|
||
| const mapPath = `${frameFile}.map` | ||
| if (!existsSync(mapPath)) { | ||
| cache.set(frameFile, null) | ||
| return null | ||
| } | ||
|
|
||
| try { | ||
| const sourceMapModule = require('next/dist/compiled/source-map') as { | ||
| SourceMapConsumer: new(payload: unknown, sourceMapURL: string) => SourceMapConsumerInstance | ||
| } | ||
| const payload = JSON.parse(readFileSync(mapPath, 'utf8')) as unknown | ||
| const chunkUrl = pathToFileURL(frameFile).href | ||
| const consumer = new sourceMapModule.SourceMapConsumer(payload, `${chunkUrl}.map`) | ||
| cache.set(frameFile, consumer) | ||
| return consumer | ||
| } catch { | ||
| cache.set(frameFile, null) | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| function mapFrame( | ||
| frame: NextParsedFrame, | ||
| cache: Map<string, SourceMapConsumerInstance | null>, | ||
| ): { frame: NextParsedFrame, skipped: boolean } { | ||
| if (frame.file?.startsWith('node:')) { | ||
| return { frame, skipped: true } | ||
| } | ||
|
|
||
| const frameFile = resolveFrameFile(frame) | ||
| if (!frameFile || frame.line1 === null) { | ||
| return { frame, skipped: false } | ||
| } | ||
|
|
||
| const consumer = getSourceMapConsumer(frameFile, cache) | ||
| if (!consumer) { | ||
| if (frameFile.includes('.next/')) { | ||
| return { frame, skipped: true } | ||
| } | ||
| return { frame, skipped: false } | ||
| } | ||
|
|
||
| const sourcePosition = consumer.originalPositionFor({ | ||
| line: frame.line1, | ||
| column: (frame.column1 ?? 1) - 1, | ||
| }) | ||
|
|
||
| if (!sourcePosition.source || sourcePosition.line === null) { | ||
| return { frame, skipped: frameFile.includes('.next/') } | ||
| } | ||
|
|
||
| if (shouldSkipMappedSource(sourcePosition.source)) { | ||
| return { frame, skipped: true } | ||
| } | ||
|
|
||
| return { | ||
| frame: { | ||
| ...frame, | ||
| file: sourcePosition.source, | ||
| line1: sourcePosition.line, | ||
| column1: sourcePosition.column === null ? null : sourcePosition.column + 1, | ||
| methodName: sourcePosition.name ?? frame.methodName, | ||
| }, | ||
| skipped: false, | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Rewrite `error.stack` with Turbopack/Webpack source-mapped frames in Next.js dev. | ||
| * Reads sibling `.map` files for `.next` chunks (same resolution as the dev overlay). | ||
| */ | ||
| export function enrichErrorStackFromNextDev(error: Error): void { | ||
| if (process.env.NODE_ENV === 'production') return | ||
| if (!error.stack) return | ||
|
|
||
| try { | ||
| const { parseStack } = require('next/dist/server/lib/parse-stack') as { | ||
| parseStack: (stack: string, distDir?: string) => NextParsedFrame[] | ||
| } | ||
|
|
||
| const frames = parseStack(error.stack) | ||
| if (frames.length === 0) return | ||
|
|
||
| const cache = new Map<string, SourceMapConsumerInstance | null>() | ||
| const mappedLines: string[] = [] | ||
|
|
||
| for (const frame of frames) { | ||
| const { frame: mapped, skipped } = mapFrame(frame, cache) | ||
| if (skipped) continue | ||
| mappedLines.push(formatMappedFrame(mapped.methodName, mapped.file, mapped.line1, mapped.column1)) | ||
| } | ||
|
|
||
| if (mappedLines.length === 0) return | ||
|
|
||
| error.stack = `${error.name || 'Error'}: ${error.message}\n${mappedLines.join('\n')}` | ||
| } catch { | ||
| // Next internals unavailable — keep the original stack | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing JSDoc on public API.
Per coding guidelines, public APIs in
packages/evlog/src/**/*.{ts,tsx}require JSDoc comments. The exportedenrichNextErrorStackForDevfunction lacks documentation.📝 Suggested JSDoc
🤖 Prompt for AI Agents
Source: Coding guidelines