Skip to content

Commit 2227441

Browse files
committed
don't cache unknown function results
The CheckPrior function results validation did not take into account unknown values, because unknown values were not originally allowed in the plugin protocol from which this check was adapted. The test is simple, in that we can just reverse the check which originally didn't allow this change in value. Provider function return values types are defined by the protocol, and should be validated at that call site. The CheckPrior code paths should only be used for the exact purpose of detecting invalid changes in the return value between successive phases of execution.
1 parent 2e5b5de commit 2227441

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

internal/lang/function_results.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ func (f *FunctionResults) CheckPrior(name string, args []cty.Value, result cty.V
5151
// CheckPriorProvider compares the provider function call against any cached
5252
// results, and returns an error if the result does not match a prior call.
5353
func (f *FunctionResults) CheckPriorProvider(provider addrs.Provider, name string, args []cty.Value, result cty.Value) error {
54+
// Don't cache unknown values. We could technically store types and
55+
// refinements for validation, but we don't currently have a way to
56+
// serialize those in the plan. Unknowns are also handled much more
57+
// gracefully throughout the evaluation system, whereas invalid data is
58+
// harder to trace back to the source since it's usually only visible due to
59+
// unexpected side-effects.
60+
if !result.IsKnown() {
61+
return nil
62+
}
63+
5464
argSum := sha256.New()
5565

5666
if !provider.IsZero() {

internal/lang/function_results_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestFunctionCache(t *testing.T) {
7272
result: cty.False,
7373
},
7474
// result changed from unknown => false
75-
expectErr: true,
75+
expectErr: false,
7676
},
7777
{
7878
first: testCall{
@@ -172,6 +172,9 @@ func TestFunctionCache(t *testing.T) {
172172
if err != nil && !test.expectErr {
173173
t.Fatal(err)
174174
}
175+
if err == nil && test.expectErr {
176+
t.Fatal("expected error")
177+
}
175178

176179
// reload the data to ensure we validate identically
177180
newResults := NewFunctionResultsTable(results.GetHashes())

0 commit comments

Comments
 (0)