From a3b79b4122c970cb8fbf492f582b224d44c54282 Mon Sep 17 00:00:00 2001 From: Emin Aktas Date: Thu, 9 Jan 2025 03:18:44 +0300 Subject: [PATCH] =?UTF-8?q?Fix=20package=20name=20handling=20to=20retain?= =?UTF-8?q?=20version=20and=20strip=20=E2=80=98@=E2=80=99=20suffix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Emin Aktas --- pkg/build/lock.go | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/pkg/build/lock.go b/pkg/build/lock.go index c3f76a206..e00e85890 100644 --- a/pkg/build/lock.go +++ b/pkg/build/lock.go @@ -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] } @@ -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 @@ -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) } @@ -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) @@ -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)