@preventive/deptree 1.1.0 → 1.2.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@preventive/deptree",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "In-memory dependency tree, as a package manager installs it",
5
5
  "type": "module",
6
6
  "exports": {
package/pnpm.d.ts CHANGED
@@ -261,9 +261,10 @@ export interface PnpmProject {
261
261
  //
262
262
  // `lazy`, false unless given true, lays the tree out as sizeEstimateOnly
263
263
  // does, each package's directory made and left empty and each link made,
264
- // but fetches nothing, not even a version document: each package is
265
- // unpacked into it once asked for by the tree's `unpack`, and only then
266
- // fetched, as `metadata` and `cache` say. A package unpacked is what the
264
+ // but fetches nothing, not even a version document, but where the files
265
+ // could change the layout (below): each package is unpacked into it once
266
+ // asked for by the tree's `unpack`, and only then fetched, as `metadata`
267
+ // and `cache` say. A package unpacked is what the
267
268
  // full tree has at its path, file for file, patched and fixed as linking
268
269
  // bins fixes it, and held to every check of its tarball, package.json and
269
270
  // patch the full tree holds it to; one never asked for is never fetched, so
@@ -282,12 +283,24 @@ export interface PnpmProject {
282
283
  // the lockfile says (a project's dependencies with pnpm 9 and 10, their
283
284
  // peers with pnpm 11 and 12, and those beside a dependency outside the tree
284
285
  // with pnpm 9 to 11), and directories, which are read with the tree; with
285
- // the hoisted linker, every package. Any other is read as having no bins,
286
- // as the lockfile says: where a patch is configured and pnpm builds one,
287
- // the bins it links of its own are read once it is unpacked, which turn on
288
- // nothing else's. Where the files could change the layout, the lockfile is
289
- // refused as with sizeEstimateOnly. A `vfs` is not taken with it, nor
290
- // `sizeEstimateOnly`.
286
+ // the hoisted linker, those the lockfile says have bins, those that bundle
287
+ // others, and directories, and where a patch is configured, those patched
288
+ // and those that depend on one with bins, whose bins pnpm's build pass
289
+ // links where it builds one. Any other is read as having no bins, as the
290
+ // lockfile says, and with the hoisted linker as not built: where a patch is
291
+ // configured and pnpm builds one, what that does to it, and the bins it
292
+ // links of its own, are read once it is unpacked, which turn on nothing
293
+ // else's. The hoisted linker links the bins of each package it puts in a
294
+ // node_modules, whatever the lockfile says: one whose package.json's `bin`
295
+ // is empty text, false or 0 beside a directories.bin, which pnpm 10's
296
+ // lockfile says has none, has bins all the same, and the others' bins of
297
+ // their names in that node_modules are fixed as though it had none. Where
298
+ // the files could change the layout, with the hoisted linker of pnpm 10.21
299
+ // to before 11.25 where any patch is configured, a package of more than one
300
+ // copy, one with a node_modules, is fetched with the tree, held to its
301
+ // checks then, and the lockfile refused as the full tree refuses it where
302
+ // pnpm builds the package. A
303
+ // `vfs` is not taken with it, nor `sizeEstimateOnly`.
291
304
  //
292
305
  // The two ways the files come, one or the other: given, with `lockfile`
293
306
  // and `manifests`, `project` read only for directories; or read, with
package/src/pnpm/bins.js CHANGED
@@ -232,6 +232,23 @@ function linkCopies(builds, phases, fixed, contested, { keepsModules, major }) {
232
232
  }
233
233
  }
234
234
 
235
+ // The packages of `nodes` whose files hoistedBinTargets reads, with
236
+ // hoistedBuilds where any patch is configured (`building`), but those it
237
+ // reads as pnpm's hoisted linker reads each package in a node_modules it
238
+ // fills, which the lockfile says have no bins, and those it reads for their
239
+ // own build alone: building one none of whose dependencies have bins, as
240
+ // `packages`, the lockfile's, say, links nothing of the others'. The others
241
+ // are read as having no bins, and as built where patched alone (lazy.js).
242
+ export function hoistedBinReads({ nodes, building, packages }) {
243
+ const reads = new Set()
244
+ for (const node of nodes.values()) {
245
+ const { hasBin, resolution, bundledDependencies, patchHash, dependencies, optionalDependencies } = node.pkg
246
+ if (hasBin || resolution.type === 'directory' || bundledDependencies !== undefined) reads.add(node.dir)
247
+ else if (building && (patchHash !== undefined || Object.values({ ...dependencies, ...optionalDependencies }).some((key) => packages[key]?.hasBin))) reads.add(node.dir)
248
+ }
249
+ return reads
250
+ }
251
+
235
252
  // pnpm's hoisted linker links the bins of each node_modules it fills, by
236
253
  // what it reads there, as it fills it; then each project's again, with its
237
254
  // links in, a command of its own dependencies over any other. `modules` are
@@ -202,20 +202,29 @@ export function hoistedBuilds(byDir, projects, builds, { hardlinks, keepsModules
202
202
  return built
203
203
  }
204
204
 
205
- // Where hoistedBuilds is not followed, as for a tree's layout alone, where
206
- // any patch is configured, from pnpm 10.21 to before 11.25: pnpm builds one
207
- // copy of each package patched or with an install script, which only its
208
- // tarball tells, and puts hardlinks of it in place of the others, dropping
209
- // their node_modules. A package of more than one copy, one with a
210
- // node_modules, may lose it. `placed` are the copies, as hoistedLayout
211
- // places them; `option` the one the tree is laid out with, which does not
212
- // know it: sizeEstimateOnly or lazy.
213
- export function checkNoneDropped(placed, pnpm, option = 'sizeEstimateOnly') {
205
+ // Where any patch is configured, from pnpm 10.21 to before 11.25, pnpm
206
+ // builds one copy of each package patched or with an install script, which
207
+ // only its tarball tells, and puts hardlinks of it in place of the others,
208
+ // dropping their node_modules (hoistedBuilds): the packages of more than one
209
+ // copy, one with a node_modules, which may lose it, by key, each with that
210
+ // copy. `placed` are the copies, as hoistedLayout places them.
211
+ export function mayDrop(placed) {
214
212
  const placedIn = new Set(placed.values().map((node) => node.modules))
213
+ const dropping = new Map()
215
214
  for (const [key, copies] of Map.groupBy(placed.values(), (node) => node.key)) {
216
215
  const nested = copies.length > 1 ? copies.find((copy) => placedIn.has(`${copy.dir}/node_modules`)) : undefined
217
- if (nested !== undefined) throw new DeptreeError(`pnpm ${pnpm} builds one of its copies where any patch is configured, if it is patched or has an install script, and makes the others hardlinks of it, dropping their node_modules: ${quote(`${nested.dir}/node_modules`)} may be dropped, which with ${option} is not known, and is not supported`, quote(key))
216
+ if (nested !== undefined) dropping.set(key, nested)
218
217
  }
218
+ return dropping
219
+ }
220
+
221
+ // For a tree's layout alone, which fetches nothing, any of `dropping` is
222
+ // refused, as which pnpm builds is not known.
223
+ export function checkNoneDropped(dropping, pnpm) {
224
+ const [first] = dropping
225
+ if (first === undefined) return
226
+ const [key, nested] = first
227
+ throw new DeptreeError(`pnpm ${pnpm} builds one of its copies where any patch is configured, if it is patched or has an install script, and makes the others hardlinks of it, dropping their node_modules: ${quote(`${nested.dir}/node_modules`)} may be dropped, which with sizeEstimateOnly is not known, and is not supported`, quote(key))
219
228
  }
220
229
 
221
230
  // What linkBins reads to link bins (bins.js): each node_modules pnpm fills,
package/src/pnpm/lazy.js CHANGED
@@ -68,8 +68,9 @@ function filesIn(vfs, dir, dirs) {
68
68
  // is bound once unpacked has the fixes read again, with its own files. A
69
69
  // package's files are let go once it is unpacked, and its tarball once each
70
70
  // of its snapshots is, but a patched one's, and every one's once the fixes
71
- // are read.
72
- export function lazyUnpacker({ vfs, byDir, stats, fixes, reads, builds, checks, patched, composing, entryOf, fetching, folded }) {
71
+ // are read. `early.nodes` are fetched before anything is unpacked, and held
72
+ // to `early.check()`.
73
+ export async function lazyUnpacker({ vfs, byDir, stats, fixes, reads, builds, early, checks, patched, composing, entryOf, fetching, folded }) {
73
74
  const { major } = composing
74
75
  const limit = limited()
75
76
  const fetched = new Map()
@@ -129,6 +130,10 @@ export function lazyUnpacker({ vfs, byDir, stats, fixes, reads, builds, checks,
129
130
  if (made === undefined && node.pkg.resolution.type !== 'directory' && node.pkg.patchHash === undefined) Object.assign(node, { files: undefined, unpacked: true })
130
131
  }
131
132
 
133
+ if (early !== undefined) {
134
+ await eachConcurrently(early.nodes, prepare, (node) => quote(node.key))
135
+ early.check()
136
+ }
132
137
  return unpacker(new Map([...byDir.values()].map((node) => [node.dir, node])), async (node) => {
133
138
  await prepare(node)
134
139
  const { targets, executable } = bound(node, major, builds) ? await fixesFor(node) : { targets: new Map(), executable: new Map() }
package/src/pnpm/tree.js CHANGED
@@ -16,10 +16,10 @@ import { DeptreeError, quote, refusalOf } from '../error.js'
16
16
  import { checkNoModules, fold, makeDirs, mount, writeLink } from '../mount.js'
17
17
  import { checkProject } from '../project.js'
18
18
  import { NO_ABOUT, REGISTRY, fetchSize, fetchingOf, tarballUrl } from '../tarball.js'
19
- import { binReads, binTargets, hoistedBinTargets, requiresBuild } from './bins.js'
19
+ import { binReads, binTargets, hoistedBinReads, hoistedBinTargets, requiresBuild } from './bins.js'
20
20
  import { buildGraph } from './graph.js'
21
21
  import { hoist } from './hoist.js'
22
- import { checkNoneDropped, hoistedBuilds, hoistedLayout, hoistedModules, packageIdOf, prodPackages, workspaceHoists } from './hoisted.js'
22
+ import { checkNoneDropped, hoistedBuilds, hoistedLayout, hoistedModules, mayDrop, packageIdOf, prodPackages, workspaceHoists } from './hoisted.js'
23
23
  import { checkLocalOverrides, createFreshnessCheck, readDirectoryPackage, readLinked } from './local.js'
24
24
  import { createPatchedCheck, skippedSnapshots } from './install.js'
25
25
  import { lazyUnpacker } from './lazy.js'
@@ -182,7 +182,8 @@ const COPYING = new Set(['copy', 'clone', 'clone-or-copy'])
182
182
  // bins does to the files, with what pnpm's build pass does to them, read once
183
183
  // every package's files are at hand; with `sizeEstimateOnly` they are not
184
184
  // followed, nor with `lazy` but where a package unpacked needs them. `builds`
185
- // tells, where any patch is configured, what the build pass builds.
185
+ // tells, where any patch is configured, what the build pass builds; `reads`
186
+ // the packages the fixes read the files of (hoistedBinReads).
186
187
  async function buildHoisted({ lockfile, settings, host, project, hook, installed, manifests, names, patched, skipped, fetching }) {
187
188
  const { major } = host
188
189
  const since = (version) => compareVersions(host.pnpm, version) >= 0
@@ -197,20 +198,24 @@ async function buildHoisted({ lockfile, settings, host, project, hook, installed
197
198
  const building = Object.keys(settings.patchedDependencies ?? {}).length > 0
198
199
  const hardlinks = since('10.21.0')
199
200
  const keepsModules = since('11.25.0')
200
- if ((fetching.sizeEstimateOnly || fetching.lazy) && building && hardlinks && !keepsModules) checkNoneDropped(placed, host.pnpm, fetching.lazy ? 'lazy' : 'sizeEstimateOnly')
201
+ const dropping = building && hardlinks && !keepsModules ? mayDrop(placed) : new Map()
202
+ if (fetching.sizeEstimateOnly) checkNoneDropped(dropping, host.pnpm)
201
203
  const { byDir, tarballs, checks } = await fetchNodes(placed, lockfile, hook, project, host, fetching)
202
204
  const linked = readLinked(links, byDir, manifests, project)
203
205
  const prod = prodPackages(lockfile, skipped, directories)
204
206
  const dev = (node) => !prod.has(packageIdOf(node.key, node.pkg, directories))
205
207
  const builds = building ? (node) => node.pkg.patchHash !== undefined || requiresBuild(node.manifest, node.files, major) : undefined
208
+ const idOf = since('11.23.0') ? (key) => packageIdOf(key, lockfile.packages[key], directories) : (key) => key
209
+ const changesOf = (node) => parsedPatch(patched, node.pkg.patchHash).parsed
210
+ const builtOf = (built) => hoistedBuilds(byDir, new Set(projects), built, { hardlinks, keepsModules, idOf, changesOf, pnpm: host.pnpm })
206
211
  const fixes = () => {
207
- const idOf = since('11.23.0') ? (key) => packageIdOf(key, lockfile.packages[key], directories) : (key) => key
208
- const changesOf = (node) => parsedPatch(patched, node.pkg.patchHash).parsed
209
- const built = building ? hoistedBuilds(byDir, new Set(projects), builds, { hardlinks, keepsModules, idOf, changesOf, pnpm: host.pnpm }) : []
212
+ const built = building ? builtOf(builds) : []
210
213
  const targets = hoistedBinTargets({ nodes: byDir, projects: new Map([...manifests, ...linked]), modules: hoistedModules(byDir, links, projects, manifests, lockfile), builds: built, hardlinks, keepsModules, idOf, major })
211
214
  return { targets, executable: new Map() }
212
215
  }
213
- return { byDir, links, fixes, builds, tarballs, dev, checks }
216
+ // With lazy, those that may drop a node_modules are fetched with the tree.
217
+ const early = dropping.size === 0 ? undefined : { nodes: [...byDir.values()].filter((node) => dropping.has(node.key)), check: () => builtOf((node) => dropping.has(node.key) && builds(node)) }
218
+ return { byDir, links, fixes, reads: () => hoistedBinReads({ nodes: byDir, building, packages: lockfile.packages }), builds, early, tarballs, dev, checks }
214
219
  }
215
220
 
216
221
  // The isolated layout: each package in node_modules/.pnpm, linked from what
@@ -280,7 +285,7 @@ export async function buildPnpmTree(options) {
280
285
  const names = settings.hoistWorkspacePackages ? workspaceNames(manifests) : new Map()
281
286
  const { skipped, incompatible } = skippedSnapshots(lockfile, { host, settings })
282
287
  const hoisted = settings.nodeLinker === 'hoisted'
283
- const { byDir, links, fixes, reads, builds, tarballs, dev, checks } = await (hoisted ? buildHoisted : buildIsolated)({ lockfile, settings, host, project, hook, installed, manifests, names, patched, skipped, fetching })
288
+ const { byDir, links, fixes, reads, builds, early, tarballs, dev, checks } = await (hoisted ? buildHoisted : buildIsolated)({ lockfile, settings, host, project, hook, installed, manifests, names, patched, skipped, fetching })
284
289
  const laidOut = fetching.sizeEstimateOnly || fetching.lazy
285
290
  const { targets, executable } = laidOut ? { targets: new Map(), executable: new Map() } : fixes()
286
291
 
@@ -313,5 +318,5 @@ export async function buildPnpmTree(options) {
313
318
  const tree = mount(vfs, into, folded)
314
319
  if (!fetching.lazy) return { vfs: tree, stats, installed: listed }
315
320
  const entryOf = (node) => installedOf(node, dev(node), patched, false)
316
- return { vfs: tree, stats, installed: listed, unpack: lazyUnpacker({ vfs: tree, byDir, stats, fixes, reads, builds, checks, patched, composing, entryOf, fetching, folded }) }
321
+ return { vfs: tree, stats, installed: listed, unpack: await lazyUnpacker({ vfs: tree, byDir, stats, fixes, reads, builds, early, checks, patched, composing, entryOf, fetching, folded }) }
317
322
  }