Unverified Commit a2551c49 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Revert "Address review feedback: extract modules, clean up imports" (#91159)

This is causing issues in unit tests so reverting to avoid regression
there.

Reverts vercel/next.js#90813
parent 69f53fd9
......@@ -17,7 +17,7 @@ import type { NextConfigComplete } from '../server/config-shared'
import { defaultOverrides } from '../server/require-hook'
import { hasExternalOtelApiPackage } from './webpack-config'
import { NEXT_PROJECT_ROOT } from './next-dir-paths'
import { shouldUseReactServerCondition } from './webpack-layer'
import { shouldUseReactServerCondition } from './utils'
interface CompilerAliases {
[alias: string]: string | string[]
......
......@@ -10,8 +10,7 @@ import {
NODE_ESM_RESOLVE_OPTIONS,
NODE_RESOLVE_OPTIONS,
} from './webpack-config'
import { isWebpackBundledLayer } from './utils'
import { shouldUseReactServerCondition } from './webpack-layer'
import { isWebpackBundledLayer, shouldUseReactServerCondition } from './utils'
import { normalizePathSep } from '../shared/lib/page-path/normalize-path-sep'
const reactPackagesRegex = /^(react|react-dom|react-server-dom-webpack)($|\/)/
......
import {
formatIssue,
isRelevantWarning,
} from '../shared/lib/turbopack/format-issue'
import { formatIssue, isRelevantWarning } from '../shared/lib/turbopack/utils'
import type { TurbopackResult } from './swc/types'
/**
......
......@@ -40,7 +40,6 @@ import type {
WrittenEndpoint,
} from './types'
import { runLoaderWorkerPool } from './loaderWorkerPool'
import { throwTurbopackInternalError } from '../../shared/lib/turbopack/internal-error'
export enum HmrTarget {
Client = 'client',
......@@ -1243,7 +1242,9 @@ function bindingToApi(
await rustifyProjectOptions(options),
turboEngineOptions || {},
{
throwTurbopackInternalError,
throwTurbopackInternalError: (
require('../../shared/lib/turbopack/internal-error') as typeof import('../../shared/lib/turbopack/internal-error')
).throwTurbopackInternalError,
onBeforeDeferredEntries: callbacks?.onBeforeDeferredEntries,
}
)
......
......@@ -7,10 +7,7 @@ import type {
StyledComponentsConfig,
} from '../../server/config-shared'
import type { ResolvedBaseUrl } from '../load-jsconfig'
import {
shouldUseReactServerCondition,
isWebpackAppPagesLayer,
} from '../webpack-layer'
import { shouldUseReactServerCondition, isWebpackAppPagesLayer } from '../utils'
import { escapeStringRegexp } from '../../shared/lib/escape-regexp'
const nextDirname = path.dirname(require.resolve('next/package.json'))
......
......@@ -1489,6 +1489,16 @@ export class NestedMiddlewareError extends Error {
}
}
export { getSupportedBrowsers } from './get-supported-browsers'
export function shouldUseReactServerCondition(
layer: WebpackLayerName | null | undefined
): boolean {
return Boolean(
layer && WEBPACK_LAYERS.GROUP.serverOnly.includes(layer as any)
)
}
export function isWebpackClientOnlyLayer(
layer: WebpackLayerName | null | undefined
): boolean {
......@@ -1515,6 +1525,12 @@ export function isWebpackBundledLayer(
return Boolean(layer && WEBPACK_LAYERS.GROUP.bundled.includes(layer as any))
}
export function isWebpackAppPagesLayer(
layer: WebpackLayerName | null | undefined
): boolean {
return Boolean(layer && WEBPACK_LAYERS.GROUP.appPages.includes(layer as any))
}
export function collectMeta({
status,
headers,
......
......@@ -13,10 +13,10 @@ import type { WebpackLayerName } from '../lib/constants'
import {
isWebpackBundledLayer,
isWebpackClientOnlyLayer,
shouldUseReactServerCondition,
isWebpackDefaultLayer,
RSPACK_DEFAULT_LAYERS_REGEX,
} from './utils'
import { shouldUseReactServerCondition } from './webpack-layer'
import type { CustomRoutes } from '../lib/load-custom-routes.js'
import {
CLIENT_STATIC_FILES_RUNTIME_MAIN,
......
import type { WebpackLayerName } from '../lib/constants'
import { WEBPACK_LAYERS } from '../lib/constants'
export function shouldUseReactServerCondition(
layer: WebpackLayerName | null | undefined
): boolean {
return Boolean(
layer && WEBPACK_LAYERS.GROUP.serverOnly.includes(layer as any)
)
}
export function isWebpackAppPagesLayer(
layer: WebpackLayerName | null | undefined
): boolean {
return Boolean(layer && WEBPACK_LAYERS.GROUP.appPages.includes(layer as any))
}
......@@ -85,8 +85,8 @@ import { isDeferredEntry } from '../../build/entries'
import { isMetadataRouteFile } from '../../lib/metadata/is-metadata-route'
import { setBundlerFindSourceMapImplementation } from '../patch-error-inspect'
import { getNextErrorFeedbackMiddleware } from '../../next-devtools/server/get-next-error-feedback-middleware'
import { formatIssue } from '../../shared/lib/turbopack/format-issue'
import {
formatIssue,
isFileSystemCacheEnabledForDev,
isWellKnownError,
processIssues,
......
......@@ -28,10 +28,8 @@ import { isMetadataRoute } from '../../lib/metadata/is-metadata-route'
import type { CustomRoutes } from '../../lib/load-custom-routes'
import {
formatIssue,
isRelevantWarning,
} from '../../shared/lib/turbopack/format-issue'
import {
getIssueKey,
isRelevantWarning,
processIssues,
renderStyledStringToErrorAnsi,
type EntryIssuesMap,
......
import type { Issue, PlainTraceItem } from '../../../build/swc/types'
import isInternal from '../is-internal'
import { codeFrameColumns } from '../errors/code-frame'
import { renderStyledStringToErrorAnsi } from './utils'
const VERBOSE_ISSUES = !!process.env.NEXT_TURBOPACK_VERBOSE_ISSUES
export function formatIssue(issue: Issue) {
const { filePath, title, description, detail, source, importTraces } = issue
let { documentationLink } = issue
const formattedTitle = renderStyledStringToErrorAnsi(title).replace(
/\n/g,
'\n '
)
// TODO: Use error codes to identify these
// TODO: Generalize adapting Turbopack errors to Next.js errors
if (formattedTitle.includes('Module not found')) {
// For compatiblity with webpack
// TODO: include columns in webpack errors.
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 &&
// ignore Next.js/React internals, as these can often be huge bundled files.
!isInternal(filePath)
) {
const { start, end } = source.range
// TODO(lukesandberg): move codeFrame formatting into turbopack, it would be more efficient than passing the source back and forth
const frame = codeFrameColumns(
source.source.content,
{
start: {
line: start.line + 1,
column: start.column + 1,
},
end: {
line: end.line + 1,
column: end.column + 1,
},
},
{ color: true }
)
if (frame) {
message += frame.trimEnd() + '\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\n'
} else {
message += renderStyledStringToErrorAnsi(description) + '\n\n'
}
}
// TODO: make it easier to enable this for debugging
if (VERBOSE_ISSUES && detail) {
message += renderStyledStringToErrorAnsi(detail) + '\n\n'
}
if (importTraces?.length) {
// This is the same logic as in turbopack/crates/turbopack-cli-utils/src/issue.rs
// We end up with multiple traces when the file with the error is reachable from multiple
// different entry points (e.g. ssr, client)
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 this is true, layer must be present
if (everyTraceHasADistinctRootLayer) {
message += ` ${layer}:\n`
} else {
if (importTraces.length > 1) {
// Otherwise use simple 1 based indices to disambiguate
message += ` #${i + 1}`
if (layer) {
message += ` [${layer}]`
}
message += ':\n'
} else if (layer) {
message += ` [${layer}]:\n`
} else {
// If there is a single trace and no layer name just don't indent it.
traceIndent = ' '
}
}
message += formatIssueTrace(trace, traceIndent, !identicalLayers(trace))
}
}
if (documentationLink) {
message += documentationLink + '\n\n'
}
return message
}
/** Returns the first present layer name in the trace */
function leafLayerName(items: PlainTraceItem[]): string | undefined {
for (const item of items) {
const layer = item.layer
if (layer != null) return layer
}
return undefined
}
/**
* Returns whether or not all items share the same layer.
* If a layer is absent we ignore it in this analysis
*/
function identicalLayers(items: PlainTraceItem[]): boolean {
const firstPresentLayer = items.findIndex((t) => t.layer != null)
if (firstPresentLayer === -1) return true // all layers are absent
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/'))
)
}
......@@ -3,7 +3,7 @@ import type {
PlainTraceItem,
StyledString,
} from '../../../build/swc/types'
import { formatIssue } from './format-issue'
import { formatIssue } from './utils'
function styledText(value: string): StyledString {
return { type: 'text', value }
......
import type {
Issue,
PlainTraceItem,
StyledString,
TurbopackResult,
} from '../../../build/swc/types'
import { bold, green, magenta, red } from '../../../lib/picocolors'
import isInternal from '../is-internal'
import { deobfuscateText } from '../magic-identifier'
import type { EntryKey } from './entry-key'
import { formatIssue } from './format-issue'
import * as Log from '../../../build/output/log'
import type { NextConfigComplete } from '../../../server/config-shared'
import { codeFrameColumns } from '../errors/code-frame'
type IssueKey = `${Issue['severity']}-${Issue['filePath']}-${string}-${string}`
export type IssuesMap = Map<IssueKey, Issue>
export type EntryIssuesMap = Map<EntryKey, IssuesMap>
export type TopLevelIssuesMap = IssuesMap
const VERBOSE_ISSUES = !!process.env.NEXT_TURBOPACK_VERBOSE_ISSUES
/**
* An error generated from emitted Turbopack issues. This can include build
* errors caused by issues with user code.
......@@ -89,6 +93,201 @@ export function processIssues(
}
}
export function formatIssue(issue: Issue) {
const { filePath, title, description, detail, source, importTraces } = issue
let { documentationLink } = issue
const formattedTitle = renderStyledStringToErrorAnsi(title).replace(
/\n/g,
'\n '
)
// TODO: Use error codes to identify these
// TODO: Generalize adapting Turbopack errors to Next.js errors
if (formattedTitle.includes('Module not found')) {
// For compatiblity with webpack
// TODO: include columns in webpack errors.
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 &&
// ignore Next.js/React internals, as these can often be huge bundled files.
!isInternal(filePath)
) {
const { start, end } = source.range
// TODO(lukesandberg): move codeFrame formatting into turbopack, it would be more efficient than passing the source back and forth
const frame = codeFrameColumns(
source.source.content,
{
start: {
line: start.line + 1,
column: start.column + 1,
},
end: {
line: end.line + 1,
column: end.column + 1,
},
},
{ color: true }
)
if (frame) {
message += frame.trimEnd() + '\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\n'
} else {
message += renderStyledStringToErrorAnsi(description) + '\n\n'
}
}
// TODO: make it easier to enable this for debugging
if (VERBOSE_ISSUES && detail) {
message += renderStyledStringToErrorAnsi(detail) + '\n\n'
}
if (importTraces?.length) {
// This is the same logic as in turbopack/crates/turbopack-cli-utils/src/issue.rs
// We end up with multiple traces when the file with the error is reachable from multiple
// different entry points (e.g. ssr, client)
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 this is true, layer must be present
if (everyTraceHasADistinctRootLayer) {
message += ` ${layer}:\n`
} else {
if (importTraces.length > 1) {
// Otherwise use simple 1 based indices to disambiguate
message += ` #${i + 1}`
if (layer) {
message += ` [${layer}]`
}
message += ':\n'
} else if (layer) {
message += ` [${layer}]:\n`
} else {
// If there is a single trace and no layer name just don't indent it.
traceIndent = ' '
}
}
message += formatIssueTrace(trace, traceIndent, !identicalLayers(trace))
}
}
if (documentationLink) {
message += documentationLink + '\n\n'
}
return message
}
/** Returns the first present layer name in the trace */
function leafLayerName(items: PlainTraceItem[]): string | undefined {
for (const item of items) {
const layer = item.layer
if (layer != null) return layer
}
return undefined
}
/**
* Returns whether or not all items share the same layer.
* If a layer is absent we ignore it in this analysis
*/
function identicalLayers(items: PlainTraceItem[]): boolean {
const firstPresentLayer = items.findIndex((t) => t.layer != null)
if (firstPresentLayer === -1) return true // all layers are absent
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 applyDeobfuscation(str: string): string {
// Use shared deobfuscate function and apply magenta color to identifiers
......
import { checkBuildFreshness } from './lib/check-build-freshness'
// Load the unhandled rejection filter in the host context.
// This is needed because some unit tests (like fast-set-immediate.external.test.ts)
// need to intercept unhandledRejection events via process.on(). In Jest's VM context,
// process.on() operates on a separate EventEmitter from the real process where
// unhandled rejections fire. The filter patches process.on() to use an internal queue
// that both the host and VM contexts share, making the interception work.
import 'next/dist/server/node-environment-extensions/unhandled-rejection.external'
export default async function globalSetup() {
await checkBuildFreshness()
}
......@@ -137,10 +137,17 @@ async function readNormalizedNFT(next, name) {
"/node_modules/next/dist/build/adapter/setup-node-env.external.js",
"/node_modules/next/dist/build/define-env.js",
"/node_modules/next/dist/build/duration-to-string.js",
"/node_modules/next/dist/build/get-supported-browsers.js",
"/node_modules/next/dist/build/next-config-ts/require-hook.js",
"/node_modules/next/dist/build/next-config-ts/transpile-config.js",
"/node_modules/next/dist/build/output/format.js",
"/node_modules/next/dist/build/output/log.js",
"/node_modules/next/dist/build/segment-config/app/app-segment-config.js",
"/node_modules/next/dist/build/segment-config/app/app-segments.js",
"/node_modules/next/dist/build/segment-config/app/collect-root-param-keys.js",
"/node_modules/next/dist/build/static-paths/app.js",
"/node_modules/next/dist/build/static-paths/app/extract-pathname-route-param-segments-from-loader-tree.js",
"/node_modules/next/dist/build/static-paths/pages.js",
"/node_modules/next/dist/build/static-paths/utils.js",
"/node_modules/next/dist/build/swc/helpers.js",
"/node_modules/next/dist/build/swc/index.js",
......@@ -149,7 +156,7 @@ async function readNormalizedNFT(next, name) {
"/node_modules/next/dist/build/swc/loaderWorkerPool.js",
"/node_modules/next/dist/build/swc/options.js",
"/node_modules/next/dist/build/swc/types.js",
"/node_modules/next/dist/build/webpack-layer.js",
"/node_modules/next/dist/build/utils.js",
"/node_modules/next/dist/cli/next-test.js",
"/node_modules/next/dist/client/*",
"/node_modules/next/dist/compiled/@edge-runtime/cookies/index.js",
......@@ -167,6 +174,7 @@ async function readNormalizedNFT(next, name) {
"/node_modules/next/dist/compiled/@napi-rs/triples/index.js",
"/node_modules/next/dist/compiled/@opentelemetry/api/index.js",
"/node_modules/next/dist/compiled/async-retry/index.js",
"/node_modules/next/dist/compiled/async-sema/index.js",
"/node_modules/next/dist/compiled/busboy/index.js",
"/node_modules/next/dist/compiled/bytes/index.js",
"/node_modules/next/dist/compiled/ci-info/index.js",
......@@ -210,6 +218,7 @@ async function readNormalizedNFT(next, name) {
"/node_modules/next/dist/compiled/strip-ansi/index.js",
"/node_modules/next/dist/compiled/superstruct/index.cjs",
"/node_modules/next/dist/compiled/tar/index.js",
"/node_modules/next/dist/compiled/text-table/index.js",
"/node_modules/next/dist/compiled/watchpack/watchpack.js",
"/node_modules/next/dist/compiled/ws/index.js",
"/node_modules/next/dist/compiled/zod-validation-error/index.js",
......@@ -219,6 +228,7 @@ async function readNormalizedNFT(next, name) {
"/node_modules/next/dist/experimental/testmode/httpget.js",
"/node_modules/next/dist/experimental/testmode/server-edge.js",
"/node_modules/next/dist/experimental/testmode/server.js",
"/node_modules/next/dist/export/helpers/create-incremental-cache.js",
"/node_modules/next/dist/lib/batcher.js",
"/node_modules/next/dist/lib/build-custom-route.js",
"/node_modules/next/dist/lib/bundler.js",
......
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