Skip to content

Commit

Permalink
fix: avoid checking the schema in case of syntax errors (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
i4ki committed Jun 15, 2022
1 parent 3830aae commit 4957232
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
22 changes: 8 additions & 14 deletions hcl/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()").
Expand Down Expand Up @@ -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().
Expand Down
28 changes: 22 additions & 6 deletions hcl/hcl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 4957232

Please sign in to comment.