22'use strict'
33
44const path = require ( 'path' )
5+ const fs = require ( 'fs' )
56const { spawnSync } = require ( 'child_process' )
67
7- const bindingPath = path . join ( __dirname , '..' , 'build' , 'Release' , 'node_expat.node' )
8+ const PACKAGE_ROOT = path . join ( __dirname , '..' )
9+ const BINDING_PATH = path . join ( PACKAGE_ROOT , 'build' , 'Release' , 'node_expat.node' )
810
911function bindingLoadsCleanly ( ) {
1012 try {
11- require ( bindingPath )
13+ require ( BINDING_PATH )
1214 return true
1315 } catch ( _ ) {
1416 return false
@@ -32,10 +34,39 @@ try {
3234}
3335
3436const result = nodeGypBin
35- ? spawnSync ( process . execPath , [ nodeGypBin , 'rebuild' ] , { stdio : 'inherit' } )
37+ ? spawnSync ( process . execPath , [ nodeGypBin , 'rebuild' ] , {
38+ cwd : PACKAGE_ROOT ,
39+ stdio : 'inherit' ,
40+ } )
3641 : spawnSync ( process . platform === 'win32' ? 'node-gyp.cmd' : 'node-gyp' , [ 'rebuild' ] , {
42+ cwd : PACKAGE_ROOT ,
3743 stdio : 'inherit' ,
3844 shell : process . platform === 'win32' ,
3945 } )
4046
41- process . exit ( result . status == null ? 1 : result . status )
47+ if ( result . error ) {
48+ console . error ( 'node-expat: failed to spawn node-gyp:' , result . error . message )
49+ process . exit ( 1 )
50+ }
51+
52+ if ( result . status !== 0 ) {
53+ console . error ( 'node-expat: node-gyp rebuild exited with code' , result . status )
54+ process . exit ( result . status || 1 )
55+ }
56+
57+ // Refuse to exit 0 unless the binding is actually on disk AND loadable.
58+ // This prevents pnpm's side-effects cache from storing a "built" state
59+ // that has no artifact, which would silently propagate across every
60+ // subsequent install that hits this cache.
61+ if ( ! fs . existsSync ( BINDING_PATH ) ) {
62+ console . error ( 'node-expat: node-gyp reported success but binding is missing at:' , BINDING_PATH )
63+ process . exit ( 1 )
64+ }
65+
66+ if ( ! bindingLoadsCleanly ( ) ) {
67+ console . error ( 'node-expat: binding exists at' , BINDING_PATH , 'but does not load on this runtime' )
68+ process . exit ( 1 )
69+ }
70+
71+ console . log ( 'node-expat: binding successfully built and verified at' , BINDING_PATH )
72+ process . exit ( 0 )
0 commit comments