@preventive/deptree 1.0.0-alpha.2 → 1.0.0-alpha.3

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.
@@ -19,15 +19,23 @@
19
19
  // or not, with a warning; each reachable snapshot not taken is left out.
20
20
  // Where a package of an optional edge names no os, cpu or libc, pnpm 11
21
21
  // infers them from its name, as `@nx/nx-win32-arm64-msvc` names win32.
22
+ //
23
+ // pnpm 12 leaves out the same, but refuses with engineStrict whatever it
24
+ // installs that the host cannot run, the lockfile's optional mark or not,
25
+ // and holds a patched package's published engines to the host as it walks
26
+ // it. Of a project it checks only the root's engines.node, and only with
27
+ // engineStrict. It reads an engines.node npm's semver does not, or a
28
+ // range of a `-` with an `x`, otherwise than npm's semver, which is
29
+ // refused where it would decide what is installed.
22
30
 
23
- import { satisfies } from '@preventive/upstream/semver.js'
31
+ import { satisfies, validRange } from '@preventive/upstream/semver.js'
24
32
  import { DeptreeError, quote } from '../error.js'
25
33
 
26
34
  // pnpm's checkList: `any` alone is anything, `!x` excludes, and a list of
27
35
  // exclusions alone takes what none of them names. pnpm 10 counts the
28
36
  // exclusions over every value the host is taken to be, so a list of them
29
37
  // takes nothing where the host is taken to be more than one thing; pnpm 11
30
- // does not.
38
+ // does not. pnpm 12 takes, of each value, the first item that names it.
31
39
  function checkList(values, list, major) {
32
40
  if (list.length === 1 && list[0] === 'any') return true
33
41
  let match = false
@@ -37,7 +45,10 @@ function checkList(values, list, major) {
37
45
  if (item.startsWith('!')) {
38
46
  if (item.slice(1) === value) return false
39
47
  excluded++
40
- } else if (item === value) match = true
48
+ } else if (item === value) {
49
+ match = true
50
+ if (major >= 12) break
51
+ }
41
52
  }
42
53
  }
43
54
  return match || (major >= 11 ? list.every((item) => item.startsWith('!')) : excluded === list.length)
@@ -76,8 +87,17 @@ const takesPlatform = (pkg, host, supported) => checkList(current(host.os, suppo
76
87
 
77
88
  const takesEngine = (engines, node) => !engines.node || satisfies(node, engines.node, { includePrerelease: true })
78
89
 
79
- // The Node pnpm checks engines.node against.
80
- const nodeOf = ({ host, settings }) => settings.nodeVersion ?? host.node
90
+ // Refuses an engines.node, `what`, that pnpm 12 may read otherwise than
91
+ // npm's semver: it drops what of a range it cannot parse, where npm's
92
+ // takes none of it, and reads a `-` range with an `x` as taking nothing.
93
+ function checkSure12(range, where, what = 'its engines.node') {
94
+ if (typeof range !== 'string' || (validRange(range) !== null && !(/\s-\s/u.test(range) && /[*xX]/u.test(range)))) return
95
+ throw new DeptreeError(`${what}, ${quote(range)}, pnpm 12 reads otherwise than npm's semver, which is not supported`, where)
96
+ }
97
+
98
+ // The Node pnpm checks engines.node against: nodeVersion, else the Node
99
+ // the root's engines.runtime pins, else the host's.
100
+ const nodeOf = ({ host, settings }) => settings.nodeVersion ?? settings.runtimeNodeVersion ?? host.node
81
101
 
82
102
  // With engineStrict, pnpm 11 passes over the engines the lockfile records
83
103
  // of a patched snapshot, and holds those of its package.json, once
@@ -93,12 +113,27 @@ function platformList(value, where) {
93
113
  throw new DeptreeError('expected a string or a list of them', where)
94
114
  }
95
115
 
116
+ // pnpm 12 checks the root project's engines.node, where it is text, with
117
+ // engineStrict alone.
118
+ function checkRoot12(manifest, where, context) {
119
+ const range = manifest.engines?.node
120
+ if (!context.settings.engineStrict || typeof range !== 'string' || range === '') return
121
+ const node = nodeOf(context)
122
+ checkSure12(range, where)
123
+ if (!takesEngine({ node: range }, node)) throw new DeptreeError(`its engines.node, ${quote(range)}, does not take Node ${node}, which engineStrict refuses`, where)
124
+ }
125
+
96
126
  // A project as pnpm's packageIsInstallable holds it when it reads its
97
127
  // package.json: a platform the host is not only warns, and so does an
98
128
  // engines.node it does not take, unless engineStrict; an engines.pnpm it
99
129
  // does not take is always refused. As pnpm has it, a platform that does
100
130
  // not match is found first, and the engines are then not looked at.
101
- export function checkProject(manifest, where, { host, settings }) {
131
+ // pnpm 12's is checkRoot12, of the root alone.
132
+ export function checkProject(manifest, where, { host, settings, root }) {
133
+ if (host.major >= 12) {
134
+ if (root) checkRoot12(manifest, where, { host, settings })
135
+ return
136
+ }
102
137
  const platform = { os: platformList(manifest.os, `${where}.os`), cpu: platformList(manifest.cpu, `${where}.cpu`), libc: platformList(manifest.libc, `${where}.libc`) }
103
138
  if (!takesPlatform(platform, host, settings.supportedArchitectures)) return
104
139
  const engines = manifest.engines
@@ -113,18 +148,22 @@ export function checkProject(manifest, where, { host, settings }) {
113
148
  // A check of one snapshot: true where the host can run it, false where it
114
149
  // is optional and cannot, and null where it cannot but is not optional and
115
150
  // is installed anyway. With `engineStrict` that last is refused, where the
116
- // lockfile does not mark the snapshot optional. pnpm 10 takes a snapshot
117
- // to be optional as the lockfile marks it; pnpm 11 as the edge it is
118
- // reached by.
151
+ // lockfile does not mark the snapshot optional, or with pnpm 12. pnpm 10
152
+ // takes a snapshot to be optional as the lockfile marks it; pnpm 11 as the
153
+ // edge it is reached by. A patched one's engines wait for its package.json,
154
+ // patched (patchedLater), but where pnpm 12 is `walking` the lockfile,
155
+ // which holds it to its published ones.
119
156
  function createCheck(context) {
120
157
  const { host, settings } = context
121
158
  const node = nodeOf(context)
122
159
  const later = patchedLater(context)
123
- return (key, pkg, optional = pkg.optional) => {
160
+ return (key, pkg, optional = pkg.optional, walking = false) => {
124
161
  const platform = takesPlatform(host.major >= 11 && optional ? effectivePlatform(pkg) : pkg, host, settings.supportedArchitectures)
125
- if (platform && takesEngine(later && pkg.patchHash !== undefined ? {} : pkg.engines, node)) return true
162
+ const engines = later && !(walking && host.major >= 12) && pkg.patchHash !== undefined ? {} : pkg.engines
163
+ if (host.major >= 12 && platform && (optional || settings.engineStrict)) checkSure12(engines.node, quote(key))
164
+ if (platform && takesEngine(engines, node)) return true
126
165
  if (optional) return false
127
- if (settings.engineStrict && !pkg.optional) throw new DeptreeError(`the host does not take its ${platform ? 'engines.node' : 'os, cpu or libc'}, which engineStrict refuses`, quote(key))
166
+ 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))
128
167
  return null
129
168
  }
130
169
  }
@@ -132,13 +171,17 @@ function createCheck(context) {
132
171
  // A check of a patched package's package.json, once patched, where pnpm
133
172
  // makes one (patchedLater): it fails where the Node is not taken; where
134
173
  // the package is optional, it removes it from the tree it has linked,
135
- // which is refused too.
174
+ // which is refused too. pnpm 12 holds it to nodeVersion, else the host's
175
+ // Node, whatever the root's engines.runtime pins.
136
176
  export function createPatchedCheck(context) {
137
177
  if (!patchedLater(context)) return undefined
138
- const node = nodeOf(context)
178
+ const { host, settings } = context
179
+ const node = host.major >= 12 ? settings.nodeVersion ?? host.node : nodeOf(context)
139
180
  return (manifest, where) => {
140
- if (manifest.engines === undefined || manifest.engines === null || takesEngine(manifest.engines, node)) return
141
- 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)
181
+ if (manifest.engines === undefined || manifest.engines === null) return
182
+ if (host.major >= 12) checkSure12(manifest.engines.node, where, 'the engines.node of its package.json, patched')
183
+ if (takesEngine(manifest.engines, node)) return
184
+ 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)
142
185
  }
143
186
  }
144
187
 
@@ -175,20 +218,22 @@ function skippedSnapshots11(lockfile, check) {
175
218
  const starts = queue.map(({ key }) => key)
176
219
  const installed = new Set()
177
220
  const required = new Set()
178
- const incompatible = new Map()
221
+ // Each snapshot reached, and whether the host cannot run it, as checked
222
+ // where an optional edge first reaches it.
223
+ const reached = new Map()
179
224
  for (let i = 0; i < queue.length; i++) {
180
225
  const { key, optional } = queue[i]
181
226
  if (!optional) required.add(key)
182
227
  if (installed.has(key)) continue
183
228
  const pkg = lockfile.packages[key]
184
- if (!incompatible.has(key)) incompatible.set(key, check(key, pkg, true) === false)
185
- if (optional && incompatible.get(key)) continue
229
+ if (!reached.has(key)) reached.set(key, optional && check(key, pkg, true, true) === false)
230
+ if (optional && reached.get(key)) continue
186
231
  installed.add(key)
187
232
  queue.push(...edgesOf(pkg.dependencies, false), ...edgesOf(pkg.optionalDependencies, true))
188
233
  }
189
234
  const skipped = new Set()
190
235
  const warned = new Set()
191
- for (const key of incompatible.keys()) {
236
+ for (const key of reached.keys()) {
192
237
  const pkg = lockfile.packages[key]
193
238
  const ok = check(key, pkg, !installed.has(key) || !required.has(key))
194
239
  if (ok === false) skipped.add(key)
package/src/pnpm/local.js CHANGED
@@ -5,9 +5,13 @@
5
5
  // bins take their names as pnpm links them. An override to a directory is
6
6
  // read only from a project given. One by `file:` has pnpm install the
7
7
  // directory as a package, of the files npm-packlist picks (packlist.js);
8
- // one to a tarball is refused.
8
+ // one to a tarball is refused. pnpm 12 holds one a project depends on to
9
+ // the lockfile before it installs (createFreshnessCheck).
9
10
 
10
- import { DeptreeError, quote } from '../error.js'
11
+ import { satisfies, valid, validRange } from '@preventive/upstream/semver.js'
12
+ import { DeptreeError, difference, quote } from '../error.js'
13
+ import { targetName, versionRange } from './frozen.js'
14
+ import { localOf } from './overrides.js'
11
15
  import { packDirectory } from './packlist.js'
12
16
  import { readText, typeOf } from './project.js'
13
17
  import { readManifest } from './projects.js'
@@ -55,3 +59,69 @@ export function readLinked(links, nodes, projects, project) {
55
59
  }
56
60
  return linked
57
61
  }
62
+
63
+ // Whether pnpm 12 takes a dependency on `name` by `spec`, a specifier not
64
+ // to a directory, to be what the lockfile resolved it to, `target`: a
65
+ // package of the name it asks for, `npm:` aliasing another, at a version
66
+ // in its range, where it is a range and the version is one.
67
+ function resolvesTo(name, spec, target, packages) {
68
+ if (target.startsWith('link:')) return false
69
+ const locked = packages[target]
70
+ if (locked.name !== targetName(spec, name)) return false
71
+ const range = versionRange(spec)
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
+ // pnpm 12 holds a directory a project depends on by `file:` to the
79
+ // lockfile as it reads the directory's package.json, overridden, `read`:
80
+ // each dependency the snapshot `pkg` has is one it asks for, a peer among
81
+ // them, and each it asks for is there, resolved as it asks, an optional one
82
+ // there or not; its peers are exactly what the lockfile records, and so is
83
+ // which of them are optional. A `file:` or `link:` one is held to where it
84
+ // leads by tarball.js's checkDependencies; a path alone, or `workspace:`,
85
+ // is refused. `packages` is the lockfile's.
86
+ function checkFresh(read, pkg, packages, where) {
87
+ 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)
88
+ const wanted = { dependencies: read.dependencies ?? {}, optionalDependencies: read.optionalDependencies ?? {} }
89
+ const peers = read.peerDependencies ?? {}
90
+ for (const [kind, others] of [['dependencies', peers], ['optionalDependencies', {}]]) {
91
+ for (const name of Object.keys(pkg[kind])) {
92
+ if (!Object.hasOwn(wanted[kind], name) && !Object.hasOwn(others, name)) throw outdated(`${kind}.${name} is not one it asks for`)
93
+ }
94
+ for (const [name, spec] of Object.entries(wanted[kind])) {
95
+ const target = pkg[kind][name]
96
+ if (target === undefined) {
97
+ if (kind === 'optionalDependencies') continue
98
+ throw outdated(`${kind}.${name} is not in the lockfile`)
99
+ }
100
+ if (spec.startsWith('file:') || spec.startsWith('link:')) continue
101
+ 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)
102
+ if (localOf(spec, where) !== undefined || !resolvesTo(name, spec, target, packages)) throw outdated(`${kind}.${name} is not resolved as it asks`)
103
+ }
104
+ }
105
+ const peersDiffer = difference(peers, pkg.peerDependencies, ['its package.json', 'the lockfile'])
106
+ if (peersDiffer !== undefined) throw outdated(`its peerDependencies are not the lockfile's: ${peersDiffer}`)
107
+ if (JSON.stringify(optionalPeers(read.peerDependenciesMeta)) !== JSON.stringify(optionalPeers(pkg.peerDependenciesMeta))) throw outdated('which of its peers are optional is not the lockfile\'s')
108
+ }
109
+
110
+ // pnpm 12's check, before it installs, of each snapshot a project depends
111
+ // on by a `file:` specifier to a directory: undefined for another pnpm.
112
+ // It is given the node of a snapshot and its package.json as hook.js reads
113
+ // it.
114
+ export function createFreshnessCheck(lockfile, major) {
115
+ if (major < 12) return undefined
116
+ const direct = new Set()
117
+ for (const importer of Object.values(lockfile.importers)) {
118
+ for (const [name, target] of Object.entries({ ...importer.dependencies, ...importer.devDependencies, ...importer.optionalDependencies })) {
119
+ const spec = importer.specifiers[name]
120
+ if (target.startsWith('link:') || !spec?.startsWith('file:') || /\.(?:tgz|tar\.gz|tar)$/u.test(spec)) continue
121
+ if (lockfile.packages[target].resolution.type === 'directory') direct.add(target)
122
+ }
123
+ }
124
+ return (node, read) => {
125
+ if (direct.has(node.key)) checkFresh(read, node.pkg, lockfile.packages, quote(node.key))
126
+ }
127
+ }
@@ -1,8 +1,9 @@
1
1
  // The files pnpm installs of a directory a `file:` dependency names: its
2
2
  // directory fetcher picks them with npm-packlist — 5.1.3 for pnpm 10,
3
- // 10.0.4 for pnpm 11 — and hardlinks them into the tree. Only
4
- // npm-packlist's built-in rules are followed here, which leave out, by
5
- // name and whatever its case:
3
+ // 10.0.4 for pnpm 11, its own port of it (fs-packlist) for pnpm 12 — and
4
+ // hardlinks or copies them into the tree (tree.js). Only the built-in
5
+ // rules are followed here. npm-packlist's leave out, by name and whatever
6
+ // its case:
6
7
  //
7
8
  // - anywhere, `.git`, `.svn`, `.hg`, `CVS`, `.npmrc`, `.DS_Store`,
8
9
  // `npm-debug.log`, `.npmignore`, `.gitignore`, and a name starting
@@ -14,15 +15,25 @@
14
15
  // - at the top, `node_modules` and the lockfiles `package-lock.json`,
15
16
  // `yarn.lock`, `pnpm-lock.yaml` and, with pnpm 11, `bun.lockb`;
16
17
  //
17
- // and every name with a `*` in it. A directory those do not describe is
18
- // refused: one with a .npmignore or .gitignore in it, or a package.json
19
- // with `files` or bundled dependencies, whose rules are not followed
20
- // here; one with a link in it, which the two pass over differently; and
21
- // one where a file those leave out is one npm-packlist would keep, a
22
- // readme, copying, license or licence file, or one `main`, `browser` or
23
- // `bin` names, as the two keep them differently. So is a file whose mode
24
- // is not 0o644 or 0o755, which linking a bin would change otherwise than
25
- // fixBin has it.
18
+ // and every name with a `*` in it. pnpm 12's leave out, by name as it is
19
+ // spelled:
20
+ //
21
+ // - anywhere, `.git`, `.svn`, `.hg` and `CVS`, with everything in them,
22
+ // and a file `.npmrc`, `npm-debug.log`, `.DS_Store`,
23
+ // `package-lock.json`, `yarn.lock` or `pnpm-lock.yaml`, or one whose
24
+ // name ends in `.orig`;
25
+ // - at the top, `node_modules`.
26
+ //
27
+ // A directory those do not describe is refused: one with a .npmignore
28
+ // or .gitignore in it, or a package.json with `files` or bundled
29
+ // dependencies, whose rules are not followed here; one with a link in
30
+ // it, which the two pass over differently; and one where a file those
31
+ // leave out is one npm-packlist would keep, a readme, copying, license or
32
+ // licence file, or one `main`, `browser` or `bin` names, as the two keep
33
+ // them differently, or, with pnpm 12, one in node_modules that `main` or
34
+ // `bin` names, the only file it keeps of those its rules leave out. So is
35
+ // a file whose mode is not 0o644 or 0o755, which linking a bin would
36
+ // change otherwise than fixBin has it.
26
37
 
27
38
  import { join } from '@preventive/vfs/path.js'
28
39
  import { DeptreeError, quote } from '../error.js'
@@ -41,9 +52,28 @@ const anchored = (names, directory) => (names.length === 1 && (names[0] === '.lo
41
52
  const TOP = ['node_modules', 'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml']
42
53
  const TOP_11 = [...TOP, 'bun.lockb']
43
54
 
44
- // Whether the built-in rules leave out the entry at `names`, its path from
45
- // the package's directory by name, which is a directory where `directory`.
46
- function leftOut(names, directory, major) {
55
+ // pnpm 12's rules: the names left out wherever they are, and the files.
56
+ const VCS = new Set(['.git', '.svn', '.hg', 'CVS'])
57
+ const CRUFT = new Set(['.npmrc', 'npm-debug.log', '.DS_Store', 'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml'])
58
+
59
+ // Whether pnpm 12's rules leave out the entry at `names`, its path from the
60
+ // package's directory by name, which is a directory where `directory`.
61
+ function leftOut12(names, directory) {
62
+ const name = names.at(-1)
63
+ return VCS.has(name) || (names.length === 1 && name === 'node_modules') || (!directory && (CRUFT.has(name) || name.endsWith('.orig')))
64
+ }
65
+
66
+ // Whether pnpm 12 leaves out the file at `path` even where `main` or `bin`
67
+ // names it: by all its rules but node_modules's.
68
+ function alwaysLeftOut12(path) {
69
+ const names = path.split('/')
70
+ const name = names.at(-1)
71
+ return names.some((each) => VCS.has(each)) || CRUFT.has(name) || name.endsWith('.orig')
72
+ }
73
+
74
+ // Whether npm-packlist's built-in rules, as pnpm `major` runs it, leave out
75
+ // the entry at `names`, as for leftOut12.
76
+ function leftOutByNpm(names, directory, major) {
47
77
  const folded = names.map((name) => name.toLowerCase())
48
78
  const readIn = major >= 11 ? folded.map((_, i) => folded.slice(i)) : [folded]
49
79
  return ANYWHERE.test(folded.at(-1)) || readIn.some((path) => anchored(path, directory))
@@ -54,11 +84,14 @@ function leftOut(names, directory, major) {
54
84
  const MUST_HAVE = /^(?:readme|copying|license|licence)(?:\..*[^~$])?$/iu
55
85
 
56
86
  // The paths a package.json's main, browser and bin name, from its
57
- // directory, whatever their case.
58
- function namedByManifest(manifest) {
87
+ // directory, whatever their case, that pnpm `major` may keep whatever its
88
+ // rules say: with pnpm 12, those main and bin name that its rules but
89
+ // node_modules's do not leave out.
90
+ function namedByManifest(manifest, major) {
59
91
  const { main, browser, bin } = manifest
60
- const paths = [main, browser, ...typeof bin === 'string' ? [bin] : Object.values(bin ?? {})]
61
- return paths.filter((path) => typeof path === 'string').map((path) => join('.', path).toLowerCase())
92
+ const bins = typeof bin === 'string' ? [bin] : Object.values(bin ?? {})
93
+ const paths = (major >= 12 ? [main, ...bins] : [main, browser, ...bins]).filter((path) => typeof path === 'string').map((path) => join('.', path))
94
+ return (major >= 12 ? paths.filter((path) => !alwaysLeftOut12(path)) : paths).map((path) => path.toLowerCase())
62
95
  }
63
96
 
64
97
  // Whether a package.json's list of bundled dependencies may name any: an
@@ -71,7 +104,8 @@ const bundles = (list) => Boolean(list) && !(Array.isArray(list) && list.length
71
104
  export function packDirectory(project, dir, manifest, major, where) {
72
105
  if (manifest.files !== undefined) throw new DeptreeError('its package.json has `files`, which npm-packlist picks the files pnpm installs by, and which is not followed here', where)
73
106
  if (bundles(manifest.bundleDependencies) || bundles(manifest.bundledDependencies)) throw new DeptreeError('a directory with bundled dependencies is not supported', where)
74
- const named = namedByManifest(manifest)
107
+ const named = namedByManifest(manifest, major)
108
+ const leftOut = major >= 12 ? leftOut12 : (names, directory) => leftOutByNpm(names, directory, major)
75
109
  const files = new Map()
76
110
  const visit = (names) => {
77
111
  for (const entry of project.readdir(['', dir, ...names].join('/'))) {
@@ -81,11 +115,11 @@ export function packDirectory(project, dir, manifest, major, where) {
81
115
  const { type, mode } = project.lstat(`/${dir}/${rel}`)
82
116
  if (type === 'symlink') throw new DeptreeError('a link in a directory pnpm installs a copy of is not supported', here)
83
117
  if (entry === '.npmignore' || entry === '.gitignore') throw new DeptreeError(`${entry}'s rules, which npm-packlist picks the files pnpm installs by, are not followed here`, here)
84
- if (names.length === 0 && entry !== 'node_modules' && entry.toLowerCase() === 'node_modules') throw new DeptreeError('a name that is node_modules but for its case is kept by pnpm 10 and left out by pnpm 11', here)
85
- if (entry.includes('*')) continue
86
- if (leftOut(at, type === 'directory', major)) {
118
+ if (major < 12 && names.length === 0 && entry !== 'node_modules' && entry.toLowerCase() === 'node_modules') throw new DeptreeError('a name that is node_modules but for its case is kept by pnpm 10 and left out by pnpm 11', here)
119
+ if (major < 12 && entry.includes('*')) continue
120
+ if (leftOut(at, type === 'directory')) {
87
121
  const folded = rel.toLowerCase()
88
- if (MUST_HAVE.test(entry) || named.some((kept) => kept === folded || kept.startsWith(`${folded}/`))) {
122
+ if ((major < 12 && MUST_HAVE.test(entry)) || named.some((kept) => kept === folded || kept.startsWith(`${folded}/`))) {
89
123
  throw new DeptreeError('whether pnpm installs it turns on rules of npm-packlist not followed here', here)
90
124
  }
91
125
  continue
@@ -33,6 +33,7 @@
33
33
 
34
34
  import { isExactVersion, satisfies, valid, validRange } from '@preventive/upstream/semver.js'
35
35
  import { DeptreeError, difference, quote } from '../error.js'
36
+ import { createMatcher } from '../matcher.js'
36
37
  import { KINDS, checkCatalogResolutions, checkLinkTargets, checkLinkedPackages, indexProjects, resolvedOf, sameSpecifier } from './frozen.js'
37
38
  import { checkProject } from './install.js'
38
39
  import { validForOldPackages } from './overrides.js'
@@ -61,7 +62,8 @@ export function readManifests(manifests, lockfile) {
61
62
  const where = `manifests[${quote(id)}]`
62
63
  if (!(id in lockfile.importers)) {
63
64
  checkProjectId(id, where)
64
- lockfile.importers[id] = { specifiers: {}, dependencies: {}, devDependencies: {}, optionalDependencies: {}, dependenciesMeta: {}, linkDirectory: true }
65
+ // `made`, as the lockfile has no importer for the project.
66
+ lockfile.importers[id] = { specifiers: {}, dependencies: {}, devDependencies: {}, optionalDependencies: {}, dependenciesMeta: {}, linkDirectory: true, made: true }
65
67
  }
66
68
  read.set(id, readManifest(text, where))
67
69
  }
@@ -157,13 +159,73 @@ export function pinnedPnpm(packageManager) {
157
159
  return isExactVersion(version) ? version : undefined
158
160
  }
159
161
 
162
+ // The package manager the root package.json pins as pnpm 12 reads it
163
+ // (wanted_package_manager): devEngines.packageManager over packageManager,
164
+ // the pnpm one of a list, else its first, whose own onFail it takes, else
165
+ // `error` for the last of a list and `ignore` for another; a version that
166
+ // is not a range is none, and packageManager's has to be exact.
167
+ function wantedPackageManager(manifest) {
168
+ const declared = manifest.devEngines?.packageManager
169
+ const list = Array.isArray(declared) ? declared : declared === undefined || declared === null ? [] : [declared]
170
+ const index = Array.isArray(declared) ? Math.max(list.findIndex((entry) => entry?.name === 'pnpm'), 0) : 0
171
+ const entry = list[index]
172
+ if (typeof entry?.name === 'string') {
173
+ const onFail = typeof entry.onFail === 'string' ? entry.onFail : Array.isArray(declared) ? (index === list.length - 1 ? 'error' : 'ignore') : undefined
174
+ const version = typeof entry.version === 'string' && validRange(entry.version) !== null ? entry.version : undefined
175
+ return { name: entry.name, version, devEngines: true, onFail }
176
+ }
177
+ if (typeof manifest.packageManager !== 'string') return undefined
178
+ const [, name, version] = /^(@?[^@]*)(?:@(.*))?$/u.exec(manifest.packageManager)
179
+ const exact = version === undefined ? undefined : version.replace(/\+.*$/u, '')
180
+ return { name, version: isExactVersion(exact) && valid(exact) === exact ? exact : undefined, devEngines: false, onFail: undefined }
181
+ }
182
+
183
+ // Whether the root package.json pins a pnpm that `pnpm` is, which makes
184
+ // pnpm 12 fail on a setting it does not know.
185
+ export function pinsPnpm(manifest, pnpm) {
186
+ const wanted = wantedPackageManager(manifest)
187
+ return wanted?.name === 'pnpm' && wanted.version !== undefined && satisfies(pnpm, wanted.version, { includePrerelease: true })
188
+ }
189
+
190
+ // pnpm 12 runs, of a pin that is not to be ignored, the pnpm the lockfile's
191
+ // env document records for it; it records a devEngines one, or a
192
+ // packageManager one of 12 or later, and a frozen install fails where it
193
+ // has none: there has to be one, of `pnpm` alone at the pinned version, or
194
+ // at the running one for a range it is in, and its package. The pnpm that
195
+ // runs has to be in the pin, which it would be switched from or refused
196
+ // by, unless it is only to warn. Another package manager fails it unless
197
+ // it is only to warn.
198
+ function checkPackageManager12(manifest, host, pmOnFail, env) {
199
+ const wanted = wantedPackageManager(manifest)
200
+ if (wanted === undefined) return
201
+ const where = `manifests["."].${wanted.devEngines ? 'devEngines.packageManager' : 'packageManager'}`
202
+ const onFail = pmOnFail ?? wanted.onFail ?? 'download'
203
+ if (onFail === 'ignore' || (onFail === 'warn' && wanted.name !== 'pnpm')) return
204
+ if (wanted.name !== 'pnpm') throw new DeptreeError(`the project is installed by ${quote(wanted.name)}, which pnpm 12 refuses`, where)
205
+ if (wanted.version === undefined) throw new DeptreeError('it names no version of pnpm, or none pnpm takes, which is not supported', where)
206
+ const running = satisfies(host.pnpm, wanted.version, { includePrerelease: true })
207
+ if (!running && onFail !== 'warn') throw new DeptreeError(`pnpm ${host.pnpm} is not in ${quote(wanted.version)}, which pnpm 12 switches from or refuses`, where)
208
+ if (!wanted.devEngines && Number(wanted.version.split('.')[0]) < 12) return
209
+ const version = isExactVersion(wanted.version) ? wanted.version : running ? host.pnpm : undefined
210
+ if (version === undefined) return
211
+ const recorded = env?.importers['.'].packageManagerDependencies ?? {}
212
+ const here = 'env.importers["."].packageManagerDependencies'
213
+ if (recorded.pnpm === undefined) throw new DeptreeError(`the lockfile records no pnpm for ${where}, which a frozen install of pnpm 12 fails on`, here)
214
+ for (const [name, key] of Object.entries(recorded)) {
215
+ if (key !== `${name}@${version}`) throw new DeptreeError(`${quote(key)} is not ${name} ${version}, which pnpm 12 would run or fail on`, `${here}.${name}`)
216
+ if (env.packages[key] === undefined) throw new DeptreeError(`the lockfile records no package ${quote(key)}, which a frozen install of pnpm 12 fails on`, `${here}.${name}`)
217
+ }
218
+ }
219
+
160
220
  // packageManager as pnpm's parsePackageManager reads it, held to be this
161
221
  // pnpm, exactly: pnpm switches to the version it names, and one it cannot
162
222
  // switch to, or another package manager, is refused rather than run over.
163
223
  // pnpm 11 reads devEngines.packageManager over it, and switches to the
164
224
  // pnpm the lockfile pins for that, which is refused; either is passed
165
225
  // over where what to do on a mismatch, or pmOnFail, is to warn or ignore.
166
- function checkPackageManager(manifest, host, pmOnFail) {
226
+ // pnpm 12's are checkPackageManager12's.
227
+ function checkPackageManager(manifest, host, pmOnFail, env) {
228
+ if (host.major >= 12) return checkPackageManager12(manifest, host, pmOnFail, env)
167
229
  const { packageManager } = manifest
168
230
  if (host.major >= 11) {
169
231
  const where = 'manifests["."].devEngines.packageManager'
@@ -210,22 +272,40 @@ function checkRuntimes(manifest, where, { host, root, onFail }) {
210
272
  }
211
273
  }
212
274
 
275
+ // Whether pnpm 12 takes a project to have dependencies, where the lockfile
276
+ // has no importer for it: any of its devDependencies, or of its
277
+ // dependencies and optionalDependencies, but optional ones it ignores, as
278
+ // its package.json has them; its peers are none.
279
+ function dependsOn(manifest, ignored) {
280
+ const names = (deps) => (deps !== null && typeof deps === 'object' ? Object.keys(deps) : [])
281
+ const optional = new Set(names(manifest.optionalDependencies).filter((name) => ignored(name)))
282
+ return names(manifest.devDependencies).length > 0 || [...names(manifest.dependencies), ...names(manifest.optionalDependencies)].some((name) => !optional.has(name))
283
+ }
284
+
213
285
  // `hook` is hook.js's, which each package.json is read through; `host`
214
- // and `settings` what its engines are held to.
215
- export function checkProjects(lockfile, manifests, { hook, host, settings }) {
216
- checkPackageManager(manifests.get('.'), host, settings.pmOnFail)
286
+ // and `settings` what its engines are held to; `env` the lockfile's env
287
+ // document, where it has one. pnpm 12 holds a project the lockfile has no
288
+ // importer for to none, if it has no dependencies, and refuses it
289
+ // otherwise; and makes none of pnpm 11's further checks.
290
+ export function checkProjects(lockfile, manifests, { hook, host, settings, env }) {
291
+ checkPackageManager(manifests.get('.'), host, settings.pmOnFail, env)
292
+ const ignored = createMatcher(settings.ignoredOptionalDependencies ?? [])
217
293
  let index
218
294
  for (const [id, manifest] of manifests) {
219
295
  const where = `manifests[${quote(id)}]`
220
296
  const root = id === '.'
221
- checkProject(manifest, where, { host, settings })
297
+ checkProject(manifest, where, { host, settings, root })
222
298
  checkRuntimes(manifest, where, { host, root, onFail: root ? settings.runtimeOnFail : undefined })
223
- const hooked = hook(manifest, where, { dir: id })
224
299
  const importer = lockfile.importers[id]
300
+ if (host.major >= 12 && importer.made) {
301
+ if (dependsOn(manifest, ignored)) throw new DeptreeError('the lockfile has no importer for this project, which has dependencies, and pnpm 12 refuses it', where)
302
+ continue
303
+ }
304
+ const hooked = hook(manifest, where, { dir: id })
225
305
  const reason = mismatch(importer, hooked, settings.autoInstallPeers, host.major, where)
226
306
  if (reason !== undefined) throw new DeptreeError(`the lockfile is not up to date with this package.json, which a frozen install refuses: ${reason}`, where)
227
307
  checkLinkTargets({ id, manifest: hooked, importer }, where)
228
- if (host.major < 11) continue
308
+ if (host.major !== 11) continue
229
309
  checkCatalogResolutions(importer, lockfile.catalogs, where)
230
310
  index ??= indexProjects(manifests)
231
311
  checkLinkedPackages({ manifest: hooked, importer, index, linkWorkspacePackages: settings.linkWorkspacePackages }, where)