Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid checking the schema in case of syntax errors #396

Merged
merged 4 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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