Repository: chainguard-dev/apko
Pull request #1664 (2025-05-07 18:31:25)

Respect arch flags when using lockfiles
This ~regressed in https://github.com/chainguard-dev/apko/pull/1654

jonjohnsonjr: Added a tiny test.

Patch:
diff --git a/pkg/build/lock.go b/pkg/build/lock.go
index 29ed637..902494a 100644
--- a/pkg/build/lock.go
+++ b/pkg/build/lock.go
@@ -73,7 +73,7 @@ func LockImageConfiguration(ctx context.Context, ic types.ImageConfiguration, op
 				return nil, nil, err
 			}
 		}
-		pls = l.Arch2LockedPackages()
+		pls = l.Arch2LockedPackages(input.Archs)
 	}
 
 	ics := make(map[string]*types.ImageConfiguration, len(mc.Contexts)+1)
diff --git a/pkg/lock/lock.go b/pkg/lock/lock.go
index 6dca46f..78b0c14 100644
--- a/pkg/lock/lock.go
+++ b/pkg/lock/lock.go
@@ -4,6 +4,8 @@ import (
 	"encoding/json"
 	"fmt"
 	"os"
+
+	"chainguard.dev/apko/pkg/build/types"
 )
 
 type Lock struct {
@@ -79,12 +81,21 @@ func (lock Lock) SaveToFile(lockFile string) error {
 	return os.WriteFile(lockFile, jsonb, os.ModePerm)
 }
 
-// Arch2LockedPackages returns map: for each arch -> list of {package_name}={version}.
-func (lock Lock) Arch2LockedPackages() map[string][]string {
+// Arch2LockedPackages returns map: for each arch -> list of {package_name}={version} in archs.
+func (lock Lock) Arch2LockedPackages(archs []types.Architecture) map[string][]string {
+	want := map[string]struct{}{}
+	for _, arch := range archs {
+		want[arch.String()] = struct{}{}
+	}
 	lockedPackages := map[string][]string{}
 	for _, p := range lock.Contents.Packages {
 		_, ok := lockedPackages[p.Architecture]
 		if !ok {
+			if _, ok := want[types.Architecture(p.Architecture).String()]; ok {
+				// Ignore architectures we aren't building.
+				continue
+			}
+
 			lockedPackages[p.Architecture] = []string{}
 		}
 		lockedPackages[p.Architecture] = append(lockedPackages[p.Architecture], fmt.Sprintf("%s=%s", p.Name, p.Version))


Test patch:
diff --git a/pkg/lock/lock_test.go b/pkg/lock/lock_test.go
new file mode 100644
index 0000000..487b4ce
--- /dev/null
+++ b/pkg/lock/lock_test.go
@@ -0,0 +1,42 @@
+// Copyright 2025 Chainguard, Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package lock
+
+import (
+	"testing"
+
+	"chainguard.dev/apko/pkg/build/types"
+)
+
+func TestArch2LockedPackages(t *testing.T) {
+	l := Lock{
+		Contents: LockContents{
+			Packages: []LockPkg{{
+				Name:         "avx",
+				Version:      "1.2.3",
+				Architecture: "x86_64",
+			}, {
+				Name:         "sve",
+				Version:      "2.3.4",
+				Architecture: "aarch64",
+			}},
+		},
+	}
+
+	archs := []types.Architecture{types.Architecture("aarch64")}
+	if got, want := len(l.Arch2LockedPackages(archs)), 1; got != want {
+		t.Errorf("wanted %d arch, got %d", want, got)
+	}
+}