Skip to content

Commit 3492340

Browse files
authored
fix: indeterministic node-gyp makefile that is only used during install hook (#2321)
Fixes #2302 ### Changes are visible to end-users: yes/no Likely no, this file is an implementation of node-gyp that is highly unlikely to be used. ### Test plan - Manual testing; please provide instructions so we can reproduce: #2322
1 parent 572a00e commit 3492340

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

npm/private/lifecycle/lifecycle-hooks.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,15 @@ async function makeBins(nodeModulesPath, scope, segmentsUp) {
8989
}
9090
}
9191

92+
// Returns true if the package uses node-gyp.
93+
async function useNodeGyp(root) {
94+
return await exists(path.join(root, 'binding.gyp'))
95+
}
96+
9297
// Helper which is exported from @pnpm/lifecycle:
9398
// https://github.com/pnpm/pnpm/blob/bc18d33fe00d9ed43f1562d4cc6d37f49d9c2c38/exec/lifecycle/src/index.ts#L52
9499
async function checkBindingGyp(root, scripts) {
95-
if (await exists(path.join(root, 'binding.gyp'))) {
100+
if (await useNodeGyp(root)) {
96101
scripts['install'] = 'node-gyp rebuild'
97102
}
98103
}
@@ -124,6 +129,23 @@ async function runLifecycleHooks(opts, hooks) {
124129
}
125130
}
126131

132+
// Remove the indeterministic Makefile from the resulting directory.
133+
// Can be removed when https://github.com/nodejs/gyp-next/pull/293
134+
// has sufficiently propagated and the vast majority of downloads on
135+
// npm are for packages that are >[email protected], where the fix is
136+
// first included
137+
//
138+
// https://www.npmjs.com/package/node-gyp?activeTab=versions
139+
async function cleanupNodeGypIndeterminism(root) {
140+
if (await useNodeGyp(root)) {
141+
try {
142+
await fs.promises.rm(path.join(root,'build', 'Makefile'))
143+
} catch (e) {
144+
// Best effort, ignore errors.
145+
}
146+
}
147+
}
148+
127149
function isWindows() {
128150
return os.platform() === 'win32'
129151
}
@@ -260,6 +282,8 @@ async function main(args) {
260282
// Run user specified custom postinstall hook
261283
await runLifecycleHook('custom_postinstall', rulesJsJson, opts)
262284
}
285+
286+
await cleanupNodeGypIndeterminism(opts.pkgRoot)
263287
}
264288

265289
// Copy contents of a package dir to a destination dir (without copying the package dir itself)

npm/private/lifecycle/min/index.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)