@preventive/deptree 1.0.0-alpha.6 → 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.
- package/npm.d.ts +2 -2
- package/package.json +10 -7
- package/pnpm.d.ts +49 -10
- package/src/hash.js +2 -6
- package/src/matcher.js +3 -2
- package/src/mount.js +9 -0
- package/src/npm/bins.js +2 -19
- package/src/npm/compat.js +11 -6
- package/src/npm/inputs.js +4 -9
- package/src/npm/manifests.js +4 -12
- package/src/npmrc.js +1 -2
- package/src/patch.js +4 -13
- package/src/pnpm/bins.js +129 -37
- package/src/pnpm/commands.js +4 -10
- package/src/pnpm/frozen.js +3 -6
- package/src/pnpm/gitignore.js +3 -1
- package/src/pnpm/glob.js +3 -3
- package/src/pnpm/hoist.js +3 -6
- package/src/pnpm/hoisted.js +234 -0
- package/src/pnpm/inputs.js +24 -34
- package/src/pnpm/install.js +17 -20
- package/src/pnpm/minimatch.js +6 -15
- package/src/pnpm/nm-hoist-graph.js +299 -0
- package/src/pnpm/nm-hoist.js +149 -0
- package/src/pnpm/npm-packlist.js +7 -11
- package/src/pnpm/overrides.js +1 -2
- package/src/pnpm/package.js +2 -1
- package/src/pnpm/packlist.js +5 -4
- package/src/pnpm/readers.js +17 -28
- package/src/pnpm/settings.js +9 -8
- package/src/pnpm/tree.js +73 -30
- package/src/pnpm/workspace.js +2 -2
- package/src/project.js +22 -6
- package/src/soldeer/inputs.js +4 -11
- package/src/soldeer/tree.js +2 -4
- package/src/tarball.js +21 -2
- package/src/yarn1/inputs.js +4 -11
- package/src/yarn1/resolve.js +1 -2
- package/src/yarn1/settings.js +1 -2
- package/yarn1.d.ts +4 -3
package/src/pnpm/bins.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
// The files pnpm's bin linking fixes (@pnpm/link-bins, bin-links' fixBin):
|
|
2
2
|
// no .bin is written, but the file of each command it links by a name is
|
|
3
3
|
// made executable and a CRLF ending its `#!` line made LF. pnpm 10 sets
|
|
4
|
-
// 0o755 and pnpm 11 adds 0o111,
|
|
5
|
-
//
|
|
4
|
+
// 0o755 and pnpm 11 adds 0o111, as pnpm 12 (its cmd-shim crate) does, which
|
|
5
|
+
// rewrites no `#!` line.
|
|
6
6
|
// Where any patch is configured, pnpm 10 and 11 build each package patched
|
|
7
7
|
// or with an install script, which links its own bins beside its children's
|
|
8
8
|
// before the patch; pnpm 12 links them together, with no build pass. pnpm 9
|
|
9
9
|
// links as pnpm 10 does, but fails on a bin that is a directory.
|
|
10
10
|
|
|
11
11
|
import { DeptreeError, quote } from '../error.js'
|
|
12
|
+
import { crlfShebang, fixShebang } from '../tarball.js'
|
|
12
13
|
import { UNKNOWN, binsOf, bundledCommands, commandsOf, compare, normalized, parseManifest } from './commands.js'
|
|
13
|
-
|
|
14
|
-
const decoder = new TextDecoder('utf-8', { fatal: true })
|
|
14
|
+
import { reachable } from './nm-hoist.js'
|
|
15
15
|
|
|
16
16
|
// The peers whose bins pnpm 11 links into a project's .bin.
|
|
17
17
|
function peersOf(children, nodes) {
|
|
@@ -66,23 +66,38 @@ function linker(major) {
|
|
|
66
66
|
return { fixed, contested, link }
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
// The
|
|
70
|
-
// `
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const
|
|
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 } = {}) => {
|
|
74
75
|
const node = nodes.get(dir)
|
|
75
76
|
if (node !== undefined) {
|
|
76
|
-
if (!
|
|
77
|
-
return
|
|
77
|
+
if (!cache.has(dir)) cache.set(dir, commandsOf(dir, node.manifest, node.files, dir, quote(node.key), major))
|
|
78
|
+
return cache.get(dir)
|
|
78
79
|
}
|
|
79
|
-
// A project or `link:` target, outside the tree: its bins may take any
|
|
80
|
-
// name where its package.json is not given (local.js).
|
|
81
80
|
const manifest = projects.get(dir)
|
|
82
81
|
if (manifest === undefined) return [UNKNOWN]
|
|
83
82
|
const where = `manifests[${quote(dir)}]`
|
|
84
83
|
return commandsOf(dir, normalize ? normalized(manifest, where) : manifest, undefined, undefined, where, major)
|
|
85
84
|
}
|
|
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)
|
|
86
101
|
// The lockfile has no hasBin for a directory, whose bins pnpm 12 reads.
|
|
87
102
|
const mayHaveBin = (dir) => nodes.get(dir)?.pkg.hasBin === true || nodes.get(dir)?.pkg.resolution.type === 'directory'
|
|
88
103
|
|
|
@@ -125,10 +140,8 @@ export function binTargets({ nodes, projects, direct, links, publicHoist, buildi
|
|
|
125
140
|
// pnpm 12 links what it hoists of the projects apart, before.
|
|
126
141
|
const entries = [...links].filter(([path, dir]) => /^node_modules\/(?:@[^/]+\/)?[^/@.][^/]*$/u.test(path)
|
|
127
142
|
&& (major < 12 || isOwn(path) || mayHaveBin(dir)))
|
|
128
|
-
const commands = entries.flatMap(([path, dir]) => commandsOfDir(dir).map((command) => ({ ...command, direct: isOwn(path) })))
|
|
129
|
-
const names = new Set(commands.filter((command) => command.direct).map(({ name }) => name))
|
|
130
143
|
relink ||= major >= 12 && entries.some(([path, dir]) => !isOwn(path) && mayHaveBin(dir))
|
|
131
|
-
linked = link(
|
|
144
|
+
linked = link(preferOwn(entries.map(([path, dir]) => [path.slice('node_modules/'.length), dir]), manifest, commandsOfDir), { ordered: false, where, fixNode: relink })
|
|
132
145
|
} else {
|
|
133
146
|
const dirs = [...children.values()].filter((dir) => major < 11 || !nodes.has(dir) || (major >= 12 ? mayHaveBin(dir) : nodes.get(dir).pkg.hasBin))
|
|
134
147
|
// pnpm 12 comes to them in the order of a map, which varies.
|
|
@@ -144,6 +157,100 @@ export function binTargets({ nodes, projects, direct, links, publicHoist, buildi
|
|
|
144
157
|
return fixedFiles(nodes, fixed, contested, major)
|
|
145
158
|
}
|
|
146
159
|
|
|
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
|
+
|
|
147
254
|
function fixedFiles(nodes, fixed, contested, major) {
|
|
148
255
|
// pnpm 9 and 12 read each linked file's `#!` line. '' is the package.
|
|
149
256
|
const checkDirectory = (node, path) => {
|
|
@@ -158,8 +265,8 @@ function fixedFiles(nodes, fixed, contested, major) {
|
|
|
158
265
|
const path = target.slice(owner.length + 1)
|
|
159
266
|
checkDirectory(node, path)
|
|
160
267
|
const file = node.files.get(path)
|
|
161
|
-
|
|
162
|
-
if (
|
|
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
|
|
163
270
|
throw new DeptreeError(`whether pnpm makes ${quote(path)} executable turns on ${why}`, quote(node.key))
|
|
164
271
|
}
|
|
165
272
|
const byNode = new Map()
|
|
@@ -173,27 +280,12 @@ function fixedFiles(nodes, fixed, contested, major) {
|
|
|
173
280
|
return byNode
|
|
174
281
|
}
|
|
175
282
|
|
|
176
|
-
|
|
177
|
-
function hasCrlfShebang(data) {
|
|
178
|
-
if (data[0] !== 0x23 || data[1] !== 0x21) return false
|
|
179
|
-
const newline = data.subarray(0, 2048).indexOf(0x0a)
|
|
180
|
-
return newline >= 4 && data[newline - 1] === 0x0d
|
|
181
|
-
}
|
|
283
|
+
export const executableMode = (mode, major) => (major >= 11 ? mode | 0o111 : 0o755)
|
|
182
284
|
|
|
183
285
|
// pnpm reads and writes the file as UTF-8, which changes one that is not.
|
|
184
286
|
export function fixBin(file, where, major) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
try {
|
|
188
|
-
decoder.decode(file.data)
|
|
189
|
-
} catch {
|
|
190
|
-
throw new DeptreeError('a bin with a CRLF `#!` line is not UTF-8, which pnpm would rewrite', where)
|
|
191
|
-
}
|
|
192
|
-
const newline = file.data.indexOf(0x0a)
|
|
193
|
-
const data = new Uint8Array(file.data.length - 1)
|
|
194
|
-
data.set(file.data.subarray(0, newline - 1))
|
|
195
|
-
data.set(file.data.subarray(newline), newline - 1)
|
|
196
|
-
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 }
|
|
197
289
|
}
|
|
198
290
|
|
|
199
291
|
const BIN_FIELDS = (manifest) => JSON.stringify([manifest.name, manifest.version, manifest.bin, manifest.directories?.bin])
|
|
@@ -218,7 +310,7 @@ export function checkPatchOfBins(node, patched, targets, where, major) {
|
|
|
218
310
|
for (const path of targets) {
|
|
219
311
|
const before = node.files.get(path).data
|
|
220
312
|
const after = patched.get(path).data
|
|
221
|
-
if (before !== after && (
|
|
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)
|
|
222
314
|
}
|
|
223
315
|
return manifest
|
|
224
316
|
}
|
package/src/pnpm/commands.js
CHANGED
|
@@ -7,6 +7,7 @@ import { compareVersions, valid } from '@preventive/upstream/semver.js'
|
|
|
7
7
|
import { basename, compareNames, join } from '@preventive/vfs/path.js'
|
|
8
8
|
import { DeptreeError, quote } from '../error.js'
|
|
9
9
|
import { checkNesting } from '../manifest.js'
|
|
10
|
+
import { within } from '../mount.js'
|
|
10
11
|
|
|
11
12
|
// A package whose directory has no package.json is still linked by these names
|
|
12
13
|
// by pnpm 10, to the runtime's binary inside it; pnpm 9 links none.
|
|
@@ -23,8 +24,7 @@ function inPackage(dir, rel, where) {
|
|
|
23
24
|
if (typeof rel !== 'string') throw new DeptreeError(`${quote(String(rel))} is not a path, which pnpm fails on`, where)
|
|
24
25
|
const path = join(dir, rel)
|
|
25
26
|
if (path.endsWith('/')) throw new DeptreeError(`${quote(rel)} ends in a slash, which is not supported`, where)
|
|
26
|
-
|
|
27
|
-
return inside ? path : undefined
|
|
27
|
+
return within(dir, path) ? path : undefined
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
// Stands for the commands of a directory whose bins are not known here.
|
|
@@ -33,14 +33,8 @@ export const UNKNOWN = { unknown: true }
|
|
|
33
33
|
// As tinyglobby's `**` finds them, or with `dots` as pnpm 12's walk does.
|
|
34
34
|
function filesUnder(files, base, dir, { dots = false } = {}) {
|
|
35
35
|
const prefix = dir === base ? '' : `${dir.slice(base.length + 1)}/`
|
|
36
|
-
const found = []
|
|
37
|
-
|
|
38
|
-
if (file.data === undefined || !path.startsWith(prefix)) continue
|
|
39
|
-
const rest = path.slice(prefix.length)
|
|
40
|
-
if (!dots && rest.split('/').some((name) => name.startsWith('.'))) continue
|
|
41
|
-
found.push(rest)
|
|
42
|
-
}
|
|
43
|
-
return found
|
|
36
|
+
const found = [...files].filter(([path, file]) => file.data !== undefined && path.startsWith(prefix)).map(([path]) => path.slice(prefix.length))
|
|
37
|
+
return dots ? found : found.filter((rest) => !rest.split('/').some((name) => name.startsWith('.')))
|
|
44
38
|
}
|
|
45
39
|
|
|
46
40
|
// Which of two of one name wins turns on the order directories are read in.
|
package/src/pnpm/frozen.js
CHANGED
|
@@ -6,6 +6,7 @@ import { satisfies, valid, validRange } from '@preventive/upstream/semver.js'
|
|
|
6
6
|
import { join } from '@preventive/vfs/path.js'
|
|
7
7
|
import { DeptreeError, quote } from '../error.js'
|
|
8
8
|
import { own } from '../manifest.js'
|
|
9
|
+
import { within } from '../mount.js'
|
|
9
10
|
import { catalogEntry, catalogOf } from './overrides.js'
|
|
10
11
|
|
|
11
12
|
const GIT_HOSTS = new Set(['github.com', 'gitlab.com', 'bitbucket.org'])
|
|
@@ -76,7 +77,6 @@ function specPath(dir, path, where) {
|
|
|
76
77
|
}
|
|
77
78
|
|
|
78
79
|
const isWorkspacePath = (spec) => /^(?:[./\\]|~[/\\]|[A-Za-z]:)/u.test(spec)
|
|
79
|
-
const within = (parent, child) => child === parent || (parent === '.' ? child !== '..' && !child.startsWith('../') : child.startsWith(`${parent}/`))
|
|
80
80
|
|
|
81
81
|
export function parseSpec(spec, alias) {
|
|
82
82
|
if (spec.startsWith('workspace:')) {
|
|
@@ -148,12 +148,9 @@ export function indexProjects(projects) {
|
|
|
148
148
|
export function checkLinkedPackages({ manifest, importer, index: { projects, byName, byDir }, linkWorkspacePackages }, where) {
|
|
149
149
|
const outdated = (detail) => new DeptreeError(`the lockfile is not up to date with this package.json, which pnpm 11 refuses a frozen install for: ${detail}`, where)
|
|
150
150
|
for (const kind of KINDS) {
|
|
151
|
-
const wanted = manifest[kind]
|
|
152
|
-
if (wanted == null) continue
|
|
153
151
|
for (const [alias, target] of Object.entries(importer[kind])) {
|
|
154
|
-
const spec = own(
|
|
152
|
+
const spec = own(manifest[kind], alias)
|
|
155
153
|
if (!spec) continue
|
|
156
|
-
const here = `${where}.${kind}.${alias}`
|
|
157
154
|
const workspaceRange = spec.startsWith('workspace:') && !isWorkspacePath(spec.slice('workspace:'.length))
|
|
158
155
|
const linked = target.startsWith('link:')
|
|
159
156
|
// pnpm 11's frozen install skips a local directory or tarball.
|
|
@@ -172,7 +169,7 @@ export function checkLinkedPackages({ manifest, importer, index: { projects, byN
|
|
|
172
169
|
if (linked && workspaceRange && named !== undefined && ![...named.values()].some((root) => within(root, dir))) {
|
|
173
170
|
throw outdated(`${alias} is linked to ${quote(dir)}, which is in no workspace package named ${quote(name)}`)
|
|
174
171
|
}
|
|
175
|
-
if (!byDir.has(dir)) throw new DeptreeError(`it is linked to ${quote(dir)}, which is no project, and whose package.json pnpm 11 reads`,
|
|
172
|
+
if (!byDir.has(dir)) throw new DeptreeError(`it is linked to ${quote(dir)}, which is no project, and whose package.json pnpm 11 reads`, `${where}.${kind}.${alias}`)
|
|
176
173
|
const { version } = byDir.get(dir)
|
|
177
174
|
if (linked !== inRange(version, range)) {
|
|
178
175
|
throw outdated(linked ? `the linked workspace package ${alias} (${version ?? 'unknown'}) is not in the range ${quote(spec)}` : `the workspace package ${alias} (${version ?? 'unknown'}) is in the range ${quote(spec)} and not linked`)
|
package/src/pnpm/gitignore.js
CHANGED
|
@@ -8,8 +8,10 @@ import { checkGlob, checkLength } from './minimatch.js'
|
|
|
8
8
|
// What Rust's trim_end takes off: White_Space.
|
|
9
9
|
const TRAILING = /[\t-\r \u0085\u00A0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000]+$/u
|
|
10
10
|
|
|
11
|
+
const encoder = new TextEncoder()
|
|
12
|
+
|
|
11
13
|
// A path as its UTF-8 bytes, as globset matches it.
|
|
12
|
-
export const bytesOf = (path) =>
|
|
14
|
+
export const bytesOf = (path) => encoder.encode(path)
|
|
13
15
|
|
|
14
16
|
const SLASH = 0x2F
|
|
15
17
|
const NEWLINE = 0x0A
|
package/src/pnpm/glob.js
CHANGED
|
@@ -4,17 +4,17 @@
|
|
|
4
4
|
// it, and a file named by a pattern with no part to match against names
|
|
5
5
|
// found alone where it is there.
|
|
6
6
|
|
|
7
|
-
import { GLOBSTAR, OBJECT_5, checkGlob, checkLength, checkParts,
|
|
7
|
+
import { GLOBSTAR, OBJECT_5, checkGlob, checkLength, checkParts, readPart } from './minimatch.js'
|
|
8
8
|
|
|
9
9
|
// npm-packlist globs `[object Object]` for a `browser` or `main` that is an
|
|
10
10
|
// object.
|
|
11
|
-
|
|
11
|
+
function globParts(pattern, where) {
|
|
12
12
|
if (pattern === '[object Object]') return [OBJECT_5]
|
|
13
13
|
checkLength(pattern, where)
|
|
14
14
|
checkGlob(pattern, where)
|
|
15
15
|
const parts = pattern.split(/\/+/u)
|
|
16
16
|
checkParts(pattern, parts, where)
|
|
17
|
-
return parts.map(
|
|
17
|
+
return parts.map((part) => readPart(part))
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
// `sets` are a pattern's sets of parts, or the pattern. glob walks one
|
package/src/pnpm/hoist.js
CHANGED
|
@@ -44,12 +44,9 @@ const lexCompare = (a, b) => (a > b ? 1 : a < b ? -1 : 0)
|
|
|
44
44
|
function hoistGraph(nodes, starts, taken, typeOf, { projects = new Map(), installed = nodes, major = 10 } = {}) {
|
|
45
45
|
const named = new Map([...projects].map(([id, name]) => [name, { project: id }]))
|
|
46
46
|
const root = new Map(major < 12 ? named : [])
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
listed.add(alias)
|
|
51
|
-
root.set(alias, dir)
|
|
52
|
-
}
|
|
47
|
+
// The first of each alias in `starts`.
|
|
48
|
+
const first = new Map(starts.toReversed())
|
|
49
|
+
for (const [alias] of starts) root.set(alias, first.get(alias))
|
|
53
50
|
// pnpm 12 orders the nodes of one depth by their directories' names.
|
|
54
51
|
const keyOf = major >= 12 ? (node) => node.modules.slice(0, -'/node_modules'.length) : (node) => node.dir
|
|
55
52
|
const order = [
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
// Where the hoisted linker (nodeLinker: hoisted) of pnpm 10 and 11 puts
|
|
2
|
+
// each package: @pnpm/real-hoist makes a tree of the lockfile, the root's
|
|
3
|
+
// dependencies and each other project's under the root, which @yarnpkg/nm
|
|
4
|
+
// hoists (nm-hoist.js; what pnpm 11 has of it, 4.0.7 to 4.1.1, differs
|
|
5
|
+
// only with hoistingLimits, which is not supported); each package is then
|
|
6
|
+
// copied to node_modules/<alias> under where it landed, and each project's
|
|
7
|
+
// own lands in its node_modules. A project's `link:` dependencies are linked
|
|
8
|
+
// there, as are none of a package's.
|
|
9
|
+
|
|
10
|
+
import { packageKeyOf } from '@preventive/lockfile/pnpm.js'
|
|
11
|
+
import { valid } from '@preventive/upstream/semver.js'
|
|
12
|
+
import { relative } from '@preventive/vfs/path.js'
|
|
13
|
+
import { DeptreeError, quote } from '../error.js'
|
|
14
|
+
import { createMatcher } from '../matcher.js'
|
|
15
|
+
import { REGULAR, WORKSPACE, depthFirst, hoist, reachable } from './nm-hoist.js'
|
|
16
|
+
|
|
17
|
+
// pnpm's name@version of a snapshot, by which it takes every snapshot of a
|
|
18
|
+
// package for the first it comes to. A directory has no version in the
|
|
19
|
+
// lockfile, so all of one name are one; from pnpm 11.24 (`directories`),
|
|
20
|
+
// each snapshot of one is its own.
|
|
21
|
+
export function packageIdOf(key, pkg, directories = false) {
|
|
22
|
+
if (directories && pkg.resolution.type === 'directory') return key
|
|
23
|
+
const base = packageKeyOf(key)
|
|
24
|
+
const name = base.slice(0, base.indexOf('@', 1))
|
|
25
|
+
const version = pkg.resolution.type === 'directory' ? undefined : valid(base.slice(name.length + 1)) ?? undefined
|
|
26
|
+
return `${name}@${version}`
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const modulesOf = (id) => (id === '.' ? 'node_modules' : `${id}/node_modules`)
|
|
30
|
+
|
|
31
|
+
// A project's dependencies as pnpm reads them from the lockfile, a link
|
|
32
|
+
// spelled from the project, as pnpm writes it.
|
|
33
|
+
function asWritten(id, deps) {
|
|
34
|
+
return Object.fromEntries(Object.entries(deps).map(([alias, target]) => [alias, target.startsWith('link:') ? `link:${relative(`/${id}`, `/${target.slice(5)}`)}` : target]))
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// A link pnpm from 11.28.1 takes for one into the package that asks for it,
|
|
38
|
+
// which it does not hoist: `link:<root>/` and a plain path.
|
|
39
|
+
const rootLink = (ref) => ref.startsWith('link:<root>/') && ref.slice('link:<root>/'.length).split('/').every((name) => name !== '' && name !== '.' && name !== '..' && !name.includes('\\') && !name.includes(':'))
|
|
40
|
+
|
|
41
|
+
// real-hoist's tree: a node by alias and snapshot, each made once and with
|
|
42
|
+
// its own dependencies made before the next, depth first as pnpm recurses.
|
|
43
|
+
function treeOf(lockfile, autoInstallPeers, { directories, rootLinks }) {
|
|
44
|
+
const nodes = new Map()
|
|
45
|
+
const referenceById = new Map()
|
|
46
|
+
const nodeOf = (alias, ref) => {
|
|
47
|
+
if (ref.startsWith('link:')) return { name: alias, identName: alias, reference: ref, dependencyKind: rootLinks && rootLink(ref) ? WORKSPACE : REGULAR, dependencies: new Set(), peerNames: new Set() }
|
|
48
|
+
const pkg = lockfile.packages[ref]
|
|
49
|
+
const id = packageIdOf(ref, pkg, directories)
|
|
50
|
+
if (!referenceById.has(id)) referenceById.set(id, ref)
|
|
51
|
+
const peerNames = new Set(autoInstallPeers ? [] : [...Object.keys(pkg.peerDependencies), ...pkg.transitivePeerDependencies])
|
|
52
|
+
return { name: alias, identName: pkg.name, reference: referenceById.get(id), dependencyKind: REGULAR, dependencies: new Set(), peerNames }
|
|
53
|
+
}
|
|
54
|
+
const toTree = (deps) => {
|
|
55
|
+
const top = new Set()
|
|
56
|
+
depthFirst({ items: Object.entries(deps).values(), into: top }, ([alias, ref], { into }) => {
|
|
57
|
+
const key = `${alias}:${ref}`
|
|
58
|
+
const made = !nodes.has(key)
|
|
59
|
+
if (made) nodes.set(key, nodeOf(alias, ref))
|
|
60
|
+
const node = nodes.get(key)
|
|
61
|
+
into.add(node)
|
|
62
|
+
if (!made || ref.startsWith('link:')) return undefined
|
|
63
|
+
const pkg = lockfile.packages[ref]
|
|
64
|
+
return { items: Object.entries({ ...pkg.dependencies, ...pkg.optionalDependencies }).values(), into: node.dependencies }
|
|
65
|
+
})
|
|
66
|
+
return top
|
|
67
|
+
}
|
|
68
|
+
const projectNode = (id, name, reference) => {
|
|
69
|
+
const { dependencies, devDependencies, optionalDependencies } = lockfile.importers[id]
|
|
70
|
+
return { name, identName: name, reference, peerNames: new Set(), dependencyKind: WORKSPACE, dependencies: toTree(asWritten(id, { ...dependencies, ...devDependencies, ...optionalDependencies })) }
|
|
71
|
+
}
|
|
72
|
+
const tree = projectNode('.', '.', '')
|
|
73
|
+
for (const [id, importer] of Object.entries(lockfile.importers)) {
|
|
74
|
+
if (id !== '.' && !importer.made) tree.dependencies.add(projectNode(id, encodeURIComponent(id), `workspace:${id}`))
|
|
75
|
+
}
|
|
76
|
+
return tree
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// pnpm reads a package's link as its writer left it, which the lockfile
|
|
80
|
+
// reader does not keep.
|
|
81
|
+
function checkNoPackageLinks(lockfile) {
|
|
82
|
+
for (const [key, pkg] of Object.entries(lockfile.packages)) {
|
|
83
|
+
for (const [alias, target] of Object.entries({ ...pkg.dependencies, ...pkg.optionalDependencies })) {
|
|
84
|
+
if (target.startsWith('link:')) throw new DeptreeError('a package\'s `link:` dependency is not supported with the hoisted linker', `packages[${quote(key)}].dependencies[${quote(alias)}]`)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// The tree as hoisted, of nodes with a name, an identName, references and
|
|
90
|
+
// dependencies; with `directories` from pnpm 11.24, and `rootLinks` from
|
|
91
|
+
// 11.28.1.
|
|
92
|
+
export function hoistedTree(lockfile, autoInstallPeers, bounds, { directories = false, rootLinks = false } = {}) {
|
|
93
|
+
checkNoPackageLinks(lockfile)
|
|
94
|
+
return hoist(treeOf(lockfile, autoInstallPeers, { directories, rootLinks }), bounds)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Each directory a package is copied to, from the lockfile's, in the order
|
|
98
|
+
// pnpm makes them, a parent before what lands in its node_modules, with the
|
|
99
|
+
// snapshot copied there, the node_modules it is in and its alias there; the
|
|
100
|
+
// `link:`s of each project, by where they go; and the node_modules of the
|
|
101
|
+
// projects pnpm fills, the root's first. `skipped` are the snapshots left
|
|
102
|
+
// out, which are hoisted all the same. pnpm hoists from the importers the
|
|
103
|
+
// lockfile has, not those made for projects it has none for.
|
|
104
|
+
export function hoistedLayout(lockfile, { autoInstallPeers, skipped, directories = false, rootLinks = false }, bounds) {
|
|
105
|
+
const tree = hoistedTree(lockfile, autoInstallPeers, bounds, { directories, rootLinks })
|
|
106
|
+
const placed = new Map()
|
|
107
|
+
const place = (modules, deps) => depthFirst({ modules, items: deps.values() }, (dep, frame) => {
|
|
108
|
+
const [key] = dep.references
|
|
109
|
+
if (skipped.has(key) || key.startsWith('workspace:') || !(key in lockfile.packages)) return undefined
|
|
110
|
+
const dir = `${frame.modules}/${dep.name}`
|
|
111
|
+
const pkg = lockfile.packages[key]
|
|
112
|
+
placed.set(dir, { key, pkg, name: pkg.name, dir, modules: frame.modules, alias: dep.name })
|
|
113
|
+
return { modules: `${dir}/node_modules`, items: dep.dependencies.values() }
|
|
114
|
+
})
|
|
115
|
+
place('node_modules', tree.dependencies)
|
|
116
|
+
const projects = ['node_modules']
|
|
117
|
+
for (const dep of tree.dependencies) {
|
|
118
|
+
const [reference] = dep.references
|
|
119
|
+
if (!reference.startsWith('workspace:')) continue
|
|
120
|
+
const modules = modulesOf(reference.slice('workspace:'.length))
|
|
121
|
+
projects.push(modules)
|
|
122
|
+
place(modules, dep.dependencies)
|
|
123
|
+
}
|
|
124
|
+
const links = new Map()
|
|
125
|
+
for (const [id, importer] of Object.entries(lockfile.importers)) {
|
|
126
|
+
for (const [alias, target] of Object.entries({ ...importer.devDependencies, ...importer.dependencies, ...importer.optionalDependencies })) {
|
|
127
|
+
if (target.startsWith('link:')) links.set(`${modulesOf(id)}/${alias}`, target.slice(5))
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return { placed, links, projects }
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// pnpm from 11.28 links each project named in `names` into the root's
|
|
134
|
+
// node_modules by its name where a hoist pattern matches it, unless the root
|
|
135
|
+
// depends on that name, or a package landed there by it, with their case
|
|
136
|
+
// folded.
|
|
137
|
+
export function workspaceHoists(placed, lockfile, { hoistPattern, publicHoistPattern }, names) {
|
|
138
|
+
const isPublic = createMatcher(publicHoistPattern ?? [])
|
|
139
|
+
const isPrivate = createMatcher(hoistPattern ?? [])
|
|
140
|
+
const { dependencies, devDependencies, optionalDependencies } = lockfile.importers['.']
|
|
141
|
+
const taken = new Set(Object.keys({ ...dependencies, ...devDependencies, ...optionalDependencies }).map((alias) => alias.toLowerCase()))
|
|
142
|
+
for (const { modules, alias } of placed.values()) if (modules === 'node_modules') taken.add(alias.toLowerCase())
|
|
143
|
+
const links = new Map()
|
|
144
|
+
for (const [id, name] of names) {
|
|
145
|
+
if ((!isPublic(name) && !isPrivate(name)) || taken.has(name.toLowerCase())) continue
|
|
146
|
+
taken.add(name.toLowerCase())
|
|
147
|
+
links.set(`node_modules/${name}`, id)
|
|
148
|
+
}
|
|
149
|
+
return links
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// What pnpm's build pass does where any patch is configured, which it runs
|
|
153
|
+
// though scripts are ignored: it builds each package patched or with an
|
|
154
|
+
// install script in one of its copies, the first it comes to of those a
|
|
155
|
+
// project's node_modules reaches through each package's dependencies, each
|
|
156
|
+
// at the first copy of its snapshot, or from pnpm 11.23 of its package
|
|
157
|
+
// (`idOf`). Then it links the built copy's files into every other: from
|
|
158
|
+
// 10.21 (`hardlinks`) it puts a copy of hardlinks in place of the copy, with
|
|
159
|
+
// no node_modules, which drops the copy's own; before, in place, where a
|
|
160
|
+
// file is not already, which leaves the others unpatched. From pnpm 11.25
|
|
161
|
+
// (`keepsModules`) it links each file but those under a node_modules in
|
|
162
|
+
// place of the copy's own, and leaves the copy's node_modules and what the
|
|
163
|
+
// built one has not. Which copy it builds turns on its build order, which is
|
|
164
|
+
// not followed here. `projects` are the node_modules of the projects;
|
|
165
|
+
// `byDir` the copies, in the order pnpm makes them; `changesOf` the changes
|
|
166
|
+
// of a patched one's patch; `pnpm` the version, for what is refused.
|
|
167
|
+
export function hoistedBuilds(byDir, projects, builds, { hardlinks, keepsModules = false, idOf = (key) => key, changesOf, pnpm = '10' }) {
|
|
168
|
+
const first = new Map()
|
|
169
|
+
const placedIn = new Set()
|
|
170
|
+
for (const node of byDir.values()) {
|
|
171
|
+
if (!first.has(idOf(node.key))) first.set(idOf(node.key), node.dir)
|
|
172
|
+
placedIn.add(node.modules)
|
|
173
|
+
}
|
|
174
|
+
const childrenOf = (node) => Object.entries({ ...node.pkg.dependencies, ...node.pkg.optionalDependencies }).map(([alias, key]) => [alias, first.get(idOf(key))]).filter(([, dir]) => dir !== undefined)
|
|
175
|
+
const tops = [...byDir.values()].filter((node) => projects.has(node.modules)).map((node) => node.dir)
|
|
176
|
+
const reached = reachable(tops, (dir) => childrenOf(byDir.get(dir)).map(([, child]) => child))
|
|
177
|
+
const built = []
|
|
178
|
+
for (const [key, copies] of Map.groupBy(byDir.values(), (node) => node.key)) {
|
|
179
|
+
const [node] = copies
|
|
180
|
+
if (!builds(node)) continue
|
|
181
|
+
const candidates = copies.filter((copy) => reached.has(copy.dir)).map((copy) => copy.dir)
|
|
182
|
+
const patched = node.pkg.patchHash !== undefined
|
|
183
|
+
if (candidates.length === 0) {
|
|
184
|
+
if (patched) throw new DeptreeError(`pnpm ${pnpm} reaches none of its copies through the dependencies it builds by, and leaves it unpatched, which is not supported`, quote(key))
|
|
185
|
+
continue
|
|
186
|
+
}
|
|
187
|
+
if (copies.length > 1 && !hardlinks) {
|
|
188
|
+
if (patched) throw new DeptreeError('pnpm 10 before 10.21 patches one of its copies alone, and leaves the others as they were, which is not supported', quote(key))
|
|
189
|
+
} else if (copies.length > 1 && keepsModules) {
|
|
190
|
+
for (const { path, change } of patched ? changesOf(node) : []) {
|
|
191
|
+
if (change === 'delete') throw new DeptreeError(`pnpm ${pnpm} patches one of its copies, and links its files into the others, which keep ${quote(path)} the patch deletes, which is not supported`, quote(key))
|
|
192
|
+
if (path.split('/').includes('node_modules')) throw new DeptreeError(`pnpm ${pnpm} patches one of its copies, and links its files but those under a node_modules into the others, which leaves ${quote(path)} unpatched there, which is not supported`, quote(key))
|
|
193
|
+
}
|
|
194
|
+
} else if (copies.length > 1) {
|
|
195
|
+
const own = node.files.keys().find((path) => path.split('/').includes('node_modules'))
|
|
196
|
+
if (own !== undefined) throw new DeptreeError(`pnpm ${pnpm} builds one of its copies where any patch is configured, and makes the others hardlinks of it, with no ${quote(own)}, which is not supported`, quote(key))
|
|
197
|
+
const nested = copies.find((copy) => placedIn.has(`${copy.dir}/node_modules`))
|
|
198
|
+
if (nested !== undefined) throw new DeptreeError(`pnpm ${pnpm} builds one of its copies where any patch is configured, and makes the others hardlinks of it, dropping their node_modules: ${quote(`${nested.dir}/node_modules`)} may be dropped, which is not supported`, quote(key))
|
|
199
|
+
}
|
|
200
|
+
built.push({ key, copies: copies.map((copy) => copy.dir), candidates, children: childrenOf(node) })
|
|
201
|
+
}
|
|
202
|
+
return built
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// What linkBins reads to link bins (bins.js): each node_modules pnpm fills,
|
|
206
|
+
// with what it put there, the projects' first, then each project's with its
|
|
207
|
+
// links too.
|
|
208
|
+
export function hoistedModules(byDir, links, projects, manifests, lockfile) {
|
|
209
|
+
const placedIn = new Map()
|
|
210
|
+
const add = (modules, alias, dir) => {
|
|
211
|
+
if (!placedIn.has(modules)) placedIn.set(modules, [])
|
|
212
|
+
placedIn.get(modules).push([alias, dir])
|
|
213
|
+
}
|
|
214
|
+
for (const { dir, modules, alias } of byDir.values()) add(modules, alias, dir)
|
|
215
|
+
const filled = [...projects, ...[...byDir.keys()].map((dir) => `${dir}/node_modules`)]
|
|
216
|
+
const all = filled.filter((dir) => placedIn.has(dir)).map((dir) => ({ dir, entries: placedIn.get(dir) }))
|
|
217
|
+
for (const [path, target] of links) {
|
|
218
|
+
const end = path.lastIndexOf('/node_modules/') + '/node_modules'.length
|
|
219
|
+
add(path.slice(0, end), path.slice(end + 1), target)
|
|
220
|
+
}
|
|
221
|
+
for (const id of Object.keys(lockfile.importers)) {
|
|
222
|
+
const dir = modulesOf(id)
|
|
223
|
+
if (placedIn.has(dir)) all.push({ dir, entries: placedIn.get(dir), manifest: manifests.get(id) })
|
|
224
|
+
}
|
|
225
|
+
return all
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// The snapshots `pnpm install --prod` would install, by pnpm's name@version.
|
|
229
|
+
export function prodPackages(lockfile, skipped, directories) {
|
|
230
|
+
const edges = ({ dependencies, optionalDependencies }) => [...Object.values(dependencies), ...Object.values(optionalDependencies)]
|
|
231
|
+
const reached = reachable(Object.values(lockfile.importers).flatMap(edges), (key) => edges(lockfile.packages[key]), (key) => key.startsWith('link:') || skipped.has(key))
|
|
232
|
+
return new Set([...reached].map((key) => packageIdOf(key, lockfile.packages[key], directories)))
|
|
233
|
+
}
|
|
234
|
+
|