Skip to content

Commit e1e263d

Browse files
committed
Pass pkg pins from meta package to installable package
1 parent 015b8e8 commit e1e263d

File tree

2 files changed

+70
-23
lines changed

2 files changed

+70
-23
lines changed

pkg/build/lock.go

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"maps"
2121
"reflect"
2222
"regexp"
23+
"slices"
2324
"sort"
2425
"strings"
2526

@@ -267,50 +268,37 @@ func unify(originals []string, inputs []resolved) (map[string][]string, map[stri
267268

268269
// Allocate a list sufficient for holding all of our locked package versions
269270
// as well as the packages we were unable to lock.
270-
pl := make([]string, 0, len(acc.versions)+missing.Len())
271+
allPl := make([]string, 0, len(acc.versions)+missing.Len())
271272

272273
// Append any missing packages with their original constraints coming in.
273274
// NOTE: the originalPackages "versions" includes the remainder of the
274275
// package constraint including the operator.
275276
for _, pkg := range sets.List(missing) {
276277
if ver := originalPackages.versions[pkg]; ver != "" {
277278
if pin := originalPackages.versions[pkg]; pin != "" {
278-
pl = append(pl, fmt.Sprintf("%s%s%s", pkg, ver, pin))
279+
allPl = append(allPl, fmt.Sprintf("%s%s%s", pkg, ver, pin))
279280
} else {
280-
pl = append(pl, fmt.Sprintf("%s%s", pkg, ver))
281+
allPl = append(allPl, fmt.Sprintf("%s%s", pkg, ver))
281282
}
282283
} else {
283-
pl = append(pl, pkg)
284+
allPl = append(allPl, pkg)
284285
}
285286
}
286287

287-
// Append all of the resolved and unified packages with an exact match
288-
// based on the resolved version we found.
289-
for _, pkg := range sets.List(acc.packages) {
290-
pkgName := fmt.Sprintf("%s=%s", pkg, acc.versions[pkg])
291-
if pin := originalPackages.pinned[pkg]; pin != "" {
292-
pkgName = fmt.Sprintf("%s%s", pkgName, pin)
293-
}
294-
pl = append(pl, pkgName)
295-
}
288+
allPl = slices.Concat(setPkgNames(acc, originalPackages), allPl)
289+
296290
// Sort the package list explicitly with the `=` included.
297291
// This is because (foo, foo-bar) sorts differently than (foo=1, foo-bar=1)
298292
// due to the presence or absence of the `=` character.
299-
sort.Strings(pl)
293+
sort.Strings(allPl)
300294

301295
// "index" is a sentinel value for the intersectino of all architectures.
302296
// This is a reference to the OCI image index we'll be producing with it.
303-
byArch["index"] = pl
297+
byArch["index"] = allPl
304298

305299
for _, input := range inputs {
306-
pl := make([]string, 0, len(input.packages))
307-
for _, pkg := range sets.List(input.packages) {
308-
pkgName := fmt.Sprintf("%s=%s", pkg, input.versions[pkg])
309-
if pin := originalPackages.pinned[pkg]; pin != "" {
310-
pkgName = fmt.Sprintf("%s%s", pkgName, pin)
311-
}
312-
pl = append(pl, pkgName)
313-
}
300+
pl := setPkgNames(input, originalPackages)
301+
314302
// Sort the package list explicitly with the `=` included.
315303
// This is because (foo, foo-bar) sorts differently than (foo=1, foo-bar=1)
316304
// due to the presence or absence of the `=` character.
@@ -334,5 +322,34 @@ func unify(originals []string, inputs []resolved) (map[string][]string, map[stri
334322
return byArch, nil, nil
335323
}
336324

325+
// setPkgNames returns a list of package names with their versions and pins
326+
func setPkgNames(input resolved, original resolved) []string {
327+
pl := make([]string, 0, len(input.packages))
328+
329+
// Append all the resolved and unified packages with an exact match based on the
330+
// resolved version we found.
331+
for _, pkg := range sets.List(input.packages) {
332+
pkgName := fmt.Sprintf("%s=%s", pkg, input.versions[pkg])
333+
if pin := original.pinned[pkg]; pin != "" {
334+
pkgName = fmt.Sprintf("%s%s", pkgName, pin)
335+
}
336+
337+
// If the package provides something (such as a meta package) that has been
338+
// specified with a pin, then we populate the pin based on what it provides.
339+
for _, prov := range input.provided[pkg].UnsortedList() {
340+
if strings.IndexAny(prov, ":") >= 0 {
341+
continue
342+
}
343+
if pin := original.pinned[prov]; pin != "" {
344+
pkgName = fmt.Sprintf("%s%s", pkgName, pin)
345+
break
346+
}
347+
}
348+
349+
pl = append(pl, pkgName)
350+
}
351+
return pl
352+
}
353+
337354
// Copied from go-apk's version.go
338355
var packageNameRegex = regexp.MustCompile(`^([^@=><~]+)(([=><~]+)([^@]+))?(@([a-zA-Z0-9]+))?$`)

pkg/build/lock_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,36 @@ func TestUnify(t *testing.T) {
8585
"foo=1.2.3",
8686
},
8787
},
88+
}, {
89+
name: "locked and pinned parent meta-package",
90+
originals: []string{"foo=1.2.3", "bar-parent=2.4.6@local", "baz=0.0.1"},
91+
inputs: []resolved{{
92+
arch: "amd64",
93+
packages: sets.New("foo", "bar", "baz"),
94+
versions: map[string]string{
95+
"foo": "1.2.3",
96+
"bar": "2.4.6",
97+
"baz": "0.0.1",
98+
},
99+
pinned: map[string]string{
100+
"bar-parent": "@local",
101+
},
102+
provided: map[string]sets.Set[string]{
103+
"bar": sets.New("bar-parent"),
104+
},
105+
}},
106+
want: map[string][]string{
107+
"amd64": {
108+
"bar=2.4.6@local",
109+
"baz=0.0.1",
110+
"foo=1.2.3",
111+
},
112+
"index": {
113+
"bar=2.4.6@local",
114+
"baz=0.0.1",
115+
"foo=1.2.3",
116+
},
117+
},
88118
}, {
89119
name: "transitive dependency",
90120
originals: []string{"foo", "bar", "baz"},

0 commit comments

Comments
 (0)