Files
deepseek-harness/packages/boot/app-boot/tests/hooks-thread-stack.ts
T
Yichen Jiang 2152881fcf fix(app-boot): preserve routed resolution errors under async module hooks
Node's module-hooks thread serializes resolver errors and returns the stack
accessor as a read-only configurable data property. Rewriting the routed
importer then assigned error.stack, threw a TypeError, and replaced the
original ERR_PACKAGE_PATH_NOT_EXPORTED or ERR_MODULE_NOT_FOUND error, so
plugin metadata reported a failure instead of falling back to package fields.

The importer rewrite now redefines a rejected stack value in place and
rethrows the original error with its code.
2026-09-23 19:42:34 +08:00

23 lines
901 B
TypeScript

/** Node resolver errors with the stack property that Node's module-hooks thread returns. */
import { registerHooks, type ModuleHooks } from 'node:module'
/**
* Register a synchronous resolve hook that rethrows Node's resolver errors with the `stack` property an
* asynchronous `module.register()` hook chain returns: Node serializes the error on its hooks thread, and the
* main thread receives the stack accessor as a read-only configurable data property.
* @returns the hook registration, which the test deregisters.
*/
export function registerHooksThreadStacks(): ModuleHooks {
return registerHooks({
resolve(specifier, context, nextResolve) {
try {
return nextResolve(specifier, context)
} catch (error) {
if (error instanceof Error) Object.defineProperty(error, 'stack', { value: error.stack, writable: false })
throw error
}
},
})
}