Skip to content

Commit ad898c9

Browse files
committed
fix: packer validate unsupported type error
`packer validate` would output the same error message four times per unsupported root block type found in a template (e.g., 'src' instead of 'source'). This behavior was due to a function being called four times for each file on each stage of the parsing.
1 parent 77bf028 commit ad898c9

File tree

5 files changed

+19
-8
lines changed

5 files changed

+19
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
src "docker" "ubuntu" {
2+
image = var.docker_image
3+
commit = true
4+
}

command/validate_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ func TestValidateCommand(t *testing.T) {
3636
// wrong version field
3737
{path: filepath.Join(testFixture("version_req", "wrong_field_name")), exitCode: 1},
3838

39+
// wrong packer block type
40+
{path: filepath.Join(testFixture("validate", "invalid_block_type.pkr.hcl")), exitCode: 1},
41+
3942
// wrong packer block
4043
{path: filepath.Join(testFixture("validate", "invalid_packer_block.pkr.hcl")), exitCode: 1},
4144

hcl2template/parser.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@ func (p *Parser) Parse(filename string, varFiles []string, argVars map[string]st
165165
return cfg, diags
166166
}
167167

168+
// Looks for invalid arguments or unsupported block types
169+
{
170+
for _, file := range files {
171+
_, moreDiags := file.Body.Content(configSchema)
172+
diags = append(diags, moreDiags...)
173+
}
174+
}
175+
168176
// Decode required_plugins blocks.
169177
//
170178
// Note: using `latest` ( or actually an empty string ) in a config file
@@ -585,8 +593,7 @@ func (p *Parser) decodeDatasources(file *hcl.File, cfg *PackerConfig) hcl.Diagno
585593
var diags hcl.Diagnostics
586594

587595
body := file.Body
588-
content, moreDiags := body.Content(configSchema)
589-
diags = append(diags, moreDiags...)
596+
content, _ := body.Content(configSchema)
590597

591598
for _, block := range content.Blocks {
592599
switch block.Type {

hcl2template/types.packer_config.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@ func (cfg *PackerConfig) EvalContext(ctx BlockContext, variables map[string]cty.
156156
func (c *PackerConfig) decodeInputVariables(f *hcl.File) hcl.Diagnostics {
157157
var diags hcl.Diagnostics
158158

159-
content, moreDiags := f.Body.Content(configSchema)
160-
diags = append(diags, moreDiags...)
159+
content, _ := f.Body.Content(configSchema)
161160

162161
// for input variables we allow to use env in the default value section.
163162
ectx := &hcl.EvalContext{
@@ -188,8 +187,7 @@ func (c *PackerConfig) decodeInputVariables(f *hcl.File) hcl.Diagnostics {
188187
func parseLocalVariableBlocks(f *hcl.File) ([]*LocalBlock, hcl.Diagnostics) {
189188
var diags hcl.Diagnostics
190189

191-
content, moreDiags := f.Body.Content(configSchema)
192-
diags = append(diags, moreDiags...)
190+
content, _ := f.Body.Content(configSchema)
193191

194192
var locals []*LocalBlock
195193

hcl2template/types.required_plugins.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import (
1515
func (cfg *PackerConfig) decodeRequiredPluginsBlock(f *hcl.File) hcl.Diagnostics {
1616
var diags hcl.Diagnostics
1717

18-
content, moreDiags := f.Body.Content(configSchema)
19-
diags = append(diags, moreDiags...)
18+
content, _ := f.Body.Content(configSchema)
2019

2120
for _, block := range content.Blocks {
2221
switch block.Type {

0 commit comments

Comments
 (0)