Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix handling of injected dependencies for pnpm prune #2121

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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