From 1d01ad3651cdbea63b0ad36de1580311a4eafc7e Mon Sep 17 00:00:00 2001 From: teddylear Date: Thu, 27 Jan 2022 08:40:45 -0500 Subject: [PATCH] feat: print all locals errors when there is a circular error (#11527) --- .../validate/circular_error.pkr.hcl | 5 +++++ command/validate_test.go | 3 +++ hcl2template/types.packer_config.go | 17 ++++++++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 command/test-fixtures/validate/circular_error.pkr.hcl diff --git a/command/test-fixtures/validate/circular_error.pkr.hcl b/command/test-fixtures/validate/circular_error.pkr.hcl new file mode 100644 index 000000000..4ee895cf2 --- /dev/null +++ b/command/test-fixtures/validate/circular_error.pkr.hcl @@ -0,0 +1,5 @@ + +locals { + timestamp = formatdate("YYYY-MM-DDX", timestamp()) + other_local = "test-${local.timestamp}" +} diff --git a/command/validate_test.go b/command/validate_test.go index e6a51d4cb..52e4ef5ea 100644 --- a/command/validate_test.go +++ b/command/validate_test.go @@ -33,6 +33,9 @@ func TestValidateCommand(t *testing.T) { // wrong packer block {path: filepath.Join(testFixture("validate", "invalid_packer_block.pkr.hcl")), exitCode: 1}, + + // Should return multiple errors, + {path: filepath.Join(testFixture("validate", "circular_error.pkr.hcl")), exitCode: 1}, } for _, tc := range tt { diff --git a/hcl2template/types.packer_config.go b/hcl2template/types.packer_config.go index f91d3ae4d..41c0c03f2 100644 --- a/hcl2template/types.packer_config.go +++ b/hcl2template/types.packer_config.go @@ -225,6 +225,21 @@ func (c *PackerConfig) parseLocalVariables(f *hcl.File) ([]*LocalBlock, hcl.Diag return locals, diags } +func (c *PackerConfig) evaluateAllLocalVariables(locals []*LocalBlock) hcl.Diagnostics { + var local_diags hcl.Diagnostics + + // divide by 2 so that you don't get duplicate locals + // appear to have double locals in LocalBlock, not sure if intentional + for i := 0; i < len(locals)/2; i++ { + diags := c.evaluateLocalVariable(locals[i]) + if diags.HasErrors() { + local_diags = append(local_diags, diags...) + } + } + + return local_diags +} + func (c *PackerConfig) evaluateLocalVariables(locals []*LocalBlock) hcl.Diagnostics { var diags hcl.Diagnostics @@ -245,7 +260,7 @@ func (c *PackerConfig) evaluateLocalVariables(locals []*LocalBlock) hcl.Diagnost if previousL == len(locals) { if retry == 100 { // To get to this point, locals must have a circle dependency - return append(diags, moreDiags...) + return c.evaluateAllLocalVariables(locals) } retry++ }