Skip to content

Commit a9fcdf3

Browse files
committed
Only skip missing entry points for lifecycle-built packages
Keep `npm pack --dry-run` script-free when collecting packed files so prerequisite checks do not fail on broken lifecycle hooks or noisy script output. Still run entry-point validation against that stable packlist, but only skip the failure when an entry point is missing and the package declares pack-related lifecycle scripts. That preserves validation for packages whose entry points are already present without scripts, while avoiding false negatives for packages that generate publishable files during `prepare` or `prepack`. Fixes #780
1 parent 4c58afc commit a9fcdf3

File tree

7 files changed

+54
-1
lines changed

7 files changed

+54
-1
lines changed

source/npm/util.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ export const getFilesToBePacked = async rootDirectory => {
210210
'--dry-run',
211211
'--json',
212212
'--silent',
213+
'--ignore-scripts',
213214
// TODO: Remove this once [npm/cli#7354](https://github.com/npm/cli/issues/7354) is resolved.
214215
'--foreground-scripts=false',
215216
], {cwd: rootDirectory});
@@ -226,6 +227,21 @@ export const getFilesToBePacked = async rootDirectory => {
226227
}
227228
};
228229

230+
const hasPackLifecycleScript = package_ => {
231+
const {scripts} = package_;
232+
233+
if (typeof scripts !== 'object' || scripts === null) {
234+
return false;
235+
}
236+
237+
return [
238+
'prepare',
239+
'prepack',
240+
'prepublish',
241+
'prepublishOnly',
242+
].some(scriptName => typeof scripts[scriptName] === 'string');
243+
};
244+
229245
const isValidEntryPoint = value => typeof value === 'string' && !value.includes('*');
230246

231247
const getExportsFiles = exports => {
@@ -295,6 +311,10 @@ export const verifyPackageEntryPoints = async (package_, rootDirectory) => {
295311
}
296312

297313
if (missingEntryPoints.length > 0) {
314+
if (hasPackLifecycleScript(package_)) {
315+
return;
316+
}
317+
298318
const missing = missingEntryPoints.map(({field, path: entryPath}) => ` "${field}": ${entryPath}`).join('\n');
299319
throw new Error(`Missing entry points in published files:\n${missing}\n\nEnsure these files exist and are included in the "files" field.`);
300320
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('foo');
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "foo",
3+
"version": "0.0.0",
4+
"files": ["index.js"],
5+
"scripts": {
6+
"prepack": "exit 1"
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "prepack-generated-entry-point",
3+
"version": "0.0.0",
4+
"main": "dist/index.js",
5+
"files": ["dist"],
6+
"scripts": {
7+
"prepack": "node --input-type=module -e \"import fs from 'node:fs'; fs.mkdirSync('dist', {recursive: true}); fs.writeFileSync('dist/index.js', 'export default 1;\\n');\""
8+
}
9+
}

test/fixtures/files/prepare-script/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"version": "0.0.0",
44
"files": ["index.js"],
55
"scripts": {
6-
"prepare": "echo '> foo@0.0.0 prepare' && echo '> test prepare script'"
6+
"prepare": "echo '[build] compiling'"
77
}
88
}

test/npm/util/entry-points.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,14 @@ test('verifyPackageEntryPoints - valid entry points', async t => {
191191

192192
await t.notThrowsAsync(npm.verifyPackageEntryPoints({main: 'index.js'}, fixtureDirectory));
193193
});
194+
195+
test('verifyPackageEntryPoints - skipped when prepack script may generate files', async t => {
196+
const fixtureDirectory = getFixture('prepack-generated-entry-point');
197+
198+
await t.notThrowsAsync(npm.verifyPackageEntryPoints({
199+
main: 'dist/index.js',
200+
scripts: {
201+
prepack: 'build',
202+
},
203+
}, fixtureDirectory));
204+
});

test/npm/util/packed-files.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,7 @@ test('doesn\'t show files in .github', verifyPackedFiles, 'dot-github', [
7474
test('handles prepare script output (e.g., Husky)', verifyPackedFiles, 'prepare-script', [
7575
'index.js',
7676
]);
77+
78+
test('ignores failing prepack script', verifyPackedFiles, 'failing-prepack-script', [
79+
'index.js',
80+
]);

0 commit comments

Comments
 (0)