From 6db82a6ecedaed8e951ee6c69c30b31227a0bc13 Mon Sep 17 00:00:00 2001 From: Randy Reddig Date: Fri, 12 Mar 2021 10:44:17 -0800 Subject: [PATCH] patch: scan Go packages in deterministic order This fixes a flaky bug where packages were scanned in the wrong order, which can break imported symbols. --- CHANGELOG.md | 9 +++++++-- patch/patcher.go | 5 ++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa2d6f5..02fd210 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,12 +2,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [v0.3.1] +## [Unreleased] + +### Fixed +- Scan Go packages in a deterministic order (FIFO) to fix a flaky recurrence of a bug related to the fix for [#40](https://github.com/alta/protopatch/issues/40). + +## [v0.3.1] — 2021-03-11 ### Fixed - [#40](https://github.com/alta/protopatch/issues/40): enum values renamed with `go.lint` are now usable when imported into another proto file. -## [v0.3.0] +## [v0.3.0] — 2021-01-04 ### Added - [#37](https://github.com/alta/protopatch/pull/32): new file-level `go.lint` option. When specified, `protoc-gen-go-patch` will attempt to fix generated Go names to their idiomatic equivalents, e.g. `Id` → `ID`, `Url` → `URL`, etc. It will also eliminate stutter from enum values, e.g. `Foo_FOO_UNKNOWN` → `FooUnknown`. Thanks to [@Green7](https://github.com/Green7) for the initial implementation in [#22](https://github.com/alta/protopatch/pull/22). diff --git a/patch/patcher.go b/patch/patcher.go index bb3f8a0..0dc007d 100644 --- a/patch/patcher.go +++ b/patch/patcher.go @@ -36,6 +36,7 @@ type Patcher struct { fset *token.FileSet filesByName map[string]*ast.File info *types.Info + packages []*Package packagesByPath map[string]*Package packagesByName map[string]*Package renames map[protogen.GoIdent]string @@ -543,7 +544,8 @@ func (p *Patcher) checkPackages() error { pkg.Reset() } - for _, pkg := range p.packagesByName { + // Iterate packages deterministically + for _, pkg := range p.packages { if len(pkg.files) == 0 { continue } @@ -615,6 +617,7 @@ func (p *Patcher) getPackage(path, name string, create bool) *Package { name = pkg.pkg.Name() // Get real name p.packagesByPath[path] = pkg p.packagesByName[name] = pkg + p.packages = append(p.packages, pkg) return pkg }