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

cmd/modules: Ensure modules are sorted by reference key #36268

Merged
merged 1 commit into from
Jan 6, 2025
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
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
Loading