Skip to content

Commit

Permalink
cmd/modules: Ensure modules are sorted by reference key (#36268)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Jan 6, 2025
1 parent fa27595 commit 908828b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
5 changes: 5 additions & 0 deletions internal/command/views/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
})
Expand Down
16 changes: 14 additions & 2 deletions internal/moduleref/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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]
}
2 changes: 1 addition & 1 deletion internal/moduleref/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewResolver(internalManifest modsdir.Manifest) *Resolver {
internalManifest: internalManifestCopy,
manifest: &Manifest{
FormatVersion: FormatVersion,
Records: []*Record{},
Records: Records{},
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/moduleref/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down

0 comments on commit 908828b

Please sign in to comment.