8000 Make gazelle_python_manifest output deterministic (#813) · comius/rules_python@0dfc546 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0dfc546

Browse files
Make gazelle_python_manifest output deterministic (bazel-contrib#813)
* Make gazelle_python_manifest output deterministic Fixes 812 * address review comments Co-authored-by: Thulio Ferraz Assis <3149049+f0rmiga@users.noreply.github.com>
1 parent cf6542d commit 0dfc546

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

gazelle/manifest/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ go_library(
55
srcs = ["manifest.go"],
66
importpath = "github.com/bazelbuild/rules_python/gazelle/manifest",
77
visibility = ["//visibility:public"],
8-
deps = ["@in_gopkg_yaml_v2//:yaml_v2"],
8+
deps = [
9+
"@com_github_emirpasic_gods//sets/treeset",
10+
"@in_gopkg_yaml_v2//:yaml_v2",
11+
],
912
)
1013

1114
go_test(

gazelle/manifest/manifest.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"io"
77
"os"
88

9+
"github.com/emirpasic/gods/sets/treeset"
10+
911
yaml "gopkg.in/yaml.v2"
1012
)
1113

@@ -94,11 +96,30 @@ func (f *File) Decode(manifestPath string) error {
9496
return nil
9597
}
9698

99+
// ModulesMapping is the type used to map from importable Python modules to
100+
// the wheel names that provide these modules.
101+
type ModulesMapping map[string]string
102+
103+
// MarshalYAML makes sure that we sort the module names before marshaling
104+
// the contents of `ModulesMapping` to a YAML file. This ensures that the
105+
// file is deterministically generated from the map.
106+
func (m ModulesMapping) MarshalYAML() (interface{}, error) {
107+
var mapslice yaml.MapSlice
108+
keySet := treeset.NewWithStringComparator()
109+
for key := range m {
110+
keySet.Add(key)
111+
}
112+
for _, key := range keySet.Values() {
113+
mapslice = append(mapslice, yaml.MapItem{Key: key, Value: m[key.(string)]})
114+
}
115+
return mapslice, nil
116+
}
117+
97118
// Manifest represents the structure of the Gazelle manifest file.
98119
type Manifest struct {
99120
// ModulesMapping is the mapping from importable modules to which Python
100121
// wheel name provides these modules.
101-
ModulesMapping map[string]string `yaml:"modules_mapping"`
122+
ModulesMapping ModulesMapping `yaml:"modules_mapping"`
102123
// PipDepsRepositoryName is the name of the pip_install repository target.
103124
// DEPRECATED
104125
PipDepsRepositoryName string `yaml:"pip_deps_repository_name,omitempty"`

gazelle/manifest/manifest_ 8000 test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/bazelbuild/rules_python/gazelle/manifest"
1111
)
1212

13-
var modulesMapping = map[string]string{
13+
var modulesMapping = manifest.ModulesMapping{
1414
"arrow": "arrow",
1515
"arrow.__init__": "arrow",
1616
"arrow.api": "arrow",
@@ -76,4 +76,4 @@ func TestFile(t *testing.T) {
7676
t.FailNow()
7777
}
7878
})
79-
}
79+
}

0 commit comments

Comments
 (0)
0