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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/LICENSE-APACHE +202 -0
  2. package/npm.d.ts +288 -0
  3. package/npm.js +7 -0
  4. package/package.json +41 -12
  5. package/pnpm.d.ts +158 -25
  6. package/pnpm.js +2 -10
  7. package/soldeer.d.ts +13 -0
  8. package/soldeer.js +1 -7
  9. package/src/concurrent.js +16 -17
  10. package/src/error.js +5 -9
  11. package/src/glob.js +55 -0
  12. package/src/hash.js +60 -13
  13. package/src/manifest.js +52 -0
  14. package/src/matcher.js +60 -10
  15. package/src/mount.js +104 -44
  16. package/src/npm/bins.js +54 -0
  17. package/src/npm/compat.js +97 -0
  18. package/src/npm/dev-engines.js +52 -0
  19. package/src/npm/graph.js +68 -0
  20. package/src/npm/inputs.js +75 -0
  21. package/src/npm/manifests.js +130 -0
  22. package/src/npm/package.js +57 -0
  23. package/src/npm/ratio.js +34 -0
  24. package/src/npm/settings.js +67 -0
  25. package/src/npm/tree.js +136 -0
  26. package/src/npm/versions.js +20 -0
  27. package/src/npmrc.js +24 -0
  28. package/src/patch.js +22 -41
  29. package/src/pnpm/bins.js +152 -131
  30. package/src/pnpm/checks.js +8 -35
  31. package/src/pnpm/commands.js +53 -96
  32. package/src/pnpm/filename.js +10 -21
  33. package/src/pnpm/frozen.js +29 -75
  34. package/src/pnpm/gitignore.js +139 -0
  35. package/src/pnpm/glob.js +65 -0
  36. package/src/pnpm/graph.js +15 -43
  37. package/src/pnpm/hoist.js +68 -61
  38. package/src/pnpm/hoisted.js +234 -0
  39. package/src/pnpm/hook.js +26 -31
  40. package/src/pnpm/inputs.js +73 -56
  41. package/src/pnpm/install.js +85 -131
  42. package/src/pnpm/local.js +21 -40
  43. package/src/pnpm/minimatch.js +319 -0
  44. package/src/pnpm/nm-hoist-graph.js +299 -0
  45. package/src/pnpm/nm-hoist.js +149 -0
  46. package/src/pnpm/npm-packlist.js +217 -0
  47. package/src/pnpm/overrides.js +11 -51
  48. package/src/pnpm/package.js +121 -0
  49. package/src/pnpm/packlist.js +169 -109
  50. package/src/pnpm/patches.js +18 -25
  51. package/src/pnpm/projects.js +63 -107
  52. package/src/pnpm/readers.js +63 -83
  53. package/src/pnpm/settings.js +64 -100
  54. package/src/pnpm/tree.js +197 -169
  55. package/src/pnpm/uptodate.js +16 -28
  56. package/src/pnpm/workspace.js +124 -133
  57. package/src/project.js +57 -0
  58. package/src/soldeer/config.js +8 -24
  59. package/src/soldeer/inputs.js +5 -15
  60. package/src/soldeer/tree.js +22 -60
  61. package/src/soldeer/zip.js +17 -36
  62. package/src/tarball.js +55 -140
  63. package/src/yarn1/bins.js +9 -19
  64. package/src/yarn1/compat.js +9 -28
  65. package/src/yarn1/hoist.js +53 -68
  66. package/src/yarn1/inputs.js +19 -61
  67. package/src/yarn1/manifest.js +8 -23
  68. package/src/yarn1/package.js +24 -63
  69. package/src/yarn1/peers.js +8 -15
  70. package/src/yarn1/requests.js +19 -37
  71. package/src/yarn1/resolve.js +36 -113
  72. package/src/yarn1/settings.js +15 -43
  73. package/src/yarn1/tree.js +67 -91
  74. package/yarn1.d.ts +34 -5
  75. package/yarn1.js +1 -8
  76. package/src/pnpm/npmrc.js +0 -31
  77. package/src/pnpm/project.js +0 -97
  78. package/src/yarn1/glob.js +0 -52
  79. /package/{LICENSE → LICENSE-MIT} +0 -0
package/src/pnpm/bins.js CHANGED
@@ -1,64 +1,19 @@
1
- // What pnpm does to the files its bins run (@pnpm/link-bins, and
2
- // bin-links' fixBin): no .bin directory is written here, but each file a
3
- // bin pnpm links runs is left as linking leaves it, executable and, where
4
- // it starts with a `#!` line that ends in CRLF, with that line ending in
5
- // LF instead. pnpm 10 gives it mode 0o755, and pnpm 11 adds 0o111 to its
6
- // mode where that lacks any of it, which for the 0o644 and 0o755 a
7
- // package's files have here is the same.
8
- //
9
- // pnpm links a package's commands (commands.js) into many .bin
10
- // directories, and runs fixBin on a file only where its command is the
11
- // one linked by that name there and the file is there:
12
- //
13
- // - each package's own node_modules/.bin, for its children's bins (or,
14
- // where a child is a `link:`, for whatever is beside it), and for those
15
- // of the packages it bundles;
16
- // - node_modules/.pnpm/node_modules/.bin, for what is privately hoisted;
17
- // - each project's node_modules/.bin, for its direct dependencies' bins,
18
- // and at the root, where anything is publicly hoisted, for all it holds,
19
- // a direct dependency's command over a hoisted one's; with pnpm 11 and
20
- // autoInstallPeers, then for the bins of each peer a direct dependency
21
- // requires, by names not linked there yet;
22
- // - where any patch is configured, each package that is patched or has
23
- // an install script is built, scripts or not, which links its own bins
24
- // beside its children's in its own .bin before its patch is applied.
25
- //
26
- // Where the command linked of two of one name is the first pnpm came to
27
- // by the order a directory is read in, which varies, and a file's mode or
28
- // text would turn on it, the tree is refused; so it is where that would
29
- // turn on the commands of a directory outside the tree that are not known
30
- // here, which may take any name: those of one a `link:` leads to whose
31
- // package.json is not given (local.js), or of a project or such a
32
- // directory by the files of its directories.bin. So is a patch that
33
- // would be applied between two links of a file whose fix it would
34
- // change: one that makes or removes a file a bin names, or one under a
35
- // directories.bin, changes a bin file with a CRLF `#!` line, or changes
36
- // the bins a package.json names, its own or a bundled package's, or the
37
- // name or version they are ranked by.
38
- //
39
- // pnpm 12 (its cmd-shim crate) only adds the 0o111 a file lacks, and
40
- // rewrites no `#!` line. It links each package's own bins into its own
41
- // .bin, beside its children's that the lockfile says have some and those
42
- // of the packages it bundles, in one go and with no build pass; a `link:`
43
- // child's take no part. A bin it links whose target is a directory fails
44
- // the install.
45
- //
46
- // pnpm 11 and 12 link a command named `node` without making its file
47
- // executable; but pnpm 12 links a project's bins again after it has
48
- // patched any package, publicly hoisted any with bins, or where its
49
- // dependencies' peers it installs have some, and there makes the file of
50
- // a direct dependency's `node` executable, where its link leads to it
51
- // already.
1
+ // The files pnpm's bin linking fixes (@pnpm/link-bins, bin-links' fixBin):
2
+ // no .bin is written, but the file of each command it links by a name is
3
+ // made executable and a CRLF ending its `#!` line made LF. pnpm 10 sets
4
+ // 0o755 and pnpm 11 adds 0o111, as pnpm 12 (its cmd-shim crate) does, which
5
+ // rewrites no `#!` line.
6
+ // Where any patch is configured, pnpm 10 and 11 build each package patched
7
+ // or with an install script, which links its own bins beside its children's
8
+ // before the patch; pnpm 12 links them together, with no build pass. pnpm 9
9
+ // links as pnpm 10 does, but fails on a bin that is a directory.
52
10
 
53
11
  import { DeptreeError, quote } from '../error.js'
12
+ import { crlfShebang, fixShebang } from '../tarball.js'
54
13
  import { UNKNOWN, binsOf, bundledCommands, commandsOf, compare, normalized, parseManifest } from './commands.js'
14
+ import { reachable } from './nm-hoist.js'
55
15
 
56
- const decoder = new TextDecoder('utf-8', { fatal: true })
57
-
58
- // Where the peers pnpm 11 links the bins of into a project's .bin lead:
59
- // each peer a direct dependency has that is not optional and that its
60
- // snapshot resolves, by the path of its link beside the dependency, in
61
- // the order of those paths.
16
+ // The peers whose bins pnpm 11 links into a project's .bin.
62
17
  function peersOf(children, nodes) {
63
18
  const byPath = new Map()
64
19
  for (const dir of children.values()) {
@@ -74,19 +29,14 @@ function peersOf(children, nodes) {
74
29
  return [...byPath.keys()].sort().map((path) => byPath.get(path))
75
30
  }
76
31
 
77
- // Whether pnpm builds a package, as its pkgRequiresBuild tells: an
78
- // install script, a binding.gyp, which pnpm 11 and 12 pass over where
79
- // package.json's gypfile is false, or a .hooks directory.
32
+ // pnpm's pkgRequiresBuild.
80
33
  export const requiresBuild = (manifest, files, major) => Boolean((manifest.scripts != null && (manifest.scripts.preinstall || manifest.scripts.install || manifest.scripts.postinstall))
81
34
  || (files.has('binding.gyp') && !(major >= 11 && manifest.gypfile === false)) || files.keys().some((path) => /^\.hooks[\\/]/u.test(path)))
82
35
 
83
- // What linking commands into .bin directories fixes: `fixed`, by each
84
- // file's path in the tree, the package it is in, and `contested`, of those
85
- // that may be fixed or not, the package and why. `link` links commands
86
- // into one directory, where `ordered` says whether the order they come in
87
- // is one pnpm always has, and `fixNode` whether pnpm 12 fixes the file of
88
- // a direct dependency's `node`; it gives back the names linked, and
89
- // whether others may be.
36
+ // `contested` holds the files that may be fixed or not, and why; `ordered`
37
+ // is whether pnpm always comes to the commands in this order; `fixNode`
38
+ // whether pnpm 12 links a project's bins again, which fixes a direct
39
+ // dependency's `node` already linked.
90
40
  function linker(major) {
91
41
  const fixed = new Map()
92
42
  const contested = new Map()
@@ -116,31 +66,39 @@ function linker(major) {
116
66
  return { fixed, contested, link }
117
67
  }
118
68
 
119
- // The files pnpm runs fixBin on, by the directory of the package they
120
- // are in: `nodes` is the graph by directory, each node with its package's
121
- // `files` and `manifest`; `projects` each project's package.json by its
122
- // directory, `direct` each project's direct dependencies, `links` every
123
- // link in the tree, `publicHoist` whether anything is publicly hoisted,
124
- // `building` whether any patch is configured, `peers` whether peers are
125
- // installed automatically, and `major` pnpm's major version.
126
- export function binTargets({ nodes, projects, direct, links, publicHoist, building, peers, major }) {
127
- const commandCache = new Map()
128
- const commandsOfDir = (dir, { normalize = false } = {}) => {
69
+ // The commands of a directory, read once for each package of `nodes`. A
70
+ // project or `link:` target, outside the tree: its bins may take any name
71
+ // where its package.json is not given (local.js).
72
+ function commandReader(nodes, projects, major) {
73
+ const cache = new Map()
74
+ return (dir, { normalize = false } = {}) => {
129
75
  const node = nodes.get(dir)
130
76
  if (node !== undefined) {
131
- if (!commandCache.has(dir)) commandCache.set(dir, commandsOf(dir, node.manifest, node.files, dir, quote(node.key), major))
132
- return commandCache.get(dir)
77
+ if (!cache.has(dir)) cache.set(dir, commandsOf(dir, node.manifest, node.files, dir, quote(node.key), major))
78
+ return cache.get(dir)
133
79
  }
134
- // A project, or a directory a `link:` leads to, whose bins are outside
135
- // the tree but may take a name; one whose package.json is not given
136
- // (local.js) may take any.
137
80
  const manifest = projects.get(dir)
138
81
  if (manifest === undefined) return [UNKNOWN]
139
82
  const where = `manifests[${quote(dir)}]`
140
83
  return commandsOf(dir, normalize ? normalized(manifest, where) : manifest, undefined, undefined, where, major)
141
84
  }
142
- // Whether pnpm 12 takes a node to have bins: the lockfile says it has,
143
- // or it is a directory, for which the lockfile says nothing.
85
+ }
86
+
87
+ // pnpm's preferDirectCmds for a project of `manifest`: of the commands of
88
+ // `entries`, [alias, directory], those of its own dependencies, then of the
89
+ // others those whose names none of them has.
90
+ function preferOwn(entries, manifest, commandsOfDir) {
91
+ const own = new Set(Object.keys({ ...manifest.devDependencies, ...manifest.dependencies, ...manifest.optionalDependencies }))
92
+ const commands = entries.flatMap(([alias, dir]) => commandsOfDir(dir).map((command) => ({ ...command, direct: own.has(alias) })))
93
+ const names = new Set(commands.filter((command) => command.direct).map(({ name }) => name))
94
+ return [...commands.filter((command) => command.direct), ...commands.filter((command) => !names.has(command.name))]
95
+ }
96
+
97
+ // The files pnpm runs fixBin on, as sets of paths by package directory.
98
+ // `building` is whether any patch is configured, `peers` autoInstallPeers.
99
+ export function binTargets({ nodes, projects, direct, links, publicHoist, building, peers, major }) {
100
+ const commandsOfDir = commandReader(nodes, projects, major)
101
+ // The lockfile has no hasBin for a directory, whose bins pnpm 12 reads.
144
102
  const mayHaveBin = (dir) => nodes.get(dir)?.pkg.hasBin === true || nodes.get(dir)?.pkg.resolution.type === 'directory'
145
103
 
146
104
  const { fixed, contested, link } = linker(major)
@@ -178,15 +136,13 @@ export function binTargets({ nodes, projects, direct, links, publicHoist, buildi
178
136
  if (id === '.' && publicHoist) {
179
137
  const manifest = projects.get('.')
180
138
  const own = new Set(Object.keys({ ...manifest.devDependencies, ...manifest.dependencies, ...manifest.optionalDependencies }))
139
+ const isOwn = (path) => own.has(path.slice('node_modules/'.length))
181
140
  // pnpm 12 links what it hoists of the projects apart, before.
182
141
  const entries = [...links].filter(([path, dir]) => /^node_modules\/(?:@[^/]+\/)?[^/@.][^/]*$/u.test(path)
183
- && (major < 12 || own.has(path.slice('node_modules/'.length)) || mayHaveBin(dir)))
184
- const commands = entries.flatMap(([path, dir]) => commandsOfDir(dir).map((command) => ({ ...command, direct: own.has(path.slice('node_modules/'.length)) })))
185
- const names = new Set(commands.filter((command) => command.direct).map(({ name }) => name))
186
- relink ||= major >= 12 && entries.some(([path, dir]) => !own.has(path.slice('node_modules/'.length)) && mayHaveBin(dir))
187
- linked = link([...commands.filter((command) => command.direct), ...commands.filter((command) => !command.direct && !names.has(command.name))], { ordered: false, where, fixNode: relink })
142
+ && (major < 12 || isOwn(path) || mayHaveBin(dir)))
143
+ relink ||= major >= 12 && entries.some(([path, dir]) => !isOwn(path) && mayHaveBin(dir))
144
+ linked = link(preferOwn(entries.map(([path, dir]) => [path.slice('node_modules/'.length), dir]), manifest, commandsOfDir), { ordered: false, where, fixNode: relink })
188
145
  } else {
189
- // pnpm 11 reads the bins only of what the lockfile says has some.
190
146
  const dirs = [...children.values()].filter((dir) => major < 11 || !nodes.has(dir) || (major >= 12 ? mayHaveBin(dir) : nodes.get(dir).pkg.hasBin))
191
147
  // pnpm 12 comes to them in the order of a map, which varies.
192
148
  linked = link(dirs.flatMap((dir) => commandsOfDir(dir)), { ordered: major < 12, where, fixNode: relink })
@@ -201,18 +157,107 @@ export function binTargets({ nodes, projects, direct, links, publicHoist, buildi
201
157
  return fixedFiles(nodes, fixed, contested, major)
202
158
  }
203
159
 
204
- // The files `fixed` has fixBin run on, by the directory of their package.
205
- // One `contested` may be or not, which is refused, unless it is fixed
206
- // anyway, is not there, or is one fixBin leaves as it is: executable, with
207
- // no CRLF `#!` line, and not patched.
160
+ // The copies of one package pnpm's build pass makes hardlinks of the one it
161
+ // builds (hoisted.js): what is fixed in that one before, as each copy's own
162
+ // bins are where pnpm fills its node_modules, is in all; and so is what is
163
+ // fixed in any after. Where the copies it may build differ in what is fixed
164
+ // before, so may the outcome. A phase's marks are `before` that, or not, or
165
+ // a function of the package's build saying which. pnpm 11 links no file
166
+ // under a node_modules (`keepsModules`), which each copy keeps its own of.
167
+ function linkCopies(builds, phases, fixed, contested, { keepsModules, major }) {
168
+ const ownerOf = new Map()
169
+ for (const build of builds) {
170
+ if (build.copies.length > 1) for (const copy of build.copies) ownerOf.set(copy, build)
171
+ }
172
+ const groups = new Map()
173
+ const mark = (target, owner, before, why) => {
174
+ const build = ownerOf.get(owner)
175
+ if (build === undefined || (keepsModules && target.slice(owner.length + 1).split('/').includes('node_modules'))) {
176
+ if (why === undefined) fixed.set(target, owner)
177
+ else contested.set(target, { owner, why })
178
+ return
179
+ }
180
+ if (!groups.has(build)) groups.set(build, [])
181
+ groups.get(build).push({ rel: target.slice(owner.length + 1), owner, before: typeof before === 'function' ? before(build) : before, why })
182
+ }
183
+ for (const { fixed: phaseFixed, contested: phaseContested, before } of phases) {
184
+ for (const [target, owner] of phaseFixed) mark(target, owner, before, undefined)
185
+ for (const [target, { owner, why }] of phaseContested) mark(target, owner, before, why)
186
+ }
187
+ for (const [build, marks] of groups) {
188
+ for (const rel of new Set(marks.map((item) => item.rel))) {
189
+ const mine = marks.filter((item) => item.rel === rel && (!item.before || build.candidates.includes(item.owner)))
190
+ const after = mine.filter((item) => !item.before)
191
+ const before = mine.filter((item) => item.before)
192
+ const beforeFixed = new Set(before.filter((item) => item.why === undefined).map(({ owner }) => owner))
193
+ let why
194
+ if (!after.some((item) => item.why === undefined)) {
195
+ why = after.find((item) => item.why !== undefined)?.why ?? before.find((item) => item.why !== undefined)?.why
196
+ if (why === undefined && beforeFixed.size === 0) continue
197
+ if (why === undefined && beforeFixed.size < build.candidates.length) why = `which of its copies pnpm ${major} builds, which turns on its build order and is not followed here`
198
+ }
199
+ for (const copy of build.copies) {
200
+ if (why === undefined) fixed.set(`${copy}/${rel}`, copy)
201
+ else contested.set(`${copy}/${rel}`, { owner: copy, why })
202
+ }
203
+ }
204
+ }
205
+ }
206
+
207
+ // pnpm's hoisted linker links the bins of each node_modules it fills, by
208
+ // what it reads there, as it fills it; then each project's again, with its
209
+ // links in, a command of its own dependencies over any other. `modules` are
210
+ // those node_modules, each with its packages by alias, and its project's
211
+ // package.json where it is a project's. Between the two, where any patch is
212
+ // configured, each package of `builds` links its dependencies' bins and its
213
+ // own (hoisted.js), as the isolated linker's build does.
214
+ export function hoistedBinTargets({ nodes, projects, modules, builds = [], hardlinks = true, keepsModules = false, idOf = (key) => key, major }) {
215
+ const commandsOfDir = commandReader(nodes, projects, major)
216
+ const filling = linker(major)
217
+ const projectsAgain = linker(major)
218
+ for (const { dir, entries, manifest } of modules) {
219
+ const where = quote(dir)
220
+ if (manifest === undefined) {
221
+ filling.link(entries.flatMap(([, target]) => commandsOfDir(target)), { ordered: false, where })
222
+ continue
223
+ }
224
+ projectsAgain.link(preferOwn(entries, manifest, commandsOfDir), { ordered: false, where })
225
+ }
226
+ // A package's dependencies are built first, but for those it is built
227
+ // before or after as they depend on each other.
228
+ // What each snapshot reaches through the copies' dependencies, each taken
229
+ // for the copies of its id.
230
+ const pkgs = new Map([...nodes.values()].map((node) => [idOf(node.key), node.pkg]))
231
+ const edges = (id) => Object.values({ ...pkgs.get(id)?.dependencies, ...pkgs.get(id)?.optionalDependencies }).map(idOf)
232
+ const building = builds.map((build) => {
233
+ const where = quote(build.key)
234
+ const node = nodes.get(build.candidates[0])
235
+ const { fixed, contested, link } = linker(major)
236
+ link([...build.children.filter(([, dir]) => nodes.get(dir)?.pkg.hasBin).flatMap(([, dir]) => commandsOfDir(dir)), ...commandsOfDir(node.dir)], { ordered: true, where })
237
+ if (node.pkg.bundledDependencies !== undefined) link(bundledCommands(node, where, major), { ordered: false, where })
238
+ // Not linked to the others, the copy built alone has its own bins fixed.
239
+ if (!hardlinks && build.candidates.length > 1) {
240
+ for (const [target, owner] of fixed) {
241
+ if (owner !== node.dir) continue
242
+ fixed.delete(target)
243
+ for (const copy of build.candidates) contested.set(`${copy}${target.slice(owner.length)}`, { owner: copy, why: 'which of its copies pnpm 10 builds, which turns on its build order and is not followed here' })
244
+ }
245
+ }
246
+ return { fixed, contested, before: (other) => other !== build && reachable(edges(idOf(other.key)), edges).has(idOf(build.key)) }
247
+ })
248
+ const fixed = new Map()
249
+ const contested = new Map()
250
+ linkCopies(hardlinks ? builds : [], [{ ...filling, before: true }, ...building, { ...projectsAgain, before: false }], fixed, contested, { keepsModules, major })
251
+ return fixedFiles(nodes, fixed, contested, major)
252
+ }
253
+
208
254
  function fixedFiles(nodes, fixed, contested, major) {
209
- // pnpm 12 reads the `#!` line of a file it links, and fails on one
210
- // that is a directory: `path`, in the package, '' for the package itself.
255
+ // pnpm 9 and 12 read each linked file's `#!` line. '' is the package.
211
256
  const checkDirectory = (node, path) => {
212
- if (major < 12) return
257
+ if (major >= 10 && major < 12) return
213
258
  const prefix = `${path}/`
214
259
  if (path === '' || node.files.keys().some((name) => name.startsWith(prefix))) {
215
- throw new DeptreeError(`its bin ${quote(path === '' ? '.' : path)} is a directory, which pnpm 12 fails on`, quote(node.key))
260
+ throw new DeptreeError(`its bin ${quote(path === '' ? '.' : path)} is a directory, which pnpm ${major} fails on`, quote(node.key))
216
261
  }
217
262
  }
218
263
  for (const [target, { owner, why }] of contested) {
@@ -220,8 +265,8 @@ function fixedFiles(nodes, fixed, contested, major) {
220
265
  const path = target.slice(owner.length + 1)
221
266
  checkDirectory(node, path)
222
267
  const file = node.files.get(path)
223
- const harmless = major >= 12 ? (file?.mode & 0o111) === 0o111 : file?.mode === 0o755 && !hasCrlfShebang(file.data) && node.pkg.patchHash === undefined
224
- if (fixed.has(target) || file?.data === undefined || harmless) continue
268
+ if (fixed.has(target) || file?.data === undefined) continue
269
+ if (executableMode(file.mode, major) === file.mode && (major >= 12 || (crlfShebang(file.data) === -1 && node.pkg.patchHash === undefined))) continue
225
270
  throw new DeptreeError(`whether pnpm makes ${quote(path)} executable turns on ${why}`, quote(node.key))
226
271
  }
227
272
  const byNode = new Map()
@@ -235,41 +280,18 @@ function fixedFiles(nodes, fixed, contested, major) {
235
280
  return byNode
236
281
  }
237
282
 
283
+ export const executableMode = (mode, major) => (major >= 11 ? mode | 0o111 : 0o755)
238
284
 
239
- // Whether a file starts with a `#!` line ending in CRLF, as fixBin reads
240
- // its first 2048 bytes to tell.
241
- function hasCrlfShebang(data) {
242
- if (data[0] !== 0x23 || data[1] !== 0x21) return false
243
- const newline = data.subarray(0, 2048).indexOf(0x0a)
244
- return newline >= 4 && data[newline - 1] === 0x0d
245
- }
246
-
247
- // fixBin on a file: mode 0o755, and a CRLF ending the `#!` line made LF.
248
- // pnpm reads and writes the file as UTF-8 to do that, which would change
249
- // one that is not; that is refused. pnpm 12 only adds the 0o111 it lacks.
285
+ // pnpm reads and writes the file as UTF-8, which changes one that is not.
250
286
  export function fixBin(file, where, major) {
251
- if (major >= 12) return { data: file.data, mode: file.mode | 0o111 }
252
- if (!hasCrlfShebang(file.data)) return { data: file.data, mode: 0o755 }
253
- try {
254
- decoder.decode(file.data)
255
- } catch {
256
- throw new DeptreeError('a bin with a CRLF `#!` line is not UTF-8, which pnpm would rewrite', where)
257
- }
258
- const newline = file.data.indexOf(0x0a)
259
- const data = new Uint8Array(file.data.length - 1)
260
- data.set(file.data.subarray(0, newline - 1))
261
- data.set(file.data.subarray(newline), newline - 1)
262
- return { data, mode: 0o755 }
287
+ const mode = executableMode(file.mode, major)
288
+ return { data: major >= 12 ? file.data : fixShebang(file.data, 'a bin with a CRLF `#!` line is not UTF-8, which pnpm would rewrite', where), mode }
263
289
  }
264
290
 
265
291
  const BIN_FIELDS = (manifest) => JSON.stringify([manifest.name, manifest.version, manifest.bin, manifest.directories?.bin])
266
292
 
267
- // A patch is applied between two times its package's bins are linked,
268
- // so it may not change what either does: `patched` is the package's files
269
- // with the patch applied, `targets` those that fixBin is run on. pnpm
270
- // reads package.json again after it, for the name and version a command
271
- // is taken by as well as the bins. It gives back the package's
272
- // package.json, patched, as parsed.
293
+ // A patch is applied between two links of its package's bins, so it may not
294
+ // change what either does. It gives back the patched package.json.
273
295
  export function checkPatchOfBins(node, patched, targets, where, major) {
274
296
  const file = patched.get('package.json')
275
297
  if (file?.data === undefined) throw new DeptreeError('the patch removes package.json', where)
@@ -284,12 +306,11 @@ export function checkPatchOfBins(node, patched, targets, where, major) {
284
306
  throw new DeptreeError(`the patch makes or removes ${quote(path)}, which a bin names, and which pnpm links before and after it`, where)
285
307
  }
286
308
  }
287
- // pnpm 12 rewrites no bin.
288
309
  if (major >= 12) return manifest
289
310
  for (const path of targets) {
290
311
  const before = node.files.get(path).data
291
312
  const after = patched.get(path).data
292
- if (before !== after && (hasCrlfShebang(before) || hasCrlfShebang(after))) throw new DeptreeError(`the patch changes ${quote(path)}, a bin with a CRLF \`#!\` line, which pnpm rewrites before and after it`, where)
313
+ if (before !== after && (crlfShebang(before) !== -1 || crlfShebang(after) !== -1)) throw new DeptreeError(`the patch changes ${quote(path)}, a bin with a CRLF \`#!\` line, which pnpm rewrites before and after it`, where)
293
314
  }
294
315
  return manifest
295
316
  }
@@ -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