@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,31 +1,20 @@
1
- // Checks of a lockfile and of the tree built from it that pnpm does not
2
- // make, each of something a lockfile pnpm writes always holds to, or a
3
- // tree it installs always is: where one fails, the lockfile was not
4
- // written by pnpm as it stands, or the tree would not be one tree
5
- // everywhere it is installed.
1
+ // Checks pnpm does not make, of what a lockfile pnpm writes always holds to or
2
+ // a tree it installs always is.
6
3
 
7
4
  import { DeptreeError, quote } from '../error.js'
8
- import { fold } from '../mount.js'
9
5
 
10
- // A snapshot is optional exactly where no importer reaches it through
11
- // dependencies alone, its devDependencies counted among them, as pnpm
12
- // marks it: one marked otherwise would be left out, or kept, where it
13
- // should not be.
6
+ // pnpm marks a snapshot optional exactly where no importer requires it; one
7
+ // marked otherwise would be left out, or kept, where it should not be.
14
8
  export function checkOptional(lockfile) {
15
9
  const required = new Set()
16
- const queue = []
17
10
  const reach = (targets) => {
18
- for (const target of Object.values(targets)) {
19
- if (target.startsWith('link:') || required.has(target)) continue
20
- required.add(target)
21
- queue.push(target)
22
- }
11
+ for (const target of Object.values(targets)) if (!target.startsWith('link:')) required.add(target)
23
12
  }
24
13
  for (const importer of Object.values(lockfile.importers)) {
25
14
  reach(importer.dependencies)
26
15
  reach(importer.devDependencies)
27
16
  }
28
- while (queue.length > 0) reach(lockfile.packages[queue.pop()].dependencies)
17
+ for (const key of required) reach(lockfile.packages[key].dependencies)
29
18
  for (const [key, pkg] of Object.entries(lockfile.packages)) {
30
19
  if (pkg.optional === required.has(key)) {
31
20
  throw new DeptreeError(`marked ${pkg.optional ? 'optional where an importer requires it' : 'required where only optional dependencies reach it'}, which pnpm never writes`, `snapshots[${quote(key)}]`)
@@ -33,24 +22,8 @@ export function checkOptional(lockfile) {
33
22
  }
34
23
  }
35
24
 
36
- // macOS's filesystems take a name whatever its case and normalization, so
37
- // two names that differ only in those are one there, and one would be
38
- // lost: such a tree is refused where the host is macOS.
39
- export function checkCollisions(vfs) {
40
- for (const { path, type } of vfs.walk('/')) {
41
- if (type !== 'directory') continue
42
- const folded = new Map()
43
- for (const name of vfs.readdir(path)) {
44
- const key = fold(name)
45
- if (folded.has(key)) throw new DeptreeError(`${quote(folded.get(key))} and ${quote(name)} are one name on macOS`, quote(path))
46
- folded.set(key, name)
47
- }
48
- }
49
- }
50
-
51
- // Every link into node_modules leads to a directory in the tree; a link
52
- // out of it leads to a project or a `link:` directory, which the tree
53
- // does not hold.
25
+ // Only links into node_modules are checked: a link out of it leads to a
26
+ // project or a `link:` directory, which the tree does not hold.
54
27
  export function checkLinks(vfs, links) {
55
28
  for (const [path, target] of links) {
56
29
  if (!target.startsWith('node_modules/') && !target.includes('/node_modules/')) continue
@@ -0,0 +1,230 @@
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.
5
+
6
+ import { compareVersions, valid } from '@preventive/upstream/semver.js'
7
+ import { basename, compareNames, join } from '@preventive/vfs/path.js'
8
+ import { DeptreeError, quote } from '../error.js'
9
+ import { checkNesting } from '../manifest.js'
10
+ import { within } from '../mount.js'
11
+
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.
14
+ const RUNTIMES = { __proto__: null, node: 'bin/node', deno: 'deno', bun: 'bun' }
15
+
16
+ // The packages pnpm 11 and 12 take to own a command by a name not their own.
17
+ const OWNERS = { __proto__: null, npx: ['npm'], pn: ['pnpm', '@pnpm/exe'], pnpm: ['@pnpm/exe'], pnpx: ['pnpm', '@pnpm/exe'], pnx: ['pnpm', '@pnpm/exe'] }
18
+
19
+ const collator = new Intl.Collator('en')
20
+ const decoder = new TextDecoder('utf-8', { fatal: true })
21
+
22
+ // As path.join spells it, or undefined where is-subdir finds it out of `dir`.
23
+ function inPackage(dir, rel, where) {
24
+ if (typeof rel !== 'string') throw new DeptreeError(`${quote(String(rel))} is not a path, which pnpm fails on`, where)
25
+ const path = join(dir, rel)
26
+ if (path.endsWith('/')) throw new DeptreeError(`${quote(rel)} ends in a slash, which is not supported`, where)
27
+ return within(dir, path) ? path : undefined
28
+ }
29
+
30
+ // Stands for the commands of a directory whose bins are not known here.
31
+ export const UNKNOWN = { unknown: true }
32
+
33
+ // As tinyglobby's `**` finds them, or with `dots` as pnpm 12's walk does.
34
+ function filesUnder(files, base, dir, { dots = false } = {}) {
35
+ const prefix = dir === base ? '' : `${dir.slice(base.length + 1)}/`
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('.')))
38
+ }
39
+
40
+ // Which of two of one name wins turns on the order directories are read in.
41
+ function filesAsCommands(found, root, common) {
42
+ const names = found.map((path) => basename(path))
43
+ const counts = new Map()
44
+ for (const name of names) counts.set(name, (counts.get(name) ?? 0) + 1)
45
+ return found.map((path, i) => ({ ...common, name: names[i], target: `${root}/${path}`, unordered: counts.get(names[i]) > 1 }))
46
+ }
47
+
48
+ // pnpm 11 looks a name up among OWNERS as a plain object's key, and fails on
49
+ // one that Object.prototype has.
50
+ function owns({ name, pkgName, own }, major, where) {
51
+ if (own || name === pkgName) return true
52
+ if (major < 11) return false
53
+ if (major === 11 && name in Object.prototype) throw new DeptreeError(`two bins are named ${quote(name)}, which pnpm 11 fails on`, where)
54
+ return OWNERS[name]?.includes(pkgName) === true
55
+ }
56
+
57
+ // pnpm's runtimeHasNodeDownloaded.
58
+ function downloadsNode(runtime, where) {
59
+ if (!runtime) return false
60
+ if (!Array.isArray(runtime)) return runtime.name === 'node' && runtime.onFail === 'download'
61
+ for (const item of runtime) {
62
+ if (item == null) throw new DeptreeError('its engines.runtime lists nothing where pnpm reads a runtime, which pnpm fails on', where)
63
+ if (item.name === 'node') return item.onFail === 'download'
64
+ }
65
+ return false
66
+ }
67
+
68
+ // pnpm 12's command names: those encodeURIComponent leaves as they are.
69
+ const safeName = (name) => name === '$' || (name !== '.' && name !== '..' && /^[\w\-.!~*'()]+$/u.test(name))
70
+
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.
73
+ function commands12(dir, manifest, files, base, where) {
74
+ const name = typeof manifest.name === 'string' ? manifest.name : undefined
75
+ const common = { pkgName: name ?? '', pkgVersion: manifest.version, owner: base }
76
+ const { bin } = manifest
77
+ if (bin === null && typeof manifest.directories?.bin === 'string' && files !== undefined && dir === base) {
78
+ throw new DeptreeError('its bin is null beside a directories.bin, which pnpm 12 links as its store has the package or not', where)
79
+ }
80
+ if (bin !== undefined && bin !== '') {
81
+ const entries = typeof bin === 'string' ? (name === undefined ? [] : [[name, bin]])
82
+ : bin !== null && typeof bin === 'object' && !Array.isArray(bin) ? Object.entries(bin).filter(([, rel]) => typeof rel === 'string') : []
83
+ const commands = []
84
+ for (const [command, rel] of entries) {
85
+ const unscoped = command[0] === '@' && command.includes('/') ? command.slice(command.indexOf('/') + 1) : command
86
+ if (!safeName(unscoped) || rel.startsWith('/')) continue
87
+ const target = inPackage(dir, rel, where)
88
+ if (target !== undefined) commands.push({ ...common, name: unscoped, target })
89
+ }
90
+ return commands
91
+ }
92
+ const binDir = manifest.directories?.bin
93
+ if (typeof binDir !== 'string') return []
94
+ const root = inPackage(dir, binDir, where)
95
+ if (root === undefined || binDir.startsWith('/')) return []
96
+ if (files === undefined) return [UNKNOWN]
97
+ return filesAsCommands(filesUnder(files, base, root, { dots: true }).filter((path) => safeName(basename(path))), root, common)
98
+ }
99
+
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.
109
+ export function commandsOf(dir, manifest, files, base, where, major) {
110
+ if (major >= 12) return commands12(dir, manifest, files, base, where)
111
+ const common = { pkgName: manifest.name, pkgVersion: manifest.version, owner: base }
112
+ if (typeof manifest.bin === 'string' && !manifest.name) throw new DeptreeError('it has a bin and no name, which pnpm fails on', where)
113
+ // pnpm 9 knows no runtime.
114
+ if (major >= 10 && downloadsNode(manifest.engines?.runtime, where)) {
115
+ throw new DeptreeError('its engines.runtime has pnpm look for a Node to run its bins with, which is not supported', where)
116
+ }
117
+ if (manifest.bin) {
118
+ const string = typeof manifest.bin === 'string'
119
+ const entries = string ? [[manifest.name, manifest.bin]] : Object.entries(manifest.bin)
120
+ const commands = []
121
+ for (const [command, rel] of entries) {
122
+ const name = command[0] === '@' ? command.slice(command.indexOf('/') + 1) : command
123
+ if (!(major < 10 ? string || takes9(command) : uriSafe(name))) continue
124
+ if (name === '' || name === '.' || name === '..' || name.includes('/')) {
125
+ if (major >= 11) continue
126
+ throw new DeptreeError(`a bin named ${quote(name)} is not supported`, where)
127
+ }
128
+ const target = inPackage(dir, rel, where)
129
+ if (target === undefined && major < 10 && string) throw outOf(rel, where)
130
+ if (target !== undefined) commands.push({ ...common, name, target })
131
+ }
132
+ return commands
133
+ }
134
+ const binDir = manifest.directories?.bin
135
+ if (!binDir) return []
136
+ const root = inPackage(dir, binDir, where)
137
+ if (root === undefined && major < 10) throw outOf(binDir, where)
138
+ if (root === undefined) return []
139
+ if (files === undefined) return [UNKNOWN]
140
+ return filesAsCommands(filesUnder(files, base, root), root, common)
141
+ }
142
+
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.
146
+ export function compare(a, b, where, major) {
147
+ const aOwns = owns(a, major, where)
148
+ const bOwns = owns(b, major, where)
149
+ if (aOwns !== bOwns) return aOwns ? 1 : -1
150
+ if (major >= 12) {
151
+ if (a.pkgName !== b.pkgName) return compareNames(b.pkgName, a.pkgName)
152
+ const versions = [a.pkgVersion, b.pkgVersion].map((version) => (typeof version === 'string' ? valid(version) : null))
153
+ return versions.includes(null) ? 0 : compareVersions(versions[0], versions[1])
154
+ }
155
+ if (a.pkgName !== b.pkgName) {
156
+ if (typeof a.pkgName !== 'string' || typeof b.pkgName !== 'string') throw new DeptreeError(`two bins named ${quote(a.name)} are of packages not both named, which pnpm fails on`, where)
157
+ return collator.compare(a.pkgName, b.pkgName)
158
+ }
159
+ if (valid(a.pkgVersion) === null || valid(b.pkgVersion) === null) throw new DeptreeError(`two bins named ${quote(a.name)} are of ${quote(a.pkgName)} at versions pnpm cannot compare, which it fails on`, where)
160
+ return compareVersions(a.pkgVersion, b.pkgVersion)
161
+ }
162
+
163
+ // As normalize-package-data leaves it for pnpm's readPackageJson.
164
+ export function normalized(manifest, where) {
165
+ const name = manifest.name || ''
166
+ if (typeof name !== 'string') throw new DeptreeError('its package.json\'s name is not a string, which pnpm fails on', where)
167
+ const trimmed = name.trim()
168
+ const scoped = /^@([^/]+)\/([^/]+)$/u.exec(trimmed)
169
+ const encoded = (part) => part === encodeURIComponent(part)
170
+ const validName = trimmed === '' || (!trimmed.startsWith('.') && trimmed.toLowerCase() !== 'node_modules' && trimmed.toLowerCase() !== 'favicon.ico'
171
+ && ((scoped !== null && encoded(scoped[1]) && encoded(scoped[2])) || (!/[/@\s+%:]/u.test(trimmed) && encoded(trimmed))))
172
+ if (!validName) throw new DeptreeError(`its package.json's name, ${quote(trimmed)}, is one pnpm fails on`, where)
173
+ const version = manifest.version ? valid(manifest.version, { loose: true }) : ''
174
+ if (version === null) throw new DeptreeError(`its package.json's version, ${quote(String(manifest.version))}, is one pnpm fails on`, where)
175
+ return { ...manifest, name: trimmed, version }
176
+ }
177
+
178
+ // As pnpm's readModulesDir finds them.
179
+ function bundledIn(files) {
180
+ const names = new Set()
181
+ for (const path of files.keys()) {
182
+ const parts = path.split('/')
183
+ if (parts[0] !== 'node_modules' || parts.length < 3 || parts[1].startsWith('.')) continue
184
+ if (!parts[1].startsWith('@')) names.add(parts[1])
185
+ else if (parts.length > 3) names.add(`${parts[1]}/${parts[2]}`)
186
+ }
187
+ return [...names]
188
+ }
189
+
190
+ export function parseManifest(file, where) {
191
+ try {
192
+ const manifest = JSON.parse(decoder.decode(file.data))
193
+ if (manifest === null || typeof manifest !== 'object' || Array.isArray(manifest)) throw new Error('not an object')
194
+ return checkNesting(manifest, where)
195
+ } catch (error) {
196
+ if (error instanceof DeptreeError) throw error
197
+ throw new DeptreeError('a package.json pnpm reads for its bins is not JSON, which pnpm fails on', where)
198
+ }
199
+ }
200
+
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.
203
+ export function bundledCommands(node, where, major) {
204
+ const commands = []
205
+ for (const name of bundledIn(node.files)) {
206
+ const here = `${where}: node_modules/${name}`
207
+ const file = node.files.get(`node_modules/${name}/package.json`)
208
+ const dir = `${node.dir}/node_modules/${name}`
209
+ if (file !== undefined) {
210
+ const manifest = parseManifest(file, here)
211
+ commands.push(...commandsOf(dir, major >= 12 ? manifest : normalized(manifest, here), node.files, node.dir, here, major))
212
+ } else if (major >= 11) {
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) {
215
+ commands.push({ name, target: `${dir}/${RUNTIMES[name]}`, owner: node.dir, own: true, pkgName: '', pkgVersion: '' })
216
+ }
217
+ }
218
+ return commands
219
+ }
220
+
221
+ // Every path the package and those it bundles name as bins, linked or not.
222
+ export function binsOf(node, manifest, files, major) {
223
+ const where = quote(node.key)
224
+ const bins = new Map()
225
+ for (const { target, name, pkgName, pkgVersion } of [...commandsOf(node.dir, manifest, files, node.dir, where, major), ...bundledCommands({ ...node, files }, where, major)]) {
226
+ const path = target.slice(node.dir.length + 1)
227
+ bins.set(path, [...bins.get(path) ?? [], JSON.stringify([name, pkgName, pkgVersion])].sort())
228
+ }
229
+ return bins
230
+ }
@@ -1,26 +1,29 @@
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.
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.
12
5
 
13
6
  import { quote } from '../error.js'
14
- import { sha256Hex } from '../hash.js'
7
+ import { md5Base32, sha256Hex } from '../hash.js'
15
8
 
16
- const HASHED = 33 // `_` and 32 hex characters
9
+ const encoder = new TextEncoder()
10
+
11
+ // pnpm 12 cuts by bytes, back to where a character starts.
12
+ function cut(filename, length, major) {
13
+ if (major < 12) return filename.slice(0, length)
14
+ return filename.slice(0, encoder.encodeInto(filename, new Uint8Array(length)).read)
15
+ }
17
16
 
18
17
  export async function depPathToFilename(depPath, maxLength, major = 10) {
19
- let filename = depPath.replace(/[\\/:*?"<>|#]/gu, '+')
18
+ let filename = depPath.replace(major < 10 ? /[\\/:*?"<>|]/gu : /[\\/:*?"<>|#]/gu, '+')
20
19
  if (filename.includes('(')) filename = filename.replace(/\)$/u, '').replace(/\)\(|\(|\)/gu, '_')
21
20
  const hashed = filename
22
21
  const trailing = major >= 11 ? /[. ]*$/u.exec(filename)[0].length : 0
23
22
  if (trailing > 0) filename = `${filename.slice(0, -trailing)}${'+'.repeat(trailing)}`
24
- if (trailing === 0 && filename.length <= maxLength && filename === filename.toLowerCase()) return filename
25
- return `${filename.slice(0, Math.max(maxLength - HASHED, 0))}_${(await sha256Hex(hashed, quote(depPath))).slice(0, 32)}`
23
+ const length = major >= 12 ? encoder.encode(filename).length : filename.length
24
+ const lower = major >= 12 ? filename.replace(/[A-Z]+/gu, (capitals) => capitals.toLowerCase()) : filename.toLowerCase()
25
+ if (trailing === 0 && length <= maxLength && filename === lower) return filename
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}`
26
29
  }
@@ -1,15 +1,13 @@
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
- import { join, normalize } from '@preventive/vfs/path.js'
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'
10
+ import { catalogEntry, catalogOf } from './overrides.js'
13
11
 
14
12
  const GIT_HOSTS = new Set(['github.com', 'gitlab.com', 'bitbucket.org'])
15
13
  const SHORTCUTS = [['github:', 'github.com'], ['gitlab:', 'gitlab.com'], ['bitbucket:', 'bitbucket.org']]
@@ -21,8 +19,7 @@ function gitUrl(host, path, committish) {
21
19
  return `git+https://${host}/${repository}.git${committish ? `#${committish}` : ''}`
22
20
  }
23
21
 
24
- // A git specifier as one URL, as gitSpecifiersAreEquivalent spells it, or
25
- // undefined for one that is not.
22
+ // A git specifier as one URL, as gitSpecifiersAreEquivalent spells it.
26
23
  function gitSpecifier(specifier) {
27
24
  const hash = specifier.indexOf('#')
28
25
  const committish = hash === -1 ? undefined : specifier.slice(hash + 1)
@@ -46,9 +43,7 @@ function gitSpecifier(specifier) {
46
43
  return gitUrl(host, path, committish)
47
44
  }
48
45
 
49
- // dependencySpecifiersAreEqual: pnpm 11 takes two specifiers of one git
50
- // repository and commit, however spelled, to be the same; pnpm 10 only
51
- // two spelled the same.
46
+ // dependencySpecifiersAreEqual: pnpm 11 matches git specifiers however spelled.
52
47
  export function sameSpecifier(a, b, major) {
53
48
  if (a === b) return true
54
49
  if (major < 11 || a === undefined || b === undefined) return false
@@ -56,67 +51,47 @@ export function sameSpecifier(a, b, major) {
56
51
  return git !== undefined && git === gitSpecifier(b)
57
52
  }
58
53
 
59
- // The version an importer's target resolved to, as the lockfile spells
60
- // it: a key of the alias's own name is its version and peers.
61
- export const refOf = (alias, target) => (target.startsWith(`${alias}@`) ? target.slice(alias.length + 1) : target)
54
+ // A target under the alias's own name is spelled as its version alone.
55
+ export const resolvedOf = (alias, target) => packageKeyOf(target.startsWith(`${alias}@`) ? target.slice(alias.length + 1) : target)
62
56
 
63
57
  const targetOf = (importer, alias) => importer.dependencies[alias] ?? importer.devDependencies[alias] ?? importer.optionalDependencies[alias]
64
58
 
65
59
  // catalogResolutionsAreUpToDate.
66
60
  export function checkCatalogResolutions(importer, catalogs, where) {
67
61
  for (const [alias, specifier] of Object.entries(importer.specifiers)) {
68
- if (!specifier.startsWith('catalog:')) continue
69
- const name = specifier.slice('catalog:'.length).trim() || 'default'
70
- const version = Object.hasOwn(catalogs, name) && Object.hasOwn(catalogs[name], alias) ? catalogs[name][alias].version : undefined
62
+ const name = catalogOf(specifier)
63
+ if (name === undefined) continue
64
+ const version = catalogEntry(catalogs, name, alias)?.version
71
65
  const target = targetOf(importer, alias)
72
66
  if (version === undefined || target === undefined) continue
73
- const resolved = packageKeyOf(refOf(alias, target))
67
+ const resolved = resolvedOf(alias, target)
74
68
  if (valid(resolved) !== null && resolved !== version) throw new DeptreeError(`${quote(alias)} resolved to ${quote(resolved)}, and the lockfile's catalog ${quote(name)} to ${quote(version)}, which pnpm 11 refuses a frozen install for`, where)
75
69
  }
76
70
  }
77
71
 
78
- // A path pnpm resolves from a directory, `./` and all, relative to the
79
- // lockfile's directory as the lockfile's links are; one from the home
80
- // directory or the root cannot be told here.
72
+ // Relative to the lockfile's directory, as the lockfile's links are.
81
73
  function specPath(dir, path, where) {
82
74
  const clean = path.startsWith('./') ? path.slice(2) : path
83
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)
84
- return normalize(join(dir, clean.replace(/\/+$/u, '')))
76
+ return join(dir, clean.replace(/\/+$/u, ''))
85
77
  }
86
78
 
87
79
  const isWorkspacePath = (spec) => /^(?:[./\\]|~[/\\]|[A-Za-z]:)/u.test(spec)
88
- // Of two paths in the lockfile's normal form, whether one is the other or
89
- // in it.
90
- const within = (parent, child) => child === parent || (parent === '.' ? child !== '..' && !child.startsWith('../') : child.startsWith(`${parent}/`))
91
80
 
92
- function targetName(spec, alias) {
81
+ export function parseSpec(spec, alias) {
93
82
  if (spec.startsWith('workspace:')) {
94
83
  const raw = spec.slice('workspace:'.length)
95
84
  const at = raw.lastIndexOf('@')
96
- 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 }
97
86
  }
98
87
  if (spec.startsWith('npm:')) {
99
88
  const raw = spec.slice('npm:'.length)
100
- if (validRange(raw) !== null) return alias
101
- const at = raw.lastIndexOf('@')
102
- return at > 0 ? raw.slice(0, at) : raw
103
- }
104
- return alias
105
- }
106
-
107
- function versionRange(spec) {
108
- if (spec.startsWith('workspace:')) {
109
- const raw = spec.slice('workspace:'.length)
110
- const at = raw.lastIndexOf('@')
111
- return at > 0 ? raw.slice(at + 1) || '*' : raw
112
- }
113
- if (spec.startsWith('npm:')) {
114
- const raw = spec.slice('npm:'.length)
115
- if (validRange(raw) !== null) return raw
116
- const at = raw.indexOf('@', 1)
117
- 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) || '*' }
118
93
  }
119
- return spec
94
+ return { name: alias, range: spec }
120
95
  }
121
96
 
122
97
  const inRange = (version, range) => range === '*' || range === '^' || range === '~' || (typeof version === 'string' && satisfies(version, range, { loose: true }))
@@ -124,29 +99,23 @@ const inRange = (version, range) => range === '*' || range === '^' || range ===
124
99
  // version-selector-type's tag: a name that is no version or range.
125
100
  const isTag = (range) => valid(range, { loose: true }) === null && validRange(range, { loose: true }) === null && encodeURIComponent(range) === range
126
101
 
127
- const KINDS = ['optionalDependencies', 'dependencies', 'devDependencies']
102
+ export const KINDS = ['optionalDependencies', 'dependencies', 'devDependencies']
128
103
 
129
- // The directory a specifier names, where it names one: by `link:`,
130
- // `file:`, a `workspace:` path, or a path alone, as pnpm reads one. One
131
- // led by a backslash, a path to pnpm on Windows alone, is taken for one
132
- // too, for specPath to refuse, as a lockfile pnpm writes elsewhere never
133
- // 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.
134
106
  function pathOf(spec) {
135
107
  if (spec.startsWith('link:') || spec.startsWith('file:')) return spec.slice(5)
136
- if (spec.startsWith('workspace:')) return isWorkspacePath(spec.slice('workspace:'.length)) ? spec.slice('workspace:'.length) : undefined
137
- return /^(?:[./\\]|~[/\\]|[a-z]:)/iu.test(spec) ? spec : undefined
108
+ const path = spec.startsWith('workspace:') ? spec.slice('workspace:'.length) : spec
109
+ return isWorkspacePath(path) ? path : undefined
138
110
  }
139
111
 
140
- // A dependency the lockfile links has to be linked where the project's
141
- // package.json, read through the read-package hook, names a directory for
142
- // it, as a lockfile pnpm writes always has it: pnpm 11 holds a `link:` or
143
- // `workspace:` path to that, and pnpm 10, or pnpm 11 a path alone, would
144
- // link whatever the lockfile says. A `file:` one is pnpm's to link, where
145
- // 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.
146
115
  export function checkLinkTargets({ id, manifest, importer }, where) {
147
116
  for (const kind of KINDS) {
148
117
  for (const [alias, target] of Object.entries(importer[kind])) {
149
- const spec = manifest[kind]?.[alias]
118
+ const spec = own(manifest[kind], alias)
150
119
  if (!spec || !target.startsWith('link:') || importer.specifiers[alias].startsWith('file:')) continue
151
120
  const path = pathOf(spec)
152
121
  const linkedTo = target.slice('link:'.length)
@@ -157,43 +126,40 @@ export function checkLinkTargets({ id, manifest, importer }, where) {
157
126
  }
158
127
  }
159
128
 
160
- // checkLinkedPackagesAreUpToDate, for the project at `id`, less the
161
- // directories a package.json names, which checkLinkTargets holds links to:
162
- // `manifest` is its package.json as read through the read-package hook,
163
- // `projects` every project's by its directory, as given, and
164
- // `linkWorkspacePackages` the setting.
165
- export function checkLinkedPackages({ manifest, importer, projects, linkWorkspacePackages }, where) {
129
+ // pnpm 11 links a project from its publishConfig.directory too.
130
+ export function indexProjects(projects) {
166
131
  const byName = new Map()
167
132
  const byDir = new Map()
168
133
  for (const [dir, project] of projects) {
169
134
  byDir.set(dir, project)
170
135
  const directory = project.publishConfig?.directory
171
- if (typeof directory === 'string' && project.publishConfig.linkDirectory !== false) byDir.set(normalize(join(dir, directory)), project)
136
+ if (typeof directory === 'string' && project.publishConfig.linkDirectory !== false) byDir.set(join(dir, directory), project)
172
137
  if (!project.name) continue
173
138
  if (!byName.has(project.name)) byName.set(project.name, new Map())
174
139
  const version = project.version ?? '0.0.0'
175
140
  if (byName.get(project.name).has(version)) throw new DeptreeError(`${quote(String(project.name))} at ${quote(String(version))} is another project's name and version too, which leaves which pnpm 11 checks a dependency against to the order it finds them in`, `manifests[${quote(dir)}]`)
176
141
  byName.get(project.name).set(version, dir)
177
142
  }
143
+ return { projects, byName, byDir }
144
+ }
145
+
146
+ // checkLinkedPackagesAreUpToDate on the hooked package.json, less the
147
+ // directories it names, which checkLinkTargets checks.
148
+ export function checkLinkedPackages({ manifest, importer, index: { projects, byName, byDir }, linkWorkspacePackages }, where) {
178
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)
179
150
  for (const kind of KINDS) {
180
- const wanted = manifest[kind]
181
- if (wanted == null) continue
182
151
  for (const [alias, target] of Object.entries(importer[kind])) {
183
- const spec = wanted[alias]
152
+ const spec = own(manifest[kind], alias)
184
153
  if (!spec) continue
185
- const here = `${where}.${kind}.${alias}`
186
154
  const workspaceRange = spec.startsWith('workspace:') && !isWorkspacePath(spec.slice('workspace:'.length))
187
155
  const linked = target.startsWith('link:')
188
- const local = importer.specifiers[alias].startsWith('file:') || packageKeyOf(target).includes('@file:')
189
- if (local && !linked) throw new DeptreeError('a dependency on a local directory or tarball is not supported', here)
190
- 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
191
158
  if (linked && pathOf(spec) !== undefined) continue
192
- const name = targetName(spec, alias)
193
- const range = versionRange(spec)
159
+ const { name, range } = parseSpec(spec, alias)
194
160
  if (linked && isTag(range)) continue
195
161
  const named = byName.get(name)
196
- const dir = linked ? target.slice('link:'.length) : named?.get(packageKeyOf(refOf(alias, target)))
162
+ const dir = linked ? target.slice('link:'.length) : named?.get(resolvedOf(alias, target))
197
163
  if (dir === undefined) {
198
164
  const taking = workspaceRange && named !== undefined ? [...named.keys()].find((version) => inRange(projects.get(named.get(version)).version, range)) : undefined
199
165
  if (taking !== undefined) throw outdated(`the workspace package ${quote(name)} (${taking}) is in the range ${quote(spec)} and not linked`)
@@ -203,7 +169,7 @@ export function checkLinkedPackages({ manifest, importer, projects, linkWorkspac
203
169
  if (linked && workspaceRange && named !== undefined && ![...named.values()].some((root) => within(root, dir))) {
204
170
  throw outdated(`${alias} is linked to ${quote(dir)}, which is in no workspace package named ${quote(name)}`)
205
171
  }
206
- 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}`)
207
173
  const { version } = byDir.get(dir)
208
174
  if (linked !== inRange(version, range)) {
209
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`)