fix: do not run node-gyp rebuild if preinstall lifecycle script is present (#7206)

This commit is contained in:
Andrea Culot
2023-10-15 21:36:42 +01:00
committed by GitHub
parent 4246f41bed
commit 84f81c9ae3
5 changed files with 39 additions and 3 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/lifecycle": patch
"pnpm": patch
---
Do not run node-gyp rebuild if `preinstall` lifecycle script is present [#7206](https://github.com/pnpm/pnpm/pull/7206).

View File

@@ -27,7 +27,7 @@ export async function runPostinstallHooks (
pkg.scripts = {}
}
if (!pkg.scripts.install) {
if (!pkg.scripts.install && !pkg.scripts.preinstall) {
await checkBindingGyp(opts.pkgRoot, pkg.scripts)
}
@@ -47,8 +47,8 @@ export async function runPostinstallHooks (
}
/**
* Run node-gyp when binding.gyp is available. Only do this when there's no
* `install` script (see `npm help scripts`).
* Run node-gyp when binding.gyp is available. Only do this when there are no
* `install` and `preinstall` scripts (see `npm help scripts`).
*/
async function checkBindingGyp (
root: string,

View File

@@ -0,0 +1,8 @@
{
"targets": [
{
"target_name": "binding",
"sources": [ "binding.cc" ]
}
]
}

View File

@@ -0,0 +1,7 @@
{
"name": "gyp-with-preinstall",
"version": "1.0.0",
"scripts": {
"preinstall": "node -e \"process.stdout.write('preinstall')\" | json-append output.json"
}
}

View File

@@ -84,3 +84,18 @@ test('runLifecycleHook() should throw an error while missing script start or fil
})
).rejects.toThrow(new PnpmError('NO_SCRIPT_OR_SERVER', 'Missing script start or file server.js'))
})
test('preinstall script does not trigger node-gyp rebuild', async () => {
const pkgRoot = f.find('gyp-with-preinstall')
await rimraf(path.join(pkgRoot, 'output.json'))
await runPostinstallHooks({
depPath: '/gyp-with-preinstall/1.0.0',
optional: false,
pkgRoot,
rawConfig: {},
rootModulesDir,
unsafePerm: true,
})
expect(loadJsonFile.sync(path.join(pkgRoot, 'output.json'))).toStrictEqual(['preinstall'])
})