@preventive/deptree 1.0.2 → 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.0.2",
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
@@ -275,11 +276,31 @@ export interface PnpmProject {
275
276
  // on; or any bin of a patched package. With the hoisted linker where any
276
277
  // patch is configured, so does what pnpm's build pass does to a package it
277
278
  // builds, patched or with an install script, which is read for every such
278
- // package. The first such unpack fetches every package not unpacked yet,
279
- // reading those that are back from the tree, as the full tree does, and
280
- // keeps them all. Where the files could change the layout, the lockfile is
281
- // refused as with sizeEstimateOnly. A `vfs` is not taken with it, nor
282
- // `sizeEstimateOnly`.
279
+ // package. The first such unpack fetches the packages whose bins those are
280
+ // read from, but those unpacked already, which are read back from the
281
+ // tree, and keeps them: with the isolated linker, those the lockfile says
282
+ // have bins, those that bundle others, those whose bins pnpm reads whatever
283
+ // the lockfile says (a project's dependencies with pnpm 9 and 10, their
284
+ // peers with pnpm 11 and 12, and those beside a dependency outside the tree
285
+ // with pnpm 9 to 11), and directories, which are read with the tree; with
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`.
283
304
  //
284
305
  // The two ways the files come, one or the other: given, with `lockfile`
285
306
  // and `manifests`, `project` read only for directories; or read, with
package/src/pnpm/bins.js CHANGED
@@ -157,6 +157,34 @@ export function binTargets({ nodes, projects, direct, links, publicHoist, buildi
157
157
  return fixedFiles(nodes, fixed, contested, major)
158
158
  }
159
159
 
160
+ // The packages of `nodes` whose files binTargets reads, given the same
161
+ // options, but those it reads for their own build alone, which the lockfile
162
+ // says have no bins: building one whose children are all packages links
163
+ // again what their first link did, and fixes nothing of theirs. The others
164
+ // are read as having no bins (lazy.js).
165
+ export function binReads({ nodes, projects, direct, links, publicHoist, peers, major }) {
166
+ const mayHaveBin = (dir) => nodes.get(dir)?.pkg.hasBin === true || nodes.get(dir)?.pkg.resolution.type === 'directory'
167
+ const reads = new Set()
168
+ for (const node of nodes.values()) {
169
+ if (mayHaveBin(node.dir) || node.pkg.bundledDependencies !== undefined) reads.add(node.dir)
170
+ const children = [...node.children.values()]
171
+ if (major < 12 && !children.every((dir) => nodes.has(dir))) for (const dir of [node.dir, ...children]) reads.add(dir)
172
+ }
173
+ for (const [id, children] of direct) {
174
+ if (major >= 11 && peers) for (const dir of peersOf(children, nodes)) reads.add(dir)
175
+ if (id === '.' && publicHoist) {
176
+ const manifest = projects.get('.')
177
+ const own = new Set(Object.keys({ ...manifest.devDependencies, ...manifest.dependencies, ...manifest.optionalDependencies }))
178
+ for (const [path, dir] of links) {
179
+ if (/^node_modules\/(?:@[^/]+\/)?[^/@.][^/]*$/u.test(path) && (major < 12 || own.has(path.slice('node_modules/'.length)))) reads.add(dir)
180
+ }
181
+ } else if (major < 11) {
182
+ for (const dir of children.values()) reads.add(dir)
183
+ }
184
+ }
185
+ return new Set([...reads].filter((dir) => nodes.has(dir)))
186
+ }
187
+
160
188
  // The copies of one package pnpm's build pass makes hardlinks of the one it
161
189
  // builds (hoisted.js): what is fixed in that one before, as each copy's own
162
190
  // bins are where pnpm fills its node_modules, is in all; and so is what is
@@ -204,6 +232,23 @@ function linkCopies(builds, phases, fixed, contested, { keepsModules, major }) {
204
232
  }
205
233
  }
206
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
+
207
252
  // pnpm's hoisted linker links the bins of each node_modules it fills, by
208
253
  // what it reads there, as it fills it; then each project's again, with its
209
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
@@ -61,12 +61,16 @@ function filesIn(vfs, dir, dirs) {
61
61
  // full tree writes it, `patched` and `composing` being what compose takes, and
62
62
  // listed by `entryOf`. What linking bins fixes of it turns on the other
63
63
  // packages' bins only where a package is bound (above): the first such
64
- // unpack fetches every package not yet unpacked, and reads them all by
65
- // `fixes`, as the full tree does: one unpacked before is read back from the
66
- // tree, where it is as it came. A package's files are let go once it is
67
- // unpacked, and its tarball once each of its snapshots is, but a patched
68
- // one's, and every one's once the fixes are read.
69
- export function lazyUnpacker({ vfs, byDir, stats, fixes, builds, checks, patched, composing, entryOf, fetching, folded }) {
64
+ // unpack fetches those of `reads()` not unpacked yet, every package where it
65
+ // is not given, and reads the fixes by `fixes`, as the full tree does, one
66
+ // unpacked before read back from the tree, where it is as it came, and one
67
+ // not at hand read as having no bins, as the lockfile says. One read so that
68
+ // is bound once unpacked has the fixes read again, with its own files. A
69
+ // package's files are let go once it is unpacked, and its tarball once each
70
+ // of its snapshots is, but a patched one's, and every one's once the fixes
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 }) {
70
74
  const { major } = composing
71
75
  const limit = limited()
72
76
  const fetched = new Map()
@@ -99,11 +103,24 @@ export function lazyUnpacker({ vfs, byDir, stats, fixes, builds, checks, patched
99
103
  checkFetched(node, got, checks)
100
104
  Object.assign(node, { files: got.files, manifest: got.manifest, about: got.about ?? NO_ABOUT })
101
105
  }
102
- const allFixes = () => {
106
+ // The fixes, with the packages not at hand read as having no bins.
107
+ const readFixes = () => {
108
+ const stubs = [...byDir.values()].filter((node) => node.files === undefined)
109
+ const manifests = stubs.map((node) => node.manifest)
110
+ for (const node of stubs) Object.assign(node, { files: new Map(), manifest: {} })
111
+ try {
112
+ return { ...fixes(), stubbed: new Set(stubs.map((node) => node.dir)) }
113
+ } finally {
114
+ for (const [i, node] of stubs.entries()) Object.assign(node, { files: undefined, manifest: manifests[i] })
115
+ }
116
+ }
117
+ const fixesFor = async (node) => {
103
118
  made ??= (async () => {
104
- await eachConcurrently(byDir.values(), prepare, (node) => quote(node.key))
105
- return fixes()
119
+ const read = reads?.()
120
+ await eachConcurrently([...byDir.values()].filter((other) => read?.has(other.dir) ?? true), prepare, (other) => quote(other.key))
121
+ return readFixes()
106
122
  })()
123
+ if ((await made).stubbed.has(node.dir)) made = Promise.try(readFixes)
107
124
  return made
108
125
  }
109
126
  const release = (node) => {
@@ -113,9 +130,13 @@ export function lazyUnpacker({ vfs, byDir, stats, fixes, builds, checks, patched
113
130
  if (made === undefined && node.pkg.resolution.type !== 'directory' && node.pkg.patchHash === undefined) Object.assign(node, { files: undefined, unpacked: true })
114
131
  }
115
132
 
133
+ if (early !== undefined) {
134
+ await eachConcurrently(early.nodes, prepare, (node) => quote(node.key))
135
+ early.check()
136
+ }
116
137
  return unpacker(new Map([...byDir.values()].map((node) => [node.dir, node])), async (node) => {
117
138
  await prepare(node)
118
- const { targets, executable } = bound(node, major, builds) ? await allFixes() : { targets: new Map(), executable: new Map() }
139
+ const { targets, executable } = bound(node, major, builds) ? await fixesFor(node) : { targets: new Map(), executable: new Map() }
119
140
  const tree = new Vfs()
120
141
  const written = { files: 0, bytes: 0 }
121
142
  try {
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 { 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,24 +198,29 @@ 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
217
- // needs it and where it is hoisted; `fixes` as buildHoisted's.
222
+ // needs it and where it is hoisted; `fixes` as buildHoisted's, and `reads`
223
+ // the packages they read the files of (binReads).
218
224
  async function buildIsolated({ lockfile, settings, host, project, hook, installed, manifests, names, skipped, fetching }) {
219
225
  const { major } = host
220
226
  const { nodes, direct, hoisting } = await buildGraph(lockfile, skipped, settings.virtualStoreDirMaxLength, major)
@@ -224,20 +230,21 @@ async function buildIsolated({ lockfile, settings, host, project, hook, installe
224
230
  const linked = readLinked(links, byDir, manifests, project)
225
231
  const prod = reachedInProd(lockfile.importers, nodes, byDir)
226
232
  const dev = (node) => !prod.has(node.dir)
233
+ const linking = {
234
+ nodes: byDir,
235
+ projects: new Map([...manifests, ...linked]),
236
+ direct,
237
+ links,
238
+ publicHoist: settings.publicHoistPattern?.length > 0,
239
+ building: Object.keys(settings.patchedDependencies ?? {}).length > 0,
240
+ peers: settings.autoInstallPeers,
241
+ major,
242
+ }
227
243
  const fixes = () => {
228
- const targets = binTargets({
229
- nodes: byDir,
230
- projects: new Map([...manifests, ...linked]),
231
- direct,
232
- links,
233
- publicHoist: settings.publicHoistPattern?.length > 0,
234
- building: Object.keys(settings.patchedDependencies ?? {}).length > 0,
235
- peers: settings.autoInstallPeers,
236
- major,
237
- })
244
+ const targets = binTargets(linking)
238
245
  return { targets, executable: executableElsewhere(byDir, targets, settings.packageImportMethod, major) }
239
246
  }
240
- return { byDir, links, fixes, tarballs, dev, checks }
247
+ return { byDir, links, fixes, reads: () => binReads(linking), tarballs, dev, checks }
241
248
  }
242
249
 
243
250
  export async function buildPnpmTree(options) {
@@ -278,7 +285,7 @@ export async function buildPnpmTree(options) {
278
285
  const names = settings.hoistWorkspacePackages ? workspaceNames(manifests) : new Map()
279
286
  const { skipped, incompatible } = skippedSnapshots(lockfile, { host, settings })
280
287
  const hoisted = settings.nodeLinker === 'hoisted'
281
- const { byDir, links, fixes, 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 })
282
289
  const laidOut = fetching.sizeEstimateOnly || fetching.lazy
283
290
  const { targets, executable } = laidOut ? { targets: new Map(), executable: new Map() } : fixes()
284
291
 
@@ -311,5 +318,5 @@ export async function buildPnpmTree(options) {
311
318
  const tree = mount(vfs, into, folded)
312
319
  if (!fetching.lazy) return { vfs: tree, stats, installed: listed }
313
320
  const entryOf = (node) => installedOf(node, dev(node), patched, false)
314
- return { vfs: tree, stats, installed: listed, unpack: lazyUnpacker({ vfs: tree, byDir, stats, fixes, 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 }) }
315
322
  }