diff --git a/internal/lang/funcs/filesystem.go b/internal/lang/funcs/filesystem.go index 39b3125e20..289a09ea90 100644 --- a/internal/lang/funcs/filesystem.go +++ b/internal/lang/funcs/filesystem.go @@ -158,15 +158,21 @@ func MakeFileExistsFunc(baseDir string) function.Function { return function.New(&function.Spec{ Params: []function.Parameter{ { - Name: "path", - Type: cty.String, - AllowMarked: true, + Name: "path", + Type: cty.String, + AllowMarked: true, + AllowUnknown: true, }, }, Type: function.StaticReturnType(cty.Bool), RefineResult: refineNotNull, Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) { pathArg, pathMarks := args[0].Unmark() + + if !pathArg.IsKnown() { + return cty.UnknownVal(cty.Bool).WithMarks(pathMarks), nil + } + path := pathArg.AsString() path, err := homedir.Expand(path) if err != nil { @@ -226,24 +232,30 @@ func MakeFileSetFunc(baseDir string) function.Function { return function.New(&function.Spec{ Params: []function.Parameter{ { - Name: "path", - Type: cty.String, - AllowMarked: true, + Name: "path", + Type: cty.String, + AllowMarked: true, + AllowUnknown: true, }, { - Name: "pattern", - Type: cty.String, - AllowMarked: true, + Name: "pattern", + Type: cty.String, + AllowMarked: true, + AllowUnknown: true, }, }, Type: function.StaticReturnType(cty.Set(cty.String)), RefineResult: refineNotNull, Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) { pathArg, pathMarks := args[0].Unmark() - path := pathArg.AsString() patternArg, patternMarks := args[1].Unmark() - pattern := patternArg.AsString() + if !pathArg.IsKnown() || !patternArg.IsKnown() { + return cty.UnknownVal(retType).WithMarks(pathMarks, patternMarks), nil + } + + path := pathArg.AsString() + pattern := patternArg.AsString() marks := []cty.ValueMarks{pathMarks, patternMarks} if !filepath.IsAbs(path) { diff --git a/internal/lang/funcs/filesystem_test.go b/internal/lang/funcs/filesystem_test.go index 2d88663b7e..5439661424 100644 --- a/internal/lang/funcs/filesystem_test.go +++ b/internal/lang/funcs/filesystem_test.go @@ -314,6 +314,11 @@ func TestFileExists(t *testing.T) { cty.BoolVal(false), `failed to stat (sensitive value)`, }, + { + cty.UnknownVal(cty.String).Mark(marks.Sensitive), + cty.UnknownVal(cty.Bool).RefineNotNull().Mark(marks.Sensitive), + ``, + }, } // Ensure "unreadable" directory cannot be listed during the test run @@ -546,6 +551,18 @@ func TestFileSet(t *testing.T) { }), ``, }, + { + cty.StringVal("testdata"), + cty.UnknownVal(cty.String), + cty.UnknownVal(cty.Set(cty.String)).RefineNotNull(), + ``, + }, + { + cty.StringVal("testdata"), + cty.UnknownVal(cty.String).Mark(marks.Sensitive), + cty.UnknownVal(cty.Set(cty.String)).RefineNotNull().Mark(marks.Sensitive), + ``, + }, } for _, test := range tests {