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 deduplication of strings because slices.Compact doesn't sort the input #814

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
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
7 changes: 3 additions & 4 deletions pkg/build/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ import (
"strings"
"text/template"

"golang.org/x/exp/slices"

apko_types "chainguard.dev/apko/pkg/build/types"

"github.com/klauspost/compress/gzip"
"github.com/klauspost/pgzip"

"chainguard.dev/melange/pkg/config"
"chainguard.dev/melange/pkg/sca"
"chainguard.dev/melange/pkg/util"

"chainguard.dev/apko/pkg/log"
"github.com/chainguard-dev/go-apk/pkg/tarball"
Expand Down Expand Up @@ -364,10 +363,10 @@ func (pc *PackageBuild) GenerateDependencies() error {
unvendored := removeSelfProvidedDeps(generated.Runtime, generated.Vendored)

newruntime := append(pc.Dependencies.Runtime, unvendored...)
pc.Dependencies.Runtime = slices.Compact(newruntime)
pc.Dependencies.Runtime = util.Dedup(newruntime)

newprovides := append(pc.Dependencies.Provides, generated.Provides...)
pc.Dependencies.Provides = slices.Compact(newprovides)
pc.Dependencies.Provides = util.Dedup(newprovides)

pc.Dependencies.Runtime = removeSelfProvidedDeps(pc.Dependencies.Runtime, pc.Dependencies.Provides)

Expand Down
4 changes: 1 addition & 3 deletions pkg/build/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
"strconv"
"strings"

"golang.org/x/exp/slices"

"go.opentelemetry.io/otel"

"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -478,7 +476,7 @@ func (pctx *PipelineContext) ApplyNeeds(pb *PipelineBuild) error {
}
}

ic.Contents.Packages = slices.Compact(ic.Contents.Packages)
ic.Contents.Packages = util.Dedup(ic.Contents.Packages)

for _, sp := range pctx.Pipeline.Pipeline {
spctx, err := NewPipelineContext(&sp, pb.Build.Logger)
Expand Down
8 changes: 8 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
package util

import (
"cmp"
"context"
"encoding/hex"
"fmt"
"hash"
"io"
"net/http"
"os"
"slices"
"sort"
)

Expand Down Expand Up @@ -115,3 +117,9 @@ func Contains[T comparable](s []T, e T) bool {
}
return false
}

// 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)
}
14 changes: 14 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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")
}
Loading