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

hcl2: avoid panic on unset variable #10045

Merged
merged 1 commit into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all 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: 22 additions & 0 deletions jobspec2/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,28 @@ job "example" {
require.Equal(t, meta, out.Meta)
}

// TestParse_UnsetVariables asserts that variables that have neither types nor
// values return early instead of panicking.
func TestParse_UnsetVariables(t *testing.T) {
hcl := `
variable "region_var" {}
job "example" {
datacenters = [for s in ["dc1", "dc2"] : upper(s)]
region = var.region_var
}
`

_, err := ParseWithConfig(&ParseConfig{
Path: "input.hcl",
Body: []byte(hcl),
ArgVars: []string{},
AllowFS: true,
})

require.Error(t, err)
require.Contains(t, err.Error(), "Unset variable")
}

func TestParse_Locals(t *testing.T) {
hcl := `
variables {
Expand Down
7 changes: 7 additions & 0 deletions jobspec2/types.config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ func (c *jobConfig) decodeBody(body hcl.Body) hcl.Diagnostics {
diags = append(diags, moreDiags...)
diags = append(diags, c.evaluateLocalVariables(c.LocalBlocks)...)

// Errors at this point are likely syntax errors which can result in
// invalid state when we try to decode the rest of the job. If we continue
// we may panic and that will obscure the error, so return early so the
// user can be told how to fix their jobspec.
if diags.HasErrors() {
return diags
}
nctx := c.EvalContext()

diags = append(diags, c.decodeJob(content, nctx)...)
Expand Down