core: Do not create "delete" changes for nonexistent outputs #31471
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.
If there are outputs in configuration, a destroy plan will always contain a "delete" change for each of these outputs.
If an output was not present in state to begin with, the change will look like this:
Since there is a change in the plan, this plan will be considered applyable, and the user will be presented with text instructing them to apply a plan in which there are no actual changes.
Applying the destroy plan will not actually do anything, since the output was not in state to begin with.
This commit stops the above from happening in the case of root module outputs.
A simple example follows. For a more realistic example and the issue that motivated this change, please see #23387 (comment).
Example
Configuration
Steps
terraform init
terraform plan -detailed-exitcode -destroy
Result prior to change
Exit code 2.
Plan prior to change
Result after change
Exit code 0.
Plan after change