Unverified Commit d93231eb authored by OJ Kwon's avatar OJ Kwon Committed by GitHub

feat(next-swc): report native bindings load err code (#52570)



Related: https://github.com/vercel/next-telemetry/pull/106

We collect wasm fallback reason (`enabled`, `fallback`, ..) but it is bit unclear why we came to reach `fallback` state for the wasm. This PR expands current reporting to include native bindings error code if node.js returns it to understand categories better. Mostly this would be in between ERR_DLOPEN or ERR_MODULE_NOT_FOUND, but worth to confirm if the guess is correct. For the targets have multiple triples (gnu / musl), we collect only the last attempt's error code.

There are 2 custom variants of the string value other than err code itself: if thrown error doesn't have code (unlikely, but) it'll be `unknown`. for the targets we falls back to wasm immediately (freebsd, for example) it'll be marked as unsupported_target.

One thing to note is this does not collect if wasm binding fails to load: this is strictly for native bindings load error.
parent 1989f494
......@@ -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
......@@ -104,6 +112,7 @@ export async function loadBindings(): Promise<any> {
)
if (shouldLoadWasmFallbackFirst) {
lastNativeBindingsLoadErrorCode = 'unsupported_target'
const fallbackBindings = await tryLoadWasmWithFallback(
attempts,
isCustomTurbopack
......@@ -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)
......@@ -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
......@@ -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(
......@@ -386,6 +404,7 @@ function loadNative(isCustomTurbopack = false) {
`Attempted to load ${pkg}, but an error occurred: ${e.message ?? e}`
)
}
lastNativeBindingsLoadErrorCode = e?.code ?? 'unknown'
}
}
}
......
......@@ -14,6 +14,7 @@ export type EventSwcLoadFailure = {
wasm?: 'enabled' | 'fallback' | 'failed'
glibcVersion?: string
installedSwcPackages?: string
nativeBindingsErrorCode?: string
}
}
......@@ -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
......
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