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

Remove possible sources of non-determinism #113

Merged
merged 6 commits into from
Dec 18, 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
3 changes: 3 additions & 0 deletions language/scala/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions pkg/glob/glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/fs"
"log"
"sort"

"github.com/bazelbuild/bazel-gazelle/rule"
"github.com/bazelbuild/buildtools/build"
Expand Down Expand Up @@ -85,6 +86,8 @@ func Apply(glob rule.GlobValue, fs fs.FS) (srcs []string) {
srcs = append(srcs, includes...)
}

sort.Strings(srcs)

return
}

Expand Down
28 changes: 27 additions & 1 deletion pkg/parser/memo_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,36 @@ 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 {
rule.ParseTimeMillis = 0 // reset for easier diff
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)
}
}
1 change: 1 addition & 0 deletions pkg/protobuf/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down
10 changes: 10 additions & 0 deletions pkg/protobuf/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -17,13 +18,19 @@ func unmarshalerForFilename(filename string) unmarshaler {
if filepath.Ext(filename) == ".json" {
return protojson.Unmarshal
}
if filepath.Ext(filename) == ".pbtext" {
return prototext.Unmarshal
}
return proto.Unmarshal
}

func marshalerForFilename(filename string) marshaler {
if filepath.Ext(filename) == ".json" {
return protojson.Marshal
}
if filepath.Ext(filename) == ".pbtext" {
return prototext.Marshal
}
return proto.Marshal
}

Expand All @@ -39,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)
Expand Down
4 changes: 3 additions & 1 deletion pkg/resolver/import_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ func (imports ImportMap) Deps(from label.Label) (deps []label.Label) {
label.NoLabel: true,
from: true,
}
for _, imp := range imports {

for _, k := range imports.Keys() {
imp := imports[k]
if imp.Symbol == nil || imp.Error != nil {
continue
}
Expand Down