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

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 (75) 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 +36 -10
  5. package/pnpm.d.ts +116 -22
  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 +64 -13
  13. package/src/manifest.js +52 -0
  14. package/src/matcher.js +59 -10
  15. package/src/mount.js +95 -44
  16. package/src/npm/bins.js +71 -0
  17. package/src/npm/compat.js +92 -0
  18. package/src/npm/dev-engines.js +52 -0
  19. package/src/npm/graph.js +68 -0
  20. package/src/npm/inputs.js +80 -0
  21. package/src/npm/manifests.js +138 -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/{pnpm/npmrc.js → npmrc.js} +5 -11
  28. package/src/patch.js +21 -31
  29. package/src/pnpm/bins.js +32 -103
  30. package/src/pnpm/checks.js +8 -35
  31. package/src/pnpm/commands.js +49 -86
  32. package/src/pnpm/filename.js +10 -21
  33. package/src/pnpm/frozen.js +27 -70
  34. package/src/pnpm/gitignore.js +137 -0
  35. package/src/pnpm/glob.js +65 -0
  36. package/src/pnpm/graph.js +15 -43
  37. package/src/pnpm/hoist.js +65 -55
  38. package/src/pnpm/hook.js +26 -31
  39. package/src/pnpm/inputs.js +67 -40
  40. package/src/pnpm/install.js +72 -115
  41. package/src/pnpm/local.js +21 -40
  42. package/src/pnpm/minimatch.js +328 -0
  43. package/src/pnpm/npm-packlist.js +221 -0
  44. package/src/pnpm/overrides.js +10 -49
  45. package/src/pnpm/package.js +120 -0
  46. package/src/pnpm/packlist.js +168 -109
  47. package/src/pnpm/patches.js +18 -25
  48. package/src/pnpm/projects.js +63 -107
  49. package/src/pnpm/readers.js +46 -55
  50. package/src/pnpm/settings.js +60 -97
  51. package/src/pnpm/tree.js +134 -149
  52. package/src/pnpm/uptodate.js +16 -28
  53. package/src/pnpm/workspace.js +122 -131
  54. package/src/project.js +41 -0
  55. package/src/soldeer/config.js +8 -24
  56. package/src/soldeer/inputs.js +2 -5
  57. package/src/soldeer/tree.js +20 -56
  58. package/src/soldeer/zip.js +17 -36
  59. package/src/tarball.js +38 -142
  60. package/src/yarn1/bins.js +9 -19
  61. package/src/yarn1/compat.js +9 -28
  62. package/src/yarn1/hoist.js +53 -68
  63. package/src/yarn1/inputs.js +16 -51
  64. package/src/yarn1/manifest.js +8 -23
  65. package/src/yarn1/package.js +24 -63
  66. package/src/yarn1/peers.js +8 -15
  67. package/src/yarn1/requests.js +19 -37
  68. package/src/yarn1/resolve.js +35 -111
  69. package/src/yarn1/settings.js +14 -41
  70. package/src/yarn1/tree.js +67 -91
  71. package/yarn1.d.ts +30 -2
  72. package/yarn1.js +1 -8
  73. package/src/pnpm/project.js +0 -97
  74. package/src/yarn1/glob.js +0 -52
  75. /package/{LICENSE → LICENSE-MIT} +0 -0
package/src/pnpm/hook.js CHANGED
@@ -1,29 +1,17 @@
1
- // What pnpm 10 makes of a package.json before it reads its dependencies:
2
- // its read-package hook (@pnpm/hooks.read-package-hook), less what is
3
- // refused elsewhere — package extensions and a pnpmfile's readPackage —
4
- // and less Yarn's compatibility database, which adds dependencies to a few
5
- // packages by name and is not reproduced. Overrides replace or, as `-`,
6
- // remove what the package asks for (createVersionsOverrider), and
7
- // ignoredOptionalDependencies removes the optional dependencies it names.
8
- // pnpm runs it on every package it resolves and on every project's
9
- // package.json before holding it to its importer, which is why a project
10
- // overridden is recorded as overridden.
11
- //
12
- // An override to a directory, `link:` or `file:`, is written into a
13
- // project relative to it, as pnpm writes it; pnpm 11 takes a path alone
14
- // for one too, where pnpm 10 writes it as it is. pnpm 11 also converges
15
- // where no override is chosen (overrides.js), and drops a peer's
16
- // peerDependenciesMeta with the peer.
1
+ // pnpm's read-package hook (@pnpm/hooks.read-package-hook), less package
2
+ // extensions and a pnpmfile's readPackage, refused elsewhere, and Yarn's
3
+ // compatibility database, not reproduced. pnpm runs it on every package and on
4
+ // each project's package.json before holding it to its importer.
17
5
 
18
6
  import { intersects, satisfies, validRange } from '@preventive/upstream/semver.js'
19
7
  import { relative } from '@preventive/vfs/path.js'
20
8
  import { DeptreeError } from '../error.js'
9
+ import { checkNesting } from '../manifest.js'
21
10
  import { createMatcher } from '../matcher.js'
22
11
 
23
12
  const KINDS = ['dependencies', 'optionalDependencies', 'devDependencies']
24
13
 
25
- // pnpm's isIntersectingRange: no range is any, one range is itself, and
26
- // two ranges meet where some version is in both.
14
+ // pnpm's isIntersectingRange.
27
15
  const meets = (range, spec) => !range || spec === range || (validRange(spec) !== null && validRange(range) !== null && intersects(spec, range))
28
16
 
29
17
  // pickMostSpecificVersionOverride, with its own comparator and sort: the
@@ -32,8 +20,6 @@ const mostSpecific = (overrides) => overrides.sort((a, b) => (meets(b.target.ran
32
20
 
33
21
  const isPeerRange = (spec) => validRange(spec) !== null || spec.includes('workspace:') || spec.includes('catalog:')
34
22
 
35
- // An override to a directory, `local` as listOverrides has it, as pnpm
36
- // writes it into the project at `dir`: relative to it.
37
23
  function localSpec({ protocol, dir: to }, dir) {
38
24
  const path = relative(dir, to) || '.'
39
25
  return protocol === '' && !path.startsWith('.') ? `./${path}` : `${protocol}${path}`
@@ -49,13 +35,16 @@ function checkFields(manifest, where) {
49
35
  }
50
36
  }
51
37
 
52
- // `overrides` is listOverrides's; `ignored` the ignoredOptionalDependencies
53
- // patterns; `major` pnpm's major version. The hook hands back a changed
54
- // copy of a package.json; `dir` is the directory of a project's, where an
55
- // override to a path is written into it as pnpm writes it, and undefined
56
- // for a package's, whose specifiers are not read.
38
+ // semver's satisfies, which throws a TypeError on a version that is neither
39
+ // a string nor falsy, as pnpm then fails.
40
+ function inParentRange(version, range, where) {
41
+ if (version && typeof version !== 'string') throw new DeptreeError('not a string, which pnpm fails on where an override names this package with a range', `${where}.version`)
42
+ return satisfies(version, range)
43
+ }
44
+
45
+ // The hook's `dir` is a project's directory, which directory overrides are
46
+ // written relative to; undefined for a package.
57
47
  export function createHook({ overrides, ignored, major = 10 }) {
58
- // Each list by the name it overrides, in order.
59
48
  const byName = (list) => Map.groupBy(list, ({ target }) => target.name)
60
49
  const withParent = overrides.filter(({ parent }) => parent !== undefined)
61
50
  const generic = byName(overrides.filter(({ parent, converge }) => parent === undefined && !converge))
@@ -63,16 +52,18 @@ export function createHook({ overrides, ignored, major = 10 }) {
63
52
  const isIgnored = createMatcher(ignored)
64
53
  return (manifest, where, { dir } = {}) => {
65
54
  checkFields(manifest, where)
66
- const copy = structuredClone(manifest)
55
+ const copy = structuredClone(checkNesting(manifest, where))
67
56
  if (overrides.length > 0) {
68
- const scoped = byName(withParent.filter(({ parent }) => parent.name === copy.name && (!parent.range || satisfies(copy.version, parent.range))))
57
+ const scoped = byName(withParent.filter(({ parent }) => parent.name === copy.name && (!parent.range || inParentRange(copy.version, parent.range, where))))
69
58
  const pick = (list, spec) => mostSpecific((list ?? []).filter(({ target }) => meets(target.range, spec)))
70
59
  const override = (deps, peers) => {
71
60
  for (const [name, spec] of Object.entries(peers ?? deps)) {
72
61
  const chosen = pick(scoped.get(name), spec) ?? pick(generic.get(name), spec)
73
62
  const version = converging.get(name)
74
- if (chosen === undefined && version !== undefined && validRange(spec, { loose: true }) !== null && satisfies(version, spec, { loose: true })) (peers ?? deps)[name] = version
75
- if (chosen === undefined) continue
63
+ if (chosen === undefined) {
64
+ if (version !== undefined && validRange(spec, { loose: true }) !== null && satisfies(version, spec, { loose: true })) (peers ?? deps)[name] = version
65
+ continue
66
+ }
76
67
  if (chosen.spec === '-') {
77
68
  delete (peers ?? deps)[name]
78
69
  if (peers !== undefined && major >= 11) delete copy.peerDependenciesMeta?.[name]
@@ -85,7 +76,11 @@ export function createHook({ overrides, ignored, major = 10 }) {
85
76
  }
86
77
  }
87
78
  for (const kind of KINDS) if (copy[kind] !== undefined) override(copy[kind])
88
- if (copy.peerDependencies !== undefined) override(copy.dependencies ??= {}, copy.peerDependencies)
79
+ if (copy.peerDependencies !== undefined) {
80
+ // pnpm 9 overrides a peer in place whatever the override is.
81
+ if (major < 10) override(copy.peerDependencies)
82
+ else override(copy.dependencies ??= {}, copy.peerDependencies)
83
+ }
89
84
  }
90
85
  for (const name of Object.keys(copy.optionalDependencies ?? {})) {
91
86
  if (!isIgnored(name)) continue
@@ -1,32 +1,66 @@
1
- // What buildPnpmTree and findPnpmProjects take, checked: the host pnpm
2
- // would install on, and the files an install reads — given as text, or
3
- // read from the project (project.js) as pnpm reads them from disk.
1
+ // The host and the files an install reads, checked, as given or read from the
2
+ // project as pnpm reads them from disk.
4
3
 
5
4
  import { LockfileError, YamlError, parsePnpmLockfile } from '@preventive/lockfile/pnpm.js'
6
5
  import { parseYaml } from '@preventive/lockfile/yaml.js'
7
6
  import { compareVersions, valid } from '@preventive/upstream/semver.js'
7
+ import { normalize } from '@preventive/vfs/path.js'
8
8
  import { DeptreeError, quote } from '../error.js'
9
- import { checkProject, readManifestTexts, readPatches, readRootFiles, readText, readWorkspaceText, typeOf } from './project.js'
10
- import { pinnedPnpm, readManifest, readManifests } from './projects.js'
9
+ import { readManifest } from '../manifest.js'
10
+ import { checkProject, readText, typeOf } from '../project.js'
11
+ import { pinnedPnpm, readManifests } from './projects.js'
11
12
  import { readers } from './readers.js'
12
13
  import { checkWorkspace, findProjects, linkedManifest } from './workspace.js'
13
14
 
14
15
  const LIBC = new Set(['glibc', 'musl', 'unknown'])
15
16
 
16
- // The major version of `pnpm`, 10, 11 or 12: what pnpm 11 and 12 do
17
- // differently is read by it, where it is. pnpm 12 before 12.8.1 installs
18
- // otherwise in places, and is refused.
17
+ const MISNAMED = ['pnpm-workspaces.yaml', 'pnpm-workspaces.yml', 'pnpm-workspace.yml', '.pnpm-workspace.yaml', '.pnpm-workspace.yml', '.pnpm-workspaces.yaml', '.pnpm-workspaces.yml']
18
+ const MISNAMED_9 = ['pnpm-workspace.yml']
19
+ function readWorkspaceText(project, major) {
20
+ const text = readText(project, '/pnpm-workspace.yaml')
21
+ const misnamed = text === undefined ? (major < 10 ? MISNAMED_9 : MISNAMED).find((name) => typeOf(project, `/${name}`) === 'file') : undefined
22
+ if (misnamed !== undefined) throw new DeptreeError('pnpm refuses a workspace manifest not named pnpm-workspace.yaml', quote(misnamed))
23
+ return text
24
+ }
25
+
26
+ // pnpm-workspace.yaml is read once the pnpm that installs is known.
27
+ function readRootFiles(project) {
28
+ const lockfile = readText(project, '/pnpm-lock.yaml')
29
+ if (lockfile === undefined) throw new DeptreeError('the project has no pnpm-lock.yaml, which a frozen install cannot do without')
30
+ return { lockfile, npmrc: readText(project, '/.npmrc') }
31
+ }
32
+
33
+ function readManifestTexts(project, ids) {
34
+ const texts = new Map()
35
+ for (const id of ids) {
36
+ texts.set(id, readText(project, id === '.' ? '/package.json' : `/${id}/package.json`, `manifests[${quote(id)}]`))
37
+ }
38
+ return texts
39
+ }
40
+
41
+ // By path from the lockfile's directory; one that is not there is left out.
42
+ function readPatches(project, configured) {
43
+ const texts = new Map()
44
+ for (const [selector, spec] of Object.entries(configured ?? {})) {
45
+ const path = normalize(spec)
46
+ if (path === '..' || path.startsWith('../') || path.startsWith('/')) throw new DeptreeError(`the patch ${quote(spec)} is not in the project: it is outside the lockfile's directory`, `patchedDependencies[${quote(selector)}]`)
47
+ const text = readText(project, `/${path}`, `patches[${quote(path)}]`)
48
+ if (text !== undefined) texts.set(path, text)
49
+ }
50
+ return texts
51
+ }
52
+
53
+ // pnpm 9 before 9.15.0, and pnpm 12 before 12.8.1, install otherwise in
54
+ // places, and are refused.
55
+ const SINCE = { 9: '9.15.0', 12: '12.8.1' }
19
56
  function majorOf(pnpm) {
20
57
  const major = Number(valid(pnpm)?.split('.')[0])
21
- if (major !== 10 && major !== 11 && major !== 12) throw new DeptreeError(`pnpm ${quote(pnpm)} is not supported: only pnpm 10, 11 and 12 are`, 'host.pnpm')
22
- if (major === 12 && compareVersions(pnpm, '12.8.1') < 0) throw new DeptreeError(`pnpm ${quote(pnpm)} is not supported: pnpm 12 is from 12.8.1 on`, 'host.pnpm')
58
+ if (major < 9 || major > 12 || !Number.isInteger(major)) throw new DeptreeError(`pnpm ${quote(pnpm)} is not supported: only pnpm 9, 10, 11 and 12 are`, 'host.pnpm')
59
+ if (major in SINCE && compareVersions(pnpm, SINCE[major]) < 0) throw new DeptreeError(`pnpm ${quote(pnpm)} is not supported: pnpm ${major} is from ${SINCE[major]} on`, 'host.pnpm')
23
60
  return major
24
61
  }
25
62
 
26
- // The pnpm that installs, and its major version: `pnpm`, host.pnpm, or
27
- // where that is left out, the one the root package.json pins by its
28
- // packageManager, which is the only one it takes; `root` gives that
29
- // package.json as parsed, and is called only then.
63
+ // `root` reads the root package.json, only where host.pnpm is left out.
30
64
  function pnpmOf(pnpm, root) {
31
65
  if (pnpm !== undefined && (typeof pnpm !== 'string' || pnpm === '')) throw new TypeError('host.pnpm must be a non-empty string, or left out')
32
66
  const version = pnpm ?? pinnedPnpm(root()?.packageManager)
@@ -57,19 +91,16 @@ function readNamed(file, read, text) {
57
91
  }
58
92
  }
59
93
 
60
- // Before any install, pnpm 11 may write the env document alone, with no lockfile to install from.
61
94
  export function readLockfile(text) {
62
95
  const read = readNamed('pnpm-lock.yaml', parsePnpmLockfile, text)
63
96
  if (read.lockfile === undefined) throw new DeptreeError('it holds the env document pnpm 11 writes alone, not the project\'s lockfile, which a frozen install cannot do without', 'pnpm-lock.yaml')
64
97
  return read
65
98
  }
66
99
 
67
- // pnpm-workspace.yaml as parsed; one of comments alone, or nothing, sets
68
- // nothing, as pnpm reads it.
100
+ // One of comments alone, or empty, sets nothing, as pnpm reads it.
69
101
  const readWorkspace = (text) => (text === undefined || /^(?:[\t ]*(?:#.*)?(?:\r?\n|$))*$/u.test(text) ? undefined : readNamed('pnpm-workspace.yaml', parseYaml, text))
70
102
 
71
- // The root package.json in `project`, as parsed, where there is one; one
72
- // that is a link is refused unread, as findProjects refuses it.
103
+ // A root package.json that is a link is refused unread, as findProjects does.
73
104
  const ROOT = 'manifests["."]'
74
105
  function readRoot(project) {
75
106
  if (typeOf(project, '/package.json', false) === 'symlink') throw linkedManifest('package.json')
@@ -77,39 +108,40 @@ function readRoot(project) {
77
108
  return text === undefined ? undefined : readManifest(text, ROOT)
78
109
  }
79
110
 
80
- // pnpm-workspace.yaml as pnpm 12 has it where there is none: of the root
81
- // package.json's `workspaces` list, its strings but empty ones are the
82
- // packages, as pnpm 12 writes a pnpm-workspace.yaml of them. `text` is
83
- // pnpm-workspace.yaml's, and `workspace` as parsed.
111
+ // Where there is no pnpm-workspace.yaml, pnpm 12 writes one whose packages are
112
+ // the root package.json's non-empty `workspaces` strings. pnpm 9 takes one that
113
+ // sets nothing for `**`.
84
114
  function workspaceOf(workspace, text, major, root) {
115
+ if (major < 10) {
116
+ const mapping = workspace !== null && typeof workspace === 'object' && !Array.isArray(workspace)
117
+ if (text !== undefined && (workspace == null || (mapping && Object.keys(workspace).length === 0))) return { packages: ['**'] }
118
+ if (mapping && !workspace.packages) throw new DeptreeError('pnpm 9 fails on a workspace manifest that sets anything and no packages', 'pnpm-workspace.yaml: packages')
119
+ return workspace
120
+ }
85
121
  if (major < 12 || text !== undefined) return workspace
86
122
  const listed = root()?.workspaces
87
123
  const packages = Array.isArray(listed) ? listed.filter((pattern) => typeof pattern === 'string' && pattern !== '') : []
88
124
  return packages.length === 0 ? workspace : { packages }
89
125
  }
90
126
 
91
- // The packages of `workspace`, pnpm-workspace.yaml as parsed, read here
92
- // before the rest of it is, to find the projects by.
127
+ // Read before the rest of pnpm-workspace.yaml, to find the projects by.
93
128
  function packagesOf(workspace) {
94
129
  if (workspace !== undefined && (workspace === null || typeof workspace !== 'object' || Array.isArray(workspace))) throw new DeptreeError('expected a mapping', 'pnpm-workspace.yaml')
95
130
  return workspace?.packages === undefined ? undefined : readers.globs(workspace.packages, 'pnpm-workspace.yaml: packages')
96
131
  }
97
132
 
98
- // The directories of the projects pnpm finds in `project`, which
99
- // buildPnpmTree takes the package.json of each of.
133
+ // The directories of the projects whose package.json buildPnpmTree takes.
100
134
  export function findPnpmProjects(options) {
101
135
  const { project, host } = options ?? {}
102
136
  checkProject(project)
103
137
  if (host !== undefined && (host === null || typeof host !== 'object')) throw new TypeError('host must be an object, or left out')
104
138
  const { major } = pnpmOf(host?.pnpm, () => readRoot(project))
105
- const text = readWorkspaceText(project)
139
+ const text = readWorkspaceText(project, major)
106
140
  return findProjects(project, packagesOf(workspaceOf(readWorkspace(text), text, major, () => readRoot(project))), major)
107
141
  }
108
142
 
109
143
  const LOCKFILE = 'lockfile must be the text of pnpm-lock.yaml, or left out with a project given to read it from'
110
144
 
111
- // The files an install reads but the package.json and the patches: given,
112
- // with `lockfile`, or read from `project` without it.
113
145
  export function inputsOf(options) {
114
146
  const { lockfile, manifests, workspace, npmrc, patches, project } = options
115
147
  if (lockfile === undefined) {
@@ -126,30 +158,25 @@ export function inputsOf(options) {
126
158
  return { reading: false, lockfile, manifests, workspace, npmrc, patches }
127
159
  }
128
160
 
129
- // The package.json of every project, the pnpm that installs as pnpmOf
130
- // reads it from the root one, and pnpm-workspace.yaml as workspaceOf has
131
- // it: as given, or read from the project for each project pnpm finds
132
- // there, and no other, as pnpm reads them: an importer the globs do not
133
- // take is refused first, and one they take that pnpm does not find has
134
- // none. `pnpm` is host.pnpm.
161
+ // An importer the globs do not take is refused first; one they take that pnpm
162
+ // does not find has no package.json.
135
163
  export function manifestsOf(inputs, lockfile, pnpm) {
136
- const workspace = readWorkspace(inputs.workspace)
137
164
  if (!inputs.reading) {
165
+ const workspace = readWorkspace(inputs.workspace)
138
166
  const manifests = readManifests(inputs.manifests, lockfile)
139
167
  const installs = pnpmOf(pnpm, () => manifests.get('.'))
140
168
  return { manifests, ...installs, workspace: workspaceOf(workspace, inputs.workspace, installs.major, () => manifests.get('.')) }
141
169
  }
142
170
  const { project } = inputs
143
171
  const installs = pnpmOf(pnpm, () => readRoot(project))
144
- const effective = workspaceOf(workspace, inputs.workspace, installs.major, () => readRoot(project))
172
+ const text = readWorkspaceText(project, installs.major)
173
+ const effective = workspaceOf(readWorkspace(text), text, installs.major, () => readRoot(project))
145
174
  const packages = packagesOf(effective)
146
175
  checkWorkspace(Object.keys(lockfile.importers), packages, installs.major)
147
176
  const ids = findProjects(project, packages, installs.major)
148
177
  return { manifests: readManifests(readManifestTexts(project, ids), lockfile), ...installs, workspace: effective }
149
178
  }
150
179
 
151
- // The patch files, by path: as given, or read from the project, each that
152
- // `configured`, patchedDependencies, names.
153
180
  export function patchesOf(inputs, configured) {
154
181
  if (inputs.reading) return readPatches(inputs.project, configured)
155
182
  const entries = inputs.patches instanceof Map ? [...inputs.patches] : Object.entries(inputs.patches ?? {})
@@ -1,41 +1,15 @@
1
- // Which snapshots a pnpm 10 install from a frozen lockfile leaves out: an
2
- // optional one that the host cannot run — its `os`, `cpu` or `libc` do not
3
- // take the host's, or its `engines.node` does not take the Node pnpm runs
4
- // on — and an optional one first reached, depth first from the projects,
5
- // through a snapshot that is not installed. That first reach decides it,
6
- // as it does for pnpm (@pnpm/lockfile.filtering's pkgAllDeps, then
7
- // @pnpm/deps.graph-builder checking every snapshot again), so a snapshot
8
- // reached both ways is left out or not by which way is walked first.
9
- // Anything else incompatible is installed, as pnpm installs it with a
10
- // warning, or refused where `engineStrict` has pnpm refuse it.
11
- //
12
- // The projects are walked in the lockfile's order, which is the order of
13
- // their directories; pnpm walks them in the order it finds them on disk,
14
- // which only a snapshot reached both ways as above can tell apart.
15
- //
16
- // pnpm 11 walks breadth first, and by the kind of each edge rather than of
17
- // each snapshot: an optional dependency the host cannot run is not taken,
18
- // and what a taken package requires is taken whether the host can run it
19
- // or not, with a warning; each reachable snapshot not taken is left out.
20
- // Where a package of an optional edge names no os, cpu or libc, pnpm 11
21
- // infers them from its name, as `@nx/nx-win32-arm64-msvc` names win32.
22
- //
23
- // 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.
1
+ // Which snapshots a frozen install leaves out. pnpm 9 and 10 walk depth first
2
+ // (@pnpm/lockfile.filtering's pkgAllDeps) and leave out an optional one the
3
+ // host cannot run or first reached through one not installed: the first
4
+ // reach decides. Projects go in lockfile order, pnpm's being the order it
5
+ // finds them on disk, which only a snapshot reached both ways can tell.
30
6
 
31
7
  import { satisfies, validRange } from '@preventive/upstream/semver.js'
32
8
  import { DeptreeError, quote } from '../error.js'
33
9
 
34
- // pnpm's checkList: `any` alone is anything, `!x` excludes, and a list of
35
- // exclusions alone takes what none of them names. pnpm 10 counts the
36
- // exclusions over every value the host is taken to be, so a list of them
37
- // takes nothing where the host is taken to be more than one thing; pnpm 11
38
- // does not. pnpm 12 takes, of each value, the first item that names it.
10
+ // pnpm's checkList. pnpm 10 counts the exclusions over every value the host
11
+ // is taken to be, so a list of them alone takes nothing where there is more
12
+ // than one. pnpm 12 takes, of each value, the first item that names it.
39
13
  function checkList(values, list, major) {
40
14
  if (list.length === 1 && list[0] === 'any') return true
41
15
  let match = false
@@ -54,8 +28,7 @@ function checkList(values, list, major) {
54
28
  return match || (major >= 11 ? list.every((item) => item.startsWith('!')) : excluded === list.length)
55
29
  }
56
30
 
57
- // pnpm 11's inferPlatformFromPackageName: the os, cpu and libc the words
58
- // of a package's name, less its scope, say it is for.
31
+ // pnpm 11's inferPlatformFromPackageName, from the unscoped name's words.
59
32
  const OS_WORDS = { __proto__: null, aix: 'aix', android: 'android', darwin: 'darwin', macos: 'darwin', osx: 'darwin', freebsd: 'freebsd', linux: 'linux', netbsd: 'netbsd', openbsd: 'openbsd', openharmony: 'openharmony', sunos: 'sunos', win32: 'win32', windows: 'win32' }
60
33
  const CPU_WORDS = { __proto__: null, arm: 'arm', armv6: 'arm', armv7: 'arm', arm64: 'arm64', aarch64: 'arm64', ia32: 'ia32', loong64: 'loong64', mips64el: 'mips64el', ppc64: 'ppc64', ppc64le: 'ppc64', riscv64: 'riscv64', s390x: 's390x', x64: 'x64', amd64: 'x64', wasm32: 'wasm32' }
61
34
  const LIBC_WORDS = { __proto__: null, glibc: 'glibc', gnu: 'glibc', gnueabihf: 'glibc', musl: 'musl', musleabihf: 'musl' }
@@ -77,8 +50,6 @@ function effectivePlatform(pkg) {
77
50
  return { ...pkg, os: pkg.os ?? inferred.os, cpu: pkg.cpu ?? inferred.cpu, libc: pkg.libc ?? inferred.libc }
78
51
  }
79
52
 
80
- // What the host is taken to be: itself, or what `supportedArchitectures`
81
- // lists for each, `current` standing for itself.
82
53
  const current = (value, supported) => (supported ?? ['current']).map((item) => (item === 'current' ? value : item))
83
54
 
84
55
  const takesPlatform = (pkg, host, supported) => checkList(current(host.os, supported?.os), pkg.os ?? ['any'], host.major)
@@ -87,34 +58,27 @@ const takesPlatform = (pkg, host, supported) => checkList(current(host.os, suppo
87
58
 
88
59
  const takesEngine = (engines, node) => !engines.node || satisfies(node, engines.node, { includePrerelease: true })
89
60
 
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.
61
+ // pnpm 12 drops what of a range it cannot parse, where npm's semver takes
62
+ // none of it, and reads a `-` range with an `x` as taking nothing.
93
63
  function checkSure12(range, where, what = 'its engines.node') {
94
64
  if (typeof range !== 'string' || (validRange(range) !== null && !(/\s-\s/u.test(range) && /[*xX]/u.test(range)))) return
95
65
  throw new DeptreeError(`${what}, ${quote(range)}, pnpm 12 reads otherwise than npm's semver, which is not supported`, where)
96
66
  }
97
67
 
98
- // The Node pnpm checks engines.node against: nodeVersion, else the Node
99
- // the root's engines.runtime pins, else the host's.
100
68
  const nodeOf = ({ host, settings }) => settings.nodeVersion ?? settings.runtimeNodeVersion ?? host.node
101
69
 
102
- // With engineStrict, pnpm 11 passes over the engines the lockfile records
103
- // of a patched snapshot, and holds those of its package.json, once
104
- // patched, to the Node instead.
70
+ // With engineStrict, pnpm 11 checks a patched snapshot's engines from its
71
+ // package.json, once patched, rather than from the lockfile.
105
72
  const patchedLater = ({ host, settings }) => host.major >= 11 && settings.engineStrict === true
106
73
 
107
- // A package.json's os, cpu or libc as pnpm's checkList reads one: a string
108
- // is a list of it, and what is not a string in a list is passed over.
74
+ // A package.json's os, cpu or libc as pnpm's checkList reads one.
109
75
  function platformList(value, where) {
110
- if (value === undefined || value === null) return undefined
76
+ if (value == null) return undefined
111
77
  if (typeof value === 'string') return [value]
112
78
  if (Array.isArray(value)) return value.filter((item) => typeof item === 'string')
113
79
  throw new DeptreeError('expected a string or a list of them', where)
114
80
  }
115
81
 
116
- // pnpm 12 checks the root project's engines.node, where it is text, with
117
- // engineStrict alone.
118
82
  function checkRoot12(manifest, where, context) {
119
83
  const range = manifest.engines?.node
120
84
  if (!context.settings.engineStrict || typeof range !== 'string' || range === '') return
@@ -123,21 +87,18 @@ function checkRoot12(manifest, where, context) {
123
87
  if (!takesEngine({ node: range }, node)) throw new DeptreeError(`its engines.node, ${quote(range)}, does not take Node ${node}, which engineStrict refuses`, where)
124
88
  }
125
89
 
126
- // A project as pnpm's packageIsInstallable holds it when it reads its
127
- // package.json: a platform the host is not only warns, and so does an
128
- // engines.node it does not take, unless engineStrict; an engines.pnpm it
129
- // does not take is always refused. As pnpm has it, a platform that does
130
- // not match is found first, and the engines are then not looked at.
131
- // pnpm 12's is checkRoot12, of the root alone.
90
+ // As pnpm's packageIsInstallable: a platform the host is not only warns, and
91
+ // is found first, so the engines are then not checked. pnpm 9 holds it to the
92
+ // host alone, before it reads supportedArchitectures.
132
93
  export function checkProject(manifest, where, { host, settings, root }) {
133
94
  if (host.major >= 12) {
134
95
  if (root) checkRoot12(manifest, where, { host, settings })
135
96
  return
136
97
  }
137
98
  const platform = { os: platformList(manifest.os, `${where}.os`), cpu: platformList(manifest.cpu, `${where}.cpu`), libc: platformList(manifest.libc, `${where}.libc`) }
138
- if (!takesPlatform(platform, host, settings.supportedArchitectures)) return
139
- const engines = manifest.engines
140
- if (engines === undefined || engines === null) return
99
+ if (!takesPlatform(platform, host, host.major < 10 ? undefined : settings.supportedArchitectures)) return
100
+ const { engines } = manifest
101
+ if (engines == null) return
141
102
  const node = nodeOf({ host, settings })
142
103
  if (engines.pnpm && !satisfies(host.pnpm, engines.pnpm, { includePrerelease: true })) {
143
104
  throw new DeptreeError(`its engines.pnpm, ${quote(String(engines.pnpm))}, does not take pnpm ${host.pnpm}, which pnpm refuses`, where)
@@ -145,14 +106,10 @@ export function checkProject(manifest, where, { host, settings, root }) {
145
106
  if (settings.engineStrict && !takesEngine(engines, node)) throw new DeptreeError(`its engines.node, ${quote(String(engines.node))}, does not take Node ${node}, which engineStrict refuses`, where)
146
107
  }
147
108
 
148
- // A check of one snapshot: true where the host can run it, false where it
149
- // is optional and cannot, and null where it cannot but is not optional and
150
- // is installed anyway. With `engineStrict` that last is refused, where the
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.
109
+ // True where the host can run a snapshot, false where it is optional and
110
+ // cannot, null where it cannot but is installed anyway. pnpm 11 and 12 take
111
+ // `optional` from the edge. A patched one's engines are checked later
112
+ // (patchedLater), save its published ones by pnpm 12 `walking` the lockfile.
156
113
  function createCheck(context) {
157
114
  const { host, settings } = context
158
115
  const node = nodeOf(context)
@@ -168,24 +125,20 @@ function createCheck(context) {
168
125
  }
169
126
  }
170
127
 
171
- // A check of a patched package's package.json, once patched, where pnpm
172
- // makes one (patchedLater): it fails where the Node is not taken; where
173
- // the package is optional, it removes it from the tree it has linked,
174
- // which is refused too. pnpm 12 holds it to nodeVersion, else the host's
175
- // Node, whatever the root's engines.runtime pins.
128
+ // pnpm removes an optional package that fails this from the tree already
129
+ // linked, also refused. pnpm 12 passes over engines.runtime's Node here.
176
130
  export function createPatchedCheck(context) {
177
131
  if (!patchedLater(context)) return undefined
178
132
  const { host, settings } = context
179
133
  const node = host.major >= 12 ? settings.nodeVersion ?? host.node : nodeOf(context)
180
134
  return (manifest, where) => {
181
- if (manifest.engines === undefined || manifest.engines === null) return
135
+ if (manifest.engines == null) return
182
136
  if (host.major >= 12) checkSure12(manifest.engines.node, where, 'the engines.node of its package.json, patched')
183
137
  if (takesEngine(manifest.engines, node)) return
184
138
  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)
185
139
  }
186
140
  }
187
141
 
188
- // A reference's key, or the project a `link:` leads to, if it is one.
189
142
  function split(targets, lockfile) {
190
143
  const keys = []
191
144
  const projects = []
@@ -196,30 +149,33 @@ function split(targets, lockfile) {
196
149
  return { keys, projects }
197
150
  }
198
151
 
199
- // Every snapshot a project depends on, then those of the projects it
200
- // links to that were not walked yet, as pnpm's toImporterDepPaths lists
201
- // them: dependencies, devDependencies, optionalDependencies.
152
+ // As pnpm's toImporterDepPaths lists them: a project's snapshots, then those
153
+ // of projects it links to not walked yet. A loop where pnpm recurses, so a
154
+ // long chain of projects runs out of no stack.
202
155
  function projectKeys(lockfile, ids, walked) {
203
- const targets = ids.flatMap((id) => {
204
- const importer = lockfile.importers[id]
205
- return Object.values({ ...importer.dependencies, ...importer.devDependencies, ...importer.optionalDependencies })
206
- })
207
- const { keys, projects } = split(targets, lockfile)
208
- const more = projects.filter((id) => !walked.has(id))
209
- for (const id of more) walked.add(id)
210
- return more.length === 0 ? keys : [...keys, ...projectKeys(lockfile, more, walked)]
156
+ const all = []
157
+ for (let more = ids; more.length > 0;) {
158
+ const targets = more.flatMap((id) => {
159
+ const importer = lockfile.importers[id]
160
+ return Object.values({ ...importer.dependencies, ...importer.devDependencies, ...importer.optionalDependencies })
161
+ })
162
+ const { keys, projects } = split(targets, lockfile)
163
+ for (const key of keys) all.push(key)
164
+ more = projects.filter((id) => !walked.has(id))
165
+ for (const id of more) walked.add(id)
166
+ }
167
+ return all
211
168
  }
212
169
 
213
- // pnpm 11's filterLockfileByImportersAndEngine: the snapshots left out,
214
- // and those installed although the host cannot run them.
170
+ // pnpm 11's filterLockfileByImportersAndEngine, breadth first by edge kind:
171
+ // an optional edge to a snapshot the host cannot run is not taken, a
172
+ // required one is, with a warning; each reachable one not taken is left out.
215
173
  function skippedSnapshots11(lockfile, check) {
216
174
  const edgesOf = (deps, optional) => Object.values(deps).filter((target) => !target.startsWith('link:')).map((key) => ({ key, optional }))
217
175
  const queue = Object.values(lockfile.importers).flatMap((importer) => [...edgesOf(importer.dependencies, false), ...edgesOf(importer.devDependencies, false), ...edgesOf(importer.optionalDependencies, true)])
218
176
  const starts = queue.map(({ key }) => key)
219
177
  const installed = new Set()
220
178
  const required = new Set()
221
- // Each snapshot reached, and whether the host cannot run it, as checked
222
- // where an optional edge first reaches it.
223
179
  const reached = new Map()
224
180
  for (let i = 0; i < queue.length; i++) {
225
181
  const { key, optional } = queue[i]
@@ -232,27 +188,22 @@ function skippedSnapshots11(lockfile, check) {
232
188
  queue.push(...edgesOf(pkg.dependencies, false), ...edgesOf(pkg.optionalDependencies, true))
233
189
  }
234
190
  const skipped = new Set()
235
- const warned = new Set()
191
+ const incompatible = new Set()
236
192
  for (const key of reached.keys()) {
237
- const pkg = lockfile.packages[key]
238
- const ok = check(key, pkg, !installed.has(key) || !required.has(key))
193
+ const ok = check(key, lockfile.packages[key], !installed.has(key) || !required.has(key))
239
194
  if (ok === false) skipped.add(key)
240
- if (ok === null) warned.add(key)
195
+ if (ok === null) incompatible.add(key)
241
196
  }
242
- const seen = new Set()
243
- for (let i = 0; i < starts.length; i++) {
244
- const key = starts[i]
245
- if (seen.has(key)) continue
246
- seen.add(key)
197
+ const seen = new Set(starts)
198
+ for (const key of seen) {
247
199
  const pkg = lockfile.packages[key]
248
200
  if (!installed.has(key) && pkg.optional) skipped.add(key)
249
- starts.push(...[...edgesOf(pkg.dependencies), ...edgesOf(pkg.optionalDependencies)].map((edge) => edge.key))
201
+ for (const edge of [...edgesOf(pkg.dependencies), ...edgesOf(pkg.optionalDependencies)]) seen.add(edge.key)
250
202
  }
251
- return { skipped, incompatible: warned }
203
+ return { skipped, incompatible }
252
204
  }
253
205
 
254
- // The keys of the snapshots left out, and of those installed although the
255
- // host cannot run them, the host and settings being `context`'s.
206
+ // The snapshots left out, and those installed that the host cannot run.
256
207
  export function skippedSnapshots(lockfile, context) {
257
208
  const check = createCheck(context)
258
209
  if (context.host.major >= 11) return skippedSnapshots11(lockfile, check)
@@ -260,20 +211,26 @@ export function skippedSnapshots(lockfile, context) {
260
211
  const walked = new Set(ids)
261
212
  const picked = new Set()
262
213
  const skipped = new Set()
263
- const visit = (keys, parentInstallable) => {
264
- for (const key of keys) {
265
- if (picked.has(key)) continue
266
- const pkg = lockfile.packages[key]
267
- const installable = parentInstallable && check(key, pkg) !== false
268
- if (!installable && pkg.optional) skipped.add(key)
269
- picked.add(key)
270
- const { keys: next, projects } = split(Object.values({ ...pkg.dependencies, ...pkg.optionalDependencies }), lockfile)
271
- for (const id of projects) walked.add(id)
272
- visit([...next, ...projectKeys(lockfile, projects, walked)], installable)
214
+ // Depth first, as pnpm recurses, on a stack of its own: a chain of
215
+ // snapshots as long as a lockfile can make runs out of none.
216
+ const stack = [{ keys: projectKeys(lockfile, ids, walked).values(), installable: true }]
217
+ while (stack.length > 0) {
218
+ const { keys, installable: parentInstallable } = stack.at(-1)
219
+ const { value: key, done } = keys.next()
220
+ if (done) {
221
+ stack.pop()
222
+ continue
273
223
  }
224
+ if (picked.has(key)) continue
225
+ const pkg = lockfile.packages[key]
226
+ const installable = parentInstallable && check(key, pkg) !== false
227
+ if (!installable && pkg.optional) skipped.add(key)
228
+ picked.add(key)
229
+ const { keys: next, projects } = split(Object.values({ ...pkg.dependencies, ...pkg.optionalDependencies }), lockfile)
230
+ for (const id of projects) walked.add(id)
231
+ stack.push({ keys: [...next, ...projectKeys(lockfile, projects, walked)].values(), installable })
274
232
  }
275
- visit(projectKeys(lockfile, ids, walked), true)
276
- // The graph is built of the rest, each checked again on its own.
233
+ // @pnpm/deps.graph-builder checks the rest again, each on its own.
277
234
  const incompatible = new Set()
278
235
  for (const key of picked) {
279
236
  if (skipped.has(key)) continue