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 package name handling to retain version and strip ‘@’ suffix #1472

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
37 changes: 33 additions & 4 deletions pkg/build/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ type resolved struct {
arch string
packages sets.Set[string]
versions map[string]string
pinned map[string]string
provided map[string]sets.Set[string]
}

Expand All @@ -124,21 +125,37 @@ func unify(originals []string, inputs []resolved) (map[string][]string, map[stri
originalPackages := resolved{
packages: make(sets.Set[string], len(originals)),
versions: make(map[string]string, len(originals)),
pinned: make(map[string]string, len(originals)),
}

byArch := map[string][]string{}

for _, orig := range originals {
name := orig
version := ""
pinned := ""

// The function we want from go-apk is private, but these are all the
// special characters that delimit the package name from the cosntraint
// so lop off the package name and stick the rest of the constraint into
// the versions map.
if idx := strings.IndexAny(orig, "=<>~"); idx >= 0 {
name = orig[:idx]
version = orig[idx:]
}

// Extract pinned version if present
if idx := strings.IndexAny(orig, "@"); idx >= 0 {
pinned = orig[idx:]
}

// Remove pinned suffix from name and version
name = strings.TrimSuffix(name, pinned)
version = strings.TrimSuffix(version, pinned)

originalPackages.packages.Insert(name)
originalPackages.versions[name] = strings.TrimPrefix(orig, name)
originalPackages.versions[name] = version
originalPackages.pinned[name] = pinned
}

// Start accumulating using the first entry, and unify it with the other
Expand Down Expand Up @@ -226,7 +243,11 @@ func unify(originals []string, inputs []resolved) (map[string][]string, map[stri
// package constraint including the operator.
for _, pkg := range sets.List(missing) {
if ver := originalPackages.versions[pkg]; ver != "" {
pl = append(pl, fmt.Sprintf("%s%s", pkg, ver))
if pin := originalPackages.versions[pkg]; pin != "" {
pl = append(pl, fmt.Sprintf("%s%s%s", pkg, ver, pin))
} else {
pl = append(pl, fmt.Sprintf("%s%s", pkg, ver))
}
} else {
pl = append(pl, pkg)
}
Expand All @@ -235,7 +256,11 @@ func unify(originals []string, inputs []resolved) (map[string][]string, map[stri
// Append all of the resolved and unified packages with an exact match
// based on the resolved version we found.
for _, pkg := range sets.List(acc.packages) {
pl = append(pl, fmt.Sprintf("%s=%s", pkg, acc.versions[pkg]))
pkgName := fmt.Sprintf("%s=%s", pkg, acc.versions[pkg])
if pin := originalPackages.pinned[pkg]; pin != "" {
pkgName = fmt.Sprintf("%s%s", pkgName, pin)
}
pl = append(pl, pkgName)
}
// Sort the package list explicitly with the `=` included.
// This is because (foo, foo-bar) sorts differently than (foo=1, foo-bar=1)
Expand All @@ -249,7 +274,11 @@ func unify(originals []string, inputs []resolved) (map[string][]string, map[stri
for _, input := range inputs {
pl := make([]string, 0, len(input.packages))
for _, pkg := range sets.List(input.packages) {
pl = append(pl, fmt.Sprintf("%s=%s", pkg, input.versions[pkg]))
pkgName := fmt.Sprintf("%s=%s", pkg, input.versions[pkg])
if pin := originalPackages.pinned[pkg]; pin != "" {
pkgName = fmt.Sprintf("%s%s", pkgName, pin)
}
pl = append(pl, pkgName)
}
// Sort the package list explicitly with the `=` included.
// This is because (foo, foo-bar) sorts differently than (foo=1, foo-bar=1)
Expand Down