diff --git a/pkg/build/compile.go b/pkg/build/compile.go index 7b49ffee8..c68136be7 100644 --- a/pkg/build/compile.go +++ b/pkg/build/compile.go @@ -245,6 +245,7 @@ func (c *Compiled) compilePipeline(ctx context.Context, sm *SubstitutionMap, pip } if parent != nil { + with = util.RightJoinMap(parent, with) } diff --git a/pkg/build/package.go b/pkg/build/package.go index 45a164002..f479588f1 100644 --- a/pkg/build/package.go +++ b/pkg/build/package.go @@ -26,6 +26,7 @@ import ( "os" "path/filepath" "runtime" + "slices" "strings" "text/template" @@ -36,7 +37,6 @@ import ( "chainguard.dev/melange/pkg/config" "chainguard.dev/melange/pkg/sca" - "chainguard.dev/melange/pkg/util" "chainguard.dev/apko/pkg/apk/tarball" "github.com/chainguard-dev/clog" @@ -342,15 +342,15 @@ func (pc *PackageBuild) GenerateDependencies(ctx context.Context, hdl sca.SCAHan unvendored := removeSelfProvidedDeps(generated.Runtime, generated.Vendored) newruntime := append(pc.Dependencies.Runtime, unvendored...) - pc.Dependencies.Runtime = util.Dedup(newruntime) + pc.Dependencies.Runtime = slices.Compact(slices.Sorted(slices.Values(newruntime))) newprovides := append(pc.Dependencies.Provides, generated.Provides...) - pc.Dependencies.Provides = util.Dedup(newprovides) + pc.Dependencies.Provides = slices.Compact(slices.Sorted(slices.Values(newprovides))) pc.Dependencies.Runtime = removeSelfProvidedDeps(pc.Dependencies.Runtime, pc.Dependencies.Provides) // Sets .PKGINFO `# vendored = ...` comments; does not affect resolution. - pc.Dependencies.Vendored = util.Dedup(generated.Vendored) + pc.Dependencies.Vendored = slices.Compact(slices.Sorted(slices.Values(generated.Vendored))) pc.Dependencies.Summarize(ctx) diff --git a/pkg/sca/e2e_test.go b/pkg/sca/e2e_test.go index dc9ba8a27..b1cc4d476 100644 --- a/pkg/sca/e2e_test.go +++ b/pkg/sca/e2e_test.go @@ -21,7 +21,6 @@ import ( "testing" "chainguard.dev/melange/pkg/config" - "chainguard.dev/melange/pkg/util" "github.com/chainguard-dev/clog/slogtest" "github.com/google/go-cmp/cmp" ) @@ -36,13 +35,13 @@ func TestGoFipsBinDeps(t *testing.T) { t.Fatal(err) } want := config.Dependencies{ - Runtime: util.Dedup([]string{ + Runtime: []string{ "openssl-config-fipshardened", "so:ld-linux-x86-64.so.2", "so:libc.so.6", "so:libcrypto.so.3", "so:libssl.so.3", - }), + }, Provides: []string{"cmd:go-fips-bin=0.0.1-r0"}, } if diff := cmp.Diff(want, got); diff != "" { diff --git a/pkg/sca/sca.go b/pkg/sca/sca.go index f60ceb32b..ca48e9123 100644 --- a/pkg/sca/sca.go +++ b/pkg/sca/sca.go @@ -32,7 +32,6 @@ import ( "github.com/chainguard-dev/go-pkgconfig" "chainguard.dev/melange/pkg/config" - "chainguard.dev/melange/pkg/util" ) var libDirs = []string{"lib/", "usr/lib/", "lib64/", "usr/lib64/"} @@ -783,9 +782,9 @@ func Analyze(ctx context.Context, hdl SCAHandle, generated *config.Dependencies) } } - generated.Runtime = util.Dedup(generated.Runtime) - generated.Provides = util.Dedup(generated.Provides) - generated.Vendored = util.Dedup(generated.Vendored) + generated.Runtime = slices.Compact(slices.Sorted(slices.Values(generated.Runtime))) + generated.Provides = slices.Compact(slices.Sorted(slices.Values(generated.Provides))) + generated.Vendored = slices.Compact(slices.Sorted(slices.Values(generated.Vendored))) if hdl.Options().NoCommands { generated.Provides = slices.DeleteFunc(generated.Provides, func(s string) bool { diff --git a/pkg/sca/sca_test.go b/pkg/sca/sca_test.go index 72938020b..f7793c5d4 100644 --- a/pkg/sca/sca_test.go +++ b/pkg/sca/sca_test.go @@ -35,7 +35,6 @@ import ( "chainguard.dev/apko/pkg/apk/apk" "chainguard.dev/apko/pkg/apk/expandapk" "chainguard.dev/melange/pkg/config" - "chainguard.dev/melange/pkg/util" "github.com/chainguard-dev/clog/slogtest" "github.com/google/go-cmp/cmp" "gopkg.in/ini.v1" @@ -151,16 +150,16 @@ func TestExecableSharedObjects(t *testing.T) { } want := config.Dependencies{ - Runtime: util.Dedup([]string{ + Runtime: []string{ "so:ld-linux-aarch64.so.1", "so:libc.so.6", "so:libcap.so.2", "so:libpsx.so.2", - }), - Provides: util.Dedup([]string{ + }, + Provides: []string{ "so:libcap.so.2=2", "so:libpsx.so.2=2", - }), + }, } if diff := cmp.Diff(want, got); diff != "" { t.Errorf("Analyze(): (-want, +got):\n%s", diff) @@ -181,18 +180,22 @@ func TestVendoredPkgConfig(t *testing.T) { } want := config.Dependencies{ - Runtime: util.Dedup([]string{ + Runtime: []string{ // We only include libecpg_compat.so.3 to test that "libexec" isn't treated as a library directory. // These are dependencies of libecpg_compat.so.3, but if we had the whole neon APK it would look different. - "so:libecpg.so.6", "so:libpgtypes.so.3", "so:libpq.so.5", "so:libc.so.6", "so:ld-linux-aarch64.so.1", - }), - Vendored: util.Dedup([]string{ - "so:libecpg_compat.so.3=3", + "so:ld-linux-aarch64.so.1", + "so:libc.so.6", + "so:libecpg.so.6", + "so:libpgtypes.so.3", + "so:libpq.so.5", + }, + Vendored: []string{ "pc:libecpg=4604-r0", "pc:libecpg_compat=4604-r0", "pc:libpgtypes=4604-r0", "pc:libpq=4604-r0", - }), + "so:libecpg_compat.so.3=3", + }, } if diff := cmp.Diff(want, got); diff != "" { @@ -252,7 +255,7 @@ func TestUnstableSonames(t *testing.T) { } want := config.Dependencies{ - Runtime: util.Dedup([]string{ + Runtime: []string{ "so:ld-linux-aarch64.so.1", "so:libaws-c-auth.so.1.0.0", "so:libaws-c-cal.so.1.0.0", @@ -262,7 +265,7 @@ func TestUnstableSonames(t *testing.T) { "so:libaws-c-s3.so.0unstable", "so:libaws-checksums.so.1.0.0", "so:libc.so.6", - }), + }, Provides: []string{"so:libaws-c-s3.so.0unstable=0"}, } @@ -278,13 +281,13 @@ func TestShbangDeps(t *testing.T) { defer th.exp.Close() want := config.Dependencies{ - Runtime: util.Dedup([]string{ + Runtime: []string{ "cmd:bash", "cmd:envDashSCmd", "cmd:python3.12", "so:ld-linux-x86-64.so.2", "so:libc.so.6", - }), + }, Provides: nil, } diff --git a/pkg/util/util.go b/pkg/util/util.go index 1f4260126..0088d6313 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -14,16 +14,9 @@ package util -import ( - "cmp" - "slices" -) - // Given a left and right map, perform a right join and return the result func RightJoinMap(left map[string]string, right map[string]string) map[string]string { - // this is the worst case possible length, assuming no overlaps. - length := len(left) + len(right) - output := make(map[string]string, length) + output := map[string]string{} // copy the left-side first for k, v := range left { @@ -37,9 +30,3 @@ func RightJoinMap(left map[string]string, right map[string]string) map[string]st return output } - -// Dedup wraps slices.Sort and slices.Compact to deduplicate a slice. -func Dedup[S ~[]E, E cmp.Ordered](s S) S { - slices.Sort(s) - return slices.Compact(s) -} diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go deleted file mode 100644 index d70128388..000000000 --- a/pkg/util/util_test.go +++ /dev/null @@ -1,14 +0,0 @@ -package util - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func TestDedup(t *testing.T) { - a := []int{0, 1, 2, 3, 1, 4, 5, 9, 16, 9, 12, 9, 9, 9, 13, 12, 15, 17, 15} - b := Dedup(a) - - require.Equal(t, len(b), 12, "the deduplicated list should have 12 elements") -}