@preventive/deptree 1.0.0-alpha.5 → 1.0.0-alpha.7

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 (79) hide show
  1. package/LICENSE-APACHE +202 -0
  2. package/npm.d.ts +288 -0
  3. package/npm.js +7 -0
  4. package/package.json +41 -12
  5. package/pnpm.d.ts +158 -25
  6. package/pnpm.js +2 -10
  7. package/soldeer.d.ts +13 -0
  8. package/soldeer.js +1 -7
  9. package/src/concurrent.js +16 -17
  10. package/src/error.js +5 -9
  11. package/src/glob.js +55 -0
  12. package/src/hash.js +60 -13
  13. package/src/manifest.js +52 -0
  14. package/src/matcher.js +60 -10
  15. package/src/mount.js +104 -44
  16. package/src/npm/bins.js +54 -0
  17. package/src/npm/compat.js +97 -0
  18. package/src/npm/dev-engines.js +52 -0
  19. package/src/npm/graph.js +68 -0
  20. package/src/npm/inputs.js +75 -0
  21. package/src/npm/manifests.js +130 -0
  22. package/src/npm/package.js +57 -0
  23. package/src/npm/ratio.js +34 -0
  24. package/src/npm/settings.js +67 -0
  25. package/src/npm/tree.js +136 -0
  26. package/src/npm/versions.js +20 -0
  27. package/src/npmrc.js +24 -0
  28. package/src/patch.js +22 -41
  29. package/src/pnpm/bins.js +152 -131
  30. package/src/pnpm/checks.js +8 -35
  31. package/src/pnpm/commands.js +53 -96
  32. package/src/pnpm/filename.js +10 -21
  33. package/src/pnpm/frozen.js +29 -75
  34. package/src/pnpm/gitignore.js +139 -0
  35. package/src/pnpm/glob.js +65 -0
  36. package/src/pnpm/graph.js +15 -43
  37. package/src/pnpm/hoist.js +68 -61
  38. package/src/pnpm/hoisted.js +234 -0
  39. package/src/pnpm/hook.js +26 -31
  40. package/src/pnpm/inputs.js +73 -56
  41. package/src/pnpm/install.js +85 -131
  42. package/src/pnpm/local.js +21 -40
  43. package/src/pnpm/minimatch.js +319 -0
  44. package/src/pnpm/nm-hoist-graph.js +299 -0
  45. package/src/pnpm/nm-hoist.js +149 -0
  46. package/src/pnpm/npm-packlist.js +217 -0
  47. package/src/pnpm/overrides.js +11 -51
  48. package/src/pnpm/package.js +121 -0
  49. package/src/pnpm/packlist.js +169 -109
  50. package/src/pnpm/patches.js +18 -25
  51. package/src/pnpm/projects.js +63 -107
  52. package/src/pnpm/readers.js +63 -83
  53. package/src/pnpm/settings.js +64 -100
  54. package/src/pnpm/tree.js +197 -169
  55. package/src/pnpm/uptodate.js +16 -28
  56. package/src/pnpm/workspace.js +124 -133
  57. package/src/project.js +57 -0
  58. package/src/soldeer/config.js +8 -24
  59. package/src/soldeer/inputs.js +5 -15
  60. package/src/soldeer/tree.js +22 -60
  61. package/src/soldeer/zip.js +17 -36
  62. package/src/tarball.js +55 -140
  63. package/src/yarn1/bins.js +9 -19
  64. package/src/yarn1/compat.js +9 -28
  65. package/src/yarn1/hoist.js +53 -68
  66. package/src/yarn1/inputs.js +19 -61
  67. package/src/yarn1/manifest.js +8 -23
  68. package/src/yarn1/package.js +24 -63
  69. package/src/yarn1/peers.js +8 -15
  70. package/src/yarn1/requests.js +19 -37
  71. package/src/yarn1/resolve.js +36 -113
  72. package/src/yarn1/settings.js +15 -43
  73. package/src/yarn1/tree.js +67 -91
  74. package/yarn1.d.ts +34 -5
  75. package/yarn1.js +1 -8
  76. package/src/pnpm/npmrc.js +0 -31
  77. package/src/pnpm/project.js +0 -97
  78. package/src/yarn1/glob.js +0 -52
  79. /package/{LICENSE → LICENSE-MIT} +0 -0
@@ -1,27 +1,16 @@
1
- // A package's commands, as pnpm reads them to link its bins
2
- // (@pnpm/package-bins; with pnpm 12, its bin_resolver crate), and which of
3
- // two of one name in a .bin directory pnpm links.
4
- //
5
- // A package's bins are its `bin` — a path, or paths by command — or, with
6
- // none, every file under `directories.bin` that is not a dotfile or in a
7
- // dot directory, each by its name. A command that is not a name, or a
8
- // path that leads out of the package, is passed over, as pnpm 11 passes
9
- // over one named `.` or `..` or nothing. pnpm 12 reads a package.json as it
10
- // is, and its bins otherwise: see commands12.
11
- //
12
- // Of two commands of one name, pnpm links the one the package of that
13
- // name has (with pnpm 11 and 12, or npm's `npx`, and pnpm's `pn`, `pnpx`,
14
- // `pnx` and `pnpm`), else the one of the package whose name sorts last (by
15
- // `localeCompare`, read here as English; with pnpm 12, the one whose name
16
- // sorts first by its bytes), else of the later version, else the first it
17
- // came to.
1
+ // A package's commands as @pnpm/package-bins reads them to link its bins, or
2
+ // pnpm 12's bin_resolver crate, which reads a package.json unnormalized. pnpm 9
3
+ // filters a command by its name before it drops the scope, and links a `bin`
4
+ // string or a directories.bin wherever it leads.
18
5
 
19
6
  import { compareVersions, valid } from '@preventive/upstream/semver.js'
20
7
  import { basename, compareNames, join } from '@preventive/vfs/path.js'
21
8
  import { DeptreeError, quote } from '../error.js'
9
+ import { checkNesting } from '../manifest.js'
10
+ import { within } from '../mount.js'
22
11
 
23
- // A package whose directory has no package.json is still linked by these
24
- // names by pnpm 10, to the runtime's binary inside it.
12
+ // A package whose directory has no package.json is still linked by these names
13
+ // by pnpm 10, to the runtime's binary inside it; pnpm 9 links none.
25
14
  const RUNTIMES = { __proto__: null, node: 'bin/node', deno: 'deno', bun: 'bun' }
26
15
 
27
16
  // The packages pnpm 11 and 12 take to own a command by a name not their own.
@@ -30,39 +19,25 @@ const OWNERS = { __proto__: null, npx: ['npm'], pn: ['pnpm', '@pnpm/exe'], pnpm:
30
19
  const collator = new Intl.Collator('en')
31
20
  const decoder = new TextDecoder('utf-8', { fatal: true })
32
21
 
33
- // The path `rel` names in the package at `dir`, as path.join spells it, or
34
- // undefined where it is not in the package, as is-subdir tells; the
35
- // package itself is `dir`, which is `.` for the root project.
22
+ // As path.join spells it, or undefined where is-subdir finds it out of `dir`.
36
23
  function inPackage(dir, rel, where) {
37
24
  if (typeof rel !== 'string') throw new DeptreeError(`${quote(String(rel))} is not a path, which pnpm fails on`, where)
38
25
  const path = join(dir, rel)
39
26
  if (path.endsWith('/')) throw new DeptreeError(`${quote(rel)} ends in a slash, which is not supported`, where)
40
- const inside = dir === '.' ? path !== '..' && !path.startsWith('../') : path === dir || path.startsWith(`${dir}/`)
41
- return inside ? path : undefined
27
+ return within(dir, path) ? path : undefined
42
28
  }
43
29
 
44
- // A command that stands for those of a directory whose bins are not known
45
- // here, which may take any name.
30
+ // Stands for the commands of a directory whose bins are not known here.
46
31
  export const UNKNOWN = { unknown: true }
47
32
 
48
- // The files of `files` under `dir`, as tinyglobby's `**` finds them: none
49
- // a dotfile or in a dot directory, unless `dots`, as pnpm 12's walk finds
50
- // them. `files` holds paths relative to `base`.
33
+ // As tinyglobby's `**` finds them, or with `dots` as pnpm 12's walk does.
51
34
  function filesUnder(files, base, dir, { dots = false } = {}) {
52
35
  const prefix = dir === base ? '' : `${dir.slice(base.length + 1)}/`
53
- const found = []
54
- for (const [path, file] of files) {
55
- if (file.data === undefined || !path.startsWith(prefix)) continue
56
- const rest = path.slice(prefix.length)
57
- if (!dots && rest.split('/').some((name) => name.startsWith('.'))) continue
58
- found.push(rest)
59
- }
60
- return found
36
+ const found = [...files].filter(([path, file]) => file.data !== undefined && path.startsWith(prefix)).map(([path]) => path.slice(prefix.length))
37
+ return dots ? found : found.filter((rest) => !rest.split('/').some((name) => name.startsWith('.')))
61
38
  }
62
39
 
63
- // The files found under `root`, each a command by its name. Of two of one
64
- // name, which wins turns on the order they were found in: tinyglobby lists
65
- // them in the order the directories are read in.
40
+ // Which of two of one name wins turns on the order directories are read in.
66
41
  function filesAsCommands(found, root, common) {
67
42
  const names = found.map((path) => basename(path))
68
43
  const counts = new Map()
@@ -70,9 +45,8 @@ function filesAsCommands(found, root, common) {
70
45
  return found.map((path, i) => ({ ...common, name: names[i], target: `${root}/${path}`, unordered: counts.get(names[i]) > 1 }))
71
46
  }
72
47
 
73
- // Whether pnpm takes a package to own a command, which wins it the name.
74
- // pnpm 11 looks a name up among OWNERS as a plain object's key, and fails
75
- // on one that Object.prototype has.
48
+ // pnpm 11 looks a name up among OWNERS as a plain object's key, and fails on
49
+ // one that Object.prototype has.
76
50
  function owns({ name, pkgName, own }, major, where) {
77
51
  if (own || name === pkgName) return true
78
52
  if (major < 11) return false
@@ -80,35 +54,22 @@ function owns({ name, pkgName, own }, major, where) {
80
54
  return OWNERS[name]?.includes(pkgName) === true
81
55
  }
82
56
 
83
- // pnpm's runtimeHasNodeDownloaded: whether engines.runtime asks for a Node
84
- // to download, the first runtime of a list named node deciding. pnpm fails
85
- // on a list with nothing in it where it reads one, before that.
57
+ // pnpm's runtimeHasNodeDownloaded.
86
58
  function downloadsNode(runtime, where) {
87
59
  if (!runtime) return false
88
60
  if (!Array.isArray(runtime)) return runtime.name === 'node' && runtime.onFail === 'download'
89
61
  for (const item of runtime) {
90
- if (item === null || item === undefined) throw new DeptreeError('its engines.runtime lists nothing where pnpm reads a runtime, which pnpm fails on', where)
62
+ if (item == null) throw new DeptreeError('its engines.runtime lists nothing where pnpm reads a runtime, which pnpm fails on', where)
91
63
  if (item.name === 'node') return item.onFail === 'download'
92
64
  }
93
65
  return false
94
66
  }
95
67
 
96
- // What pnpm 12 takes for a command's name: one encodeURIComponent leaves
97
- // as it is, or `$`, but neither `.` nor `..`.
68
+ // pnpm 12's command names: those encodeURIComponent leaves as they are.
98
69
  const safeName = (name) => name === '$' || (name !== '.' && name !== '..' && /^[\w\-.!~*'()]+$/u.test(name))
99
70
 
100
- // A package's commands as pnpm 12 reads them: a string `bin` names one
101
- // after the package, where it has a name; an object names each of its
102
- // string values; any other `bin` but '' names none, and only '' or none
103
- // at all leaves directories.bin, where it is a string, to name each file
104
- // under it, dotfiles and those in dot directories among them, by its
105
- // name. A scoped command drops its scope; a name that is not safe, or a
106
- // target that is absolute or out of the package, is passed over.
107
- //
108
- // pnpm 12 reads a package's own bins off what its store holds of its
109
- // package.json where it has the package already, which leaves out a null
110
- // `bin`, and off the package.json where it fetches the package, which
111
- // keeps one: so a null `bin` beside a directories.bin is refused.
71
+ // pnpm 12 reads a package's own package.json from its store where it has the
72
+ // package, which drops a null `bin`, and as fetched otherwise.
112
73
  function commands12(dir, manifest, files, base, where) {
113
74
  const name = typeof manifest.name === 'string' ? manifest.name : undefined
114
75
  const common = { pkgName: name ?? '', pkgVersion: manifest.version, owner: base }
@@ -136,32 +97,36 @@ function commands12(dir, manifest, files, base, where) {
136
97
  return filesAsCommands(filesUnder(files, base, root, { dots: true }).filter((path) => safeName(basename(path))), root, common)
137
98
  }
138
99
 
139
- // A package's commands: `dir` is where the package is, `manifest` its
140
- // package.json, `files` the files of the package holding it (itself, or
141
- // the one that bundles it), by their paths under `base`. Both are
142
- // undefined for a directory outside the tree, a project or one a `link:`
143
- // leads to, whose commands are known only where its package.json names
144
- // them, and whose files are not fixed.
100
+ const outOf = (rel, where) => new DeptreeError(`${quote(rel)} leads out of the package, which pnpm 9 links a bin to all the same, and that is not supported`, where)
101
+
102
+ const uriSafe = (name) => name === encodeURIComponent(name) || name === '$'
103
+
104
+ // pnpm 9 also takes a scope with whatever follows it.
105
+ const takes9 = (command) => uriSafe(command) || command[0] === '@'
106
+
107
+ // `files` are the holding package's (itself, or the one bundling it), by path
108
+ // under `base`; both are undefined for a project or `link:` target.
145
109
  export function commandsOf(dir, manifest, files, base, where, major) {
146
110
  if (major >= 12) return commands12(dir, manifest, files, base, where)
147
111
  const common = { pkgName: manifest.name, pkgVersion: manifest.version, owner: base }
148
112
  if (typeof manifest.bin === 'string' && !manifest.name) throw new DeptreeError('it has a bin and no name, which pnpm fails on', where)
149
- // pnpm looks for the Node it would download from where the package is,
150
- // and fails where there is none.
151
- if (downloadsNode(manifest.engines?.runtime, where)) {
113
+ // pnpm 9 knows no runtime.
114
+ if (major >= 10 && downloadsNode(manifest.engines?.runtime, where)) {
152
115
  throw new DeptreeError('its engines.runtime has pnpm look for a Node to run its bins with, which is not supported', where)
153
116
  }
154
117
  if (manifest.bin) {
155
- const entries = typeof manifest.bin === 'string' ? [[manifest.name, manifest.bin]] : Object.entries(manifest.bin)
118
+ const string = typeof manifest.bin === 'string'
119
+ const entries = string ? [[manifest.name, manifest.bin]] : Object.entries(manifest.bin)
156
120
  const commands = []
157
121
  for (const [command, rel] of entries) {
158
122
  const name = command[0] === '@' ? command.slice(command.indexOf('/') + 1) : command
159
- if (name !== encodeURIComponent(name) && name !== '$') continue
160
- if (name === '' || name === '.' || name === '..') {
123
+ if (!(major < 10 ? string || takes9(command) : uriSafe(name))) continue
124
+ if (name === '' || name === '.' || name === '..' || name.includes('/')) {
161
125
  if (major >= 11) continue
162
126
  throw new DeptreeError(`a bin named ${quote(name)} is not supported`, where)
163
127
  }
164
128
  const target = inPackage(dir, rel, where)
129
+ if (target === undefined && major < 10 && string) throw outOf(rel, where)
165
130
  if (target !== undefined) commands.push({ ...common, name, target })
166
131
  }
167
132
  return commands
@@ -169,13 +134,15 @@ export function commandsOf(dir, manifest, files, base, where, major) {
169
134
  const binDir = manifest.directories?.bin
170
135
  if (!binDir) return []
171
136
  const root = inPackage(dir, binDir, where)
137
+ if (root === undefined && major < 10) throw outOf(binDir, where)
172
138
  if (root === undefined) return []
173
139
  if (files === undefined) return [UNKNOWN]
174
140
  return filesAsCommands(filesUnder(files, base, root), root, common)
175
141
  }
176
142
 
177
- // compareCommandsInConflict, which pnpm keeps the greater of; with pnpm 12,
178
- // pick_winner, whose ties keep the first.
143
+ // compareCommandsInConflict, which pnpm keeps the greater of (pnpm 12's
144
+ // pick_winner, which keeps the first of a tie). pnpm compares names with
145
+ // localeCompare, read here as English.
179
146
  export function compare(a, b, where, major) {
180
147
  const aOwns = owns(a, major, where)
181
148
  const bOwns = owns(b, major, where)
@@ -193,11 +160,9 @@ export function compare(a, b, where, major) {
193
160
  return compareVersions(a.pkgVersion, b.pkgVersion)
194
161
  }
195
162
 
196
- // A package.json as normalize-package-data leaves one, or refuses it,
197
- // where pnpm reads one with readPackageJson: its name trimmed, and each
198
- // of its name and version one npm takes.
163
+ // As normalize-package-data leaves it for pnpm's readPackageJson.
199
164
  export function normalized(manifest, where) {
200
- const name = manifest.name ? manifest.name : ''
165
+ const name = manifest.name || ''
201
166
  if (typeof name !== 'string') throw new DeptreeError('its package.json\'s name is not a string, which pnpm fails on', where)
202
167
  const trimmed = name.trim()
203
168
  const scoped = /^@([^/]+)\/([^/]+)$/u.exec(trimmed)
@@ -210,9 +175,7 @@ export function normalized(manifest, where) {
210
175
  return { ...manifest, name: trimmed, version }
211
176
  }
212
177
 
213
- // The packages a package bundles, as pnpm's readModulesDir finds them in
214
- // its node_modules: each directory there not named with a leading dot,
215
- // and each in a scope's directory.
178
+ // As pnpm's readModulesDir finds them.
216
179
  function bundledIn(files) {
217
180
  const names = new Set()
218
181
  for (const path of files.keys()) {
@@ -228,16 +191,15 @@ export function parseManifest(file, where) {
228
191
  try {
229
192
  const manifest = JSON.parse(decoder.decode(file.data))
230
193
  if (manifest === null || typeof manifest !== 'object' || Array.isArray(manifest)) throw new Error('not an object')
231
- return manifest
232
- } catch {
194
+ return checkNesting(manifest, where)
195
+ } catch (error) {
196
+ if (error instanceof DeptreeError) throw error
233
197
  throw new DeptreeError('a package.json pnpm reads for its bins is not JSON, which pnpm fails on', where)
234
198
  }
235
199
  }
236
200
 
237
- // The commands of the packages `node` bundles. For one with no
238
- // package.json pnpm 11 reads the bins of the package.json of the nearest
239
- // directory above it whose publishConfig.directory it is, as far up as the
240
- // filesystem goes, which is refused.
201
+ // For a bundled package with no package.json, pnpm 11 reads the bins of any
202
+ // directory above it, up to /, whose publishConfig.directory it is.
241
203
  export function bundledCommands(node, where, major) {
242
204
  const commands = []
243
205
  for (const name of bundledIn(node.files)) {
@@ -247,21 +209,16 @@ export function bundledCommands(node, where, major) {
247
209
  if (file !== undefined) {
248
210
  const manifest = parseManifest(file, here)
249
211
  commands.push(...commandsOf(dir, major >= 12 ? manifest : normalized(manifest, here), node.files, node.dir, here, major))
250
- } else if (major >= 12) {
251
- continue
252
212
  } else if (major >= 11) {
253
- throw new DeptreeError('it bundles a package with no package.json, whose bins pnpm 11 looks for above it', here)
254
- } else if (name in RUNTIMES) {
213
+ if (major < 12) throw new DeptreeError('it bundles a package with no package.json, whose bins pnpm 11 looks for above it', here)
214
+ } else if (major >= 10 && name in RUNTIMES) {
255
215
  commands.push({ name, target: `${dir}/${RUNTIMES[name]}`, owner: node.dir, own: true, pkgName: '', pkgVersion: '' })
256
216
  }
257
217
  }
258
218
  return commands
259
219
  }
260
220
 
261
- // Every path the package at `node` and those it bundles name as bins,
262
- // relative to it, whether pnpm links them or not, where its package.json
263
- // is `manifest` and its files are `files`: each with the commands that
264
- // name it and the package name and version each is ranked by.
221
+ // Every path the package and those it bundles name as bins, linked or not.
265
222
  export function binsOf(node, manifest, files, major) {
266
223
  const where = quote(node.key)
267
224
  const bins = new Map()
@@ -1,34 +1,21 @@
1
- // The directory pnpm 10 gives a snapshot under node_modules/.pnpm, as its
2
- // depPathToFilename (@pnpm/dependency-path) spells it: the snapshot key
3
- // with each character a filesystem may refuse made `+`, the parentheses of
4
- // the peers and the patch made `_` and the last one dropped, and, where
5
- // that runs past `virtualStoreDirMaxLength` or has a capital in it, cut
6
- // short and ended with `_` and 32 hex characters of its SHA-256.
7
- // `a@1.0.0(b@2.0.0)` is `a@1.0.0_b@2.0.0`, `@s/a@1.0.0` `@s+a@1.0.0`.
8
- //
9
- // pnpm 11 escapes a trailing dot or space as well, each made `+`, and then
10
- // hashes the name whatever its length; a snapshot of the registry never
11
- // ends in either. pnpm 9 hashed with MD5 in base32, which is not done here.
12
- //
13
- // pnpm 12 measures and cuts the name by its UTF-8 bytes, back to where a
14
- // character starts, and takes only an ASCII capital for one; a name not
15
- // all ASCII, as a `file:` path may have, is the only one that tells.
1
+ // The directory pnpm gives a snapshot under node_modules/.pnpm, as its
2
+ // depPathToFilename (@pnpm/dependency-path) spells it: `a@1.0.0(b@2.0.0)` is
3
+ // `a@1.0.0_b@2.0.0`, `@s/a@1.0.0` `@s+a@1.0.0`, and one too long or with a
4
+ // capital is cut short and hashed.
16
5
 
17
6
  import { quote } from '../error.js'
18
- import { sha256Hex } from '../hash.js'
7
+ import { md5Base32, sha256Hex } from '../hash.js'
19
8
 
20
- const HASHED = 33 // `_` and 32 hex characters
21
9
  const encoder = new TextEncoder()
22
10
 
23
- // The name cut to `length` units: UTF-16 ones, or for pnpm 12 bytes, as
24
- // many whole characters as fit in them.
11
+ // pnpm 12 cuts by bytes, back to where a character starts.
25
12
  function cut(filename, length, major) {
26
13
  if (major < 12) return filename.slice(0, length)
27
14
  return filename.slice(0, encoder.encodeInto(filename, new Uint8Array(length)).read)
28
15
  }
29
16
 
30
17
  export async function depPathToFilename(depPath, maxLength, major = 10) {
31
- let filename = depPath.replace(/[\\/:*?"<>|#]/gu, '+')
18
+ let filename = depPath.replace(major < 10 ? /[\\/:*?"<>|]/gu : /[\\/:*?"<>|#]/gu, '+')
32
19
  if (filename.includes('(')) filename = filename.replace(/\)$/u, '').replace(/\)\(|\(|\)/gu, '_')
33
20
  const hashed = filename
34
21
  const trailing = major >= 11 ? /[. ]*$/u.exec(filename)[0].length : 0
@@ -36,5 +23,7 @@ export async function depPathToFilename(depPath, maxLength, major = 10) {
36
23
  const length = major >= 12 ? encoder.encode(filename).length : filename.length
37
24
  const lower = major >= 12 ? filename.replace(/[A-Z]+/gu, (capitals) => capitals.toLowerCase()) : filename.toLowerCase()
38
25
  if (trailing === 0 && length <= maxLength && filename === lower) return filename
39
- return `${cut(filename, Math.max(maxLength - HASHED, 0), major)}_${(await sha256Hex(hashed, quote(depPath))).slice(0, 32)}`
26
+ // By pnpm's createBase32Hash, or createShortHash.
27
+ const hash = `_${major < 10 ? md5Base32(hashed, quote(depPath)) : (await sha256Hex(hashed, quote(depPath))).slice(0, 32)}`
28
+ return `${cut(filename, Math.max(maxLength - hash.length, 0), major)}${hash}`
40
29
  }
@@ -1,15 +1,12 @@
1
- // What pnpm 11's frozen install holds each project to beyond what pnpm
2
- // 10's does (@pnpm/lockfile.verification): specifiers that name one git
3
- // repository and commit alike are the same; a catalog dependency has to
4
- // have resolved to the version the lockfile's catalog records; and a
5
- // dependency on a workspace package has to be linked exactly where the
6
- // package's version is in its range — which pnpm 10 checks only when it
7
- // resolves.
1
+ // What pnpm 11's frozen install checks of each project that pnpm 10 checks only
2
+ // when it resolves (@pnpm/lockfile.verification).
8
3
 
9
4
  import { packageKeyOf } from '@preventive/lockfile/pnpm.js'
10
5
  import { satisfies, valid, validRange } from '@preventive/upstream/semver.js'
11
6
  import { join } from '@preventive/vfs/path.js'
12
7
  import { DeptreeError, quote } from '../error.js'
8
+ import { own } from '../manifest.js'
9
+ import { within } from '../mount.js'
13
10
  import { catalogEntry, catalogOf } from './overrides.js'
14
11
 
15
12
  const GIT_HOSTS = new Set(['github.com', 'gitlab.com', 'bitbucket.org'])
@@ -22,8 +19,7 @@ function gitUrl(host, path, committish) {
22
19
  return `git+https://${host}/${repository}.git${committish ? `#${committish}` : ''}`
23
20
  }
24
21
 
25
- // A git specifier as one URL, as gitSpecifiersAreEquivalent spells it, or
26
- // undefined for one that is not.
22
+ // A git specifier as one URL, as gitSpecifiersAreEquivalent spells it.
27
23
  function gitSpecifier(specifier) {
28
24
  const hash = specifier.indexOf('#')
29
25
  const committish = hash === -1 ? undefined : specifier.slice(hash + 1)
@@ -47,9 +43,7 @@ function gitSpecifier(specifier) {
47
43
  return gitUrl(host, path, committish)
48
44
  }
49
45
 
50
- // dependencySpecifiersAreEqual: pnpm 11 takes two specifiers of one git
51
- // repository and commit, however spelled, to be the same; pnpm 10 only
52
- // two spelled the same.
46
+ // dependencySpecifiersAreEqual: pnpm 11 matches git specifiers however spelled.
53
47
  export function sameSpecifier(a, b, major) {
54
48
  if (a === b) return true
55
49
  if (major < 11 || a === undefined || b === undefined) return false
@@ -57,9 +51,7 @@ export function sameSpecifier(a, b, major) {
57
51
  return git !== undefined && git === gitSpecifier(b)
58
52
  }
59
53
 
60
- // The version an importer's target resolved to, as the lockfile spells
61
- // it, peers and all left out: a key of the alias's own name is its
62
- // version.
54
+ // A target under the alias's own name is spelled as its version alone.
63
55
  export const resolvedOf = (alias, target) => packageKeyOf(target.startsWith(`${alias}@`) ? target.slice(alias.length + 1) : target)
64
56
 
65
57
  const targetOf = (importer, alias) => importer.dependencies[alias] ?? importer.devDependencies[alias] ?? importer.optionalDependencies[alias]
@@ -77,9 +69,7 @@ export function checkCatalogResolutions(importer, catalogs, where) {
77
69
  }
78
70
  }
79
71
 
80
- // A path pnpm resolves from a directory, `./` and all, relative to the
81
- // lockfile's directory as the lockfile's links are; one from the home
82
- // directory or the root cannot be told here.
72
+ // Relative to the lockfile's directory, as the lockfile's links are.
83
73
  function specPath(dir, path, where) {
84
74
  const clean = path.startsWith('./') ? path.slice(2) : path
85
75
  if (/^(?:~[/\\]|[/\\]|[A-Za-z]:)/u.test(clean) || clean.includes('\\')) throw new DeptreeError(`${quote(path)} is not a path from the project, which is not supported`, where)
@@ -87,40 +77,21 @@ function specPath(dir, path, where) {
87
77
  }
88
78
 
89
79
  const isWorkspacePath = (spec) => /^(?:[./\\]|~[/\\]|[A-Za-z]:)/u.test(spec)
90
- // Of two paths in the lockfile's normal form, whether one is the other or
91
- // in it.
92
- const within = (parent, child) => child === parent || (parent === '.' ? child !== '..' && !child.startsWith('../') : child.startsWith(`${parent}/`))
93
80
 
94
- // The name of the package a specifier asks for, under `alias`.
95
- export function targetName(spec, alias) {
81
+ export function parseSpec(spec, alias) {
96
82
  if (spec.startsWith('workspace:')) {
97
83
  const raw = spec.slice('workspace:'.length)
98
84
  const at = raw.lastIndexOf('@')
99
- return isWorkspacePath(raw) || at <= 0 ? alias : raw.slice(0, at)
85
+ return { name: isWorkspacePath(raw) || at <= 0 ? alias : raw.slice(0, at), range: at > 0 ? raw.slice(at + 1) || '*' : raw }
100
86
  }
101
87
  if (spec.startsWith('npm:')) {
102
88
  const raw = spec.slice('npm:'.length)
103
- if (validRange(raw) !== null) return alias
104
- const at = raw.lastIndexOf('@')
105
- return at > 0 ? raw.slice(0, at) : raw
106
- }
107
- return alias
108
- }
109
-
110
- // The range of the version a specifier asks for.
111
- export function versionRange(spec) {
112
- if (spec.startsWith('workspace:')) {
113
- const raw = spec.slice('workspace:'.length)
114
- const at = raw.lastIndexOf('@')
115
- return at > 0 ? raw.slice(at + 1) || '*' : raw
116
- }
117
- if (spec.startsWith('npm:')) {
118
- const raw = spec.slice('npm:'.length)
119
- if (validRange(raw) !== null) return raw
120
- const at = raw.indexOf('@', 1)
121
- return at === -1 ? '*' : raw.slice(at + 1) || '*'
89
+ if (validRange(raw) !== null) return { name: alias, range: raw }
90
+ const last = raw.lastIndexOf('@')
91
+ const first = raw.indexOf('@', 1)
92
+ return { name: last > 0 ? raw.slice(0, last) : raw, range: first === -1 ? '*' : raw.slice(first + 1) || '*' }
122
93
  }
123
- return spec
94
+ return { name: alias, range: spec }
124
95
  }
125
96
 
126
97
  const inRange = (version, range) => range === '*' || range === '^' || range === '~' || (typeof version === 'string' && satisfies(version, range, { loose: true }))
@@ -130,27 +101,21 @@ const isTag = (range) => valid(range, { loose: true }) === null && validRange(ra
130
101
 
131
102
  export const KINDS = ['optionalDependencies', 'dependencies', 'devDependencies']
132
103
 
133
- // The directory a specifier names, where it names one: by `link:`,
134
- // `file:`, a `workspace:` path, or a path alone, as pnpm reads one. One
135
- // led by a backslash, a path to pnpm on Windows alone, is taken for one
136
- // too, for specPath to refuse, as a lockfile pnpm writes elsewhere never
137
- // links it.
104
+ // A path led by a backslash, a path only on Windows, is taken too for specPath
105
+ // to refuse, as a lockfile pnpm writes elsewhere never links it.
138
106
  function pathOf(spec) {
139
107
  if (spec.startsWith('link:') || spec.startsWith('file:')) return spec.slice(5)
140
108
  const path = spec.startsWith('workspace:') ? spec.slice('workspace:'.length) : spec
141
109
  return isWorkspacePath(path) ? path : undefined
142
110
  }
143
111
 
144
- // A dependency the lockfile links has to be linked where the project's
145
- // package.json, read through the read-package hook, names a directory for
146
- // it, as a lockfile pnpm writes always has it: pnpm 11 holds a `link:` or
147
- // `workspace:` path to that, and pnpm 10, or pnpm 11 a path alone, would
148
- // link whatever the lockfile says. A `file:` one is pnpm's to link, where
149
- // it does.
112
+ // Stricter than pnpm, which checks only a `link:` or `workspace:` path, and
113
+ // only from pnpm 11: a lockfile pnpm writes always links where the hooked
114
+ // package.json names a directory.
150
115
  export function checkLinkTargets({ id, manifest, importer }, where) {
151
116
  for (const kind of KINDS) {
152
117
  for (const [alias, target] of Object.entries(importer[kind])) {
153
- const spec = manifest[kind]?.[alias]
118
+ const spec = own(manifest[kind], alias)
154
119
  if (!spec || !target.startsWith('link:') || importer.specifiers[alias].startsWith('file:')) continue
155
120
  const path = pathOf(spec)
156
121
  const linkedTo = target.slice('link:'.length)
@@ -161,9 +126,7 @@ export function checkLinkTargets({ id, manifest, importer }, where) {
161
126
  }
162
127
  }
163
128
 
164
- // Every project's package.json, as given, by its directory, and by where
165
- // pnpm 11 links it from, its publishConfig.directory among them; and the
166
- // directory of each by name and version, of which no two may be one.
129
+ // pnpm 11 links a project from its publishConfig.directory too.
167
130
  export function indexProjects(projects) {
168
131
  const byName = new Map()
169
132
  const byDir = new Map()
@@ -180,29 +143,20 @@ export function indexProjects(projects) {
180
143
  return { projects, byName, byDir }
181
144
  }
182
145
 
183
- // checkLinkedPackagesAreUpToDate, less the directories a package.json
184
- // names, which checkLinkTargets holds links to: `manifest` is the
185
- // project's package.json as read through the read-package hook, `index`
186
- // indexProjects's, and `linkWorkspacePackages` the setting.
146
+ // checkLinkedPackagesAreUpToDate on the hooked package.json, less the
147
+ // directories it names, which checkLinkTargets checks.
187
148
  export function checkLinkedPackages({ manifest, importer, index: { projects, byName, byDir }, linkWorkspacePackages }, where) {
188
149
  const outdated = (detail) => new DeptreeError(`the lockfile is not up to date with this package.json, which pnpm 11 refuses a frozen install for: ${detail}`, where)
189
150
  for (const kind of KINDS) {
190
- const wanted = manifest[kind]
191
- if (wanted == null) continue
192
151
  for (const [alias, target] of Object.entries(importer[kind])) {
193
- const spec = wanted[alias]
152
+ const spec = own(manifest[kind], alias)
194
153
  if (!spec) continue
195
- const here = `${where}.${kind}.${alias}`
196
154
  const workspaceRange = spec.startsWith('workspace:') && !isWorkspacePath(spec.slice('workspace:'.length))
197
155
  const linked = target.startsWith('link:')
198
- // A local directory or tarball is up to date where the lockfile has
199
- // it, as pnpm 11's frozen install skips its own dependencies; which
200
- // is installed is held to tree.js's checkSource.
201
- const local = importer.specifiers[alias].startsWith('file:') || packageKeyOf(target).includes('@file:')
202
- if (local) continue
156
+ // pnpm 11's frozen install skips a local directory or tarball.
157
+ if (importer.specifiers[alias].startsWith('file:') || packageKeyOf(target).includes('@file:')) continue
203
158
  if (linked && pathOf(spec) !== undefined) continue
204
- const name = targetName(spec, alias)
205
- const range = versionRange(spec)
159
+ const { name, range } = parseSpec(spec, alias)
206
160
  if (linked && isTag(range)) continue
207
161
  const named = byName.get(name)
208
162
  const dir = linked ? target.slice('link:'.length) : named?.get(resolvedOf(alias, target))
@@ -215,7 +169,7 @@ export function checkLinkedPackages({ manifest, importer, index: { projects, byN
215
169
  if (linked && workspaceRange && named !== undefined && ![...named.values()].some((root) => within(root, dir))) {
216
170
  throw outdated(`${alias} is linked to ${quote(dir)}, which is in no workspace package named ${quote(name)}`)
217
171
  }
218
- if (!byDir.has(dir)) throw new DeptreeError(`it is linked to ${quote(dir)}, which is no project, and whose package.json pnpm 11 reads`, here)
172
+ if (!byDir.has(dir)) throw new DeptreeError(`it is linked to ${quote(dir)}, which is no project, and whose package.json pnpm 11 reads`, `${where}.${kind}.${alias}`)
219
173
  const { version } = byDir.get(dir)
220
174
  if (linked !== inRange(version, range)) {
221
175
  throw outdated(linked ? `the linked workspace package ${alias} (${version ?? 'unknown'}) is not in the range ${quote(spec)}` : `the workspace package ${alias} (${version ?? 'unknown'}) is in the range ${quote(spec)} and not linked`)