Skip to content

Commit

Permalink
fix handling of injected dependencies for pnpm prune (#2121)
Browse files Browse the repository at this point in the history
* fix handling of injected dependencies for pnpm prune

* add unit test for injected package behavior

* catch injected optional and dev dependencies
  • Loading branch information
chris-olszewski authored Oct 6, 2022
1 parent 4ec2013 commit 426a04a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
31 changes: 31 additions & 0 deletions cli/internal/lockfile/pnpm_lockfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ type ProjectSnapshot struct {
PublishDirectory string `yaml:"publishDirectory,omitempty"`
}

// Will try to find a resolution in any of the dependency fields
func (p *ProjectSnapshot) findResolution(dependency string) (string, bool) {
if resolution, ok := p.Dependencies[dependency]; ok {
return resolution, true
}
if resolution, ok := p.DevDependencies[dependency]; ok {
return resolution, true
}
if resolution, ok := p.OptionalDependencies[dependency]; ok {
return resolution, true
}
return "", false
}

// PackageSnapshot Snapshot used to represent a package in the packages setion
type PackageSnapshot struct {
Resolution PackageResolution `yaml:"resolution,flow"`
Expand Down Expand Up @@ -184,6 +198,23 @@ func (p *PnpmLockfile) Subgraph(workspacePackages []turbopath.AnchoredSystemPath
return nil, err
}

for _, importer := range importers {
for dependency, meta := range importer.DependenciesMeta {
if meta.Injected {
resolution, ok := importer.findResolution(dependency)
if !ok {
return nil, fmt.Errorf("Unable to find %s other than reference in dependenciesMeta", dependency)
}
entry, ok := p.Packages[resolution]
if !ok {
return nil, fmt.Errorf("Unable to find package entry for %s", resolution)
}

lockfilePackages[resolution] = entry
}
}
}

lockfile := PnpmLockfile{
Version: p.Version,
Importers: importers,
Expand Down
22 changes: 22 additions & 0 deletions cli/internal/lockfile/pnpm_lockfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,25 @@ func Test_SpecifierResolution(t *testing.T) {
}
}
}

func Test_SubgraphInjectedPackages(t *testing.T) {
contents, err := getFixture(t, "pnpm7-workspace.yaml")
if err != nil {
t.Error(err)
}
lockfile, err := DecodePnpmLockfile(contents)
assert.NilError(t, err, "decode lockfile")

packageWithInjectedPackage := turbopath.AnchoredUnixPath("apps/docs").ToSystemPath()

prunedLockfile, err := lockfile.Subgraph([]turbopath.AnchoredSystemPath{packageWithInjectedPackage}, []string{})
assert.NilError(t, err, "prune lockfile")

pnpmLockfile, ok := prunedLockfile.(*PnpmLockfile)
assert.Assert(t, ok, "got different lockfile impl")

_, hasInjectedPackage := pnpmLockfile.Packages["file:packages/ui"]

assert.Assert(t, hasInjectedPackage, "pruned lockfile is missing injected package")

}
11 changes: 10 additions & 1 deletion cli/internal/lockfile/testdata/pnpm7-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ importers:
next: 12.2.5_ir3quccc6i62x6qn6jjhyjjiey
react: 18.2.0
react-dom: 18.2.0_react@18.2.0
ui: link:../../packages/ui
ui: file:packages/ui
underscore: 1.13.4_3pbfs36izefyn2uycmknwkvuuy
devDependencies:
'@babel/core': 7.19.1
Expand All @@ -50,6 +50,9 @@ importers:
next-transpile-modules: 9.0.0
tsconfig: link:../../packages/tsconfig
typescript: 4.8.3
dependenciesMeta:
ui:
injected: true

apps/web:
specifiers:
Expand Down Expand Up @@ -2444,3 +2447,9 @@ packages:

/yallist/4.0.0:
resolution: {integrity: sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==}

file:packages/ui:
resolution: {directory: packages/ui, type: directory}
name: ui
version: 0.0.0
dev: false

3 comments on commit 426a04a

@vercel
Copy link

@vercel vercel bot commented on 426a04a Oct 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checks for Deployment have failed

turborepo-examples-basic – ./examples/basic/apps/web

@vercel
Copy link

@vercel vercel bot commented on 426a04a Oct 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

turborepo-examples-basic – ./examples/basic/apps/web

turborepo-examples-basic.vercel.sh
turborepo-docs.vercel.sh
turborepo-examples-basic-git-main.vercel.sh

@vercel
Copy link

@vercel vercel bot commented on 426a04a Oct 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

examples-basic-web – ./examples/basic/apps/web

turborepo-examples-basic-web.vercel.sh
examples-basic-web-git-main.vercel.sh
examples-basic-web.vercel.sh

Please sign in to comment.