From 76818f855cdfb4e977add1a00a6e43965f724d3c Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Mon, 6 Jan 2025 15:11:50 +0000 Subject: [PATCH] cmd/modules: Ensure modules are sorted by reference key --- internal/command/views/modules.go | 5 +++++ internal/moduleref/record.go | 16 ++++++++++++++-- internal/moduleref/resolver.go | 2 +- internal/moduleref/resolver_test.go | 2 +- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/internal/command/views/modules.go b/internal/command/views/modules.go index 5ce57cad2d46..8fa3cd8a19da 100644 --- a/internal/command/views/modules.go +++ b/internal/command/views/modules.go @@ -6,6 +6,7 @@ package views import ( encJson "encoding/json" "fmt" + "sort" "github.com/hashicorp/terraform/internal/command/arguments" "github.com/hashicorp/terraform/internal/moduleref" @@ -44,6 +45,10 @@ func (v *ModulesHuman) Display(manifest moduleref.Manifest) int { return 0 } printRoot := treeprint.New() + + // ensure output is deterministic + sort.Sort(manifest.Records) + populateTreeNode(printRoot, &moduleref.Record{ Children: manifest.Records, }) diff --git a/internal/moduleref/record.go b/internal/moduleref/record.go index fb4f918226c4..cd19ef0d6196 100644 --- a/internal/moduleref/record.go +++ b/internal/moduleref/record.go @@ -17,14 +17,14 @@ type Record struct { Source addrs.ModuleSource Version *version.Version VersionConstraints version.Constraints - Children []*Record + Children Records } // ModuleRecordManifest is the view implementation of module entries declared // in configuration type Manifest struct { FormatVersion string - Records []*Record + Records Records } func (m *Manifest) addModuleEntry(entry *Record) { @@ -34,3 +34,15 @@ func (m *Manifest) addModuleEntry(entry *Record) { func (r *Record) addChild(child *Record) { r.Children = append(r.Children, child) } + +type Records []*Record + +func (r Records) Len() int { + return len(r) +} +func (r Records) Less(i, j int) bool { + return r[i].Key < r[j].Key +} +func (r Records) Swap(i, j int) { + r[i], r[j] = r[j], r[i] +} diff --git a/internal/moduleref/resolver.go b/internal/moduleref/resolver.go index f85ddba4a158..d66ce6d5661e 100644 --- a/internal/moduleref/resolver.go +++ b/internal/moduleref/resolver.go @@ -36,7 +36,7 @@ func NewResolver(internalManifest modsdir.Manifest) *Resolver { internalManifest: internalManifestCopy, manifest: &Manifest{ FormatVersion: FormatVersion, - Records: []*Record{}, + Records: Records{}, }, } } diff --git a/internal/moduleref/resolver_test.go b/internal/moduleref/resolver_test.go index eaa9f0a92574..1075252e01c2 100644 --- a/internal/moduleref/resolver_test.go +++ b/internal/moduleref/resolver_test.go @@ -185,7 +185,7 @@ func TestResolver_ResolveNestedChildren(t *testing.T) { } } -func countAndListSources(records []*Record) (count int, sources []string) { +func countAndListSources(records Records) (count int, sources []string) { for _, record := range records { sources = append(sources, record.Source.String()) count++