Unverified Commit 49004e3e authored by OJ Kwon's avatar OJ Kwon Committed by GitHub

feat(turbopack): emit well known error into cli (#63218)

### What

This is stopgap substitution to webpack's wellknownerrorsplugin. When
webpack compilation hits known errors kind it emits compilation errors
into cli (and dev overlay both) while turbopack currently only emits
into error overlay. PR simply detects if given issue is well known, and
then logs into std.

Probably a long term there should be proper wiring instead; but for now
this allows to make few things work out of the box.

Closes PACK-2727
parent 1ff10a03
...@@ -61,6 +61,7 @@ import { ...@@ -61,6 +61,7 @@ import {
type StartBuilding, type StartBuilding,
processTopLevelIssues, processTopLevelIssues,
type TopLevelIssuesMap, type TopLevelIssuesMap,
isWellKnownError,
} from './turbopack-utils' } from './turbopack-utils'
import { import {
propagateServerField, propagateServerField,
...@@ -667,9 +668,14 @@ export async function createHotReloaderTurbopack( ...@@ -667,9 +668,14 @@ export async function createHotReloaderTurbopack(
currentEntryIssues.get(pagesEntryKey) currentEntryIssues.get(pagesEntryKey)
if (thisEntryIssues !== undefined && thisEntryIssues.size > 0) { if (thisEntryIssues !== undefined && thisEntryIssues.size > 0) {
// If there is an error related to the requesting page we display it instead of the first error // If there is an error related to the requesting page we display it instead of the first error
return [...topLevelIssues, ...thisEntryIssues.values()].map( return [...topLevelIssues, ...thisEntryIssues.values()].map((issue) => {
(issue) => new Error(formatIssue(issue)) const formattedIssue = formatIssue(issue)
) if (isWellKnownError(issue)) {
Log.error(formattedIssue)
}
return new Error(formattedIssue)
})
} }
// Otherwise, return all errors across pages // Otherwise, return all errors across pages
......
...@@ -43,6 +43,21 @@ export async function getTurbopackJsConfig( ...@@ -43,6 +43,21 @@ export async function getTurbopackJsConfig(
class ModuleBuildError extends Error {} class ModuleBuildError extends Error {}
/**
* Thin stopgap workaround layer to mimic existing wellknown-errors-plugin in webpack's build
* to emit certain type of errors into cli.
*/
export function isWellKnownError(issue: Issue): boolean {
const { title } = issue
const formattedTitle = renderStyledStringToErrorAnsi(title)
// TODO: add more well known errors
if (formattedTitle.includes('Module not found')) {
return true
}
return false
}
export function formatIssue(issue: Issue) { export function formatIssue(issue: Issue) {
const { filePath, title, description, source } = issue const { filePath, title, description, source } = issue
let { documentationLink } = issue let { documentationLink } = issue
......
...@@ -48,21 +48,24 @@ describe('jsconfig.json baseurl', () => { ...@@ -48,21 +48,24 @@ describe('jsconfig.json baseurl', () => {
const basicPage = join(appDir, 'pages/hello.js') const basicPage = join(appDir, 'pages/hello.js')
const contents = await fs.readFile(basicPage, 'utf8') const contents = await fs.readFile(basicPage, 'utf8')
await fs.writeFile( try {
basicPage, await fs.writeFile(
contents.replace('components/world', 'components/worldd') basicPage,
) contents.replace('components/world', 'components/worldd')
await renderViaHTTP(appPort, '/hello') )
const found = await check( const found = await check(
() => stripAnsi(output), async () => {
process.env.TURBOPACK await renderViaHTTP(appPort, '/hello')
? /unable to resolve module "components\/worldd"/ return stripAnsi(output)
: /Module not found: Can't resolve 'components\/worldd'/, },
false /Module not found: Can't resolve 'components\/worldd'/,
) false
await fs.writeFile(basicPage, contents) )
expect(found).toBe(true) expect(found).toBe(true)
} finally {
await fs.writeFile(basicPage, contents)
}
}) })
}) })
......
...@@ -68,15 +68,14 @@ function runTests() { ...@@ -68,15 +68,14 @@ function runTests() {
try { try {
await fs.writeFile(basicPage, contents.replace('@c/world', '@c/worldd')) await fs.writeFile(basicPage, contents.replace('@c/world', '@c/worldd'))
await renderViaHTTP(appPort, '/basic-alias')
const found = await check( const found = await check(
() => stripAnsi(output), async () => {
process.env.TURBOPACK await renderViaHTTP(appPort, '/basic-alias')
? /unable to resolve module "@c\/worldd"/ return stripAnsi(output)
: /Module not found: Can't resolve '@c\/worldd'/, },
false, /Module not found: Can't resolve '@c\/worldd'/,
10 false
) )
expect(found).toBe(true) expect(found).toBe(true)
} finally { } finally {
......
...@@ -13180,10 +13180,11 @@ ...@@ -13180,10 +13180,11 @@
"runtimeError": false "runtimeError": false
}, },
"test/integration/jsconfig-baseurl/test/index.test.js": { "test/integration/jsconfig-baseurl/test/index.test.js": {
"passed": ["jsconfig.json baseurl default behavior should render the page"], "passed": [
"failed": [ "jsconfig.json baseurl default behavior should render the page",
"jsconfig.json baseurl default behavior should have correct module not found error" "jsconfig.json baseurl default behavior should have correct module not found error"
], ],
"failed": [],
"pending": [ "pending": [
"jsconfig.json baseurl should build production mode should trace correctly" "jsconfig.json baseurl should build production mode should trace correctly"
], ],
...@@ -13218,12 +13219,11 @@ ...@@ -13218,12 +13219,11 @@
"jsconfig paths without baseurl default behavior should alias components", "jsconfig paths without baseurl default behavior should alias components",
"jsconfig paths without baseurl default behavior should resolve a single matching alias", "jsconfig paths without baseurl default behavior should resolve a single matching alias",
"jsconfig paths without baseurl default behavior should resolve the first item in the array first", "jsconfig paths without baseurl default behavior should resolve the first item in the array first",
"jsconfig paths without baseurl default behavior should resolve the second item as fallback" "jsconfig paths without baseurl default behavior should resolve the second item as fallback",
],
"failed": [
"jsconfig paths default behavior should have correct module not found error", "jsconfig paths default behavior should have correct module not found error",
"jsconfig paths without baseurl default behavior should have correct module not found error" "jsconfig paths without baseurl default behavior should have correct module not found error"
], ],
"failed": [],
"pending": [ "pending": [
"jsconfig paths should build production mode should trace correctly", "jsconfig paths should build production mode should trace correctly",
"jsconfig paths without baseurl should build production mode should trace correctly" "jsconfig paths without baseurl should build production mode should trace correctly"
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