command: adjust exit code of state rm#22300
Conversation
Commands `state rm` and `state mv` will now exit with code 1 when the target resource is not found in the current state. This is consistent with `terraform state show non_existent_resource`. Fixes #17800
pselle
left a comment
There was a problem hiding this comment.
Hi @jgpacker, thanks for this contribution! I tried out your patch and it works as described, however, now that exit code is 1, we should show an error to the user versus showing UI output -- so that the experience is consistent (the command errored, so an error is displayed).
In Terraform, our best practice to add an error is using the tfdiags package, where a summary, and then more helpful information is shared so the user can amend their error. In this scenario, something like (this uses some language from the error displayed when you use terraform show on the non-existent resource) could replace the current UI output in state rm:
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Invalid target address",
"No matching objects found. To view the available instances, use "terraform state list". Please modify the address to reference a specific instance.",
))
c.showDiagnostics(diags)
return 1
|
Thank you for the feedback. I will correct this soon |
|
I have updated the code according to the feedback. Unfortunately I have not been able to test it locally right now. So there might be still be some adjustments to be done. Thank you in advance for the review |
pselle
left a comment
There was a problem hiding this comment.
One improvement for state_mv, since we have a constant that already exists.
I had trouble figuring out a flow of terraform state mv that would hit this while testing on my machine -- perhaps you could add a test to state_mv_test.go hitting this error?
|
@pselle I opened this PR only to fix |
|
Ah in that case (if you haven't been able to replicate through with |
|
Thank you for the orientation. I have reverted the changes in |
|
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further. |
Command
state rmwill now exit with code 1 when the resource is not found in the current state.This is consistent with
terraform state show non_existent_resource.Fixes #17800