Unverified Commit 0faedd9e authored by Hannes Bornö's avatar Hannes Bornö Committed by GitHub

Fix error when CSS @import fails to resolve (#45010)

parent 0a5097c4
...@@ -45,21 +45,17 @@ function getModuleTrace(input: any, compilation: any) { ...@@ -45,21 +45,17 @@ function getModuleTrace(input: any, compilation: any) {
return moduleTrace return moduleTrace
} }
export async function getNotFoundError( async function getSourceFrame(
compilation: webpack.Compilation,
input: any, input: any,
fileName: string fileName: any,
) { compilation: any
if (input.name !== 'ModuleNotFoundError') { ): Promise<{ frame: string; lineNumber: string; column: string }> {
return false
}
const loc = input.loc
? input.loc
: input.dependencies.map((d: any) => d.loc).filter(Boolean)[0]
const originalSource = input.module.originalSource()
try { try {
const loc = input.loc
? input.loc
: input.dependencies.map((d: any) => d.loc).filter(Boolean)[0]
const originalSource = input.module.originalSource()
const result = await createOriginalStackFrame({ const result = await createOriginalStackFrame({
line: loc.start.line, line: loc.start.line,
column: loc.start.column, column: loc.start.column,
...@@ -69,10 +65,37 @@ export async function getNotFoundError( ...@@ -69,10 +65,37 @@ export async function getNotFoundError(
frame: {}, frame: {},
}) })
// If we could not result the original location we still need to show the existing error return {
if (!result) { frame: result?.originalCodeFrame ?? '',
return input lineNumber: result?.originalStackFrame?.lineNumber?.toString() ?? '',
column: result?.originalStackFrame?.column?.toString() ?? '',
} }
} catch {
return { frame: '', lineNumber: '', column: '' }
}
}
export async function getNotFoundError(
compilation: webpack.Compilation,
input: any,
fileName: string
) {
if (
input.name !== 'ModuleNotFoundError' &&
!(
input.name === 'ModuleBuildError' &&
/Error: Can't resolve '.+' in /.test(input.message)
)
) {
return false
}
try {
const { frame, lineNumber, column } = await getSourceFrame(
input,
fileName,
compilation
)
const errorMessage = input.error.message const errorMessage = input.error.message
.replace(/ in '.*?'/, '') .replace(/ in '.*?'/, '')
...@@ -88,7 +111,8 @@ export async function getNotFoundError( ...@@ -88,7 +111,8 @@ export async function getNotFoundError(
name && name &&
!/next-(app|middleware|client-pages|flight-(client|server|client-entry))-loader\.js/.test( !/next-(app|middleware|client-pages|flight-(client|server|client-entry))-loader\.js/.test(
name name
) ) &&
!/css-loader.+\.js/.test(name)
) )
if (moduleTrace.length === 0) return '' if (moduleTrace.length === 0) return ''
...@@ -97,8 +121,6 @@ export async function getNotFoundError( ...@@ -97,8 +121,6 @@ export async function getNotFoundError(
)}\n\n` )}\n\n`
} }
const frame = result.originalCodeFrame ?? ''
let message = let message =
chalk.red.bold('Module not found') + chalk.red.bold('Module not found') +
`: ${errorMessage}` + `: ${errorMessage}` +
...@@ -108,12 +130,14 @@ export async function getNotFoundError( ...@@ -108,12 +130,14 @@ export async function getNotFoundError(
importTrace() + importTrace() +
'\nhttps://nextjs.org/docs/messages/module-not-found' '\nhttps://nextjs.org/docs/messages/module-not-found'
return new SimpleWebpackError( let formattedFileName: string = chalk.cyan(fileName)
`${chalk.cyan(fileName)}:${chalk.yellow( if (lineNumber && column) {
result.originalStackFrame.lineNumber?.toString() ?? '' formattedFileName += `:${chalk.yellow(lineNumber)}:${chalk.yellow(
)}:${chalk.yellow(result.originalStackFrame.column?.toString() ?? '')}`, column
message )}`
) }
return new SimpleWebpackError(formattedFileName, message)
} catch (err) { } catch (err) {
// Don't fail on failure to resolve sourcemaps // Don't fail on failure to resolve sourcemaps
return input return input
......
...@@ -1375,5 +1375,37 @@ for (const variant of ['default', 'turbo']) { ...@@ -1375,5 +1375,37 @@ for (const variant of ['default', 'turbo']) {
await cleanup() await cleanup()
}) })
test("Can't resolve @import in CSS file", async () => {
const { session, cleanup } = await sandbox(
next,
new Map([
['app/styles1.css', '@import "./styles2.css"'],
['app/styles2.css', '@import "./boom.css"'],
])
)
await session.patch(
'app/layout.js',
`
import "./styles1.css"
export default function RootLayout({ children }) {
return (
<html>
<head></head>
<body>{children}</body>
</html>
)
}
`
)
expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxSource()).toMatchSnapshot()
await cleanup()
})
}) })
} }
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`ReactRefreshLogBox app default Can't resolve @import in CSS file 1`] = `
"./app/styles2.css
Module not found: Can't resolve './boom.css'
Import trace for requested module:
./app/styles1.css
https://nextjs.org/docs/messages/module-not-found"
`;
exports[`ReactRefreshLogBox app default Import trace when module not found in layout 1`] = ` exports[`ReactRefreshLogBox app default Import trace when module not found in layout 1`] = `
"./app/module.js:1:0 "./app/module.js:1:0
Module not found: Can't resolve 'non-existing-module' Module not found: Can't resolve 'non-existing-module'
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
import fs from 'fs-extra' import fs from 'fs-extra'
import { join } from 'path' import { join } from 'path'
import cheerio from 'cheerio' import cheerio from 'cheerio'
import stripAnsi from 'next/dist/compiled/strip-ansi'
import { import {
renderViaHTTP, renderViaHTTP,
findPort, findPort,
...@@ -54,7 +55,7 @@ describe('TypeScript Features', () => { ...@@ -54,7 +55,7 @@ describe('TypeScript Features', () => {
await renderViaHTTP(appPort, '/hello') await renderViaHTTP(appPort, '/hello')
const found = await check( const found = await check(
() => output, () => stripAnsi(output),
/Module not found: Can't resolve 'components\/worldd'/, /Module not found: Can't resolve 'components\/worldd'/,
false false
) )
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
import fs from 'fs-extra' import fs from 'fs-extra'
import { join } from 'path' import { join } from 'path'
import cheerio from 'cheerio' import cheerio from 'cheerio'
import stripAnsi from 'next/dist/compiled/strip-ansi'
import * as path from 'path' import * as path from 'path'
import { import {
renderViaHTTP, renderViaHTTP,
...@@ -74,7 +75,7 @@ function runTests() { ...@@ -74,7 +75,7 @@ function runTests() {
await renderViaHTTP(appPort, '/basic-alias') await renderViaHTTP(appPort, '/basic-alias')
const found = await check( const found = await check(
() => output, () => stripAnsi(output),
/Module not found: Can't resolve '@c\/worldd'/, /Module not found: Can't resolve '@c\/worldd'/,
false false
) )
......
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