Various fixes for "terraform validate" command #18318
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The increased precision of the new type checker has caused some assumptions to shift for
terraform validate
, and for the validate walk in general, causing it to be not fully functional at the moment.This PR doesn't quite get it all the way there, since some bigger work needs to be completed first, but this pushes the errors down the stack a couple steps:
Set root variables during
terraform validate
: the early pass we did to getterraform validate
working against the new config loader got away with not setting variables because it wasn't actually walking the graph. However, now we are walking the graph we must provide values for all of the root variables in order to prevent a "this is a bug in terraform"-type error message. Here we set them to unknown values of the declared type, allowing us to do type checking of the uses of these values regardless of which particular values are finally set when runningplan
orapply
.Don't
DynamicExpand
during evaluate: doing a dynamic-expand in the validate walk creates a loophole where we will fail to validate the contents of a resource block which either hascount = 0
or has count set to some value that won't be known until the plan walk. Here instead we just validate the configuration block itself, using an unknown number forcount.index
if appropriate, to determine of the configuration is valid for any count. More precise validation then happens during theDynamicExpand
in the plan walk.The remaining step here to repair the validate walk is to swap in the new state types and then allow the evaluator to recognize the difference between a completed resource with
count = 0
and a resource that has no state at all due to being in the validate walk. That will then allow expressions likenull_resource.example[0]
to pass validation, sincenull_resource.example
would in that case be an unknown list value ofnull_resource
's implied object type, allowing type checking to proceed.Once this is working again, we'll be partially down the road towards #15895, though we'll probably want to do some further refactoring afterwards to more cleanly distinguish the different walks. The sequence of work that this PR belongs to is just about getting
terraform validate
back into a working state when running against our new expression evaluator.This doesn't yet include any tests for the
terraform validate
breakage, with the intent that we'll do that once it's working end-to-end and thus the tests would actually pass.