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