From efd4719297b91a0338f4c89183f1422af1b5a4b1 Mon Sep 17 00:00:00 2001 From: Paul Johnston Date: Wed, 13 Dec 2023 23:43:16 -0700 Subject: [PATCH 1/6] ensure glob results are sorted --- pkg/glob/glob.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/glob/glob.go b/pkg/glob/glob.go index e5b9db0..0d93456 100644 --- a/pkg/glob/glob.go +++ b/pkg/glob/glob.go @@ -4,6 +4,7 @@ import ( "fmt" "io/fs" "log" + "sort" "github.com/bazelbuild/bazel-gazelle/rule" "github.com/bazelbuild/buildtools/build" @@ -85,6 +86,8 @@ func Apply(glob rule.GlobValue, fs fs.FS) (srcs []string) { srcs = append(srcs, includes...) } + sort.Strings(srcs) + return } From 9bd683c18eb6947d0e021f1f4a88e91216d198fa Mon Sep 17 00:00:00 2001 From: Paul Johnston Date: Thu, 14 Dec 2023 00:23:54 -0700 Subject: [PATCH 2/6] also sort keys of importmap.deps --- pkg/resolver/import_map.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/resolver/import_map.go b/pkg/resolver/import_map.go index 78631ff..51d3126 100644 --- a/pkg/resolver/import_map.go +++ b/pkg/resolver/import_map.go @@ -43,7 +43,15 @@ func (imports ImportMap) Deps(from label.Label) (deps []label.Label) { label.NoLabel: true, from: true, } - for _, imp := range imports { + + keys := make([]string, 0, len(imports)) + for k := range imports { + keys = append(keys, k) + } + sort.Strings(keys) + + for _, k := range keys { + imp := imports[k] if imp.Symbol == nil || imp.Error != nil { continue } From 060b934422f49f352cc01976df26be4fd412c645 Mon Sep 17 00:00:00 2001 From: Paul Johnston Date: Sun, 17 Dec 2023 19:31:59 -0700 Subject: [PATCH 3/6] support pbtext for cache file --- pkg/protobuf/BUILD.bazel | 1 + pkg/protobuf/io.go | 7 +++++++ pkg/resolver/import_map.go | 8 +------- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/pkg/protobuf/BUILD.bazel b/pkg/protobuf/BUILD.bazel index f40b69a..b6b2a20 100644 --- a/pkg/protobuf/BUILD.bazel +++ b/pkg/protobuf/BUILD.bazel @@ -7,6 +7,7 @@ go_library( visibility = ["//visibility:public"], deps = [ "@org_golang_google_protobuf//encoding/protojson", + "@org_golang_google_protobuf//encoding/prototext", "@org_golang_google_protobuf//proto", "@org_golang_google_protobuf//reflect/protoreflect", ], diff --git a/pkg/protobuf/io.go b/pkg/protobuf/io.go index 58bc533..8a2e5c0 100644 --- a/pkg/protobuf/io.go +++ b/pkg/protobuf/io.go @@ -6,6 +6,7 @@ import ( "path/filepath" "google.golang.org/protobuf/encoding/protojson" + "google.golang.org/protobuf/encoding/prototext" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/reflect/protoreflect" ) @@ -17,6 +18,9 @@ func unmarshalerForFilename(filename string) unmarshaler { if filepath.Ext(filename) == ".json" { return protojson.Unmarshal } + if filepath.Ext(filename) == ".pbtext" { + return prototext.Unmarshal + } return proto.Unmarshal } @@ -24,6 +28,9 @@ func marshalerForFilename(filename string) marshaler { if filepath.Ext(filename) == ".json" { return protojson.Marshal } + if filepath.Ext(filename) == ".pbtext" { + return prototext.Marshal + } return proto.Marshal } diff --git a/pkg/resolver/import_map.go b/pkg/resolver/import_map.go index 51d3126..1d57eae 100644 --- a/pkg/resolver/import_map.go +++ b/pkg/resolver/import_map.go @@ -44,13 +44,7 @@ func (imports ImportMap) Deps(from label.Label) (deps []label.Label) { from: true, } - keys := make([]string, 0, len(imports)) - for k := range imports { - keys = append(keys, k) - } - sort.Strings(keys) - - for _, k := range keys { + for _, k := range imports.Keys() { imp := imports[k] if imp.Symbol == nil || imp.Error != nil { continue From e00de6a3e464a39880911b5bb05ac686cee98cfa Mon Sep 17 00:00:00 2001 From: Paul Johnston Date: Sun, 17 Dec 2023 19:49:09 -0700 Subject: [PATCH 4/6] Sort cache file upon read --- language/scala/cache.go | 3 +++ pkg/parser/memo_parser.go | 27 ++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/language/scala/cache.go b/language/scala/cache.go index 52fe520..398a86a 100644 --- a/language/scala/cache.go +++ b/language/scala/cache.go @@ -6,6 +6,7 @@ import ( "github.com/bazelbuild/bazel-gazelle/label" scpb "github.com/stackb/scala-gazelle/build/stack/gazelle/scala/cache" + "github.com/stackb/scala-gazelle/pkg/parser" "github.com/stackb/scala-gazelle/pkg/protobuf" ) @@ -26,6 +27,8 @@ func (sl *scalaLang) readScalaRuleCacheFile() error { return nil } + parser.SortRules(sl.cache.Rules) + for _, rule := range sl.cache.Rules { from, err := label.Parse(rule.Label) if err != nil { diff --git a/pkg/parser/memo_parser.go b/pkg/parser/memo_parser.go index 7e91a66..8c0b831 100644 --- a/pkg/parser/memo_parser.go +++ b/pkg/parser/memo_parser.go @@ -91,10 +91,35 @@ func (p *MemoParser) ScalaRules() []*sppb.Rule { for _, rule := range p.rules { rules = append(rules, rule) } + SortRules(rules) + return rules +} + +func SortRules(rules []*sppb.Rule) { sort.Slice(rules, func(i, j int) bool { a := rules[i] b := rules[j] return a.Label < b.Label }) - return rules + for _, rule := range rules { + sortRuleFiles(rule.Files) + } +} + +func sortRuleFiles(files []*sppb.File) { + sort.Slice(files, func(i, j int) bool { + a := files[i] + b := files[j] + return a.Filename < b.Filename + }) + for _, file := range files { + sort.Strings(file.Imports) + sort.Strings(file.Packages) + sort.Strings(file.Classes) + sort.Strings(file.Objects) + sort.Strings(file.Traits) + sort.Strings(file.Types) + sort.Strings(file.Vals) + sort.Strings(file.Names) + } } From 130aecafc5bc2ee0c1ace56cf0afda6fde11c26c Mon Sep 17 00:00:00 2001 From: Paul Johnston Date: Sun, 17 Dec 2023 19:56:52 -0700 Subject: [PATCH 5/6] write pretty json by default --- pkg/protobuf/io.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/protobuf/io.go b/pkg/protobuf/io.go index 8a2e5c0..a4fd83f 100644 --- a/pkg/protobuf/io.go +++ b/pkg/protobuf/io.go @@ -46,6 +46,9 @@ func ReadFile(filename string, message protoreflect.ProtoMessage) error { } func WriteFile(filename string, message protoreflect.ProtoMessage) error { + if filepath.Ext(filename) == ".json" { + return WritePrettyJSONFile(filename, message) + } data, err := marshalerForFilename(filename)(message) if err != nil { return fmt.Errorf("marshal: %w", err) From 31257848deb754cfd7e392b5cf0f5e42a6486728 Mon Sep 17 00:00:00 2001 From: Paul Johnston Date: Sun, 17 Dec 2023 20:04:19 -0700 Subject: [PATCH 6/6] dont record parse time for cache file --- pkg/parser/memo_parser.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/parser/memo_parser.go b/pkg/parser/memo_parser.go index 8c0b831..6b15fb0 100644 --- a/pkg/parser/memo_parser.go +++ b/pkg/parser/memo_parser.go @@ -102,6 +102,7 @@ func SortRules(rules []*sppb.Rule) { return a.Label < b.Label }) for _, rule := range rules { + rule.ParseTimeMillis = 0 // reset for easier diff sortRuleFiles(rule.Files) } }