Skip to content

Update templatefile function error message to support multiple missing variables. #25424

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 19 additions & 1 deletion lang/funcs/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"unicode/utf8"

"github.com/bmatcuk/doublestar"
Expand Down Expand Up @@ -118,13 +119,30 @@ func MakeTemplateFileFunc(baseDir string, funcsCb func() map[string]function.Fun
// We'll pre-check references in the template here so we can give a
// more specialized error message than HCL would by default, so it's
// clearer that this problem is coming from a templatefile call.

// Tracker used to ensure that a variable does not get included
// more than once.
missingVarsTracker := make(map[string]struct{})
missingVars := []string{}
for _, traversal := range expr.Variables() {
root := traversal.RootName()
if _, ok := ctx.Variables[root]; !ok {
return cty.DynamicVal, function.NewArgErrorf(1, "vars map does not contain key %q, referenced at %s", root, traversal[0].SourceRange())
if _, ok := missingVarsTracker[root]; !ok {
missingVarsTracker[root] = struct{}{}
missingVars = append(missingVars, fmt.Sprintf(" key %q, referenced at %s", root, traversal[0].SourceRange()))
}
}
}

// If the length of missingVars is greater than 0, create an error
// message and return an error.
if len(missingVars) > 0 {
var sb strings.Builder
sb.WriteString("vars map does not contain")
sb.WriteString(strings.Join(missingVars, ","))
return cty.DynamicVal, function.NewArgErrorf(1, sb.String())
}

givenFuncs := funcsCb() // this callback indirection is to avoid chicken/egg problems
funcs := make(map[string]function.Function, len(givenFuncs))
for name, fn := range givenFuncs {
Expand Down
6 changes: 6 additions & 0 deletions lang/funcs/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ func TestTemplateFile(t *testing.T) {
cty.NilVal,
`vars map does not contain key "name", referenced at testdata/hello.tmpl:1,10-14`,
},
{
cty.StringVal("testdata/multivar.tmpl"),
cty.EmptyObjectVal,
cty.NilVal,
`vars map does not contain key "yourname", referenced at testdata/multivar.tmpl:1,10-18, key "myname", referenced at testdata/multivar.tmpl:1,34-40`,
},
{
cty.StringVal("testdata/func.tmpl"),
cty.ObjectVal(map[string]cty.Value{
Expand Down
3 changes: 3 additions & 0 deletions lang/funcs/testdata/multivar.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hello, ${yourname}! my name is ${myname}.

${myname}