@pnpm/fs.packlist 1100.0.4 → 1100.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
1
1
  # @pnpm/fs.packlist
2
2
 
3
+ ## 1100.0.6
4
+
5
+ ### Patch Changes
6
+
7
+ - `pnpm install` now works in StackBlitz WebContainers on projects without a lockfile. It used to fail there with `ENOENT ... pnpm-lock.yaml`, because pnpm did not recognize the errors that WebContainers return from asynchronous file system calls [#15649](https://github.com/pnpm/pnpm/issues/15649).
8
+
9
+ - Updated dependencies:
10
+ - @pnpm/error@1100.2.1
11
+ - @pnpm/workspace.project-manifest-reader@1100.1.1
12
+
13
+ ## 1100.0.5
14
+
15
+ ### Patch Changes
16
+
17
+ - `pnpm pack` now honors the `files` field of `package.yaml` and `package.json5` manifests. Git-hosted and injected local dependencies that use these manifests now honor it too [#7906](https://github.com/pnpm/pnpm/issues/7906).
18
+
19
+ - `pnpm pack`, `pnpm deploy`, and installs of local directory dependencies now keep symlinks that point to files or directories included in the package. `pnpm pack` leaves out symlinks that point outside the package [#8208](https://github.com/pnpm/pnpm/issues/8208).
20
+
21
+ - `pnpm pack` and `pnpm publish` now include bundled dependencies when using the isolated linker. This covers workspace packages and the dependencies of each bundled package. Bundled dependencies are also included when `publishConfig.directory` selects a build directory [pnpm/pnpm#1643](https://github.com/pnpm/pnpm/issues/1643).
22
+
23
+ - Updated dependencies:
24
+ - @pnpm/workspace.project-manifest-reader@1100.1.0
25
+
3
26
  ## 1100.0.4
4
27
 
5
28
  ### Patch Changes
package/lib/index.d.ts CHANGED
@@ -1,4 +1,18 @@
1
- export declare function packlist(pkgDir: string, opts?: {
1
+ export interface PacklistOptions {
2
2
  manifest?: Record<string, unknown>;
3
3
  workspaceDir?: string;
4
- }): Promise<string[]>;
4
+ bundledDependenciesDir?: string;
5
+ }
6
+ /**
7
+ * The files that live at their packed path under pkgDir. A bundled dependency
8
+ * resolved through an isolated node_modules layout is packed at a different
9
+ * path than it is read from; packlistWithSources returns those too.
10
+ */
11
+ export declare function packlist(pkgDir: string, opts?: PacklistOptions): Promise<string[]>;
12
+ /**
13
+ * Maps each packed path to the file it is read from. Bundled dependencies
14
+ * resolve from pkgDir upward, and never above the workspace root when pkgDir
15
+ * is a workspace package, or above bundledDependenciesDir (default pkgDir)
16
+ * otherwise.
17
+ */
18
+ export declare function packlistWithSources(pkgDir: string, opts?: PacklistOptions): Promise<Map<string, string>>;
package/lib/index.js CHANGED
@@ -1,31 +1,209 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
- import util from 'node:util';
3
+ import { isError } from '@pnpm/error';
4
+ import { safeReadProjectManifestOnly } from '@pnpm/workspace.project-manifest-reader';
4
5
  import { isSubdir } from 'is-subdir';
5
6
  import npmPacklist from 'npm-packlist';
7
+ const ALTERNATE_MANIFEST_NAMES = ['package.yaml', 'package.json5'];
8
+ /**
9
+ * npm-packlist applies the manifest rules (`files`, `main`, `bin`, and the
10
+ * always-included files) only when it finds `package.json` on disk. This walker
11
+ * also applies them to a package root that has only a `package.yaml` or
12
+ * `package.json5` manifest, and always includes those manifests like
13
+ * `package.json`.
14
+ *
15
+ * npm-packlist also drops every symlink. This walker keeps them, including
16
+ * symlinks named in `files`, and leaves it to packlistWithSources to exclude the
17
+ * ones that point outside the package.
18
+ */
19
+ class PackWalker extends npmPacklist.Walker {
20
+ onReaddir(entries) {
21
+ if (this.isPackage &&
22
+ !entries.includes('package.json') &&
23
+ entries.some((entry) => ALTERNATE_MANIFEST_NAMES.includes(entry))) {
24
+ this.processPackage(() => {
25
+ super.onReaddir(entries);
26
+ });
27
+ return;
28
+ }
29
+ super.onReaddir(entries);
30
+ }
31
+ injectRules(filename, rules, callback) {
32
+ if (rules.includes('!/package.json')) {
33
+ rules = [...this.symlinkedFilesRules(), ...rules, ...ALTERNATE_MANIFEST_NAMES.map((name) => `!/${name}`)];
34
+ }
35
+ super.injectRules(filename, rules, callback);
36
+ }
37
+ // npm-packlist walks each subdirectory with a walker of its own class.
38
+ walker(entry, opts, callback) {
39
+ new PackWalker(this.tree, this.walkerOpt(entry, opts)).on('done', callback).start();
40
+ }
41
+ onstat(opts, callback) {
42
+ if (opts.st.isSymbolicLink()) {
43
+ ignoreWalkOnstat.call(this, opts, callback);
44
+ return;
45
+ }
46
+ super.onstat(opts, callback);
47
+ }
48
+ // npm-packlist makes a `files` entry strictly required only when it is a
49
+ // regular file, so a symlink it names would otherwise be left out.
50
+ symlinkedFilesRules() {
51
+ const files = this.tree.package.files;
52
+ if (!Array.isArray(files))
53
+ return [];
54
+ const rules = [];
55
+ for (let file of files) {
56
+ if (file.startsWith('!'))
57
+ continue;
58
+ if (file.startsWith('./'))
59
+ file = file.slice(1);
60
+ let stat;
61
+ try {
62
+ stat = fs.lstatSync(path.join(this.path, file));
63
+ }
64
+ catch (err) {
65
+ if (isError(err) && 'code' in err && (err.code === 'ENOENT' || err.code === 'ENOTDIR'))
66
+ continue;
67
+ throw err;
68
+ }
69
+ if (stat.isSymbolicLink()) {
70
+ rules.push(`!${file}`);
71
+ this.requiredFiles.push(file.startsWith('/') ? file.slice(1) : file);
72
+ }
73
+ }
74
+ return rules;
75
+ }
76
+ }
77
+ // ignore-walk's onstat, which npm-packlist overrides to skip everything that
78
+ // is neither a file nor a directory.
79
+ const ignoreWalkOnstat = Object.getPrototypeOf(npmPacklist.Walker.prototype).onstat;
80
+ /**
81
+ * The files that live at their packed path under pkgDir. A bundled dependency
82
+ * resolved through an isolated node_modules layout is packed at a different
83
+ * path than it is read from; packlistWithSources returns those too.
84
+ */
6
85
  export async function packlist(pkgDir, opts) {
86
+ const resolvedPkgDir = path.resolve(pkgDir);
87
+ const files = await packlistWithSources(resolvedPkgDir, opts);
88
+ return Array.from(files)
89
+ .filter(([file, source]) => source === path.join(resolvedPkgDir, file))
90
+ .map(([file]) => file);
91
+ }
92
+ /**
93
+ * Maps each packed path to the file it is read from. Bundled dependencies
94
+ * resolve from pkgDir upward, and never above the workspace root when pkgDir
95
+ * is a workspace package, or above bundledDependenciesDir (default pkgDir)
96
+ * otherwise.
97
+ */
98
+ export async function packlistWithSources(pkgDir, opts) {
7
99
  const resolvedPkgDir = path.resolve(pkgDir);
8
100
  const workspaceDir = opts?.workspaceDir == null ? undefined : path.resolve(opts.workspaceDir);
9
- const pkg = opts?.manifest ?? readPackageJson(resolvedPkgDir);
10
- const tree = buildRootTree(resolvedPkgDir, pkg);
101
+ const pkg = opts?.manifest ?? await safeReadProjectManifestOnly(resolvedPkgDir) ?? {};
11
102
  const hasWorkspaceContext = workspaceDir != null && workspaceDir !== resolvedPkgDir && isSubdir(workspaceDir, resolvedPkgDir);
103
+ const boundary = hasWorkspaceContext ? workspaceDir : path.resolve(opts?.bundledDependenciesDir ?? resolvedPkgDir);
104
+ const { tree, packedDirs } = buildRootTree(resolvedPkgDir, pkg, boundary);
12
105
  let hasNpmIgnore = false;
13
106
  if (hasWorkspaceContext) {
14
107
  try {
15
108
  hasNpmIgnore = (await fs.promises.stat(path.join(resolvedPkgDir, '.npmignore'))).isFile();
16
109
  }
17
110
  catch (err) {
18
- if (!util.types.isNativeError(err) || !('code' in err) || err.code !== 'ENOENT')
111
+ if (!isError(err) || !('code' in err) || err.code !== 'ENOENT')
19
112
  throw err;
20
113
  }
21
114
  }
22
115
  const packlistOpts = hasWorkspaceContext && !hasNpmIgnore
23
116
  ? { prefix: workspaceDir, workspaces: [resolvedPkgDir] }
24
117
  : undefined;
25
- const files = await npmPacklist(tree, packlistOpts);
26
- return files.map((file) => file.replace(/^\.[/\\]/, ''));
118
+ const walkedFiles = await new Promise((resolve, reject) => {
119
+ new PackWalker(tree, { ...packlistOpts, isPackage: true })
120
+ .on('done', resolve)
121
+ .on('error', reject)
122
+ .start();
123
+ });
124
+ const files = walkedFiles
125
+ .map((file) => file.replace(/^\.[/\\]/, ''))
126
+ .filter((file) => isInternalFileOrSymlink(resolvedPkgDir, file));
127
+ return mapToPackedPaths(resolvedPkgDir, files, packedDirs);
128
+ }
129
+ function isEscapingRelativePath(rel) {
130
+ return rel === '..' || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel);
131
+ }
132
+ function isInternalFileOrSymlink(pkgDir, relFile) {
133
+ const absPath = path.join(pkgDir, relFile);
134
+ let lstat;
135
+ try {
136
+ lstat = fs.lstatSync(absPath);
137
+ }
138
+ catch (err) {
139
+ if (isError(err) && 'code' in err && err.code === 'ENOENT')
140
+ return false;
141
+ throw err;
142
+ }
143
+ if (!lstat.isSymbolicLink()) {
144
+ return true;
145
+ }
146
+ let linkTarget = fs.readlinkSync(absPath);
147
+ if (path.isAbsolute(linkTarget)) {
148
+ linkTarget = path.relative(path.dirname(absPath), linkTarget);
149
+ }
150
+ const relPosix = process.platform === 'win32' ? relFile.replace(/\\/g, '/') : relFile;
151
+ const posixTarget = linkTarget.replace(/\\/g, '/');
152
+ if (path.posix.isAbsolute(posixTarget)) {
153
+ return false;
154
+ }
155
+ const normalizedArchive = path.posix.normalize(path.posix.join(path.posix.dirname(relPosix), posixTarget));
156
+ if (normalizedArchive === '..' || normalizedArchive.startsWith('../')) {
157
+ return false;
158
+ }
159
+ const resolvedTarget = path.resolve(path.dirname(absPath), linkTarget);
160
+ const relToPkg = path.relative(pkgDir, resolvedTarget);
161
+ if (isEscapingRelativePath(relToPkg)) {
162
+ return false;
163
+ }
164
+ try {
165
+ const realTarget = fs.realpathSync(absPath);
166
+ const realPkgDir = fs.realpathSync(pkgDir);
167
+ const relReal = path.relative(realPkgDir, realTarget);
168
+ if (isEscapingRelativePath(relReal)) {
169
+ return false;
170
+ }
171
+ }
172
+ catch (err) {
173
+ if (!isError(err) || !('code' in err) || err.code !== 'ENOENT') {
174
+ throw err;
175
+ }
176
+ }
177
+ return true;
178
+ }
179
+ function mapToPackedPaths(pkgDir, files, packedDirs) {
180
+ const bundleDirs = Array.from(packedDirs, ([node, packed]) => ({
181
+ walked: path.relative(pkgDir, node.path).split(path.sep).join('/'),
182
+ packed,
183
+ })).sort((a, b) => b.walked.length - a.walked.length);
184
+ const result = new Map();
185
+ for (const file of files) {
186
+ const source = path.join(pkgDir, file);
187
+ const bundle = bundleDirs.find(({ walked }) => file.startsWith(`${walked}/`));
188
+ if (bundle == null) {
189
+ result.set(file, source);
190
+ continue;
191
+ }
192
+ for (const packedDir of bundle.packed) {
193
+ result.set(`${packedDir}${file.slice(bundle.walked.length)}`, source);
194
+ }
195
+ }
196
+ return result;
27
197
  }
28
- function buildRootTree(pkgDir, pkg) {
198
+ /**
199
+ * Mirrors npm-bundled: the root's bundled dependencies, then every
200
+ * dependency and optional dependency of a bundled package. Each dependency
201
+ * resolves the way Node resolves it at runtime, from the parent's real
202
+ * directory. The isolated linker keeps a package's dependencies next to its
203
+ * real directory rather than under the link, so the packed location is chosen
204
+ * separately by packedLocation().
205
+ */
206
+ function buildRootTree(pkgDir, pkg, boundary) {
29
207
  const bundledDeps = getRootBundledDeps(pkg);
30
208
  // npm-packlist's gatherBundles() iterates package.bundleDependencies directly,
31
209
  // so the field must be an array. Normalize true/undefined to an explicit list.
@@ -33,28 +211,97 @@ function buildRootTree(pkgDir, pkg) {
33
211
  normalizedPkg.bundleDependencies = bundledDeps;
34
212
  delete normalizedPkg.bundledDependencies;
35
213
  const root = makeNode(pkgDir, normalizedPkg, true);
36
- const seen = new Map([[pkgDir, root]]);
37
- populateEdges(root, bundledDeps, seen);
38
- return root;
39
- }
40
- function buildBundledTree(pkgDir, seen) {
41
- const cached = seen.get(pkgDir);
42
- if (cached)
43
- return cached;
44
- const pkg = readPackageJson(pkgDir);
45
- const node = makeNode(pkgDir, normalizePackage(pkg), false);
46
- seen.set(pkgDir, node);
47
- populateEdges(node, getNestedBundledDeps(pkg), seen);
48
- return node;
49
- }
50
- function populateEdges(node, deps, seen) {
51
- for (const dep of deps) {
52
- const depDir = resolveDependency(dep, node.path);
53
- if (!depDir)
214
+ const walk = {
215
+ pkgDir,
216
+ realPkgDir: fs.realpathSync(pkgDir),
217
+ boundary: realpathOrUndefined(boundary) ?? boundary,
218
+ rootDependencyNames: new Set(getNestedBundledDeps(pkg)),
219
+ slots: new Map(),
220
+ nodes: new Map(),
221
+ packedDirs: new Map(),
222
+ };
223
+ const queue = bundledDeps.map((name) => ({ name, parent: { packed: [], realDir: walk.realPkgDir, node: root }, depth: 0 }));
224
+ while (queue.length > 0) {
225
+ const task = queue.shift();
226
+ if (task.depth > MAX_BUNDLE_DEPTH)
227
+ continue;
228
+ const resolved = resolveDependency(task.name, task.parent.realDir, walk.boundary);
229
+ if (resolved == null)
230
+ continue;
231
+ const packed = packedLocation(walk, task.name, task.parent.packed, resolved.realDir);
232
+ if (packed == null)
54
233
  continue;
55
- const depNode = buildBundledTree(depDir, seen);
56
- node.edgesOut.set(dep, { to: depNode, peer: false, dev: false });
234
+ let node = walk.nodes.get(resolved.realDir);
235
+ if (node == null) {
236
+ const dir = isSubdir(walk.realPkgDir, resolved.dir) ? path.join(pkgDir, path.relative(walk.realPkgDir, resolved.dir)) : resolved.dir;
237
+ node = makeNode(dir, normalizePackage(readPackageJson(dir)), false);
238
+ walk.nodes.set(resolved.realDir, node);
239
+ walk.packedDirs.set(node, []);
240
+ }
241
+ task.parent.node.edgesOut.set(task.name, { to: node, peer: false, dev: false });
242
+ walk.packedDirs.get(node).push(packedDir(packed));
243
+ walk.slots.set(packed.join('\0'), resolved.realDir);
244
+ const placed = { packed, realDir: resolved.realDir, node };
245
+ for (const name of getNestedBundledDeps(node.package)) {
246
+ queue.push({ name, parent: placed, depth: task.depth + 1 });
247
+ }
248
+ }
249
+ return { tree: root, packedDirs: walk.packedDirs };
250
+ }
251
+ const MAX_BUNDLE_DEPTH = 32;
252
+ /**
253
+ * The packed location for a dependency required from the package packed at
254
+ * parent, so that Node resolves it from parent inside the tarball. Prefers the
255
+ * on-disk location when that already works, then the top-level node_modules,
256
+ * then parent's own node_modules. Undefined when a package at the same real
257
+ * directory is already visible from parent, which also ends dependency
258
+ * cycles, or when no location can make it visible.
259
+ */
260
+ function packedLocation(walk, name, parent, realDir) {
261
+ const visibleFreeSlots = [];
262
+ for (let depth = parent.length; depth >= 0; depth--) {
263
+ const slot = [...parent.slice(0, depth), name];
264
+ const occupant = walk.slots.get(slot.join('\0'));
265
+ if (occupant === realDir)
266
+ return undefined;
267
+ if (occupant != null)
268
+ break;
269
+ visibleFreeSlots.push(slot);
57
270
  }
271
+ const isVisibleFreeSlot = (slot) => visibleFreeSlots.some((free) => free.join('\0') === slot.join('\0'));
272
+ const onDisk = packedNames(walk.realPkgDir, realDir);
273
+ if (onDisk != null && isVisibleFreeSlot(onDisk))
274
+ return onDisk;
275
+ const mayHoist = parent.length === 0 || !walk.rootDependencyNames.has(name);
276
+ if (mayHoist && isVisibleFreeSlot([name]))
277
+ return [name];
278
+ return visibleFreeSlots[0];
279
+ }
280
+ function packedDir(packed) {
281
+ return packed.map((name) => `node_modules/${name}`).join('/');
282
+ }
283
+ function packedNames(pkgDir, dir) {
284
+ if (!isSubdir(pkgDir, dir) || pkgDir === dir)
285
+ return undefined;
286
+ const segments = path.relative(pkgDir, dir).split(path.sep);
287
+ const names = [];
288
+ while (segments.length > 0) {
289
+ if (segments.shift() !== 'node_modules')
290
+ return undefined;
291
+ const name = segments.shift();
292
+ if (name == null || name.startsWith('.'))
293
+ return undefined;
294
+ if (name.startsWith('@')) {
295
+ const scopedName = segments.shift();
296
+ if (scopedName == null)
297
+ return undefined;
298
+ names.push(`${name}/${scopedName}`);
299
+ }
300
+ else {
301
+ names.push(name);
302
+ }
303
+ }
304
+ return names;
58
305
  }
59
306
  function makeNode(pkgDir, pkg, isProjectRoot) {
60
307
  const node = {
@@ -81,32 +328,61 @@ function getNestedBundledDeps(pkg) {
81
328
  const optionalDependencies = (pkg.optionalDependencies ?? {});
82
329
  return [...Object.keys(dependencies), ...Object.keys(optionalDependencies)];
83
330
  }
84
- function resolveDependency(depName, fromDir) {
331
+ function resolveDependency(depName, fromDir, boundary) {
332
+ if (!isSafeBundleName(depName))
333
+ return undefined;
85
334
  let currentDir = fromDir;
86
335
  while (true) {
87
- const candidate = path.join(currentDir, 'node_modules', depName);
88
- try {
89
- const stat = fs.statSync(path.join(candidate, 'package.json'));
90
- if (stat.isFile())
91
- return candidate;
92
- }
93
- catch (err) {
94
- if (!util.types.isNativeError(err) || !('code' in err) || err.code !== 'ENOENT') {
95
- throw err;
336
+ if (path.basename(currentDir) !== 'node_modules') {
337
+ const candidate = path.join(currentDir, 'node_modules', depName);
338
+ try {
339
+ const stat = fs.statSync(path.join(candidate, 'package.json'));
340
+ if (stat.isFile()) {
341
+ const realDir = fs.realpathSync(candidate);
342
+ if (realDir !== boundary && !isSubdir(boundary, realDir))
343
+ return undefined;
344
+ return { dir: candidate, realDir };
345
+ }
346
+ }
347
+ catch (err) {
348
+ if (!isError(err) || !('code' in err) || err.code !== 'ENOENT') {
349
+ throw err;
350
+ }
96
351
  }
97
352
  }
353
+ if (currentDir === boundary)
354
+ return undefined;
98
355
  const parent = path.dirname(currentDir);
99
356
  if (parent === currentDir)
100
357
  return undefined;
101
358
  currentDir = parent;
102
359
  }
103
360
  }
361
+ // Bundle names come from package.json, so reject paths before joining them under node_modules.
362
+ function isSafeBundleName(name) {
363
+ if (typeof name !== 'string' || name.includes('\\'))
364
+ return false;
365
+ const parts = name.split('/');
366
+ if (parts.length > 2 || (parts.length === 2 && !parts[0].startsWith('@')))
367
+ return false;
368
+ return parts.every((part) => part !== '' && part !== '.' && part !== '..');
369
+ }
370
+ function realpathOrUndefined(dir) {
371
+ try {
372
+ return fs.realpathSync(dir);
373
+ }
374
+ catch (err) {
375
+ if (isError(err) && 'code' in err && err.code === 'ENOENT')
376
+ return undefined;
377
+ throw err;
378
+ }
379
+ }
104
380
  function readPackageJson(dir) {
105
381
  try {
106
382
  return JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8'));
107
383
  }
108
384
  catch (err) {
109
- if (util.types.isNativeError(err) && 'code' in err && err.code === 'ENOENT') {
385
+ if (isError(err) && 'code' in err && err.code === 'ENOENT') {
110
386
  return {};
111
387
  }
112
388
  throw err;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pnpm/fs.packlist",
3
- "version": "1100.0.4",
3
+ "version": "1100.0.6",
4
4
  "description": "Get a list of the files to add from a directory into an npm package",
5
5
  "keywords": [
6
6
  "pnpm",
@@ -27,11 +27,13 @@
27
27
  "!*.map"
28
28
  ],
29
29
  "dependencies": {
30
+ "@pnpm/error": "1100.2.1",
31
+ "@pnpm/workspace.project-manifest-reader": "1100.1.1",
30
32
  "is-subdir": "^2.0.0",
31
33
  "npm-packlist": "10.0.4"
32
34
  },
33
35
  "devDependencies": {
34
- "@pnpm/fs.packlist": "1100.0.4"
36
+ "@pnpm/fs.packlist": "1100.0.6"
35
37
  },
36
38
  "engines": {
37
39
  "node": ">=22.13"