Skip to content

Commit

Permalink
feat(next-swc): report native bindings load err code (#52570)
Browse files Browse the repository at this point in the history
Related: vercel/next-telemetry#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.
  • Loading branch information
kwonoj committed Jul 12, 2023
1 parent 1989f49 commit d93231e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
25 changes: 22 additions & 3 deletions packages/next/src/build/swc/index.ts
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
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

0 comments on commit d93231e

Please sign in to comment.