diff --git a/command/test-fixtures/validate/invalid_block_type.pkr.hcl b/command/test-fixtures/validate/invalid_block_type.pkr.hcl new file mode 100644 index 00000000000..fdfa8e6d520 --- /dev/null +++ b/command/test-fixtures/validate/invalid_block_type.pkr.hcl @@ -0,0 +1,4 @@ +src "docker" "ubuntu" { + image = var.docker_image + commit = true +} diff --git a/command/validate_test.go b/command/validate_test.go index 5b9da04d78b..56605a32697 100644 --- a/command/validate_test.go +++ b/command/validate_test.go @@ -36,6 +36,9 @@ func TestValidateCommand(t *testing.T) { // wrong version field {path: filepath.Join(testFixture("version_req", "wrong_field_name")), exitCode: 1}, + // wrong packer block type + {path: filepath.Join(testFixture("validate", "invalid_block_type.pkr.hcl")), exitCode: 1}, + // wrong packer block {path: filepath.Join(testFixture("validate", "invalid_packer_block.pkr.hcl")), exitCode: 1}, diff --git a/hcl2template/parser.go b/hcl2template/parser.go index f3c51c4f894..d6ae4ecb97a 100644 --- a/hcl2template/parser.go +++ b/hcl2template/parser.go @@ -165,6 +165,14 @@ func (p *Parser) Parse(filename string, varFiles []string, argVars map[string]st return cfg, diags } + // Looks for invalid arguments or unsupported block types + { + for _, file := range files { + _, moreDiags := file.Body.Content(configSchema) + diags = append(diags, moreDiags...) + } + } + // Decode required_plugins blocks. // // 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 var diags hcl.Diagnostics body := file.Body - content, moreDiags := body.Content(configSchema) - diags = append(diags, moreDiags...) + content, _ := body.Content(configSchema) for _, block := range content.Blocks { switch block.Type { diff --git a/hcl2template/types.packer_config.go b/hcl2template/types.packer_config.go index 626ebdb7bb2..cb81441d819 100644 --- a/hcl2template/types.packer_config.go +++ b/hcl2template/types.packer_config.go @@ -156,8 +156,7 @@ func (cfg *PackerConfig) EvalContext(ctx BlockContext, variables map[string]cty. func (c *PackerConfig) decodeInputVariables(f *hcl.File) hcl.Diagnostics { var diags hcl.Diagnostics - content, moreDiags := f.Body.Content(configSchema) - diags = append(diags, moreDiags...) + content, _ := f.Body.Content(configSchema) // for input variables we allow to use env in the default value section. ectx := &hcl.EvalContext{ @@ -188,8 +187,7 @@ func (c *PackerConfig) decodeInputVariables(f *hcl.File) hcl.Diagnostics { func parseLocalVariableBlocks(f *hcl.File) ([]*LocalBlock, hcl.Diagnostics) { var diags hcl.Diagnostics - content, moreDiags := f.Body.Content(configSchema) - diags = append(diags, moreDiags...) + content, _ := f.Body.Content(configSchema) var locals []*LocalBlock diff --git a/hcl2template/types.required_plugins.go b/hcl2template/types.required_plugins.go index 08fe694b3ac..b5aa0328dcf 100644 --- a/hcl2template/types.required_plugins.go +++ b/hcl2template/types.required_plugins.go @@ -15,8 +15,7 @@ import ( func (cfg *PackerConfig) decodeRequiredPluginsBlock(f *hcl.File) hcl.Diagnostics { var diags hcl.Diagnostics - content, moreDiags := f.Body.Content(configSchema) - diags = append(diags, moreDiags...) + content, _ := f.Body.Content(configSchema) for _, block := range content.Blocks { switch block.Type {