@preventive/deptree 1.0.0-alpha.1 → 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.
package/src/pnpm/bins.js CHANGED
@@ -6,11 +6,7 @@
6
6
  // mode where that lacks any of it, which for the 0o644 and 0o755 a
7
7
  // package's files have here is the same.
8
8
  //
9
- // A package's bins are its `bin` — a path, or paths by command — or, with
10
- // none, every file under `directories.bin` that is not a dotfile or in a
11
- // dot directory, each by its name. A command that is not a name, or a
12
- // path that leads out of the package, is passed over, as pnpm 11 passes
13
- // over one named `.` or `..` or nothing. pnpm links bins into many .bin
9
+ // pnpm links a package's commands (commands.js) into many .bin
14
10
  // directories, and runs fixBin on a file only where its command is the
15
11
  // one linked by that name there and the file is there:
16
12
  //
@@ -27,14 +23,10 @@
27
23
  // an install script is built, scripts or not, which links its own bins
28
24
  // beside its children's in its own .bin before its patch is applied.
29
25
  //
30
- // Of two commands of one name in a directory, pnpm links the one the
31
- // package of that name has (with pnpm 11, or npm's `npx`, and pnpm's
32
- // `pn`, `pnpx`, `pnx` and `pnpm`), else the one of the package whose name
33
- // sorts last (by `localeCompare`, read here as English), else of the
34
- // later version, else the first it came to. Where that first is by the
35
- // order a directory is read in, which varies, and a file's mode or text
36
- // would turn on it, the tree is refused; so it is where that would turn
37
- // on the commands of a directory outside the tree that are not known
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
38
30
  // here, which may take any name: those of one a `link:` leads to whose
39
31
  // package.json is not given (local.js), or of a project or such a
40
32
  // directory by the files of its directories.bin. So is a patch that
@@ -43,189 +35,26 @@
43
35
  // directories.bin, changes a bin file with a CRLF `#!` line, or changes
44
36
  // the bins a package.json names, its own or a bundled package's, or the
45
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.
46
52
 
47
- import { compareVersions, valid } from '@preventive/upstream/semver.js'
48
- import { basename, join } from '@preventive/vfs/path.js'
49
53
  import { DeptreeError, quote } from '../error.js'
54
+ import { UNKNOWN, binsOf, bundledCommands, commandsOf, compare, normalized, parseManifest } from './commands.js'
50
55
 
51
- // A package whose directory has no package.json is still linked by these
52
- // names by pnpm 10, to the runtime's binary inside it.
53
- const RUNTIMES = { __proto__: null, node: 'bin/node', deno: 'deno', bun: 'bun' }
54
-
55
- // The packages pnpm 11 takes to own a command by a name not their own.
56
- const OWNERS = { __proto__: null, npx: ['npm'], pn: ['pnpm', '@pnpm/exe'], pnpm: ['@pnpm/exe'], pnpx: ['pnpm', '@pnpm/exe'], pnx: ['pnpm', '@pnpm/exe'] }
57
-
58
- const collator = new Intl.Collator('en')
59
56
  const decoder = new TextDecoder('utf-8', { fatal: true })
60
57
 
61
- // The path `rel` names in the package at `dir`, as path.join spells it, or
62
- // undefined where it is not in the package, as is-subdir tells; the
63
- // package itself is `dir`, which is `.` for the root project.
64
- function inPackage(dir, rel, where) {
65
- if (typeof rel !== 'string') throw new DeptreeError(`${quote(String(rel))} is not a path, which pnpm fails on`, where)
66
- const path = join(dir, rel)
67
- if (path.endsWith('/')) throw new DeptreeError(`${quote(rel)} ends in a slash, which is not supported`, where)
68
- const inside = dir === '.' ? path !== '..' && !path.startsWith('../') : path === dir || path.startsWith(`${dir}/`)
69
- return inside ? path : undefined
70
- }
71
-
72
- // A command that stands for those of a directory whose bins are not known
73
- // here, which may take any name.
74
- const UNKNOWN = { unknown: true }
75
-
76
- // The files of `files` under `dir`, as tinyglobby's `**` finds them: none
77
- // a dotfile or in a dot directory. `files` holds paths relative to `base`.
78
- function filesUnder(files, base, dir) {
79
- const prefix = dir === base ? '' : `${dir.slice(base.length + 1)}/`
80
- const found = []
81
- for (const [path, file] of files) {
82
- if (file.data === undefined || !path.startsWith(prefix)) continue
83
- const rest = path.slice(prefix.length)
84
- if (rest.split('/').some((name) => name.startsWith('.'))) continue
85
- found.push(rest)
86
- }
87
- return found
88
- }
89
-
90
- // Whether pnpm takes a package to own a command, which wins it the name.
91
- // pnpm 11 looks a name up among OWNERS as a plain object's key, and fails
92
- // on one that Object.prototype has.
93
- function owns({ name, pkgName, own }, major, where) {
94
- if (own || name === pkgName) return true
95
- if (major < 11) return false
96
- if (name in Object.prototype) throw new DeptreeError(`two bins are named ${quote(name)}, which pnpm 11 fails on`, where)
97
- return OWNERS[name]?.includes(pkgName) === true
98
- }
99
-
100
- // pnpm's runtimeHasNodeDownloaded: whether engines.runtime asks for a Node
101
- // to download, the first runtime of a list named node deciding. pnpm fails
102
- // on a list with nothing in it where it reads one, before that.
103
- function downloadsNode(runtime, where) {
104
- if (!runtime) return false
105
- if (!Array.isArray(runtime)) return runtime.name === 'node' && runtime.onFail === 'download'
106
- for (const item of runtime) {
107
- if (item === null || item === undefined) throw new DeptreeError('its engines.runtime lists nothing where pnpm reads a runtime, which pnpm fails on', where)
108
- if (item.name === 'node') return item.onFail === 'download'
109
- }
110
- return false
111
- }
112
-
113
- // A package's commands: `dir` is where the package is, `manifest` its
114
- // package.json, `files` the files of the package holding it (itself, or
115
- // the one that bundles it), by their paths under `base`. Both are
116
- // undefined for a directory outside the tree, a project or one a `link:`
117
- // leads to, whose commands are known only where its package.json names
118
- // them, and whose files are not fixed.
119
- function commandsOf(dir, manifest, files, base, where, major) {
120
- const common = { pkgName: manifest.name, pkgVersion: manifest.version, owner: base }
121
- if (typeof manifest.bin === 'string' && !manifest.name) throw new DeptreeError('it has a bin and no name, which pnpm fails on', where)
122
- // pnpm looks for the Node it would download from where the package is,
123
- // and fails where there is none.
124
- if (downloadsNode(manifest.engines?.runtime, where)) {
125
- throw new DeptreeError('its engines.runtime has pnpm look for a Node to run its bins with, which is not supported', where)
126
- }
127
- if (manifest.bin) {
128
- const entries = typeof manifest.bin === 'string' ? [[manifest.name, manifest.bin]] : Object.entries(manifest.bin)
129
- const commands = []
130
- for (const [command, rel] of entries) {
131
- const name = command[0] === '@' ? command.slice(command.indexOf('/') + 1) : command
132
- if (name !== encodeURIComponent(name) && name !== '$') continue
133
- if (name === '' || name === '.' || name === '..') {
134
- if (major >= 11) continue
135
- throw new DeptreeError(`a bin named ${quote(name)} is not supported`, where)
136
- }
137
- const target = inPackage(dir, rel, where)
138
- if (target !== undefined) commands.push({ ...common, name, target })
139
- }
140
- return commands
141
- }
142
- const binDir = manifest.directories?.bin
143
- if (!binDir) return []
144
- const root = inPackage(dir, binDir, where)
145
- if (root === undefined) return []
146
- if (files === undefined) return [UNKNOWN]
147
- const found = filesUnder(files, base, root)
148
- // tinyglobby lists them in the order the directories are read in,
149
- // which decides between two of one name.
150
- const names = found.map((path) => basename(path))
151
- return found.map((path, i) => ({ ...common, name: names[i], target: `${root}/${path}`, unordered: names.indexOf(names[i]) !== names.lastIndexOf(names[i]) }))
152
- }
153
-
154
- // compareCommandsInConflict, which pnpm keeps the greater of.
155
- function compare(a, b, where, major) {
156
- const aOwns = owns(a, major, where)
157
- const bOwns = owns(b, major, where)
158
- if (aOwns !== bOwns) return aOwns ? 1 : -1
159
- if (a.pkgName !== b.pkgName) {
160
- if (typeof a.pkgName !== 'string' || typeof b.pkgName !== 'string') throw new DeptreeError(`two bins named ${quote(a.name)} are of packages not both named, which pnpm fails on`, where)
161
- return collator.compare(a.pkgName, b.pkgName)
162
- }
163
- if (valid(a.pkgVersion) === null || valid(b.pkgVersion) === null) throw new DeptreeError(`two bins named ${quote(a.name)} are of ${quote(a.pkgName)} at versions pnpm cannot compare, which it fails on`, where)
164
- return compareVersions(a.pkgVersion, b.pkgVersion)
165
- }
166
-
167
- // A package.json as normalize-package-data leaves one, or refuses it,
168
- // where pnpm reads one with readPackageJson: its name trimmed, and each
169
- // of its name and version one npm takes.
170
- function normalized(manifest, where) {
171
- const name = manifest.name ? manifest.name : ''
172
- if (typeof name !== 'string') throw new DeptreeError('its package.json\'s name is not a string, which pnpm fails on', where)
173
- const trimmed = name.trim()
174
- const scoped = /^@([^/]+)\/([^/]+)$/u.exec(trimmed)
175
- const encoded = (part) => part === encodeURIComponent(part)
176
- const validName = trimmed === '' || (!trimmed.startsWith('.') && trimmed.toLowerCase() !== 'node_modules' && trimmed.toLowerCase() !== 'favicon.ico'
177
- && ((scoped !== null && encoded(scoped[1]) && encoded(scoped[2])) || (!/[/@\s+%:]/u.test(trimmed) && encoded(trimmed))))
178
- if (!validName) throw new DeptreeError(`its package.json's name, ${quote(trimmed)}, is one pnpm fails on`, where)
179
- const version = manifest.version ? valid(manifest.version, { loose: true }) : ''
180
- if (version === null) throw new DeptreeError(`its package.json's version, ${quote(String(manifest.version))}, is one pnpm fails on`, where)
181
- return { ...manifest, name: trimmed, version }
182
- }
183
-
184
- // The packages a package bundles, as pnpm's readModulesDir finds them in
185
- // its node_modules: each directory there not named with a leading dot,
186
- // and each in a scope's directory.
187
- function bundledIn(files) {
188
- const names = new Set()
189
- for (const path of files.keys()) {
190
- const parts = path.split('/')
191
- if (parts[0] !== 'node_modules' || parts.length < 3 || parts[1].startsWith('.')) continue
192
- if (!parts[1].startsWith('@')) names.add(parts[1])
193
- else if (parts.length > 3) names.add(`${parts[1]}/${parts[2]}`)
194
- }
195
- return [...names]
196
- }
197
-
198
- function parseManifest(file, where) {
199
- try {
200
- const manifest = JSON.parse(decoder.decode(file.data))
201
- if (manifest === null || typeof manifest !== 'object' || Array.isArray(manifest)) throw new Error('not an object')
202
- return manifest
203
- } catch {
204
- throw new DeptreeError('a package.json pnpm reads for its bins is not JSON, which pnpm fails on', where)
205
- }
206
- }
207
-
208
- // The commands of the packages `node` bundles. For one with no
209
- // package.json pnpm 11 reads the bins of the package.json of the nearest
210
- // directory above it whose publishConfig.directory it is, as far up as the
211
- // filesystem goes, which is refused.
212
- function bundledCommands(node, where, major) {
213
- const commands = []
214
- for (const name of bundledIn(node.files)) {
215
- const here = `${where}: node_modules/${name}`
216
- const file = node.files.get(`node_modules/${name}/package.json`)
217
- const dir = `${node.dir}/node_modules/${name}`
218
- if (file !== undefined) {
219
- commands.push(...commandsOf(dir, normalized(parseManifest(file, here), here), node.files, node.dir, here, major))
220
- } else if (major >= 11) {
221
- throw new DeptreeError('it bundles a package with no package.json, whose bins pnpm 11 looks for above it', here)
222
- } else if (name in RUNTIMES) {
223
- commands.push({ name, target: `${dir}/${RUNTIMES[name]}`, owner: node.dir, own: true, pkgName: '', pkgVersion: '' })
224
- }
225
- }
226
- return commands
227
- }
228
-
229
58
  // Where the peers pnpm 11 links the bins of into a project's .bin lead:
230
59
  // each peer a direct dependency has that is not optional and that its
231
60
  // snapshot resolves, by the path of its link beside the dependency, in
@@ -246,9 +75,46 @@ function peersOf(children, nodes) {
246
75
  }
247
76
 
248
77
  // Whether pnpm builds a package, as its pkgRequiresBuild tells: an
249
- // install script, a binding.gyp or a .hooks directory.
250
- const hasInstallScript = (manifest, files) => Boolean((manifest.scripts != null && (manifest.scripts.preinstall || manifest.scripts.install || manifest.scripts.postinstall))
251
- || files.has('binding.gyp') || files.keys().some((path) => /^\.hooks[\\/]/u.test(path)))
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.
80
+ export const requiresBuild = (manifest, files, major) => Boolean((manifest.scripts != null && (manifest.scripts.preinstall || manifest.scripts.install || manifest.scripts.postinstall))
81
+ || (files.has('binding.gyp') && !(major >= 11 && manifest.gypfile === false)) || files.keys().some((path) => /^\.hooks[\\/]/u.test(path)))
82
+
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.
90
+ function linker(major) {
91
+ const fixed = new Map()
92
+ const contested = new Map()
93
+ const link = (commands, { ordered, where, fixNode = false }) => {
94
+ const unknown = commands.some((command) => command.unknown)
95
+ const best = new Map()
96
+ for (const command of commands) {
97
+ if (command.unknown) continue
98
+ const tied = best.get(command.name)
99
+ const order = tied === undefined ? 1 : compare(command, tied[0], where, major)
100
+ if (order > 0) best.set(command.name, [command])
101
+ else if (order === 0) tied.push(command)
102
+ }
103
+ for (const tied of best.values()) {
104
+ // pnpm 11 and 12 link `node` with no fix of its file.
105
+ if (major >= 11 && tied[0].name === 'node' && !(fixNode && tied.every((command) => command.direct !== false))) continue
106
+ const unordered = new Set(tied.map(({ target }) => target)).size > 1 && (!ordered || tied.some((command) => command.unordered))
107
+ for (const { target, owner } of unknown || unordered ? tied : tied.slice(0, 1)) {
108
+ if (owner === undefined) continue
109
+ if (unknown) contested.set(target, { owner, why: 'the bins of a directory outside the tree, which are not known here' })
110
+ else if (unordered) contested.set(target, { owner, why: 'the order it reads a directory in, which varies' })
111
+ else fixed.set(target, owner)
112
+ }
113
+ }
114
+ return { names: new Set(best.keys()), unknown }
115
+ }
116
+ return { fixed, contested, link }
117
+ }
252
118
 
253
119
  // The files pnpm runs fixBin on, by the directory of the package they
254
120
  // are in: `nodes` is the graph by directory, each node with its package's
@@ -273,44 +139,28 @@ export function binTargets({ nodes, projects, direct, links, publicHoist, buildi
273
139
  const where = `manifests[${quote(dir)}]`
274
140
  return commandsOf(dir, normalize ? normalized(manifest, where) : manifest, undefined, undefined, where, major)
275
141
  }
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.
144
+ const mayHaveBin = (dir) => nodes.get(dir)?.pkg.hasBin === true || nodes.get(dir)?.pkg.resolution.type === 'directory'
276
145
 
277
- // By each file's path in the tree, the package it is in; and of those
278
- // that may be fixed or not, why.
279
- const fixed = new Map()
280
- const contested = new Map()
281
- // One linking of commands into a directory: `ordered` says whether the
282
- // order they come in is one pnpm always has. It gives back the names
283
- // linked, and whether others may be.
284
- const link = (commands, { ordered, where }) => {
285
- const unknown = commands.some((command) => command.unknown)
286
- const best = new Map()
287
- for (const command of commands) {
288
- if (command.unknown) continue
289
- const tied = best.get(command.name)
290
- const order = tied === undefined ? 1 : compare(command, tied[0], where, major)
291
- if (order > 0) best.set(command.name, [command])
292
- else if (order === 0) tied.push(command)
293
- }
294
- for (const tied of best.values()) {
295
- const unordered = new Set(tied.map(({ target }) => target)).size > 1 && (!ordered || tied.some((command) => command.unordered))
296
- for (const { target, owner } of unknown || unordered ? tied : tied.slice(0, 1)) {
297
- if (owner === undefined) continue
298
- if (unknown) contested.set(target, { owner, why: 'the bins of a directory outside the tree, which are not known here' })
299
- else if (unordered) contested.set(target, { owner, why: 'the order it reads a directory in, which varies' })
300
- else fixed.set(target, owner)
301
- }
302
- }
303
- return { names: new Set(best.keys()), unknown }
304
- }
146
+ const { fixed, contested, link } = linker(major)
305
147
 
306
148
  for (const node of nodes.values()) {
307
149
  const where = quote(node.key)
308
150
  const children = [...node.children.values()]
151
+ if (major >= 12) {
152
+ // The order its children come in is a map's, which varies.
153
+ const commands = children.filter(mayHaveBin).flatMap((dir) => commandsOfDir(dir))
154
+ if (node.pkg.bundledDependencies !== undefined) commands.push(...bundledCommands(node, where, major))
155
+ if (mayHaveBin(node.dir)) commands.push(...commandsOfDir(node.dir))
156
+ link(commands, { ordered: false, where })
157
+ continue
158
+ }
309
159
  const withBins = children.filter((dir) => nodes.get(dir)?.pkg.hasBin).flatMap((dir) => commandsOfDir(dir))
310
160
  if (children.every((dir) => nodes.has(dir))) link(withBins, { ordered: true, where })
311
161
  else link(children.flatMap((dir) => commandsOfDir(dir, { normalize: true })), { ordered: false, where })
312
162
  if (node.pkg.bundledDependencies !== undefined) link(bundledCommands(node, where, major), { ordered: false, where })
313
- if (building && (node.pkg.patchHash !== undefined || hasInstallScript(node.manifest, node.files))) {
163
+ if (building && (node.pkg.patchHash !== undefined || requiresBuild(node.manifest, node.files, major))) {
314
164
  link([...withBins, ...commandsOfDir(node.dir)], { ordered: true, where })
315
165
  }
316
166
  }
@@ -319,46 +169,65 @@ export function binTargets({ nodes, projects, direct, links, publicHoist, buildi
319
169
  const hoisted = [...links].filter(([path, dir]) => path.startsWith(PRIVATE) && nodes.get(dir)?.pkg.hasBin)
320
170
  link(hoisted.flatMap(([, dir]) => commandsOfDir(dir)), { ordered: true, where: quote(PRIVATE.slice(0, -1)) })
321
171
 
172
+ const patched = [...nodes.values()].some((node) => node.pkg.patchHash !== undefined)
322
173
  for (const [id, children] of direct) {
323
174
  const where = `importers[${quote(id)}]`
175
+ const peerDirs = major >= 11 && peers ? peersOf(children, nodes) : []
176
+ let relink = major >= 12 && (patched || peerDirs.some(mayHaveBin))
324
177
  let linked
325
178
  if (id === '.' && publicHoist) {
326
179
  const manifest = projects.get('.')
327
180
  const own = new Set(Object.keys({ ...manifest.devDependencies, ...manifest.dependencies, ...manifest.optionalDependencies }))
328
- const entries = [...links].filter(([path]) => /^node_modules\/(?:@[^/]+\/)?[^/@.][^/]*$/u.test(path))
181
+ // pnpm 12 links what it hoists of the projects apart, before.
182
+ const entries = [...links].filter(([path, dir]) => /^node_modules\/(?:@[^/]+\/)?[^/@.][^/]*$/u.test(path)
183
+ && (major < 12 || own.has(path.slice('node_modules/'.length)) || mayHaveBin(dir)))
329
184
  const commands = entries.flatMap(([path, dir]) => commandsOfDir(dir).map((command) => ({ ...command, direct: own.has(path.slice('node_modules/'.length)) })))
330
185
  const names = new Set(commands.filter((command) => command.direct).map(({ name }) => name))
331
- linked = link([...commands.filter((command) => command.direct), ...commands.filter((command) => !command.direct && !names.has(command.name))], { ordered: false, where })
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 })
332
188
  } else {
333
189
  // pnpm 11 reads the bins only of what the lockfile says has some.
334
- const dirs = [...children.values()].filter((dir) => major < 11 || (nodes.get(dir)?.pkg.hasBin ?? true))
335
- linked = link(dirs.flatMap((dir) => commandsOfDir(dir)), { ordered: true, where })
190
+ const dirs = [...children.values()].filter((dir) => major < 11 || !nodes.has(dir) || (major >= 12 ? mayHaveBin(dir) : nodes.get(dir).pkg.hasBin))
191
+ // pnpm 12 comes to them in the order of a map, which varies.
192
+ linked = link(dirs.flatMap((dir) => commandsOfDir(dir)), { ordered: major < 12, where, fixNode: relink })
336
193
  }
337
194
  if (major >= 11 && peers) {
338
195
  // Where the names linked are not all known, neither are those left.
339
- const commands = peersOf(children, nodes).flatMap((dir) => commandsOfDir(dir))
196
+ const commands = peerDirs.flatMap((dir) => commandsOfDir(dir))
340
197
  link([...commands.filter(({ name }) => !linked.names.has(name)), ...linked.unknown ? [UNKNOWN] : []], { ordered: true, where })
341
198
  }
342
199
  }
343
200
 
344
- return fixedFiles(nodes, fixed, contested)
201
+ return fixedFiles(nodes, fixed, contested, major)
345
202
  }
346
203
 
347
204
  // The files `fixed` has fixBin run on, by the directory of their package.
348
205
  // One `contested` may be or not, which is refused, unless it is fixed
349
206
  // anyway, is not there, or is one fixBin leaves as it is: executable, with
350
207
  // no CRLF `#!` line, and not patched.
351
- function fixedFiles(nodes, fixed, contested) {
208
+ 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.
211
+ const checkDirectory = (node, path) => {
212
+ if (major < 12) return
213
+ const prefix = `${path}/`
214
+ 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))
216
+ }
217
+ }
352
218
  for (const [target, { owner, why }] of contested) {
353
219
  const node = nodes.get(owner)
354
220
  const path = target.slice(owner.length + 1)
221
+ checkDirectory(node, path)
355
222
  const file = node.files.get(path)
356
- if (fixed.has(target) || file?.data === undefined || (file.mode === 0o755 && !hasCrlfShebang(file.data) && node.pkg.patchHash === undefined)) continue
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
357
225
  throw new DeptreeError(`whether pnpm makes ${quote(path)} executable turns on ${why}`, quote(node.key))
358
226
  }
359
227
  const byNode = new Map()
360
228
  for (const [target, owner] of fixed) {
361
229
  const path = target.slice(owner.length + 1)
230
+ checkDirectory(nodes.get(owner), path)
362
231
  if (nodes.get(owner).files.get(path)?.data === undefined) continue
363
232
  if (!byNode.has(owner)) byNode.set(owner, new Set())
364
233
  byNode.get(owner).add(path)
@@ -366,19 +235,6 @@ function fixedFiles(nodes, fixed, contested) {
366
235
  return byNode
367
236
  }
368
237
 
369
- // Every path the package at `node` and those it bundles name as bins,
370
- // relative to it, whether pnpm links them or not, where its package.json
371
- // is `manifest` and its files are `files`: each with the commands that
372
- // name it and the package name and version each is ranked by.
373
- function binsOf(node, manifest, files, major) {
374
- const where = quote(node.key)
375
- const bins = new Map()
376
- for (const { target, name, pkgName, pkgVersion } of [...commandsOf(node.dir, manifest, files, node.dir, where, major), ...bundledCommands({ ...node, files }, where, major)]) {
377
- const path = target.slice(node.dir.length + 1)
378
- bins.set(path, [...bins.get(path) ?? [], JSON.stringify([name, pkgName, pkgVersion])].sort())
379
- }
380
- return bins
381
- }
382
238
 
383
239
  // Whether a file starts with a `#!` line ending in CRLF, as fixBin reads
384
240
  // its first 2048 bytes to tell.
@@ -390,8 +246,9 @@ function hasCrlfShebang(data) {
390
246
 
391
247
  // fixBin on a file: mode 0o755, and a CRLF ending the `#!` line made LF.
392
248
  // pnpm reads and writes the file as UTF-8 to do that, which would change
393
- // one that is not; that is refused.
394
- export function fixBin(file, where) {
249
+ // one that is not; that is refused. pnpm 12 only adds the 0o111 it lacks.
250
+ export function fixBin(file, where, major) {
251
+ if (major >= 12) return { data: file.data, mode: file.mode | 0o111 }
395
252
  if (!hasCrlfShebang(file.data)) return { data: file.data, mode: 0o755 }
396
253
  try {
397
254
  decoder.decode(file.data)
@@ -427,6 +284,8 @@ export function checkPatchOfBins(node, patched, targets, where, major) {
427
284
  throw new DeptreeError(`the patch makes or removes ${quote(path)}, which a bin names, and which pnpm links before and after it`, where)
428
285
  }
429
286
  }
287
+ // pnpm 12 rewrites no bin.
288
+ if (major >= 12) return manifest
430
289
  for (const path of targets) {
431
290
  const before = node.files.get(path).data
432
291
  const after = patched.get(path).data