Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/next/src/server/dev/hot-reloader-turbopack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
type StartBuilding,
processTopLevelIssues,
type TopLevelIssuesMap,
isWellKnownError,
} from './turbopack-utils'
import {
propagateServerField,
Expand Down Expand Up @@ -666,9 +667,14 @@ export async function createHotReloaderTurbopack(
currentEntryIssues.get(pagesEntryKey)
if (thisEntryIssues !== undefined && thisEntryIssues.size > 0) {
// If there is an error related to the requesting page we display it instead of the first error
return [...topLevelIssues, ...thisEntryIssues.values()].map(
(issue) => new Error(formatIssue(issue))
)
return [...topLevelIssues, ...thisEntryIssues.values()].map((issue) => {
const formattedIssue = formatIssue(issue)
if (isWellKnownError(issue)) {
Log.error(formattedIssue)
}

return new Error(formattedIssue)
})
}

// Otherwise, return all errors across pages
Expand Down
15 changes: 15 additions & 0 deletions packages/next/src/server/dev/turbopack-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ export async function getTurbopackJsConfig(

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) {
const { filePath, title, description, source } = issue
let { documentationLink } = issue
Expand Down
31 changes: 17 additions & 14 deletions test/integration/jsconfig-baseurl/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,24 @@ describe('jsconfig.json baseurl', () => {
const basicPage = join(appDir, 'pages/hello.js')
const contents = await fs.readFile(basicPage, 'utf8')

await fs.writeFile(
basicPage,
contents.replace('components/world', 'components/worldd')
)
await renderViaHTTP(appPort, '/hello')
try {
await fs.writeFile(
basicPage,
contents.replace('components/world', 'components/worldd')
)

const found = await check(
() => stripAnsi(output),
process.env.TURBOPACK
? /unable to resolve module "components\/worldd"/
: /Module not found: Can't resolve 'components\/worldd'/,
false
)
await fs.writeFile(basicPage, contents)
expect(found).toBe(true)
const found = await check(
async () => {
await renderViaHTTP(appPort, '/hello')
return stripAnsi(output)
},
/Module not found: Can't resolve 'components\/worldd'/,
false
)
expect(found).toBe(true)
} finally {
await fs.writeFile(basicPage, contents)
}
})
})

Expand Down
13 changes: 6 additions & 7 deletions test/integration/jsconfig-paths/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,14 @@ function runTests() {

try {
await fs.writeFile(basicPage, contents.replace('@c/world', '@c/worldd'))
await renderViaHTTP(appPort, '/basic-alias')

const found = await check(
() => stripAnsi(output),
process.env.TURBOPACK
? /unable to resolve module "@c\/worldd"/
: /Module not found: Can't resolve '@c\/worldd'/,
false,
10
async () => {
await renderViaHTTP(appPort, '/basic-alias')
return stripAnsi(output)
},
/Module not found: Can't resolve '@c\/worldd'/,
false
)
expect(found).toBe(true)
} finally {
Expand Down
10 changes: 5 additions & 5 deletions test/turbopack-tests-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13180,10 +13180,11 @@
"runtimeError": false
},
"test/integration/jsconfig-baseurl/test/index.test.js": {
"passed": ["jsconfig.json baseurl default behavior should render the page"],
"failed": [
"passed": [
"jsconfig.json baseurl default behavior should render the page",
"jsconfig.json baseurl default behavior should have correct module not found error"
],
"failed": [],
"pending": [
"jsconfig.json baseurl should build production mode should trace correctly"
],
Expand Down Expand Up @@ -13218,12 +13219,11 @@
"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 the first item in the array first",
"jsconfig paths without baseurl default behavior should resolve the second item as fallback"
],
"failed": [
"jsconfig paths without baseurl default behavior should resolve the second item as fallback",
"jsconfig paths default behavior should have correct module not found error",
"jsconfig paths without baseurl default behavior should have correct module not found error"
],
"failed": [],
"pending": [
"jsconfig paths should build production mode should trace correctly",
"jsconfig paths without baseurl should build production mode should trace correctly"
Expand Down