-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
pnpm_lockfile.go
579 lines (504 loc) · 18 KB
/
pnpm_lockfile.go
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
package lockfile
import (
"fmt"
"io"
"reflect"
"sort"
"strings"
"github.com/pkg/errors"
"github.com/vercel/turbo/cli/internal/turbopath"
"github.com/vercel/turbo/cli/internal/yaml"
)
// PnpmLockfile Go representation of the contents of 'pnpm-lock.yaml'
// Reference https://github.com/pnpm/pnpm/blob/main/packages/lockfile-types/src/index.ts
type PnpmLockfile struct {
isV6 bool
// Before 6.0 version was stored as a float, but as of 6.0+ it's a string
Version interface{} `yaml:"lockfileVersion"`
NeverBuiltDependencies []string `yaml:"neverBuiltDependencies,omitempty"`
OnlyBuiltDependencies []string `yaml:"onlyBuiltDependencies,omitempty"`
Overrides map[string]string `yaml:"overrides,omitempty"`
PackageExtensionsChecksum string `yaml:"packageExtensionsChecksum,omitempty"`
PatchedDependencies map[string]PatchFile `yaml:"patchedDependencies,omitempty"`
Importers map[string]ProjectSnapshot `yaml:"importers"`
Packages map[string]PackageSnapshot `yaml:"packages,omitempty"`
Time map[string]string `yaml:"time,omitempty"`
}
var _ Lockfile = (*PnpmLockfile)(nil)
// ProjectSnapshot Snapshot used to represent projects in the importers section
type ProjectSnapshot struct {
// for v6 we omitempty
// for pre v6 we *need* to omit the empty map
Specifiers SpecifierMap `yaml:"specifiers,omitempty"`
// The values of these maps will be string if lockfileVersion <6 or DependencyV6 if 6+
Dependencies map[string]yaml.Node `yaml:"dependencies,omitempty"`
OptionalDependencies map[string]yaml.Node `yaml:"optionalDependencies,omitempty"`
DevDependencies map[string]yaml.Node `yaml:"devDependencies,omitempty"`
DependenciesMeta map[string]DependenciesMeta `yaml:"dependenciesMeta,omitempty"`
PublishDirectory string `yaml:"publishDirectory,omitempty"`
}
// SpecifierMap is a type wrapper that overrides IsZero for Golang's map
// to match the behavior that pnpm expects
type SpecifierMap map[string]string
// IsZero is used to check whether an object is zero to
// determine whether it should be omitted when marshaling
// with the omitempty flag.
func (m SpecifierMap) IsZero() bool {
return m == nil
}
var _ (yaml.IsZeroer) = (*SpecifierMap)(nil)
// DependencyV6 are dependency entries for lockfileVersion 6.0+
type DependencyV6 struct {
Specifier string `yaml:"specifier"`
Version string `yaml:"version"`
}
// Will try to find a resolution in any of the dependency fields
func (p *ProjectSnapshot) findResolution(dependency string) (DependencyV6, bool, error) {
var getResolution func(yaml.Node) (DependencyV6, bool, error)
if len(p.Specifiers) > 0 {
getResolution = func(node yaml.Node) (DependencyV6, bool, error) {
specifier, ok := p.Specifiers[dependency]
if !ok {
return DependencyV6{}, false, nil
}
var version string
if err := node.Decode(&version); err != nil {
return DependencyV6{}, false, err
}
return DependencyV6{Version: version, Specifier: specifier}, true, nil
}
} else {
getResolution = func(node yaml.Node) (DependencyV6, bool, error) {
var resolution DependencyV6
if err := node.Decode(&resolution); err != nil {
return DependencyV6{}, false, err
}
return resolution, true, nil
}
}
if resolution, ok := p.Dependencies[dependency]; ok {
return getResolution(resolution)
}
if resolution, ok := p.DevDependencies[dependency]; ok {
return getResolution(resolution)
}
if resolution, ok := p.OptionalDependencies[dependency]; ok {
return getResolution(resolution)
}
return DependencyV6{}, false, nil
}
// PackageSnapshot Snapshot used to represent a package in the packages setion
type PackageSnapshot struct {
Resolution PackageResolution `yaml:"resolution,flow"`
ID string `yaml:"id,omitempty"`
// only needed for packages that aren't in npm
Name string `yaml:"name,omitempty"`
Version string `yaml:"version,omitempty"`
Engines struct {
Node string `yaml:"node"`
NPM string `yaml:"npm,omitempty"`
} `yaml:"engines,omitempty,flow"`
CPU []string `yaml:"cpu,omitempty,flow"`
Os []string `yaml:"os,omitempty,flow"`
LibC []string `yaml:"libc,omitempty"`
Deprecated string `yaml:"deprecated,omitempty"`
HasBin bool `yaml:"hasBin,omitempty"`
Prepare bool `yaml:"prepare,omitempty"`
RequiresBuild bool `yaml:"requiresBuild,omitempty"`
BundledDependencies []string `yaml:"bundledDependencies,omitempty"`
PeerDependencies map[string]string `yaml:"peerDependencies,omitempty"`
PeerDependenciesMeta map[string]struct {
Optional bool `yaml:"optional"`
} `yaml:"peerDependenciesMeta,omitempty"`
Dependencies map[string]string `yaml:"dependencies,omitempty"`
OptionalDependencies map[string]string `yaml:"optionalDependencies,omitempty"`
TransitivePeerDependencies []string `yaml:"transitivePeerDependencies,omitempty"`
Dev bool `yaml:"dev"`
Optional bool `yaml:"optional,omitempty"`
Patched bool `yaml:"patched,omitempty"`
}
// PackageResolution Various resolution strategies for packages
type PackageResolution struct {
Type string `yaml:"type,omitempty"`
// For npm or tarball
Integrity string `yaml:"integrity,omitempty"`
// For tarball
Tarball string `yaml:"tarball,omitempty"`
// For local directory
Dir string `yaml:"directory,omitempty"`
// For git repo
Repo string `yaml:"repo,omitempty"`
Commit string `yaml:"commit,omitempty"`
}
// PatchFile represent a patch applied to a package
type PatchFile struct {
Path string `yaml:"path"`
Hash string `yaml:"hash"`
}
func isSupportedVersion(version interface{}) error {
switch version.(type) {
case string:
if version == "6.0" {
return nil
}
case float64:
if version == 5.3 || version == 5.4 {
return nil
}
default:
return fmt.Errorf("lockfileVersion of type %T is invalid", version)
}
supportedVersions := []string{"5.3", "5.4", "6.0"}
return errors.Errorf("Unable to generate pnpm-lock.yaml with lockfileVersion: %s. Supported lockfile versions are %v", version, supportedVersions)
}
// DependenciesMeta metadata for dependencies
type DependenciesMeta struct {
Injected bool `yaml:"injected,omitempty"`
Node string `yaml:"node,omitempty"`
Patch string `yaml:"patch,omitempty"`
}
// DecodePnpmLockfile parse a pnpm lockfile
func DecodePnpmLockfile(contents []byte) (*PnpmLockfile, error) {
var lockfile PnpmLockfile
err := yaml.Unmarshal(contents, &lockfile)
if err != nil {
return nil, errors.Wrap(err, "could not unmarshal lockfile: ")
}
switch lockfile.Version.(type) {
case float64:
lockfile.isV6 = false
case string:
lockfile.isV6 = true
default:
return nil, fmt.Errorf("Unexpected type of lockfileVersion: '%T', expected float64 or string", lockfile.Version)
}
return &lockfile, nil
}
// ResolvePackage Given a package and version returns the key, resolved version, and if it was found
func (p *PnpmLockfile) ResolvePackage(workspacePath turbopath.AnchoredUnixPath, name string, version string) (Package, error) {
// Check if version is a key
if _, ok := p.Packages[version]; ok {
return Package{Key: version, Version: p.extractVersion(version), Found: true}, nil
}
resolvedVersion, ok, err := p.resolveSpecifier(workspacePath, name, version)
if !ok || err != nil {
return Package{}, err
}
key := p.formatKey(name, resolvedVersion)
if entry, ok := (p.Packages)[key]; ok {
var version string
if entry.Version != "" {
version = entry.Version
} else {
version = resolvedVersion
}
return Package{Key: key, Version: version, Found: true}, nil
}
if entry, ok := p.Packages[resolvedVersion]; ok {
var version string
if entry.Version != "" {
version = entry.Version
} else {
// If there isn't a version field in the entry then the version is
// encoded in the key and we can omit the name from the version.
version = p.extractVersion(resolvedVersion)
}
return Package{Key: resolvedVersion, Version: version, Found: true}, nil
}
return Package{}, nil
}
// AllDependencies Given a lockfile key return all (dev/optional/peer) dependencies of that package
func (p *PnpmLockfile) AllDependencies(key string) (map[string]string, bool) {
deps := map[string]string{}
entry, ok := p.Packages[key]
if !ok {
return deps, false
}
for name, version := range entry.Dependencies {
deps[name] = version
}
for name, version := range entry.OptionalDependencies {
deps[name] = version
}
// Peer dependencies appear in the Dependencies map resolved
return deps, true
}
// Subgraph Given a list of lockfile keys returns a Lockfile based off the original one that only contains the packages given
func (p *PnpmLockfile) Subgraph(workspacePackages []turbopath.AnchoredSystemPath, packages []string) (Lockfile, error) {
lockfilePackages := make(map[string]PackageSnapshot, len(packages))
for _, key := range packages {
entry, ok := p.Packages[key]
if ok {
lockfilePackages[key] = entry
} else {
return nil, fmt.Errorf("Unable to find lockfile entry for %s", key)
}
}
importers, err := pruneImporters(p.Importers, workspacePackages)
if err != nil {
return nil, err
}
for _, importer := range importers {
for dependency, meta := range importer.DependenciesMeta {
if meta.Injected {
resolution, ok, err := importer.findResolution(dependency)
if err != nil {
return nil, errors.Wrapf(err, "Unable to decode reference to %s", dependency)
}
if !ok {
return nil, fmt.Errorf("Unable to find %s other than reference in dependenciesMeta", dependency)
}
entry, ok := p.Packages[resolution.Version]
if !ok {
return nil, fmt.Errorf("Unable to find package entry for %s", resolution)
}
lockfilePackages[resolution.Version] = entry
}
}
}
lockfile := PnpmLockfile{
Version: p.Version,
Packages: lockfilePackages,
NeverBuiltDependencies: p.NeverBuiltDependencies,
OnlyBuiltDependencies: p.OnlyBuiltDependencies,
Overrides: p.Overrides,
PackageExtensionsChecksum: p.PackageExtensionsChecksum,
PatchedDependencies: p.prunePatches(p.PatchedDependencies, lockfilePackages),
Importers: importers,
}
return &lockfile, nil
}
// Prune imports to only those have all of their dependencies in the packages list
func pruneImporters(importers map[string]ProjectSnapshot, workspacePackages []turbopath.AnchoredSystemPath) (map[string]ProjectSnapshot, error) {
prunedImporters := map[string]ProjectSnapshot{}
// Copy over root level importer
if root, ok := importers["."]; ok {
prunedImporters["."] = root
}
for _, workspacePath := range workspacePackages {
workspace := workspacePath.ToUnixPath().ToString()
importer, ok := importers[workspace]
// If a workspace has no dependencies *and* it is only depended on by the
// workspace root it will not show up as an importer.
if ok {
prunedImporters[workspace] = importer
}
}
return prunedImporters, nil
}
func (p *PnpmLockfile) prunePatches(patches map[string]PatchFile, packages map[string]PackageSnapshot) map[string]PatchFile {
if len(patches) == 0 {
return nil
}
patchPackages := make(map[string]PatchFile, len(patches))
for dependency := range packages {
if p.isV6 {
// Internally pnpm partially converts the new path format to the old
// format in order for existing parsing logic to work.
dependency = convertNewToOldDepPath(dependency)
}
dp := parseDepPath(dependency)
patchKey := fmt.Sprintf("%s@%s", dp.name, dp.version)
if patch, ok := patches[patchKey]; ok && patch.Hash == dp.patchHash() {
patchPackages[patchKey] = patch
}
}
return patchPackages
}
// Encode encode the lockfile representation and write it to the given writer
func (p *PnpmLockfile) Encode(w io.Writer) error {
if err := isSupportedVersion(p.Version); err != nil {
return err
}
encoder := yaml.NewEncoder(w)
encoder.SetIndent(2)
if err := encoder.Encode(p); err != nil {
return errors.Wrap(err, "unable to encode pnpm lockfile")
}
return nil
}
// Patches return a list of patches used in the lockfile
func (p *PnpmLockfile) Patches() []turbopath.AnchoredUnixPath {
if len(p.PatchedDependencies) == 0 {
return nil
}
patches := make([]string, len(p.PatchedDependencies))
i := 0
for _, patch := range p.PatchedDependencies {
patches[i] = patch.Path
i++
}
sort.Strings(patches)
patchPaths := make([]turbopath.AnchoredUnixPath, len(p.PatchedDependencies))
for i, patch := range patches {
patchPaths[i] = turbopath.AnchoredUnixPath(patch)
}
return patchPaths
}
// GlobalChange checks if there are any differences between lockfiles that would completely invalidate
// the cache.
func (p *PnpmLockfile) GlobalChange(other Lockfile) bool {
o, ok := other.(*PnpmLockfile)
return !ok ||
p.Version != o.Version ||
p.PackageExtensionsChecksum != o.PackageExtensionsChecksum ||
!reflect.DeepEqual(p.Overrides, o.Overrides) ||
!reflect.DeepEqual(p.PatchedDependencies, o.PatchedDependencies)
}
func (p *PnpmLockfile) resolveSpecifier(workspacePath turbopath.AnchoredUnixPath, name string, specifier string) (string, bool, error) {
pnpmWorkspacePath := workspacePath.ToString()
if pnpmWorkspacePath == "" {
// For pnpm, the root is named "."
pnpmWorkspacePath = "."
}
importer, ok := p.Importers[pnpmWorkspacePath]
if !ok {
return "", false, fmt.Errorf("no workspace '%v' found in lockfile", workspacePath)
}
resolution, ok, err := importer.findResolution(name)
if err != nil {
return "", false, err
}
// Verify that the specifier in the importer matches the one given
if !ok {
// Check if the specifier is already a resolved version
if _, ok := p.Packages[p.formatKey(name, specifier)]; ok {
return specifier, true, nil
}
return "", false, fmt.Errorf("Unable to find resolved version for %s@%s in %s", name, specifier, workspacePath)
}
overrideSpecifier := p.applyOverrides(name, specifier)
if resolution.Specifier != overrideSpecifier {
if _, ok := p.Packages[p.formatKey(name, overrideSpecifier)]; ok {
return overrideSpecifier, true, nil
}
return "", false, nil
}
return resolution.Version, true, nil
}
// Apply pnpm overrides to specifier, see https://pnpm.io/package_json#pnpmoverrides
// Note this is barebones support and will only supports global overrides
// future work will support semver ranges and selector filtering.
func (p *PnpmLockfile) applyOverrides(name string, specifier string) string {
if len(p.Overrides) > 0 {
if new, ok := p.Overrides[name]; ok {
return new
}
}
return specifier
}
// Formatter of the lockfile key given a package name and version
func (p *PnpmLockfile) formatKey(name string, version string) string {
if p.isV6 {
return fmt.Sprintf("/%s@%s", name, version)
}
return fmt.Sprintf("/%s/%s", name, version)
}
// Extracts version from lockfile key
func (p *PnpmLockfile) extractVersion(key string) string {
if p.isV6 {
key = convertNewToOldDepPath(key)
}
dp := parseDepPath(key)
if dp.peerSuffix != "" {
sep := ""
if !p.isV6 {
sep = "_"
}
return fmt.Sprintf("%s%s%s", dp.version, sep, dp.peerSuffix)
}
return dp.version
}
// Parsed representation of a pnpm lockfile key
type depPath struct {
host string
name string
version string
peerSuffix string
}
func parseDepPath(dependency string) depPath {
// See https://github.com/pnpm/pnpm/blob/185ab01adfc927ea23d2db08a14723bf51d0025f/packages/dependency-path/src/index.ts#L96
var dp depPath
parts := strings.Split(dependency, "/")
shift := func() string {
if len(parts) == 0 {
return ""
}
val := parts[0]
parts = parts[1:]
return val
}
isAbsolute := dependency[0] != '/'
// Skip leading '/'
if !isAbsolute {
shift()
}
if isAbsolute {
dp.host = shift()
}
if len(parts) == 0 {
return dp
}
if strings.HasPrefix(parts[0], "@") {
dp.name = fmt.Sprintf("%s/%s", shift(), shift())
} else {
dp.name = shift()
}
version := strings.Join(parts, "/")
if len(version) > 0 {
var peerSuffixIndex int
if strings.Contains(version, "(") && strings.HasSuffix(version, ")") {
// v6 encodes peers deps using (peer=version)
// also used to encode patches using (path_hash=hash)
peerSuffixIndex = strings.Index(version, "(")
dp.peerSuffix = version[peerSuffixIndex:]
dp.version = version[0:peerSuffixIndex]
} else {
// pre v6 uses _ to separate version from peer dependencies
// if a dependency is patched and has peer dependencies its version will
// be encoded as version_patchHash_peerDepsHash
peerSuffixIndex = strings.Index(version, "_")
if peerSuffixIndex != -1 {
dp.peerSuffix = version[peerSuffixIndex+1:]
dp.version = version[0:peerSuffixIndex]
}
}
if peerSuffixIndex == -1 {
dp.version = version
}
}
return dp
}
var _patchHashKey = "patch_hash="
func (d depPath) patchHash() string {
if strings.HasPrefix(d.peerSuffix, "(") && strings.HasSuffix(d.peerSuffix, ")") {
for _, part := range strings.Split(d.peerSuffix, "(") {
if strings.HasPrefix(part, _patchHashKey) {
// drop the enclosing ')'
return part[len(_patchHashKey) : len(part)-1]
}
}
// no patch entry found
return ""
}
sepIndex := strings.Index(d.peerSuffix, "_")
if sepIndex != -1 {
return d.peerSuffix[:sepIndex]
}
// if a dependency just has a single suffix we can't tell if it's a patch or peer hash
// return it in case it's a patch hash
return d.peerSuffix
}
// Used to convert v6's dep path of /name@version to v5's /name/version
// See https://github.com/pnpm/pnpm/blob/185ab01adfc927ea23d2db08a14723bf51d0025f/lockfile/lockfile-file/src/experiments/inlineSpecifiersLockfileConverters.ts#L162
func convertNewToOldDepPath(newPath string) string {
if len(newPath) > 2 && !strings.Contains(newPath[2:], "@") {
return newPath
}
searchStartIndex := strings.Index(newPath, "/@") + 2
index := strings.Index(newPath[searchStartIndex:], "@") + searchStartIndex
if strings.Contains(newPath, "(") && index > strings.Index(newPath, "(") {
return newPath
}
return fmt.Sprintf("%s/%s", newPath[0:index], newPath[index+1:])
}