terraform-plugin-framework: Provider produced unexpected value after apply for a Computed attribute
This has been a discussion on plugin development for about two weeks with evidence leading to believe this more likely an issue than not, no hypothetical root cause or workaround, and the affected users in this situation are of interest to Hashicorp from a business perspective. I am therefore opening an issue here based on the above.
Module version
1.3.5
Relevant provider source code
See link to Hashicorp discuss.
Terraform Configuration Files
See link to Hashicorp discuss.
Debug Output
I cannot provide this easily as this is proprietary/NDA, but something could possibly be worked out if need be.
Expected Behavior
The plugin framework sets a Computed attribute to unknown during plan, and then allows setting the state to the correct actual values within the Create and/or Read.
Actual Behavior
If we are to believe the results of terraform-plugin-testing 1.5.1, and based on discussion thus far, then the Computed attribute value is set to unknown, then to null for unknown reasons post-plan creation (issue is here), and then to the correct accurate value. The plugin framework throws this behavior as an error during the State.set(). Error string is:
Error: Provider produced inconsistent result after apply … produced an unexpected new value: .bar was null, but now cty.ObjectValue(map[string]cty.Value{…})
Note the post-apply value is the expected and accurate value for the attribute according to the logs.
Steps to Reproduce
apply any resource with a nested type struct in the top level model that is also a schema nested attribute and tftype types.Object
References
n/a
About this issue
- Original URL
- State: open
- Created 10 months ago
- Comments: 18 (8 by maintainers)
Based on how you described your error, I would expect this to pass successfully, but the test would fail due to your
unexpected value after applyerror.I would expect that plancheck assertion to fail on
PostApplyPreRefresh, as I believe you stated that your error was occurring after the initial apply, during the second apply? So maybe something like this:This looks like a bug in the error message of that plan check when the attribute is nil, I actually can recreate that with this new test where I’m forcing the Terraform core “inconsistent value” error for our MCVE: https://github.com/austinvalle/terraform-provider-sandbox/blob/dc12c4ec36a2437b65fea9caf6165dd9a3925fc8/internal/provider/thing_resource_test.go
Further inspection shows that the attribute does exist, but it’s nil
I will write up an issue for
terraform-plugin-testingto have that error message adjusted. It should be something like:Context
Since I’m going to reference the Terraform Resource lifecycle and the related RPCs in this response, here are helpful references that describe them:
ReadResource)ReadResource->Read)Answering Questions
Readfunction is only called during theReadResourceRPC, which occurs after validation and before the plan.Readfunction outside of theReadResourceRPC, so if you need to confirm creation, you can handle that in theCreate.