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 = [ ...@@ -71,6 +71,14 @@ const knownDefaultWasmFallbackTriples = [
'i686-pc-windows-msvc', '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 nativeBindings: any
let wasmBindings: any let wasmBindings: any
let downloadWasmPromise: any let downloadWasmPromise: any
...@@ -104,6 +112,7 @@ export async function loadBindings(): Promise<any> { ...@@ -104,6 +112,7 @@ export async function loadBindings(): Promise<any> {
) )
if (shouldLoadWasmFallbackFirst) { if (shouldLoadWasmFallbackFirst) {
lastNativeBindingsLoadErrorCode = 'unsupported_target'
const fallbackBindings = await tryLoadWasmWithFallback( const fallbackBindings = await tryLoadWasmWithFallback(
attempts, attempts,
isCustomTurbopack isCustomTurbopack
...@@ -142,7 +151,10 @@ async function tryLoadWasmWithFallback( ...@@ -142,7 +151,10 @@ async function tryLoadWasmWithFallback(
try { try {
let bindings = await loadWasm('', isCustomTurbopack) let bindings = await loadWasm('', isCustomTurbopack)
// @ts-expect-error TODO: this event has a wrong type. // @ts-expect-error TODO: this event has a wrong type.
eventSwcLoadFailure({ wasm: 'enabled' }) eventSwcLoadFailure({
wasm: 'enabled',
nativeBindingsErrorCode: lastNativeBindingsLoadErrorCode,
})
return bindings return bindings
} catch (a) { } catch (a) {
attempts = attempts.concat(a) attempts = attempts.concat(a)
...@@ -166,7 +178,10 @@ async function tryLoadWasmWithFallback( ...@@ -166,7 +178,10 @@ async function tryLoadWasmWithFallback(
isCustomTurbopack isCustomTurbopack
) )
// @ts-expect-error TODO: this event has a wrong type. // @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 // still log native load attempts so user is
// aware it failed and should be fixed // aware it failed and should be fixed
...@@ -208,7 +223,10 @@ function logLoadFailure(attempts: any, triedWasm = false) { ...@@ -208,7 +223,10 @@ function logLoadFailure(attempts: any, triedWasm = false) {
} }
// @ts-expect-error TODO: this event has a wrong type. // @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()) .then(() => lockfilePatchPromise.cur || Promise.resolve())
.finally(() => { .finally(() => {
Log.error( Log.error(
...@@ -386,6 +404,7 @@ function loadNative(isCustomTurbopack = false) { ...@@ -386,6 +404,7 @@ function loadNative(isCustomTurbopack = false) {
`Attempted to load ${pkg}, but an error occurred: ${e.message ?? e}` `Attempted to load ${pkg}, but an error occurred: ${e.message ?? e}`
) )
} }
lastNativeBindingsLoadErrorCode = e?.code ?? 'unknown'
} }
} }
} }
......
...@@ -14,6 +14,7 @@ export type EventSwcLoadFailure = { ...@@ -14,6 +14,7 @@ export type EventSwcLoadFailure = {
wasm?: 'enabled' | 'fallback' | 'failed' wasm?: 'enabled' | 'fallback' | 'failed'
glibcVersion?: string glibcVersion?: string
installedSwcPackages?: string installedSwcPackages?: string
nativeBindingsErrorCode?: string
} }
} }
...@@ -60,6 +61,7 @@ export async function eventSwcLoadFailure( ...@@ -60,6 +61,7 @@ export async function eventSwcLoadFailure(
platform: process.platform, platform: process.platform,
nodeVersion: process.versions.node, nodeVersion: process.versions.node,
wasm: event?.wasm, wasm: event?.wasm,
nativeBindingsErrorCode: event?.nativeBindingsErrorCode,
}, },
}) })
// ensure this event is flushed before process exits // 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