Files
OmniRoute/src/instrumentation.ts
Diego Rodrigues de Sa e Souza 6d99a46d4b fix(cli): guarantee non-empty [STARTUP] Fatal log on instrumentation-hook boot throw (#10447)
* fix(cli): guarantee non-empty [STARTUP] Fatal log on instrumentation-hook boot throw

Refs #10171: on native Windows / WSL2 boots, an instrumentation-hook throw
during module-load or registerNodejs() leaves the HTTP listener up while
every DB-touching route 500s, with app.log staying completely empty. The
#7773/#7828 guard in ensureDbReadyForBoot only logs one specific failure
class (DB driver init). register() in src/instrumentation.ts now wraps the
boot call in a try/catch at the outermost boundary and unconditionally logs
a "[STARTUP] Fatal: instrumentation hook failed during boot:" line before
rethrowing, so app.log/stdout is never silently empty on a failed boot
regardless of platform or which step threw.

This is a partial diagnostic hardening, not the full fix for #10171 — the
platform-specific root cause on native Windows/WSL2 still needs the
reporter's raw child stderr from a real host (tracked separately, see
_tasks/pipeline/bugs/2-implementing/10171-instrumentation-hook-500-on-windows-wsl.plan.md).

* fix(cli): normalize instrumentation boot errors

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* fix(cli): reuse shared normalizeBootError helper in instrumentation.ts

The outermost instrumentation-hook boot boundary (#10171) was inlining its
own err-instanceof-Error normalization instead of reusing the existing
normalizeBootError() helper already defined in instrumentation-node.ts for
the same purpose (#6560/#7773). Extract it into a dependency-free
src/lib/instrumentationBootError.ts so both instrumentation.ts (which also
loads under the Edge runtime) and instrumentation-node.ts can import it
statically without risking a second failing dynamic import of
instrumentation-node.ts from within the catch block.

---------

Co-authored-by: adevwithpurpose <adevwithpurpose@users.noreply.github.com>
2026-08-18 10:57:44 -03:00

51 lines
2.6 KiB
TypeScript

/**
* Next.js Instrumentation Hook
*
* Called once when the server starts (both dev and production).
* All Node.js-specific logic lives in ./instrumentation-node.ts to prevent
* Turbopack's Edge bundler from tracing into native modules (fs, path, os, etc.)
*
* @see https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation
*/
import { normalizeBootError } from "@/lib/instrumentationBootError";
/**
* `registerNodejsFn` is only for tests to inject a fake without module-mocking
* (`node:test` does not support `mock.module` reliably here) — mirrors the
* same injection pattern as `ensureDbReadyForBoot` in `./instrumentation-node`.
*/
export async function register(registerNodejsFn?: () => Promise<void>) {
if (process.env.NEXT_RUNTIME === "nodejs") {
try {
// Literal path so Webpack emits the chunk (computed string breaks dev:
// MODULE_NOT_FOUND for ./instrumentation-node at runtime).
// Turbopack may still avoid tracing this into Edge when guarded by NEXT_RUNTIME.
const registerNodejs =
registerNodejsFn ?? (await import("./instrumentation-node")).registerNodejs;
await registerNodejs();
} catch (err: unknown) {
// Outermost boot boundary (#10171): any throw during module-load of
// ./instrumentation-node OR anywhere inside registerNodejs() runs
// BEFORE initConsoleInterceptor() is wired up. ensureDbReadyForBoot's
// own #7773 guard already logs one specific failure class (DB driver
// init), but it does not cover a throw that happens before it is even
// reached (e.g. a module-load-time error importing ./instrumentation-node
// itself, as reported on native Windows/WSL2 boots). Without an
// unconditional log line here, the process can keep its HTTP listener
// up while every DB-touching route 500s forever with a permanently
// empty app.log. This guarantees stdout/app.log is never silently
// empty on a failed boot, regardless of platform or which step threw.
// Reuses the same normalizeBootError() helper as ensureDbReadyForBoot
// (./instrumentation-node) — imported from a dependency-free module so
// this file, which also loads under the Edge runtime, never has to pull
// in Node-only code (or risk a second failing dynamic import of
// ./instrumentation-node itself) just to normalize the caught value.
const normalizedError = normalizeBootError(err);
const message = normalizedError.message;
console.error("[STARTUP] Fatal: instrumentation hook failed during boot:", message);
throw normalizedError;
}
}
}