-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathlockfileToDepGraph.ts
More file actions
374 lines (340 loc) · 13.2 KB
/
Copy pathlockfileToDepGraph.ts
File metadata and controls
374 lines (340 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import fs from 'node:fs'
import path from 'node:path'
import { packageIsInstallable } from '@pnpm/config.package-is-installable'
import { WANTED_LOCKFILE } from '@pnpm/constants'
import {
progressLogger,
} from '@pnpm/core-loggers'
import * as dp from '@pnpm/deps.path'
import type { IncludedDependencies } from '@pnpm/installing.modules-yaml'
import type { LockfileObject, LockfileResolution } from '@pnpm/lockfile.fs'
import {
packageIdFromSnapshot,
pkgSnapshotToResolution,
} from '@pnpm/lockfile.utils'
import { logger } from '@pnpm/logger'
import { getPatchInfo, type PatchGroupRecord } from '@pnpm/patching.config'
import type { PatchInfo } from '@pnpm/patching.types'
import type {
FetchResponse,
PkgRequestFetchResult,
StoreController,
} from '@pnpm/store.controller-types'
import type {
AllowBuild,
DepPath,
PkgIdWithPatchHash,
ProjectId,
Registries,
SupportedArchitectures,
} from '@pnpm/types'
import { pathExists } from 'path-exists'
import { equals, isEmpty } from 'ramda'
import { iteratePkgsForVirtualStore } from './iteratePkgsForVirtualStore.js'
const brokenModulesLogger = logger('_broken_node_modules')
export interface DependenciesGraphNode {
alias?: string // this is populated in HoistedDepGraphOnly
hasBundledDependencies: boolean
modules: string
name: string
version: string
fetching?: () => Promise<PkgRequestFetchResult>
forceImportPackage?: boolean // Used to force re-imports from the store of local tarballs that have changed.
dir: string
children: Record<string, string>
optionalDependencies: Set<string>
optional: boolean
depPath: DepPath // this option is only needed for saving pendingBuild when running with --ignore-scripts flag
pkgIdWithPatchHash: PkgIdWithPatchHash
isBuilt?: boolean
requiresBuild?: boolean
hasBin: boolean
filesIndexFile?: string
patch?: PatchInfo
resolution: LockfileResolution
}
export interface DependenciesGraph {
[depPath: string]: DependenciesGraphNode
}
export interface LockfileToDepGraphOptions {
allowBuild?: AllowBuild
autoInstallPeers: boolean
enableGlobalVirtualStore?: boolean
engineStrict: boolean
force: boolean
importerIds: ProjectId[]
include: IncludedDependencies
includeUnchangedDeps?: boolean
ignoreScripts: boolean
/**
* When true, skip fetching local dependencies (file: protocol pointing to directories).
* This is useful for `pnpm fetch` which only downloads packages from the registry
* and doesn't need local packages that won't be available (e.g., in Docker builds).
*/
ignoreLocalPackages?: boolean
lockfileDir: string
nodeVersion: string
pnpmVersion: string
patchedDependencies?: PatchGroupRecord
registries: Registries
sideEffectsCacheRead: boolean
skipped: Set<DepPath>
storeController: StoreController
storeDir: string
globalVirtualStoreDir: string
virtualStoreDir: string
supportedArchitectures?: SupportedArchitectures
virtualStoreDirMaxLength: number
}
export interface DirectDependenciesByImporterId {
[importerId: string]: { [alias: string]: string }
}
export interface DepHierarchy {
[depPath: string]: Record<string, DepHierarchy>
}
export interface LockfileToDepGraphResult {
directDependenciesByImporterId: DirectDependenciesByImporterId
graph: DependenciesGraph
hierarchy?: DepHierarchy
hoistedLocations?: Record<string, string[]>
symlinkedDirectDependenciesByImporterId?: DirectDependenciesByImporterId
prevGraph?: DependenciesGraph
injectionTargetsByDepPath: Map<string, string[]>
}
/**
* Generate a dependency graph from lockfiles.
*
* If a current lockfile is provided, this function only includes new or changed
* packages in the graph. In other words, the graph returned will be a set
* subtraction of the packages in the wanted lockfile minus the current
* lockfile. This behavior can be configured with the `includeUnchangedDeps`
* option.
*/
export async function lockfileToDepGraph (
lockfile: LockfileObject,
currentLockfile: LockfileObject | null,
opts: LockfileToDepGraphOptions
): Promise<LockfileToDepGraphResult> {
const {
graph,
locationByDepPath,
injectionTargetsByDepPath,
} = await buildGraphFromPackages(lockfile, currentLockfile, opts)
const _getChildrenPaths = getChildrenPaths.bind(null, {
force: opts.force,
graph,
lockfileDir: opts.lockfileDir,
registries: opts.registries,
sideEffectsCacheRead: opts.sideEffectsCacheRead,
skipped: opts.skipped,
storeController: opts.storeController,
storeDir: opts.storeDir,
virtualStoreDir: opts.virtualStoreDir,
virtualStoreDirMaxLength: opts.virtualStoreDirMaxLength,
locationByDepPath,
} satisfies GetChildrenPathsContext)
for (const node of Object.values(graph)) {
const pkgSnapshot = lockfile.packages![node.depPath]
const allDeps = {
...pkgSnapshot.dependencies,
...(opts.include.optionalDependencies ? pkgSnapshot.optionalDependencies : {}),
}
const peerDeps = pkgSnapshot.peerDependencies ? new Set(Object.keys(pkgSnapshot.peerDependencies)) : null
node.children = _getChildrenPaths(allDeps, peerDeps, '.')
}
const directDependenciesByImporterId: DirectDependenciesByImporterId = {}
for (const importerId of opts.importerIds) {
const projectSnapshot = lockfile.importers[importerId]
const rootDeps = {
...(opts.include.devDependencies ? projectSnapshot.devDependencies : {}),
...(opts.include.dependencies ? projectSnapshot.dependencies : {}),
...(opts.include.optionalDependencies ? projectSnapshot.optionalDependencies : {}),
}
directDependenciesByImporterId[importerId] = _getChildrenPaths(rootDeps, null, importerId)
}
return { graph, directDependenciesByImporterId, injectionTargetsByDepPath }
}
async function buildGraphFromPackages (
lockfile: LockfileObject,
currentLockfile: LockfileObject | null,
opts: LockfileToDepGraphOptions
): Promise<{
graph: DependenciesGraph
locationByDepPath: Record<string, string>
injectionTargetsByDepPath: Map<string, string[]>
}> {
const currentPackages = currentLockfile?.packages ?? {}
const graph: DependenciesGraph = {}
const locationByDepPath: Record<string, string> = {}
// Only populated for directory deps (injected workspace packages)
const injectionTargetsByDepPath = new Map<string, string[]>()
const _getPatchInfo = getPatchInfo.bind(null, opts.patchedDependencies)
const promises: Array<Promise<void>> = []
const pkgSnapshotsWithLocations = iteratePkgsForVirtualStore(lockfile, opts)
for (const { dirInVirtualStore, pkgMeta } of pkgSnapshotsWithLocations) {
promises.push((async () => {
const { pkgIdWithPatchHash, name: pkgName, version: pkgVersion, depPath, pkgSnapshot } = pkgMeta
if (opts.skipped.has(depPath)) return
const pkg = {
name: pkgName,
version: pkgVersion,
engines: pkgSnapshot.engines,
cpu: pkgSnapshot.cpu,
os: pkgSnapshot.os,
libc: pkgSnapshot.libc,
}
const packageId = packageIdFromSnapshot(depPath, pkgSnapshot)
if (!opts.force && packageIsInstallable(packageId, pkg, {
engineStrict: opts.engineStrict,
lockfileDir: opts.lockfileDir,
nodeVersion: opts.nodeVersion,
optional: pkgSnapshot.optional === true,
supportedArchitectures: opts.supportedArchitectures,
}) === false) {
opts.skipped.add(depPath)
return
}
const isDirectoryDep = 'directory' in pkgSnapshot.resolution && pkgSnapshot.resolution.directory != null
if (isDirectoryDep && opts.ignoreLocalPackages) {
logger.info({
message: `Skipping local dependency ${pkgName}@${pkgVersion} (file: protocol)`,
prefix: opts.lockfileDir,
})
return
}
const depIsPresent = !isDirectoryDep &&
currentPackages[depPath] &&
equals(currentPackages[depPath].dependencies, pkgSnapshot.dependencies)
const depIntegrityIsUnchanged = isIntegrityEqual(pkgSnapshot.resolution, currentPackages[depPath]?.resolution)
const modules = path.join(dirInVirtualStore, 'node_modules')
const dir = path.join(modules, pkgName)
locationByDepPath[depPath] = dir
// Track directory deps for injected workspace packages
if (isDirectoryDep) {
injectionTargetsByDepPath.set(depPath, [dir])
}
// In GVS mode, packages that are allowed to build may have a .pnpm-needs-build
// marker indicating a previous build failed or was interrupted. When the
// marker is present, skip the fast path to force a re-fetch/re-import/re-build.
const mightNeedBuild = opts.enableGlobalVirtualStore &&
opts.allowBuild?.(pkgName, pkgVersion) === true
let dirExists: boolean | undefined
if (
depIsPresent &&
depIntegrityIsUnchanged &&
isEmpty(currentPackages[depPath].optionalDependencies ?? {}) &&
isEmpty(pkgSnapshot.optionalDependencies ?? {}) &&
!opts.includeUnchangedDeps
) {
dirExists = await pathExists(dir)
if (dirExists) {
if (!(mightNeedBuild && fs.existsSync(path.join(dir, '.pnpm-needs-build')))) return
} else {
brokenModulesLogger.debug({ missing: dir })
}
}
let fetchResponse!: Partial<FetchResponse>
if (depIsPresent && depIntegrityIsUnchanged && equals(currentPackages[depPath].optionalDependencies, pkgSnapshot.optionalDependencies)) {
if (dirExists ?? await pathExists(dir)) {
if (!(mightNeedBuild && fs.existsSync(path.join(dir, '.pnpm-needs-build')))) {
fetchResponse = {}
}
} else {
brokenModulesLogger.debug({ missing: dir })
}
}
if (!fetchResponse && opts.enableGlobalVirtualStore && !isDirectoryDep
&& !opts.force) {
if (dirExists ?? await pathExists(dir)) {
if (!(mightNeedBuild && fs.existsSync(path.join(dir, '.pnpm-needs-build')))) {
fetchResponse = {}
}
}
}
if (!fetchResponse) {
const resolution = pkgSnapshotToResolution(depPath, pkgSnapshot, opts.registries)
progressLogger.debug({ packageId, requester: opts.lockfileDir, status: 'resolved' })
try {
fetchResponse = await opts.storeController.fetchPackage({
allowBuild: opts.allowBuild,
force: false,
lockfileDir: opts.lockfileDir,
ignoreScripts: opts.ignoreScripts,
pkg: { name: pkgName, version: pkgVersion, id: packageId, resolution },
supportedArchitectures: opts.supportedArchitectures,
})
} catch (err) {
if (pkgSnapshot.optional) return
throw err
}
}
graph[dir] = {
children: {},
pkgIdWithPatchHash,
resolution: pkgSnapshot.resolution,
depPath,
dir,
fetching: fetchResponse.fetching,
filesIndexFile: fetchResponse.filesIndexFile,
forceImportPackage: !depIntegrityIsUnchanged,
hasBin: pkgSnapshot.hasBin === true,
hasBundledDependencies: pkgSnapshot.bundledDependencies != null,
modules,
name: pkgName,
version: pkgVersion,
optional: !!pkgSnapshot.optional,
optionalDependencies: new Set(Object.keys(pkgSnapshot.optionalDependencies ?? {})),
patch: _getPatchInfo(pkgName, pkgVersion),
}
})())
}
await Promise.all(promises)
return { graph, locationByDepPath, injectionTargetsByDepPath }
}
interface GetChildrenPathsContext {
graph: DependenciesGraph
force: boolean
registries: Registries
virtualStoreDir: string
storeDir: string
skipped: Set<DepPath>
lockfileDir: string
sideEffectsCacheRead: boolean
storeController: StoreController
locationByDepPath: Record<string, string>
virtualStoreDirMaxLength: number
}
function getChildrenPaths (
ctx: GetChildrenPathsContext,
allDeps: { [alias: string]: string },
peerDeps: Set<string> | null,
importerId: string
): { [alias: string]: string } {
const children: { [alias: string]: string } = {}
for (const [alias, ref] of Object.entries(allDeps)) {
const childDepPath = dp.refToRelative(ref, alias)
if (childDepPath === null) {
children[alias] = path.resolve(ctx.lockfileDir, importerId, ref.slice(5))
continue
}
const childRelDepPath = dp.refToRelative(ref, alias)!
if (ctx.locationByDepPath[childRelDepPath]) {
children[alias] = ctx.locationByDepPath[childRelDepPath]
} else if (ctx.graph[childRelDepPath]) {
children[alias] = ctx.graph[childRelDepPath].dir
} else if (ref.startsWith('file:')) {
children[alias] = path.resolve(ctx.lockfileDir, ref.slice(5))
} else if (!ctx.skipped.has(childRelDepPath) && ((peerDeps == null) || !peerDeps.has(alias))) {
throw new Error(`${childRelDepPath} not found in ${WANTED_LOCKFILE}`)
}
}
return children
}
function isIntegrityEqual (resolutionA?: LockfileResolution, resolutionB?: LockfileResolution) {
// The LockfileResolution type is a union, but it doesn't have a "tag"
// field to perform a discriminant match on. Using a type assertion is
// required to get the integrity field.
const integrityA = (resolutionA as ({ integrity?: string } | undefined))?.integrity
const integrityB = (resolutionB as ({ integrity?: string } | undefined))?.integrity
return integrityA === integrityB
}