@@ -51,8 +51,10 @@ export function verifyNativeModule(name, requireModule) {
5151}
5252
5353function shouldRebuildNativeModule ( errorMessage ) {
54- return / N O D E _ M O D U L E _ V E R S I O N | C o u l d n o t l o c a t e t h e b i n d i n g s f i l e / . test (
55- errorMessage ,
54+ return (
55+ / N O D E _ M O D U L E _ V E R S I O N | C o u l d n o t l o c a t e t h e b i n d i n g s f i l e | M o d u l e d i d n o t s e l f - r e g i s t e r / . test (
56+ errorMessage ,
57+ )
5658 ) ;
5759}
5860
@@ -67,11 +69,38 @@ function getRepairableNativeModuleError(name, requireModule) {
6769 }
6870}
6971
72+ function getRepairedNativeModuleError ( name , pkgJsonPath ) {
73+ try {
74+ // A failed dlopen remains cached for the life of the process. Verify a
75+ // replacement binary in a fresh process so the old handle cannot poison it.
76+ execFileSync (
77+ process . execPath ,
78+ [
79+ "--input-type=module" ,
80+ "--eval" ,
81+ `import { createRequire } from "node:module";
82+ const requireModule = createRequire(${ JSON . stringify ( pkgJsonPath ) } );
83+ const NativeModule = requireModule(${ JSON . stringify ( name ) } );
84+ const instance = new NativeModule(":memory:");
85+ instance.close();` ,
86+ ] ,
87+ { stdio : [ "ignore" , "pipe" , "pipe" ] } ,
88+ ) ;
89+ return null ;
90+ } catch ( err ) {
91+ const message = formatChildProcessFailure ( err ) ;
92+ if ( ! shouldRebuildNativeModule ( message ) ) throw err ;
93+ return message ;
94+ }
95+ }
96+
7097export function ensureNativeModules ( {
7198 repoRoot = defaultRepoRoot ,
7299 modules = nativeModules ,
73100 createRequire : createRequireImpl = createRequire ,
74101 execFileSync : execFileSyncImpl = execFileSync ,
102+ verifyRepairedNativeModule : verifyRepairedNativeModuleImpl =
103+ getRepairedNativeModuleError ,
75104 log = console . log ,
76105} = { } ) {
77106 for ( const { name, resolveFrom } of modules ) {
@@ -108,9 +137,9 @@ export function ensureNativeModules({
108137 ) ;
109138 }
110139
111- const prebuildVerifyError = getRepairableNativeModuleError (
140+ const prebuildVerifyError = verifyRepairedNativeModuleImpl (
112141 name ,
113- requireModule ,
142+ pkgJsonPath ,
114143 ) ;
115144 if ( prebuildVerifyError === null ) {
116145 if ( ! prebuildInstalled ) {
@@ -147,9 +176,9 @@ export function ensureNativeModules({
147176 } ,
148177 ) ;
149178
150- const rebuildVerifyError = getRepairableNativeModuleError (
179+ const rebuildVerifyError = verifyRepairedNativeModuleImpl (
151180 name ,
152- requireModule ,
181+ pkgJsonPath ,
153182 ) ;
154183 if ( rebuildVerifyError !== null ) {
155184 throw new Error (
0 commit comments