core: Warn when creating and applying with -target #22783
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 documentation for the
-target
option warns that it's intended for exceptional circumstances only and not for routine use, but that's not a very prominent location for that warning and so some users miss it.Here we make the warning more prominent by including it directly in the Terraform output when
-target
is in use. We first warn during planning that the plan might be incomplete, and then warn again after apply concludes and direct the user to runterraform plan
to make sure that there are no further changes outstanding. The latter message is intended to reinforce that-target
should only be a one-off operation and that you should always run without it soon after to ensure that the workspace is left in a consistent, converged state.During plan, the warning message is:
During apply, the warning message is:
While implementing this I noticed some places where we missed updating for the fact that
ctx.Plan
andctx.Apply
now return diagnostics rather than errors, so there are some adjustments for that to ensure that the callers really are treating these warnings as warnings, rather than exiting with an error status.