Use Label (string) instead of Label (struct) to dedupe

Using Label struct as the map key causes issues because it contains
OriginalModuleName. The same module will have a different value for this
property when called from inside a soong namespace vs from outside.

If there are dups, we can just choose the first one. OriginalModuleName
is often used with ModuleFromName, and that function panics if there are
modules with the same name in two separate soong namespaces

Test: go test ./bp2build ./bazel

Change-Id: I2ee33efab03016a72f1eea99cb958b6198baeca2
This commit is contained in:
Spandan Das 2023-08-16 20:45:39 +00:00
parent f768e6e27e
commit 5e04d4884a
2 changed files with 12 additions and 11 deletions

View file

@ -194,14 +194,7 @@ func (ll *LabelList) Partition(predicate func(label Label) bool) (LabelList, Lab
// UniqueSortedBazelLabels takes a []Label and deduplicates the labels, and returns
// the slice in a sorted order.
func UniqueSortedBazelLabels(originalLabels []Label) []Label {
uniqueLabelsSet := make(map[Label]bool)
for _, l := range originalLabels {
uniqueLabelsSet[l] = true
}
var uniqueLabels []Label
for l, _ := range uniqueLabelsSet {
uniqueLabels = append(uniqueLabels, l)
}
uniqueLabels := FirstUniqueBazelLabels(originalLabels)
sort.SliceStable(uniqueLabels, func(i, j int) bool {
return uniqueLabels[i].Label < uniqueLabels[j].Label
})
@ -210,13 +203,13 @@ func UniqueSortedBazelLabels(originalLabels []Label) []Label {
func FirstUniqueBazelLabels(originalLabels []Label) []Label {
var labels []Label
found := make(map[Label]bool, len(originalLabels))
found := make(map[string]bool, len(originalLabels))
for _, l := range originalLabels {
if _, ok := found[l]; ok {
if _, ok := found[l.Label]; ok {
continue
}
labels = append(labels, l)
found[l] = true
found[l.Label] = true
}
return labels
}

View file

@ -33,8 +33,12 @@ func TestUniqueBazelLabels(t *testing.T) {
{Label: "b"},
{Label: "a"},
{Label: "c"},
// namespaces
{Label: "//foo:bar", OriginalModuleName: "bar"}, // when referenced from foo namespace
{Label: "//foo:bar", OriginalModuleName: "//foo:bar"}, // when reference from root namespace
},
expectedUniqueLabels: []Label{
{Label: "//foo:bar", OriginalModuleName: "bar"},
{Label: "a"},
{Label: "b"},
{Label: "c"},
@ -194,6 +198,9 @@ func TestFirstUniqueBazelLabelList(t *testing.T) {
{Label: "b"},
{Label: "a"},
{Label: "c"},
// namespaces
{Label: "//foo:bar", OriginalModuleName: "bar"}, // when referenced from foo namespace
{Label: "//foo:bar", OriginalModuleName: "//foo:bar"}, // when referenced from root namespace
},
Excludes: []Label{
{Label: "x"},
@ -207,6 +214,7 @@ func TestFirstUniqueBazelLabelList(t *testing.T) {
{Label: "a"},
{Label: "b"},
{Label: "c"},
{Label: "//foo:bar", OriginalModuleName: "bar"},
},
Excludes: []Label{
{Label: "x"},