From 5e04d4884a67b28ca8a2e838e45aa7326579f960 Mon Sep 17 00:00:00 2001 From: Spandan Das Date: Wed, 16 Aug 2023 20:45:39 +0000 Subject: [PATCH] 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 --- bazel/properties.go | 15 ++++----------- bazel/properties_test.go | 8 ++++++++ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/bazel/properties.go b/bazel/properties.go index 702c31c4d..bb0eafc7b 100644 --- a/bazel/properties.go +++ b/bazel/properties.go @@ -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 } diff --git a/bazel/properties_test.go b/bazel/properties_test.go index c98ae0ea2..751cb8b30 100644 --- a/bazel/properties_test.go +++ b/bazel/properties_test.go @@ -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"},