Skip to content
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
6 changes: 4 additions & 2 deletions terraform/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Module struct {
Files map[string]*hcl.File

primaries map[string]*hcl.File
primaryFilenames []string
overrides map[string]*hcl.File
overrideFilenames []string
}
Expand All @@ -39,6 +40,7 @@ func NewEmptyModule() *Module {
Files: map[string]*hcl.File{},

primaries: map[string]*hcl.File{},
primaryFilenames: []string{},
overrides: map[string]*hcl.File{},
overrideFilenames: []string{},
}
Expand Down Expand Up @@ -130,8 +132,8 @@ func (m *Module) PartialContent(schema *hclext.BodySchema, ctx *Evaluator) (*hcl
content := &hclext.BodyContent{}
diags := hcl.Diagnostics{}

for _, f := range m.primaries {
expanded, d := ctx.ExpandBlock(f.Body, schema)
for _, filename := range m.primaryFilenames {
expanded, d := ctx.ExpandBlock(m.primaries[filename].Body, schema)
diags = diags.Extend(d)
c, d := hclext.PartialContent(expanded, schema)
diags = diags.Extend(d)
Expand Down
76 changes: 76 additions & 0 deletions terraform/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ func TestRebuild(t *testing.T) {
primaries: map[string]*hcl.File{
"main.tf": {Bytes: []byte(`variable "foo" { default = 1 }`), Body: hcl.EmptyBody()},
},
primaryFilenames: []string{"main.tf"},
overrides: map[string]*hcl.File{
"main_override.tf": {Bytes: []byte(`variable "foo" { default = 2 }`), Body: hcl.EmptyBody()},
"override.tf": {Bytes: []byte(`variable "foo" { default = 3 }`), Body: hcl.EmptyBody()},
},
overrideFilenames: []string{"main_override.tf", "override.tf"},
Sources: map[string][]byte{
"main.tf": []byte(`variable "foo" { default = 1 }`),
"main_override.tf": []byte(`variable "foo" { default = 2 }`),
Expand Down Expand Up @@ -65,6 +67,7 @@ variable "bar" { default = "bar" }
Body: hcl.EmptyBody(),
},
},
primaryFilenames: []string{"main.tf"},
overrides: map[string]*hcl.File{
"main_override.tf": {
Bytes: []byte(`
Expand All @@ -75,6 +78,7 @@ variable "bar" { default = "baz" }
},
"override.tf": {Bytes: []byte(`variable "foo" { default = 3 }`), Body: hcl.EmptyBody()},
},
overrideFilenames: []string{"main_override.tf", "override.tf"},
Sources: map[string][]byte{
"main.tf": []byte(`
variable "foo" { default = 1 }
Expand Down Expand Up @@ -113,10 +117,12 @@ variable "bar" { default = "baz" }
primaries: map[string]*hcl.File{
"main.tf.json": {Bytes: []byte(`{"variable": {"foo": {"default": 1}}}`), Body: hcl.EmptyBody()},
},
primaryFilenames: []string{"main.tf.json"},
overrides: map[string]*hcl.File{
"main_override.tf.json": {Bytes: []byte(`{"variable": {"foo": {"default": 2}}}`), Body: hcl.EmptyBody()},
"override.tf.json": {Bytes: []byte(`{"variable": {"foo": {"default": 3}}}`), Body: hcl.EmptyBody()},
},
overrideFilenames: []string{"main_override.tf.json", "override.tf.json"},
Sources: map[string][]byte{
"main.tf.json": []byte(`{"variable": {"foo": {"default": 1}}}`),
"main_override.tf.json": []byte(`{"variable": {"foo": {"default": 2}}}`),
Expand All @@ -138,10 +144,12 @@ variable "bar" { default = "baz" }
primaries: map[string]*hcl.File{
"main.tf.json": {Bytes: []byte(`{"variable": {"foo": {"default": 1}, "bar": {"default": "bar"}}}`), Body: hcl.EmptyBody()},
},
primaryFilenames: []string{"main.tf.json"},
overrides: map[string]*hcl.File{
"main_override.tf.json": {Bytes: []byte(`{"variable": {"foo": {"default": 2}, "bar": {"default": "baz"}}}`), Body: hcl.EmptyBody()},
"override.tf.json": {Bytes: []byte(`{"variable": {"foo": {"default": 3}}}`), Body: hcl.EmptyBody()},
},
overrideFilenames: []string{"main_override.tf.json", "override.tf.json"},
Sources: map[string][]byte{
"main.tf.json": []byte(`{"variable": {"foo": {"default": 1}, "bar": {"default": "bar"}}}`),
"main_override.tf.json": []byte(`{"variable": {"foo": {"default": 2}, "bar": {"default": "baz"}}}`),
Expand Down Expand Up @@ -1769,3 +1777,71 @@ func Test_overrideBlocks(t *testing.T) {
})
}
}

func TestPartialContent_deterministicPrimaryOrder(t *testing.T) {
files := map[string]string{
"d.tf": `resource "aws_instance" "d" {}`,
"b.tf": `resource "aws_instance" "b" {}`,
"a.tf": `resource "aws_instance" "a" {}`,
"c.tf": `resource "aws_instance" "c" {}`,
}
schema := &hclext.BodySchema{
Blocks: []hclext.BlockSchema{
{
Type: "resource",
LabelNames: []string{"type", "name"},
Body: &hclext.BodySchema{},
},
},
}
wantOrder := []string{"a.tf", "b.tf", "c.tf", "d.tf"}

fs := afero.Afero{Fs: afero.NewMemMapFs()}
for name, content := range files {
if err := fs.WriteFile(name, []byte(content), os.ModePerm); err != nil {
t.Fatal(err)
}
}

parser := NewParser(fs)
mod, diags := parser.LoadConfigDir(".", ".")
if diags.HasErrors() {
t.Fatal(diags)
}

if diff := cmp.Diff(wantOrder, mod.primaryFilenames); diff != "" {
t.Fatalf("primaryFilenames mismatch:\n%s", diff)
}

config, diags := BuildConfig(mod, ModuleWalkerFunc(func(req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics) { return nil, nil, nil }), ".")
if diags.HasErrors() {
t.Fatal(diags)
}
variableValues, diags := VariableValues(config)
if diags.HasErrors() {
t.Fatal(diags)
}
ctx := &Evaluator{
Meta: &ContextMeta{Env: Workspace()},
ModulePath: config.Path.UnkeyedInstanceShim(),
Config: config,
VariableValues: variableValues,
}

for i := 0; i < 20; i++ {
got, diags := config.Module.PartialContent(schema, ctx)
if diags.HasErrors() {
t.Fatalf("iteration %d: %s", i, diags)
}
if len(got.Blocks) != len(wantOrder) {
t.Fatalf("iteration %d: got %d blocks, want %d", i, len(got.Blocks), len(wantOrder))
}
gotOrder := make([]string, len(got.Blocks))
for j, block := range got.Blocks {
gotOrder[j] = block.DefRange.Filename
}
if diff := cmp.Diff(wantOrder, gotOrder); diff != "" {
t.Fatalf("iteration %d: block order mismatch:\n%s", i, diff)
}
}
}
5 changes: 4 additions & 1 deletion terraform/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (p *Parser) LoadConfigDir(baseDir, dir string) (*Module, hcl.Diagnostics) {
}

mod := NewEmptyModule()
mod.primaryFilenames = make([]string, 0, len(primaries))
mod.overrideFilenames = make([]string, len(overrides))

for _, path := range primaries {
Expand All @@ -78,6 +79,7 @@ func (p *Parser) LoadConfigDir(baseDir, dir string) (*Module, hcl.Diagnostics) {
mod.primaries[realPath] = f
mod.Sources[realPath] = f.Bytes
mod.Files[realPath] = f
mod.primaryFilenames = append(mod.primaryFilenames, realPath)
}
for idx, path := range overrides {
f, loadDiags := p.loadHCLFile(baseDir, path)
Expand All @@ -92,7 +94,8 @@ func (p *Parser) LoadConfigDir(baseDir, dir string) (*Module, hcl.Diagnostics) {
mod.Files[realPath] = f
mod.overrideFilenames[idx] = realPath
}
// Overrides are processed in order first by filename (in lexicographical order)
// Primaries and overrides are processed in order by filename (in lexicographical order)
sort.Strings(mod.primaryFilenames)
sort.Strings(mod.overrideFilenames)
if diags.HasErrors() {
return mod, diags
Expand Down
Loading