diff --git a/hcl2template/testdata/build/invalid_source_reference.pkr.hcl b/hcl2template/testdata/build/invalid_source_reference.pkr.hcl new file mode 100644 index 000000000..a1c28dae9 --- /dev/null +++ b/hcl2template/testdata/build/invalid_source_reference.pkr.hcl @@ -0,0 +1,4 @@ + +build { + sources = ["ami"] +} diff --git a/hcl2template/types.build.go b/hcl2template/types.build.go index c6a2f68e8..2c7170918 100644 --- a/hcl2template/types.build.go +++ b/hcl2template/types.build.go @@ -51,11 +51,11 @@ func (p *Parser) decodeBuildConfig(block *hcl.Block) (*BuildBlock, hcl.Diagnosti if ref == NoSource { diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagError, - Summary: "Invalid " + sourceLabel + " reference", + Summary: "Invalid " + sourceLabel + " reference: " + buildFrom, Detail: "A " + sourceLabel + " type must start with a letter and " + "may contain only letters, digits, underscores, and dashes." + "A valid source reference looks like: `source.type.name`", - Subject: &block.LabelRanges[0], + Subject: block.DefRange.Ptr(), }) continue } @@ -67,7 +67,7 @@ func (p *Parser) decodeBuildConfig(block *hcl.Block) (*BuildBlock, hcl.Diagnosti Detail: "A " + sourceLabel + " type must start with a letter and " + "may contain only letters, digits, underscores, and dashes." + "A valid source reference looks like: `source.type.name`", - Subject: &block.LabelRanges[0], + Subject: block.DefRange.Ptr(), }) continue } @@ -75,7 +75,8 @@ func (p *Parser) decodeBuildConfig(block *hcl.Block) (*BuildBlock, hcl.Diagnosti build.Froms = append(build.Froms, ref) } - content, diags := b.Config.Content(buildSchema) + content, moreDiags := b.Config.Content(buildSchema) + diags = append(diags, moreDiags...) for _, block := range content.Blocks { switch block.Type { case buildProvisionerLabel: diff --git a/hcl2template/types.build_test.go b/hcl2template/types.build_test.go index f5e1cea69..a852432ea 100644 --- a/hcl2template/types.build_test.go +++ b/hcl2template/types.build_test.go @@ -83,6 +83,16 @@ func TestParse_build(t *testing.T) { []packer.Build{}, false, }, + {"invalid source", + defaultParser, + parseTestArgs{"testdata/build/invalid_source_reference.pkr.hcl"}, + &PackerConfig{ + Builds: nil, + }, + true, true, + []packer.Build{}, + false, + }, } testParse(t, tests) }