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(next-swc): report native bindings load err code #52570

Merged
merged 2 commits into from
Jul 12, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 22 additions & 3 deletions packages/next/src/build/swc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ const knownDefaultWasmFallbackTriples = [
'i686-pc-windows-msvc',
]

// The last attempt's error code returned when cjs require to native bindings fails.
// If node.js throws an error without error code, this should be `unknown` instead of undefined.
// For the wasm-first targets (`knownDefaultWasmFallbackTriples`) this will be `unsupported_target`.
let lastNativeBindingsLoadErrorCode:
| 'unknown'
| 'unsupported_target'
| string
| undefined = undefined
let nativeBindings: any
let wasmBindings: any
let downloadWasmPromise: any
Expand Down Expand Up @@ -104,6 +112,7 @@ export async function loadBindings(): Promise<any> {
)

if (shouldLoadWasmFallbackFirst) {
lastNativeBindingsLoadErrorCode = 'unsupported_target'
const fallbackBindings = await tryLoadWasmWithFallback(
attempts,
isCustomTurbopack
Expand Down Expand Up @@ -142,7 +151,10 @@ async function tryLoadWasmWithFallback(
try {
let bindings = await loadWasm('', isCustomTurbopack)
// @ts-expect-error TODO: this event has a wrong type.
eventSwcLoadFailure({ wasm: 'enabled' })
eventSwcLoadFailure({
wasm: 'enabled',
nativeBindingsErrorCode: lastNativeBindingsLoadErrorCode,
})
return bindings
} catch (a) {
attempts = attempts.concat(a)
Expand All @@ -166,7 +178,10 @@ async function tryLoadWasmWithFallback(
isCustomTurbopack
)
// @ts-expect-error TODO: this event has a wrong type.
eventSwcLoadFailure({ wasm: 'fallback' })
eventSwcLoadFailure({
wasm: 'fallback',
nativeBindingsErrorCode: lastNativeBindingsLoadErrorCode,
})

// still log native load attempts so user is
// aware it failed and should be fixed
Expand Down Expand Up @@ -208,7 +223,10 @@ function logLoadFailure(attempts: any, triedWasm = false) {
}

// @ts-expect-error TODO: this event has a wrong type.
eventSwcLoadFailure({ wasm: triedWasm ? 'failed' : undefined })
eventSwcLoadFailure({
wasm: triedWasm ? 'failed' : undefined,
nativeBindingsErrorCode: lastNativeBindingsLoadErrorCode,
})
.then(() => lockfilePatchPromise.cur || Promise.resolve())
.finally(() => {
Log.error(
Expand Down Expand Up @@ -386,6 +404,7 @@ function loadNative(isCustomTurbopack = false) {
`Attempted to load ${pkg}, but an error occurred: ${e.message ?? e}`
)
}
lastNativeBindingsLoadErrorCode = e?.code ?? 'unknown'
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/telemetry/events/swc-load-failure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type EventSwcLoadFailure = {
wasm?: 'enabled' | 'fallback' | 'failed'
glibcVersion?: string
installedSwcPackages?: string
nativeBindingsErrorCode?: string
}
}

Expand Down Expand Up @@ -60,6 +61,7 @@ export async function eventSwcLoadFailure(
platform: process.platform,
nodeVersion: process.versions.node,
wasm: event?.wasm,
nativeBindingsErrorCode: event?.nativeBindingsErrorCode,
},
})
// ensure this event is flushed before process exits
Expand Down