|
|
import type { |
|
|
Issue, |
|
|
PlainTraceItem, |
|
|
StyledString, |
|
|
TurbopackResult, |
|
|
} from '../../../build/swc/types' |
|
|
|
|
|
import { bold, green, magenta, red } from '../../../lib/picocolors' |
|
|
import isInternal from '../is-internal' |
|
|
import { |
|
|
decodeMagicIdentifier, |
|
|
MAGIC_IDENTIFIER_REGEX, |
|
|
} from '../magic-identifier' |
|
|
import type { EntryKey } from './entry-key' |
|
|
import * as Log from '../../../build/output/log' |
|
|
import type { NextConfigComplete } from '../../../server/config-shared' |
|
|
import loadJsConfig from '../../../build/load-jsconfig' |
|
|
|
|
|
type IssueKey = `${Issue['severity']}-${Issue['filePath']}-${string}-${string}` |
|
|
export type IssuesMap = Map<IssueKey, Issue> |
|
|
export type EntryIssuesMap = Map<EntryKey, IssuesMap> |
|
|
export type TopLevelIssuesMap = IssuesMap |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class ModuleBuildError extends Error { |
|
|
name = 'ModuleBuildError' |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isWellKnownError(issue: Issue): boolean { |
|
|
const { title } = issue |
|
|
const formattedTitle = renderStyledStringToErrorAnsi(title) |
|
|
|
|
|
if ( |
|
|
formattedTitle.includes('Module not found') || |
|
|
formattedTitle.includes('Unknown module type') |
|
|
) { |
|
|
return true |
|
|
} |
|
|
|
|
|
return false |
|
|
} |
|
|
|
|
|
export function getIssueKey(issue: Issue): IssueKey { |
|
|
return `${issue.severity}-${issue.filePath}-${JSON.stringify( |
|
|
issue.title |
|
|
)}-${JSON.stringify(issue.description)}` |
|
|
} |
|
|
|
|
|
export async function getTurbopackJsConfig( |
|
|
dir: string, |
|
|
nextConfig: NextConfigComplete |
|
|
) { |
|
|
const { jsConfig } = await loadJsConfig(dir, nextConfig) |
|
|
return jsConfig ?? { compilerOptions: {} } |
|
|
} |
|
|
|
|
|
export function processIssues( |
|
|
currentEntryIssues: EntryIssuesMap, |
|
|
key: EntryKey, |
|
|
result: TurbopackResult, |
|
|
throwIssue: boolean, |
|
|
logErrors: boolean |
|
|
) { |
|
|
const newIssues = new Map<IssueKey, Issue>() |
|
|
currentEntryIssues.set(key, newIssues) |
|
|
|
|
|
const relevantIssues = new Set() |
|
|
|
|
|
for (const issue of result.issues) { |
|
|
if ( |
|
|
issue.severity !== 'error' && |
|
|
issue.severity !== 'fatal' && |
|
|
issue.severity !== 'warning' |
|
|
) |
|
|
continue |
|
|
|
|
|
const issueKey = getIssueKey(issue) |
|
|
newIssues.set(issueKey, issue) |
|
|
|
|
|
if (issue.severity !== 'warning') { |
|
|
if (throwIssue) { |
|
|
const formatted = formatIssue(issue) |
|
|
relevantIssues.add(formatted) |
|
|
} |
|
|
|
|
|
else if (logErrors && isWellKnownError(issue)) { |
|
|
const formatted = formatIssue(issue) |
|
|
Log.error(formatted) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
if (relevantIssues.size && throwIssue) { |
|
|
throw new ModuleBuildError([...relevantIssues].join('\n\n')) |
|
|
} |
|
|
} |
|
|
|
|
|
export function formatIssue(issue: Issue) { |
|
|
const { filePath, title, description, source, importTraces } = issue |
|
|
let { documentationLink } = issue |
|
|
const formattedTitle = renderStyledStringToErrorAnsi(title).replace( |
|
|
/\n/g, |
|
|
'\n ' |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
if (formattedTitle.includes('Module not found')) { |
|
|
|
|
|
|
|
|
documentationLink = 'https://nextjs.org/docs/messages/module-not-found' |
|
|
} |
|
|
|
|
|
const formattedFilePath = filePath |
|
|
.replace('[project]/', './') |
|
|
.replaceAll('/./', '/') |
|
|
.replace('\\\\?\\', '') |
|
|
|
|
|
let message = '' |
|
|
|
|
|
if (source?.range) { |
|
|
const { start } = source.range |
|
|
message = `${formattedFilePath}:${start.line + 1}:${ |
|
|
start.column + 1 |
|
|
}\n${formattedTitle}` |
|
|
} else if (formattedFilePath) { |
|
|
message = `${formattedFilePath}\n${formattedTitle}` |
|
|
} else { |
|
|
message = formattedTitle |
|
|
} |
|
|
message += '\n' |
|
|
|
|
|
if ( |
|
|
source?.range && |
|
|
source.source.content && |
|
|
|
|
|
!isInternal(filePath) |
|
|
) { |
|
|
const { start, end } = source.range |
|
|
const { codeFrameColumns } = |
|
|
require('next/dist/compiled/babel/code-frame') as typeof import('next/dist/compiled/babel/code-frame') |
|
|
|
|
|
message += |
|
|
codeFrameColumns( |
|
|
source.source.content, |
|
|
{ |
|
|
start: { |
|
|
line: start.line + 1, |
|
|
column: start.column + 1, |
|
|
}, |
|
|
end: { |
|
|
line: end.line + 1, |
|
|
column: end.column + 1, |
|
|
}, |
|
|
}, |
|
|
{ forceColor: true } |
|
|
).trim() + '\n\n' |
|
|
} |
|
|
|
|
|
if (description) { |
|
|
if ( |
|
|
description.type === 'text' && |
|
|
description.value.includes(`Cannot find module 'sass'`) |
|
|
) { |
|
|
message += |
|
|
"To use Next.js' built-in Sass support, you first need to install `sass`.\n" |
|
|
message += 'Run `npm i sass` or `yarn add sass` inside your workspace.\n' |
|
|
message += '\nLearn more: https://nextjs.org/docs/messages/install-sass' |
|
|
} else { |
|
|
message += renderStyledStringToErrorAnsi(description) + '\n\n' |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (importTraces?.length) { |
|
|
|
|
|
message += formatImportTraces(importTraces) |
|
|
} |
|
|
if (documentationLink) { |
|
|
message += documentationLink + '\n\n' |
|
|
} |
|
|
return message |
|
|
} |
|
|
|
|
|
export function formatImportTraces(importTraces: PlainTraceItem[][]) { |
|
|
|
|
|
|
|
|
let message = `Import trace${importTraces.length > 1 ? 's' : ''}:\n` |
|
|
const everyTraceHasADistinctRootLayer = |
|
|
new Set(importTraces.map(leafLayerName).filter((l) => l != null)).size === |
|
|
importTraces.length |
|
|
for (let i = 0; i < importTraces.length; i++) { |
|
|
const trace = importTraces[i] |
|
|
const layer = leafLayerName(trace) |
|
|
let traceIndent = ' ' |
|
|
|
|
|
if (everyTraceHasADistinctRootLayer) { |
|
|
message += ` ${layer}:\n` |
|
|
} else { |
|
|
if (importTraces.length > 1) { |
|
|
|
|
|
message += ` #${i + 1}` |
|
|
if (layer) { |
|
|
message += ` [${layer}]` |
|
|
} |
|
|
message += ':\n' |
|
|
} else if (layer) { |
|
|
message += ` [${layer}]:\n` |
|
|
} else { |
|
|
|
|
|
traceIndent = ' ' |
|
|
} |
|
|
} |
|
|
message += formatIssueTrace(trace, traceIndent, !identicalLayers(trace)) |
|
|
} |
|
|
return message |
|
|
} |
|
|
|
|
|
|
|
|
function leafLayerName(items: PlainTraceItem[]): string | undefined { |
|
|
for (const item of items) { |
|
|
const layer = item.layer |
|
|
if (layer != null) return layer |
|
|
} |
|
|
return undefined |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function identicalLayers(items: PlainTraceItem[]): boolean { |
|
|
const firstPresentLayer = items.findIndex((t) => t.layer != null) |
|
|
if (firstPresentLayer === -1) return true |
|
|
const layer = items[firstPresentLayer].layer |
|
|
for (let i = firstPresentLayer + 1; i < items.length; i++) { |
|
|
const itemLayer = items[i].layer |
|
|
if (itemLayer == null || itemLayer !== layer) { |
|
|
return false |
|
|
} |
|
|
} |
|
|
return true |
|
|
} |
|
|
|
|
|
function formatIssueTrace( |
|
|
items: PlainTraceItem[], |
|
|
indent: string, |
|
|
printLayers: boolean |
|
|
): string { |
|
|
return `${items |
|
|
.map((item) => { |
|
|
let r = indent |
|
|
if (item.fsName !== 'project') { |
|
|
r += `[${item.fsName}]/` |
|
|
} else { |
|
|
// This is consistent with webpack's output |
|
|
r += './' |
|
|
} |
|
|
r += item.path |
|
|
if (printLayers && item.layer) { |
|
|
r += ` [${item.layer}]` |
|
|
} |
|
|
return r |
|
|
}) |
|
|
.join('\n')}\n\n` |
|
|
} |
|
|
|
|
|
export function isRelevantWarning(issue: Issue): boolean { |
|
|
return issue.severity === 'warning' && !isNodeModulesIssue(issue) |
|
|
} |
|
|
|
|
|
function isNodeModulesIssue(issue: Issue): boolean { |
|
|
if (issue.severity === 'warning' && issue.stage === 'config') { |
|
|
// Override for the externalize issue |
|
|
// `Package foo (serverExternalPackages or default list) can't be external` |
|
|
if ( |
|
|
renderStyledStringToErrorAnsi(issue.title).includes("can't be external") |
|
|
) { |
|
|
return false |
|
|
} |
|
|
} |
|
|
|
|
|
return ( |
|
|
issue.severity === 'warning' && |
|
|
(issue.filePath.match(/^(?:.*[\\/])?node_modules(?:[\\/].*)?$/) !== null || |
|
|
// Ignore Next.js itself when running next directly in the monorepo where it is not inside |
|
|
// node_modules anyway. |
|
|
// TODO(mischnic) prevent matches when this is published to npm |
|
|
issue.filePath.startsWith('[project]/packages/next/')) |
|
|
) |
|
|
} |
|
|
|
|
|
export function renderStyledStringToErrorAnsi(string: StyledString): string { |
|
|
function decodeMagicIdentifiers(str: string): string { |
|
|
return str.replaceAll(MAGIC_IDENTIFIER_REGEX, (ident) => { |
|
|
try { |
|
|
return magenta(`{${decodeMagicIdentifier(ident)}}`) |
|
|
} catch (e) { |
|
|
return magenta(`{${ident} (decoding failed: ${e})}`) |
|
|
} |
|
|
}) |
|
|
} |
|
|
|
|
|
switch (string.type) { |
|
|
case 'text': |
|
|
return decodeMagicIdentifiers(string.value) |
|
|
case 'strong': |
|
|
return bold(red(decodeMagicIdentifiers(string.value))) |
|
|
case 'code': |
|
|
return green(decodeMagicIdentifiers(string.value)) |
|
|
case 'line': |
|
|
return string.value.map(renderStyledStringToErrorAnsi).join('') |
|
|
case 'stack': |
|
|
return string.value.map(renderStyledStringToErrorAnsi).join('\n') |
|
|
default: |
|
|
throw new Error('Unknown StyledString type', string) |
|
|
} |
|
|
} |
|
|
|
|
|
export function isPersistentCachingEnabled( |
|
|
config: NextConfigComplete |
|
|
): boolean { |
|
|
return config.experimental?.turbopackPersistentCaching || false |
|
|
} |
|
|
|