@preventive/deptree 1.0.0-alpha.0 → 1.0.0-alpha.10

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.
Files changed (89) hide show
  1. package/LICENSE-APACHE +202 -0
  2. package/bin/compare.js +132 -0
  3. package/bin/deptree.js +211 -0
  4. package/bin/npmrc.js +21 -0
  5. package/cargo.d.ts +223 -0
  6. package/cargo.js +6 -0
  7. package/npm.d.ts +321 -0
  8. package/npm.js +7 -0
  9. package/package.json +93 -11
  10. package/pnpm.d.ts +422 -60
  11. package/pnpm.js +3 -8
  12. package/soldeer.d.ts +235 -0
  13. package/soldeer.js +6 -0
  14. package/src/cargo/crate.js +183 -0
  15. package/src/cargo/inputs.js +81 -0
  16. package/src/cargo/members.js +90 -0
  17. package/src/cargo/registry.js +65 -0
  18. package/src/cargo/tree.js +125 -0
  19. package/src/cargo/workspace.js +177 -0
  20. package/src/concurrent.js +32 -0
  21. package/src/error.js +8 -10
  22. package/src/glob.js +55 -0
  23. package/src/hash.js +51 -16
  24. package/src/manifest.js +52 -0
  25. package/src/matcher.js +60 -10
  26. package/src/mount.js +128 -39
  27. package/src/npm/bins.js +54 -0
  28. package/src/npm/compat.js +97 -0
  29. package/src/npm/dev-engines.js +52 -0
  30. package/src/npm/graph.js +68 -0
  31. package/src/npm/inputs.js +75 -0
  32. package/src/npm/manifests.js +130 -0
  33. package/src/npm/package.js +57 -0
  34. package/src/npm/ratio.js +34 -0
  35. package/src/npm/settings.js +67 -0
  36. package/src/npm/tree.js +139 -0
  37. package/src/npm/versions.js +20 -0
  38. package/src/npmrc.js +24 -0
  39. package/src/patch.js +26 -45
  40. package/src/pnpm/bins.js +207 -327
  41. package/src/pnpm/checks.js +8 -35
  42. package/src/pnpm/commands.js +230 -0
  43. package/src/pnpm/filename.js +19 -16
  44. package/src/pnpm/frozen.js +47 -81
  45. package/src/pnpm/gitignore.js +138 -0
  46. package/src/pnpm/glob.js +65 -0
  47. package/src/pnpm/graph.js +26 -34
  48. package/src/pnpm/hoist.js +97 -59
  49. package/src/pnpm/hoisted.js +234 -0
  50. package/src/pnpm/hook.js +37 -51
  51. package/src/pnpm/inputs.js +177 -0
  52. package/src/pnpm/install.js +140 -130
  53. package/src/pnpm/local.js +105 -34
  54. package/src/pnpm/minimatch.js +319 -0
  55. package/src/pnpm/nm-hoist-graph.js +299 -0
  56. package/src/pnpm/nm-hoist.js +149 -0
  57. package/src/pnpm/npm-packlist.js +217 -0
  58. package/src/pnpm/overrides.js +20 -53
  59. package/src/pnpm/package.js +123 -0
  60. package/src/pnpm/packlist.js +194 -0
  61. package/src/pnpm/patches.js +21 -28
  62. package/src/pnpm/projects.js +150 -101
  63. package/src/pnpm/readers.js +122 -77
  64. package/src/pnpm/settings.js +85 -93
  65. package/src/pnpm/tree.js +234 -188
  66. package/src/pnpm/uptodate.js +17 -29
  67. package/src/pnpm/workspace.js +210 -53
  68. package/src/project.js +56 -0
  69. package/src/soldeer/config.js +63 -0
  70. package/src/soldeer/git.js +52 -0
  71. package/src/soldeer/inputs.js +30 -0
  72. package/src/soldeer/tree.js +83 -0
  73. package/src/soldeer/zip.js +121 -0
  74. package/src/tarball.js +123 -125
  75. package/src/yarn1/bins.js +28 -0
  76. package/src/yarn1/compat.js +67 -0
  77. package/src/yarn1/hoist.js +249 -0
  78. package/src/yarn1/inputs.js +83 -0
  79. package/src/yarn1/manifest.js +64 -0
  80. package/src/yarn1/package.js +206 -0
  81. package/src/yarn1/peers.js +51 -0
  82. package/src/yarn1/requests.js +74 -0
  83. package/src/yarn1/resolve.js +289 -0
  84. package/src/yarn1/settings.js +95 -0
  85. package/src/yarn1/tree.js +219 -0
  86. package/yarn1.d.ts +368 -0
  87. package/yarn1.js +7 -0
  88. package/src/pnpm/npmrc.js +0 -31
  89. /package/{LICENSE → LICENSE-MIT} +0 -0
@@ -1,33 +1,15 @@
1
- // Which snapshots a pnpm 10 install from a frozen lockfile leaves out: an
2
- // optional one that the host cannot run — its `os`, `cpu` or `libc` do not
3
- // take the host's, or its `engines.node` does not take the Node pnpm runs
4
- // on — and an optional one first reached, depth first from the projects,
5
- // through a snapshot that is not installed. That first reach decides it,
6
- // as it does for pnpm (@pnpm/lockfile.filtering's pkgAllDeps, then
7
- // @pnpm/deps.graph-builder checking every snapshot again), so a snapshot
8
- // reached both ways is left out or not by which way is walked first.
9
- // Anything else incompatible is installed, as pnpm installs it with a
10
- // warning, or refused where `engineStrict` has pnpm refuse it.
11
- //
12
- // The projects are walked in the lockfile's order, which is the order of
13
- // their directories; pnpm walks them in the order it finds them on disk,
14
- // which only a snapshot reached both ways as above can tell apart.
15
- //
16
- // pnpm 11 walks breadth first, and by the kind of each edge rather than of
17
- // each snapshot: an optional dependency the host cannot run is not taken,
18
- // and what a taken package requires is taken whether the host can run it
19
- // or not, with a warning; each reachable snapshot not taken is left out.
20
- // Where a package of an optional edge names no os, cpu or libc, pnpm 11
21
- // infers them from its name, as `@nx/nx-win32-arm64-msvc` names win32.
22
-
23
- import { satisfies } from '@preventive/upstream/semver.js'
1
+ // Which snapshots a frozen install leaves out. pnpm 9 and 10 walk depth first
2
+ // (@pnpm/lockfile.filtering's pkgAllDeps) and leave out an optional one the
3
+ // host cannot run or first reached through one not installed: the first
4
+ // reach decides. Projects go in lockfile order, pnpm's being the order it
5
+ // finds them on disk, which only a snapshot reached both ways can tell.
6
+
7
+ import { satisfies, validRange } from '@preventive/upstream/semver.js'
24
8
  import { DeptreeError, quote } from '../error.js'
25
9
 
26
- // pnpm's checkList: `any` alone is anything, `!x` excludes, and a list of
27
- // exclusions alone takes what none of them names. pnpm 10 counts the
28
- // exclusions over every value the host is taken to be, so a list of them
29
- // takes nothing where the host is taken to be more than one thing; pnpm 11
30
- // does not.
10
+ // pnpm's checkList. pnpm 10 counts the exclusions over every value the host
11
+ // is taken to be, so a list of them alone takes nothing where there is more
12
+ // than one. pnpm 12 takes, of each value, the first item that names it.
31
13
  function checkList(values, list, major) {
32
14
  if (list.length === 1 && list[0] === 'any') return true
33
15
  let match = false
@@ -37,14 +19,16 @@ function checkList(values, list, major) {
37
19
  if (item.startsWith('!')) {
38
20
  if (item.slice(1) === value) return false
39
21
  excluded++
40
- } else if (item === value) match = true
22
+ } else if (item === value) {
23
+ match = true
24
+ if (major >= 12) break
25
+ }
41
26
  }
42
27
  }
43
28
  return match || (major >= 11 ? list.every((item) => item.startsWith('!')) : excluded === list.length)
44
29
  }
45
30
 
46
- // pnpm 11's inferPlatformFromPackageName: the os, cpu and libc the words
47
- // of a package's name, less its scope, say it is for.
31
+ // pnpm 11's inferPlatformFromPackageName, from the unscoped name's words.
48
32
  const OS_WORDS = { __proto__: null, aix: 'aix', android: 'android', darwin: 'darwin', macos: 'darwin', osx: 'darwin', freebsd: 'freebsd', linux: 'linux', netbsd: 'netbsd', openbsd: 'openbsd', openharmony: 'openharmony', sunos: 'sunos', win32: 'win32', windows: 'win32' }
49
33
  const CPU_WORDS = { __proto__: null, arm: 'arm', armv6: 'arm', armv7: 'arm', arm64: 'arm64', aarch64: 'arm64', ia32: 'ia32', loong64: 'loong64', mips64el: 'mips64el', ppc64: 'ppc64', ppc64le: 'ppc64', riscv64: 'riscv64', s390x: 's390x', x64: 'x64', amd64: 'x64', wasm32: 'wasm32' }
50
34
  const LIBC_WORDS = { __proto__: null, glibc: 'glibc', gnu: 'glibc', gnueabihf: 'glibc', musl: 'musl', musleabihf: 'musl' }
@@ -66,73 +50,94 @@ function effectivePlatform(pkg) {
66
50
  return { ...pkg, os: pkg.os ?? inferred.os, cpu: pkg.cpu ?? inferred.cpu, libc: pkg.libc ?? inferred.libc }
67
51
  }
68
52
 
69
- // What the host is taken to be: itself, or what `supportedArchitectures`
70
- // lists for each, `current` standing for itself.
71
53
  const current = (value, supported) => (supported ?? ['current']).map((item) => (item === 'current' ? value : item))
72
54
 
73
- const takesPlatform = (pkg, host, supported, major = 10) => checkList(current(host.os, supported?.os), pkg.os ?? ['any'], major)
74
- && checkList(current(host.cpu, supported?.cpu), pkg.cpu ?? ['any'], major)
75
- && (host.libc === 'unknown' || checkList(current(host.libc, supported?.libc), pkg.libc ?? ['any'], major))
55
+ const takesPlatform = (pkg, host, supported) => ['os', 'cpu', 'libc'].every((key) => (key === 'libc' && host.libc === 'unknown')
56
+ || checkList(current(host[key], supported?.[key]), pkg[key] ?? ['any'], host.major))
76
57
 
77
58
  const takesEngine = (engines, node) => !engines.node || satisfies(node, engines.node, { includePrerelease: true })
78
59
 
79
- // A package.json's os, cpu or libc as pnpm's checkList reads one: a string
80
- // is a list of it, and what is not a string in a list is passed over.
60
+ // pnpm 12 drops what of a range it cannot parse, where npm's semver takes
61
+ // none of it, and reads a `-` range with an `x` as taking nothing.
62
+ function checkSure12(range, where, what = 'its engines.node') {
63
+ if (typeof range !== 'string' || (validRange(range) !== null && !(/\s-\s/u.test(range) && /[*xX]/u.test(range)))) return
64
+ throw new DeptreeError(`${what}, ${quote(range)}, pnpm 12 reads otherwise than npm's semver, which is not supported`, where)
65
+ }
66
+
67
+ const nodeOf = ({ host, settings }) => settings.nodeVersion ?? settings.runtimeNodeVersion ?? host.node
68
+
69
+ // With engineStrict, pnpm 11 checks a patched snapshot's engines from its
70
+ // package.json, once patched, rather than from the lockfile.
71
+ const patchedLater = ({ host, settings }) => host.major >= 11 && settings.engineStrict === true
72
+
73
+ // A package.json's os, cpu or libc as pnpm's checkList reads one.
81
74
  function platformList(value, where) {
82
- if (value === undefined || value === null) return undefined
75
+ if (value == null) return undefined
83
76
  if (typeof value === 'string') return [value]
84
77
  if (Array.isArray(value)) return value.filter((item) => typeof item === 'string')
85
78
  throw new DeptreeError('expected a string or a list of them', where)
86
79
  }
87
80
 
88
- // A project as pnpm's packageIsInstallable holds it when it reads its
89
- // package.json: a platform the host is not only warns, and so does an
90
- // engines.node it does not take, unless engineStrict; an engines.pnpm it
91
- // does not take is always refused. As pnpm has it, a platform that does
92
- // not match is found first, and the engines are then not looked at.
93
- export function checkProject(manifest, where, { host, settings }) {
81
+ function checkRoot12(manifest, where, context) {
82
+ const range = manifest.engines?.node
83
+ if (!context.settings.engineStrict || typeof range !== 'string' || range === '') return
84
+ const node = nodeOf(context)
85
+ checkSure12(range, where)
86
+ if (!takesEngine({ node: range }, node)) throw new DeptreeError(`its engines.node, ${quote(range)}, does not take Node ${node}, which engineStrict refuses`, where)
87
+ }
88
+
89
+ // As pnpm's packageIsInstallable: a platform the host is not only warns, and
90
+ // is found first, so the engines are then not checked. pnpm 9 holds it to the
91
+ // host alone, before it reads supportedArchitectures.
92
+ export function checkProject(manifest, where, { host, settings, root }) {
93
+ if (host.major >= 12) {
94
+ if (root) checkRoot12(manifest, where, { host, settings })
95
+ return
96
+ }
94
97
  const platform = { os: platformList(manifest.os, `${where}.os`), cpu: platformList(manifest.cpu, `${where}.cpu`), libc: platformList(manifest.libc, `${where}.libc`) }
95
- if (!takesPlatform(platform, host, settings.supportedArchitectures, host.major)) return
96
- const engines = manifest.engines
97
- if (engines === undefined || engines === null) return
98
- const node = settings.nodeVersion ?? host.node
98
+ if (!takesPlatform(platform, host, host.major < 10 ? undefined : settings.supportedArchitectures)) return
99
+ const { engines } = manifest
100
+ if (engines == null) return
101
+ const node = nodeOf({ host, settings })
99
102
  if (engines.pnpm && !satisfies(host.pnpm, engines.pnpm, { includePrerelease: true })) {
100
103
  throw new DeptreeError(`its engines.pnpm, ${quote(String(engines.pnpm))}, does not take pnpm ${host.pnpm}, which pnpm refuses`, where)
101
104
  }
102
105
  if (settings.engineStrict && !takesEngine(engines, node)) throw new DeptreeError(`its engines.node, ${quote(String(engines.node))}, does not take Node ${node}, which engineStrict refuses`, where)
103
106
  }
104
107
 
105
- // A check of one snapshot: true where the host can run it, false where it
106
- // is optional and cannot, and null where it cannot but is not optional and
107
- // is installed anyway. With `engineStrict` that last is refused. pnpm 10
108
- // takes a snapshot to be optional as the lockfile marks it; pnpm 11 as the
109
- // edge it is reached by, and holds only one the lockfile does not mark
110
- // optional to engineStrict. With engineStrict, pnpm 11 passes over the
111
- // engines of a snapshot that is patched, which checkPatchedEngines holds
112
- // its package.json to once patched.
113
- export function createCheck({ host, settings }) {
114
- const node = settings.nodeVersion ?? host.node
115
- const patchedLater = host.major >= 11 && settings.engineStrict
116
- return (key, pkg, optional = pkg.optional, strict = settings.engineStrict) => {
117
- const platform = takesPlatform(host.major >= 11 && optional ? effectivePlatform(pkg) : pkg, host, settings.supportedArchitectures, host.major)
118
- if (platform && takesEngine(patchedLater && pkg.patchHash !== undefined ? {} : pkg.engines, node)) return true
108
+ // True where the host can run a snapshot, false where it is optional and
109
+ // cannot, null where it cannot but is installed anyway. pnpm 11 and 12 take
110
+ // `optional` from the edge. A patched one's engines are checked later
111
+ // (patchedLater), save its published ones by pnpm 12 `walking` the lockfile.
112
+ function createCheck(context) {
113
+ const { host, settings } = context
114
+ const node = nodeOf(context)
115
+ const later = patchedLater(context)
116
+ return (key, pkg, optional = pkg.optional, walking = false) => {
117
+ const platform = takesPlatform(host.major >= 11 && optional ? effectivePlatform(pkg) : pkg, host, settings.supportedArchitectures)
118
+ const engines = later && !(walking && host.major >= 12) && pkg.patchHash !== undefined ? {} : pkg.engines
119
+ if (host.major >= 12 && platform && (optional || settings.engineStrict)) checkSure12(engines.node, quote(key))
120
+ if (platform && takesEngine(engines, node)) return true
119
121
  if (optional) return false
120
- if (strict) throw new DeptreeError(`the host does not take its ${platform ? 'engines.node' : 'os, cpu or libc'}, which engineStrict refuses`, quote(key))
122
+ if (settings.engineStrict && (host.major >= 12 || !pkg.optional)) throw new DeptreeError(`the host does not take its ${platform ? 'engines.node' : 'os, cpu or libc'}, which engineStrict refuses`, quote(key))
121
123
  return null
122
124
  }
123
125
  }
124
126
 
125
- // pnpm 11, with engineStrict, holds a patched package's package.json,
126
- // once patched, to the Node, and fails where it does not take it; where
127
- // the package is optional, it removes it from the tree it has linked,
128
- // which is refused too.
129
- export function checkPatchedEngines(manifest, where, { host, settings }) {
130
- if (host.major < 11 || !settings.engineStrict || manifest.engines === undefined || manifest.engines === null) return
131
- const node = settings.nodeVersion ?? host.node
132
- if (!takesEngine(manifest.engines, node)) throw new DeptreeError(`its package.json, patched, has an engines.node, ${quote(String(manifest.engines.node))}, that does not take Node ${node}, which pnpm 11 refuses with engineStrict, or removes the package for where it is optional`, where)
127
+ // pnpm removes an optional package that fails this from the tree already
128
+ // linked, also refused. pnpm 12 passes over engines.runtime's Node here.
129
+ export function createPatchedCheck(context) {
130
+ if (!patchedLater(context)) return undefined
131
+ const { host, settings } = context
132
+ const node = host.major >= 12 ? settings.nodeVersion ?? host.node : nodeOf(context)
133
+ return (manifest, where) => {
134
+ if (manifest.engines == null) return
135
+ if (host.major >= 12) checkSure12(manifest.engines.node, where, 'the engines.node of its package.json, patched')
136
+ if (takesEngine(manifest.engines, node)) return
137
+ throw new DeptreeError(`its package.json, patched, has an engines.node, ${quote(String(manifest.engines.node))}, that does not take Node ${node}, which pnpm ${host.major} refuses with engineStrict, or removes the package for where it is optional`, where)
138
+ }
133
139
  }
134
140
 
135
- // A reference's key, or the project a `link:` leads to, if it is one.
136
141
  function split(targets, lockfile) {
137
142
  const keys = []
138
143
  const projects = []
@@ -143,87 +148,92 @@ function split(targets, lockfile) {
143
148
  return { keys, projects }
144
149
  }
145
150
 
146
- // Every snapshot a project depends on, then those of the projects it
147
- // links to that were not walked yet, as pnpm's toImporterDepPaths lists
148
- // them: dependencies, devDependencies, optionalDependencies.
151
+ // As pnpm's toImporterDepPaths lists them: a project's snapshots, then those
152
+ // of projects it links to not walked yet. A loop where pnpm recurses, so a
153
+ // long chain of projects runs out of no stack.
149
154
  function projectKeys(lockfile, ids, walked) {
150
- const targets = ids.flatMap((id) => {
151
- const importer = lockfile.importers[id]
152
- return Object.values({ ...importer.dependencies, ...importer.devDependencies, ...importer.optionalDependencies })
153
- })
154
- const { keys, projects } = split(targets, lockfile)
155
- const more = projects.filter((id) => !walked.has(id))
156
- for (const id of more) walked.add(id)
157
- return more.length === 0 ? keys : [...keys, ...projectKeys(lockfile, more, walked)]
155
+ const all = []
156
+ for (let more = ids; more.length > 0;) {
157
+ const targets = more.flatMap((id) => {
158
+ const importer = lockfile.importers[id]
159
+ return Object.values({ ...importer.dependencies, ...importer.devDependencies, ...importer.optionalDependencies })
160
+ })
161
+ const { keys, projects } = split(targets, lockfile)
162
+ for (const key of keys) all.push(key)
163
+ more = projects.filter((id) => !walked.has(id))
164
+ for (const id of more) walked.add(id)
165
+ }
166
+ return all
158
167
  }
159
168
 
160
- // pnpm 11's filterLockfileByImportersAndEngine: the snapshots left out,
161
- // and those installed although the host cannot run them.
162
- function skippedSnapshots11(lockfile, check, strict) {
169
+ // Each key by `check`: false adds it to `skipped`, null to `incompatible`.
170
+ function checkEach(keys, check, skipped = new Set()) {
171
+ const incompatible = new Set()
172
+ for (const key of keys) {
173
+ const ok = check(key)
174
+ if (ok === false) skipped.add(key)
175
+ if (ok === null) incompatible.add(key)
176
+ }
177
+ return { skipped, incompatible }
178
+ }
179
+
180
+ // pnpm 11's filterLockfileByImportersAndEngine, breadth first by edge kind:
181
+ // an optional edge to a snapshot the host cannot run is not taken, a
182
+ // required one is, with a warning; each reachable one not taken is left out.
183
+ function skippedSnapshots11(lockfile, check) {
163
184
  const edgesOf = (deps, optional) => Object.values(deps).filter((target) => !target.startsWith('link:')).map((key) => ({ key, optional }))
164
185
  const queue = Object.values(lockfile.importers).flatMap((importer) => [...edgesOf(importer.dependencies, false), ...edgesOf(importer.devDependencies, false), ...edgesOf(importer.optionalDependencies, true)])
165
186
  const starts = queue.map(({ key }) => key)
166
187
  const installed = new Set()
167
188
  const required = new Set()
168
- const incompatible = new Map()
189
+ const reached = new Map()
169
190
  for (let i = 0; i < queue.length; i++) {
170
191
  const { key, optional } = queue[i]
171
192
  if (!optional) required.add(key)
172
193
  if (installed.has(key)) continue
173
194
  const pkg = lockfile.packages[key]
174
- if (!incompatible.has(key)) incompatible.set(key, check(key, pkg, true) === false)
175
- if (optional && incompatible.get(key)) continue
195
+ if (!reached.has(key)) reached.set(key, optional && check(key, pkg, true, true) === false)
196
+ if (optional && reached.get(key)) continue
176
197
  installed.add(key)
177
198
  queue.push(...edgesOf(pkg.dependencies, false), ...edgesOf(pkg.optionalDependencies, true))
178
199
  }
179
- const skipped = new Set()
180
- const warned = new Set()
181
- for (const key of incompatible.keys()) {
182
- const pkg = lockfile.packages[key]
183
- const ok = check(key, pkg, !installed.has(key) || !required.has(key), strict && !pkg.optional)
184
- if (ok === false) skipped.add(key)
185
- if (ok === null) warned.add(key)
186
- }
187
- const seen = new Set()
188
- for (let i = 0; i < starts.length; i++) {
189
- const key = starts[i]
190
- if (seen.has(key)) continue
191
- seen.add(key)
200
+ const checked = checkEach(reached.keys(), (key) => check(key, lockfile.packages[key], !installed.has(key) || !required.has(key)))
201
+ const seen = new Set(starts)
202
+ for (const key of seen) {
192
203
  const pkg = lockfile.packages[key]
193
- if (!installed.has(key) && pkg.optional) skipped.add(key)
194
- starts.push(...[...edgesOf(pkg.dependencies), ...edgesOf(pkg.optionalDependencies)].map((edge) => edge.key))
204
+ if (!installed.has(key) && pkg.optional) checked.skipped.add(key)
205
+ for (const edge of [...edgesOf(pkg.dependencies), ...edgesOf(pkg.optionalDependencies)]) seen.add(edge.key)
195
206
  }
196
- return { skipped, incompatible: warned }
207
+ return checked
197
208
  }
198
209
 
199
- // The keys of the snapshots left out, and of those installed although the
200
- // host cannot run them. `check` is createCheck's.
201
- export function skippedSnapshots(lockfile, check, { major = 10, engineStrict = false } = {}) {
202
- if (major >= 11) return skippedSnapshots11(lockfile, check, engineStrict)
210
+ // The snapshots left out, and those installed that the host cannot run.
211
+ export function skippedSnapshots(lockfile, context) {
212
+ const check = createCheck(context)
213
+ if (context.host.major >= 11) return skippedSnapshots11(lockfile, check)
203
214
  const ids = Object.keys(lockfile.importers)
204
215
  const walked = new Set(ids)
205
216
  const picked = new Set()
206
217
  const skipped = new Set()
207
- const visit = (keys, parentInstallable) => {
208
- for (const key of keys) {
209
- if (picked.has(key)) continue
210
- const pkg = lockfile.packages[key]
211
- const installable = parentInstallable && check(key, pkg) !== false
212
- if (!installable && pkg.optional) skipped.add(key)
213
- picked.add(key)
214
- const { keys: next, projects } = split(Object.values({ ...pkg.dependencies, ...pkg.optionalDependencies }), lockfile)
215
- for (const id of projects) walked.add(id)
216
- visit([...next, ...projectKeys(lockfile, projects, walked)], installable)
218
+ // Depth first, as pnpm recurses, on a stack of its own: a chain of
219
+ // snapshots as long as a lockfile can make runs out of none.
220
+ const stack = [{ keys: projectKeys(lockfile, ids, walked).values(), installable: true }]
221
+ while (stack.length > 0) {
222
+ const { keys, installable: parentInstallable } = stack.at(-1)
223
+ const { value: key, done } = keys.next()
224
+ if (done) {
225
+ stack.pop()
226
+ continue
217
227
  }
228
+ if (picked.has(key)) continue
229
+ const pkg = lockfile.packages[key]
230
+ const installable = parentInstallable && check(key, pkg) !== false
231
+ if (!installable && pkg.optional) skipped.add(key)
232
+ picked.add(key)
233
+ const { keys: next, projects } = split(Object.values({ ...pkg.dependencies, ...pkg.optionalDependencies }), lockfile)
234
+ for (const id of projects) walked.add(id)
235
+ stack.push({ keys: [...next, ...projectKeys(lockfile, projects, walked)].values(), installable })
218
236
  }
219
- visit(projectKeys(lockfile, ids, walked), true)
220
- // The graph is built of the rest, each checked again on its own.
221
- const incompatible = new Set()
222
- for (const key of picked) {
223
- if (skipped.has(key)) continue
224
- const ok = check(key, lockfile.packages[key])
225
- if (ok === false) skipped.add(key)
226
- if (ok === null) incompatible.add(key)
227
- }
228
- return { skipped, incompatible }
237
+ // @pnpm/deps.graph-builder checks the rest again, each on its own.
238
+ return checkEach([...picked].filter((key) => !skipped.has(key)), (key) => check(key, lockfile.packages[key]), skipped)
229
239
  }
package/src/pnpm/local.js CHANGED
@@ -1,49 +1,120 @@
1
- // Directories outside node_modules the tree links to, read from the Vfs
2
- // given as `vfs` where there is one: those local overrides name, and
3
- // every other a `link:` leads to that is no project and is under the
4
- // lockfile's directory. Each has to hold a package.json there, whose bins
5
- // take their names as pnpm links them. An override to a directory is
6
- // read only from a Vfs given, and one to a copy of it, as `file:` has
7
- // pnpm install it, is refused: pnpm picks the files it copies with
8
- // npm-packlist, whose rules are not followed here.
9
-
10
- import { DeptreeError, quote } from '../error.js'
11
- import { readManifest } from './projects.js'
12
-
13
- const decoder = new TextDecoder('utf-8', { fatal: true })
14
-
15
- function manifestAt(vfs, dir, where) {
16
- const path = `/${dir}/package.json`
17
- if (!vfs.isFile(path)) throw new DeptreeError(`${quote(dir)} holds no package.json in the Vfs given`, where)
18
- let text
19
- try {
20
- text = decoder.decode(vfs.readFile(path))
21
- } catch {
22
- throw new DeptreeError(`${quote(`${dir}/package.json`)} is not UTF-8`, where)
23
- }
1
+ // Directories outside node_modules the tree links to or installs, read from the
2
+ // project given. Each must hold a package.json, whose bins take their names as
3
+ // pnpm links them.
4
+
5
+ import { satisfies, valid, validRange } from '@preventive/upstream/semver.js'
6
+ import { DeptreeError, difference, quote } from '../error.js'
7
+ import { readManifest } from '../manifest.js'
8
+ import { readText, typeOf } from '../project.js'
9
+ import { parseSpec } from './frozen.js'
10
+ import { localOf } from './overrides.js'
11
+ import { checkManifest } from './package.js'
12
+ import { packDirectory } from './packlist.js'
13
+
14
+ function manifestAt(project, dir, where) {
15
+ const text = readText(project, `/${dir}/package.json`, where)
16
+ if (text === undefined) throw new DeptreeError(`${quote(dir)} ${typeOf(project, `/${dir}`) === undefined ? 'is not in' : 'holds no package.json in'} the project given`, where)
24
17
  return readManifest(text, where)
25
18
  }
26
19
 
27
- // `overrides` is listOverrides's.
28
- export function checkLocalOverrides(overrides, vfs) {
20
+ // The directories `file:` overrides have pnpm install.
21
+ export function checkLocalOverrides(overrides, project) {
22
+ const installed = new Set()
29
23
  for (const { selector, local } of overrides) {
30
24
  if (local === undefined) continue
31
25
  const where = `overrides[${quote(selector)}]`
32
- if (local.protocol === 'file:') throw new DeptreeError('an override to a copy of a directory, as file: has pnpm install it, is not supported: pnpm picks the files it copies with npm-packlist', where)
33
- if (vfs === undefined) throw new DeptreeError(`an override to a directory, ${quote(local.dir)}, is read only from a Vfs given as vfs`, where)
34
- manifestAt(vfs, local.dir, where)
26
+ if (project === undefined) throw new DeptreeError(`an override to a directory, ${quote(local.dir)}, is read only from a project given`, where)
27
+ if (local.protocol === 'file:' && typeOf(project, `/${local.dir}`) === 'file') throw new DeptreeError(`${quote(local.dir)} is a file in the project given, and an override to a tarball is not supported`, where)
28
+ manifestAt(project, local.dir, where)
29
+ if (local.protocol === 'file:') installed.add(local.dir)
35
30
  }
31
+ return installed
32
+ }
33
+
34
+ // The lockfile records no version for a `file:` dependency's directory.
35
+ export function readDirectoryPackage(project, pkg, where, host) {
36
+ const { directory } = pkg.resolution
37
+ const manifest = manifestAt(project, directory, where)
38
+ if (manifest.name !== pkg.name) throw new DeptreeError(`its package.json is for ${quote(String(manifest.name))}`, where)
39
+ const files = packDirectory(project, directory, manifest, host.pnpm, where)
40
+ checkManifest(manifest, pkg, where, host.major)
41
+ return { files, manifest, local: true }
36
42
  }
37
43
 
38
- // By directory, the package.json of each directory the tree links to that
39
- // is no node of `nodes`, no project of `projects`, and under the
40
- // lockfile's directory, read from `vfs`; none without one.
41
- export function readLinked(links, nodes, projects, vfs) {
44
+ // pnpm takes `npm:@scope/name@workspace:<range>` for a path, for its `/`,
45
+ // which no other resolver takes, and links to a directory of that name.
46
+ const ALIASED = /(?:^|\/)(npm:(@[^/]+\/[^/]+)@(workspace:[^/]*))$/u
47
+
48
+ // pnpm links to a directory that is not there all the same: pnpm 10 and 11
49
+ // warn as they resolve, and none does as a frozen install. Refused, with the
50
+ // project given or, for the spec it takes for a path, without, but where the
51
+ // project holds a directory of that name.
52
+ export function readLinked(links, nodes, projects, project) {
42
53
  const linked = new Map()
43
- if (vfs === undefined) return linked
44
54
  for (const [path, target] of links) {
45
55
  if (nodes.has(target) || projects.has(target) || linked.has(target) || target === '..' || target.startsWith('../')) continue
46
- linked.set(target, manifestAt(vfs, target, quote(path)))
56
+ const aliased = ALIASED.exec(target)
57
+ if (aliased !== null && (project === undefined || typeOf(project, `/${target}`) === undefined)) {
58
+ const [, spec, name, range] = aliased
59
+ throw new DeptreeError(`pnpm takes ${quote(spec)} for a path, for its "/", and links to ${quote(target)}${project === undefined ? '' : ', which is not there'}; ${quote(range.replace(':', `:${name}@`))} is the spec that links the workspace package`, quote(path))
60
+ }
61
+ if (project !== undefined) linked.set(target, manifestAt(project, target, quote(path)))
47
62
  }
48
63
  return linked
49
64
  }
65
+
66
+ // Whether pnpm 12 takes `target` to be what `spec` asks for.
67
+ function resolvesTo(name, spec, target, packages) {
68
+ if (target.startsWith('link:')) return false
69
+ const locked = packages[target]
70
+ const { name: wanted, range } = parseSpec(spec, name)
71
+ if (locked.name !== wanted) return false
72
+ const version = valid(locked.version ?? '')
73
+ return validRange(range) === null || version === null || satisfies(version, range)
74
+ }
75
+
76
+ const optionalPeers = (meta) => Object.entries(meta ?? {}).filter(([, item]) => item?.optional === true).map(([name]) => name).sort()
77
+
78
+ // `read` is the hooked package.json. A peer may be among the snapshot's
79
+ // dependencies and an optional one missing; a `file:` or `link:` one is left to
80
+ // package.js's checkDependencies.
81
+ function checkFresh(read, pkg, packages, where) {
82
+ const outdated = (why) => new DeptreeError(`the lockfile is not up to date with its package.json, which a frozen install of pnpm 12 refuses: ${why}`, where)
83
+ const wanted = { dependencies: read.dependencies ?? {}, optionalDependencies: read.optionalDependencies ?? {} }
84
+ const peers = read.peerDependencies ?? {}
85
+ for (const [kind, others] of [['dependencies', peers], ['optionalDependencies', {}]]) {
86
+ for (const name of Object.keys(pkg[kind])) {
87
+ if (!Object.hasOwn(wanted[kind], name) && !Object.hasOwn(others, name)) throw outdated(`${kind}.${name} is not one it asks for`)
88
+ }
89
+ for (const [name, spec] of Object.entries(wanted[kind])) {
90
+ const target = pkg[kind][name]
91
+ if (target === undefined) {
92
+ if (kind === 'optionalDependencies') continue
93
+ throw outdated(`${kind}.${name} is not in the lockfile`)
94
+ }
95
+ if (spec.startsWith('file:') || spec.startsWith('link:')) continue
96
+ if (spec.startsWith('workspace:')) throw new DeptreeError(`a workspace: dependency, ${quote(name)}, of a directory pnpm 12 holds to the lockfile is not supported`, where)
97
+ if (localOf(spec, where) !== undefined || !resolvesTo(name, spec, target, packages)) throw outdated(`${kind}.${name} is not resolved as it asks`)
98
+ }
99
+ }
100
+ const peersDiffer = difference(peers, pkg.peerDependencies, ['its package.json', 'the lockfile'])
101
+ if (peersDiffer !== undefined) throw outdated(`its peerDependencies are not the lockfile's: ${peersDiffer}`)
102
+ if (JSON.stringify(optionalPeers(read.peerDependenciesMeta)) !== JSON.stringify(optionalPeers(pkg.peerDependenciesMeta))) throw outdated('which of its peers are optional is not the lockfile\'s')
103
+ }
104
+
105
+ // pnpm 12's check, before it installs, of each snapshot a project depends on by
106
+ // a `file:` specifier to a directory.
107
+ export function createFreshnessCheck(lockfile, major) {
108
+ if (major < 12) return undefined
109
+ const direct = new Set()
110
+ for (const importer of Object.values(lockfile.importers)) {
111
+ for (const [name, target] of Object.entries({ ...importer.dependencies, ...importer.devDependencies, ...importer.optionalDependencies })) {
112
+ const spec = importer.specifiers[name]
113
+ if (target.startsWith('link:') || !spec?.startsWith('file:') || /\.(?:tgz|tar\.gz|tar)$/u.test(spec)) continue
114
+ if (lockfile.packages[target].resolution.type === 'directory') direct.add(target)
115
+ }
116
+ }
117
+ return (node, read) => {
118
+ if (direct.has(node.key)) checkFresh(read, node.pkg, lockfile.packages, quote(node.key))
119
+ }
120
+ }