Unverified Commit 8bce51d8 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

avoid showing empty `Import trace for requested module` (#30130)



## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
parent 5f73633d
...@@ -78,17 +78,17 @@ export async function getNotFoundError( ...@@ -78,17 +78,17 @@ export async function getNotFoundError(
.replace(/Can't resolve '(.*)'/, `Can't resolve '${chalk.green('$1')}'`) .replace(/Can't resolve '(.*)'/, `Can't resolve '${chalk.green('$1')}'`)
const importTrace = () => { const importTrace = () => {
let importTraceLine = '\nImport trace for requested module:\n'
const moduleTrace = getModuleTrace(input, compilation) const moduleTrace = getModuleTrace(input, compilation)
if (moduleTrace.length === 0) return ''
let importTraceLine = '\nImport trace for requested module:\n'
for (const { origin } of moduleTrace) { for (const { origin } of moduleTrace) {
if (!origin.resource) { if (!origin.resource) {
continue continue
} }
const filePath = path.relative( const filePath = path
compilation.options.context, .relative(compilation.options.context, origin.resource)
origin.resource .replace(/\\/g, '/')
)
importTraceLine += `./${filePath}\n` importTraceLine += `./${filePath}\n`
} }
......
...@@ -35,20 +35,19 @@ function isLikelyASyntaxError(message) { ...@@ -35,20 +35,19 @@ function isLikelyASyntaxError(message) {
function formatMessage(message, verbose) { function formatMessage(message, verbose) {
// TODO: Replace this once webpack 5 is stable // TODO: Replace this once webpack 5 is stable
if (typeof message === 'object' && message.message) { if (typeof message === 'object' && message.message) {
const filteredModuleTrace =
message.moduleTrace &&
message.moduleTrace.filter(
(trace) => !trace.originName.includes('next-client-pages-loader.js')
)
message = message =
(message.moduleName ? stripAnsi(message.moduleName) + '\n' : '') + (message.moduleName ? stripAnsi(message.moduleName) + '\n' : '') +
(message.file ? stripAnsi(message.file) + '\n' : '') + (message.file ? stripAnsi(message.file) + '\n' : '') +
message.message + message.message +
(message.details && verbose ? '\n' + message.details : '') + (message.details && verbose ? '\n' + message.details : '') +
(message.moduleTrace && verbose (filteredModuleTrace && filteredModuleTrace.length && verbose
? '\n\nImport trace for requested module:' + ? '\n\nImport trace for requested module:' +
message.moduleTrace filteredModuleTrace.map((trace) => `\n${trace.originName}`).join('')
.filter(
(trace) =>
!trace.originName.includes('next-client-pages-loader.js')
)
.map((trace) => `\n${trace.originName}`)
.join('')
: '') + : '') +
(message.stack && verbose ? '\n' + message.stack : '') (message.stack && verbose ? '\n' + message.stack : '')
} }
......
...@@ -76,4 +76,28 @@ describe('ReactRefreshLogBox', () => { ...@@ -76,4 +76,28 @@ describe('ReactRefreshLogBox', () => {
await cleanup() await cleanup()
}) })
test('Module not found (empty import trace)', async () => {
const { session, cleanup } = await sandbox(next)
await session.patch(
'pages/index.js',
`import Comp from 'b'
export default function Oops() {
return (
<div>
<Comp>lol</Comp>
</div>
)
}
`
)
expect(await session.hasRedbox(true)).toBe(true)
const source = await session.getRedboxSource()
expect(source).toMatchSnapshot()
await cleanup()
})
}) })
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`ReactRefreshLogBox Module not found (empty import trace) 1`] = `
"./pages/index.js:1:0
Module not found: Can't resolve 'b'
> 1 | import Comp from 'b'
2 | export default function Oops() {
3 | return (
4 | <div>
https://nextjs.org/docs/messages/module-not-found"
`;
exports[`ReactRefreshLogBox Module not found 1`] = ` exports[`ReactRefreshLogBox Module not found 1`] = `
"./index.js:1:0 "./index.js:1:0
Module not found: Can't resolve 'b' Module not found: Can't resolve 'b'
......
...@@ -91,6 +91,8 @@ describe('Build Error Tests', () => { ...@@ -91,6 +91,8 @@ describe('Build Error Tests', () => {
) )
// should contain the importing module // should contain the importing module
expect(stderr).toContain('./pages/static-img.js') expect(stderr).toContain('./pages/static-img.js')
// should contain a import trace
expect(stderr).not.toContain('Import trace for requested module')
}) })
}) })
describe('Static Image Component Tests', () => { describe('Static Image Component Tests', () => {
......
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