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: handle unquoted undefined variables #10419

Merged
merged 2 commits into from
Apr 21, 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
15 changes: 15 additions & 0 deletions jobspec2/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,21 @@ func TestParse_UndefinedVariables(t *testing.T) {
require.Equal(t, c, *job.Region)
})
}

t.Run("unquoted", func(t *testing.T) {
hcl := `job "example" {
region = meta.mytest
}`

job, err := ParseWithConfig(&ParseConfig{
Path: "input.hcl",
Body: []byte(hcl),
})
require.NoError(t, err)

require.Equal(t, "${meta.mytest}", *job.Region)

})
}

func TestParseServiceCheck(t *testing.T) {
Expand Down
33 changes: 23 additions & 10 deletions jobspec2/types.config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package jobspec2

import (
"bytes"
"fmt"
"strings"

Expand Down Expand Up @@ -315,18 +314,32 @@ func (c *jobConfig) EvalContext() *hcl.EvalContext {
end := t.SourceRange().End.Byte

v := string(body[start:end])
if i := bytes.IndexByte(body[end:], '}'); i != -1 {
v += string(body[end : end+i+1])
} else {
// fallback for unexpected cases
v += "}"

// find the start of inclusing "${..}" if it's inclused in one; otherwise, wrap it in one
isBracketed := false
for i := start - 1; i >= 1; i-- {
if body[i] == '{' && body[i-1] == '$' {
isBracketed = true
v = string(body[i-1:start]) + v
break
} else if body[i] != ' ' {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we being using unicode.IsSpace to catch other whitespace chars?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing emprically, only a plain space works. Trying with any other (e.g. \t, \n, etc) I get an error like the following:

input.hcl:2,23-24: Invalid character; This character is not used within the language.
input.hcl:2,23-24: Extra characters after interpolation expression; Expected a closing brace to end the interpolation expression, but found extra characters.

break
}
}

if i := bytes.LastIndex(body[:start], []byte("${")); i != 0 {
v = string(body[i:start]) + v
if isBracketed {
for i := end + 1; i < len(body); i++ {
if body[i] == '}' {
v += string(body[end:i])
} else if body[i] != ' ' {
// unexpected!
v += "}"
break
}
}

} else {
// fallback for unexpected cases
v = "${" + v
v = "${" + v + "}"
}

return cty.StringVal(v), nil
Expand Down