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

fix file name in next/font unresolved import error (#46187)

Use the actual file name in the overlay when a local font can't be resolved.

Before
![image](https://user-images.githubusercontent.com/25056922/220339649-86d1ede7-178e-4d88-820a-0eda6bd7ba18.png)

After
![image](https://user-images.githubusercontent.com/25056922/220339492-1d5a755f-5393-4121-b30d-32b49574035a.png)

Fixes NEXT-600

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/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`
- [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 44c5886c
......@@ -75,10 +75,37 @@ async function getSourceFrame(
}
}
function getFormattedFileName(
fileName: string,
module: any,
lineNumber?: string,
column?: string
): string {
if (
module.loaders?.find((loader: any) =>
/next-font-loader[/\\]index.js/.test(loader.loader)
)
) {
// Parse the query and get the path of the file where the font function was called.
// provided by next-swc next_font_loaders
return JSON.parse(module.resourceResolveData.query.slice(1)).path
} else {
let formattedFileName: string = chalk.cyan(fileName)
if (lineNumber && column) {
formattedFileName += `:${chalk.yellow(lineNumber)}:${chalk.yellow(
column
)}`
}
return formattedFileName
}
}
export async function getNotFoundError(
compilation: webpack.Compilation,
input: any,
fileName: string
fileName: string,
module: any
) {
if (
input.name !== 'ModuleNotFoundError' &&
......@@ -128,12 +155,12 @@ export async function getNotFoundError(
'\nhttps://nextjs.org/docs/messages/module-not-found\n' +
importTrace()
let formattedFileName: string = chalk.cyan(fileName)
if (lineNumber && column) {
formattedFileName += `:${chalk.yellow(lineNumber)}:${chalk.yellow(
column
)}`
}
const formattedFileName = getFormattedFileName(
fileName,
module,
lineNumber,
column
)
return new SimpleWebpackError(formattedFileName, message)
} catch (err) {
......
......@@ -68,7 +68,8 @@ export async function getModuleBuildError(
const notFoundError = await getNotFoundError(
compilation,
input,
sourceFilename
sourceFilename,
input.module
)
if (notFoundError !== false) {
return notFoundError
......
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { join } from 'path'
import webdriver from 'next-webdriver'
import { getRedboxSource, hasRedbox } from 'next-test-utils'
describe('build-errors', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: new FileRef(join(__dirname, 'build-errors')),
})
})
afterAll(() => next.destroy())
it('should show a next/font error when input is wrong', async () => {
const browser = await webdriver(next.url, '/')
const content = await next.readFile('app/page.js')
await next.patchFile(
'app/page.js',
`
import localFont from 'next/font/local'
const font = localFont()
export default function Page() {
return <p className={font.className}>Hello world!</p>
}
`
)
expect(await hasRedbox(browser, true)).toBeTrue()
expect(await getRedboxSource(browser)).toMatchInlineSnapshot(`
"app/page.js
\`next/font\` error:
Missing required \`src\` property"
`)
await next.patchFile('app/page.js', content)
expect(await hasRedbox(browser, false)).toBeFalse()
})
it("should show a module not found error if local font file can' be resolved", async () => {
const browser = await webdriver(next.url, '/')
const content = await next.readFile('app/page.js')
await next.patchFile(
'app/page.js',
`
import localFont from 'next/font/local'
const font = localFont({ src: './boom.woff2'})
export default function Page() {
return <p className={font.className}>Hello world!</p>
}
`
)
expect(await hasRedbox(browser, true)).toBeTrue()
const sourceLines = (await getRedboxSource(browser)).split('\n')
// Should display the file name correctly
expect(sourceLines[0]).toEqual('app/page.js')
// Should be module not found error
expect(sourceLines[1]).toEqual(
"Module not found: Can't resolve './boom.woff2'"
)
await next.patchFile('app/page.js', content)
expect(await hasRedbox(browser, false)).toBeFalse()
})
})
export default function RootLayout({ children }) {
return (
<html>
<head></head>
<body>{children}</body>
</html>
)
}
export default function Page() {
return 'Hello world'
}
module.exports = {
experimental: { appDir: true },
}
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { join } from 'path'
import webdriver from 'next-webdriver'
import { getRedboxSource, hasRedbox } from 'next-test-utils'
describe('validation-error', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
pages: new FileRef(join(__dirname, 'validation-error/pages')),
},
})
})
afterAll(() => next.destroy())
it('show a next/font error when input is wrong', async () => {
const browser = await webdriver(next.url, '/')
expect(await hasRedbox(browser, true)).toBeTrue()
expect(await getRedboxSource(browser)).toMatchInlineSnapshot(`
"pages/index.js
\`next/font\` error:
Missing required \`src\` property"
`)
})
})
import localFont from 'next/font/local'
const font = localFont()
export default function Index() {
return <p className={font.className}>Hello world!</p>
}
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