diff --git a/hcl/hcl.go b/hcl/hcl.go index 98eb834c8..7f205f3b2 100644 --- a/hcl/hcl.go +++ b/hcl/hcl.go @@ -132,9 +132,10 @@ type PartialEvaluator func(hclsyntax.Expression) (hclwrite.Tokens, error) // this API allows you to define the exact set of files (and contents) that are // going to be included in the final configuration. type TerramateParser struct { - dir string - files map[string][]byte // path=content - hclparser *hclparse.Parser + dir string + files map[string][]byte // path=content + parsedFiles []string + hclparser *hclparse.Parser } // NewTerramateParser creates a Terramate parser for the directory dir. @@ -201,7 +202,10 @@ func (p *TerramateParser) Parse() (Config, error) { _, diags := p.hclparser.ParseHCL(data, name) if diags.HasErrors() { errs.Append(errors.E(ErrHCLSyntax, diags)) + continue } + + p.parsedFiles = append(p.parsedFiles, name) } cfg, err := p.parseTerramateSchema() @@ -599,16 +603,6 @@ func sortedAttributes(attrs hclsyntax.Attributes) []*hclsyntax.Attribute { return sorted } -func sortedFiles(fmap map[string]*hcl.File) []string { - var files []string - for fname := range fmap { - files = append(files, fname) - } - - sort.Strings(files) - return files -} - func findStringAttr(block *hclsyntax.Block, attrName string) (string, bool, error) { logger := log.With(). Str("action", "findStringAttr()"). @@ -975,7 +969,7 @@ func (p *TerramateParser) parseTerramateSchema() (Config, error) { errs := errors.L() fileMap := p.hclparser.Files() - for _, fname := range sortedFiles(fileMap) { + for _, fname := range p.parsedFiles { hclfile := fileMap[fname] logger := logger.With(). diff --git a/hcl/hcl_test.go b/hcl/hcl_test.go index 16d0b0c26..eaa2c7e7a 100644 --- a/hcl/hcl_test.go +++ b/hcl/hcl_test.go @@ -791,6 +791,28 @@ func TestHCLParserStack(t *testing.T) { }, }, }, + { + name: "schema not checked for files with syntax errors", + input: []cfgfile{ + { + filename: "stack.tm", + body: ` + terramate { + required_version = "" + } + stack { + wants = + unrecognized = "test" + } + `, + }, + }, + want: want{ + errs: []error{ + errors.E(hcl.ErrHCLSyntax), + }, + }, + }, { name: "after: empty set works", input: []cfgfile{ @@ -1046,8 +1068,6 @@ func TestHCLParserMultipleErrors(t *testing.T) { }, want: want{ errs: []error{ - errors.E(hcl.ErrTerramateSchema, - mkrange("file.tm", start(1, 1, 0), end(1, 2, 1))), errors.E(hcl.ErrHCLSyntax, mkrange("file.tm", start(2, 1, 4), end(2, 2, 5))), errors.E(hcl.ErrHCLSyntax, @@ -1069,14 +1089,10 @@ func TestHCLParserMultipleErrors(t *testing.T) { }, want: want{ errs: []error{ - errors.E(hcl.ErrTerramateSchema, - mkrange("file1.tm", start(1, 1, 0), end(1, 2, 1))), errors.E(hcl.ErrHCLSyntax, mkrange("file1.tm", start(2, 1, 4), end(2, 2, 5))), errors.E(hcl.ErrHCLSyntax, mkrange("file1.tm", start(3, 1, 8), end(3, 2, 9))), - errors.E(hcl.ErrTerramateSchema, - mkrange("file2.tm", start(1, 1, 0), end(1, 2, 1))), errors.E(hcl.ErrHCLSyntax, mkrange("file2.tm", start(2, 1, 4), end(2, 2, 5))), errors.E(hcl.ErrHCLSyntax,