fix: Avoid warnings in 'terraform output -json' (#38512)#38513
fix: Avoid warnings in 'terraform output -json' (#38512)#38513ChrisJr404 wants to merge 1 commit intohashicorp:mainfrom
Conversation
Closes hashicorp#38512. The same backend deprecation warnings that hashicorp#38487 suppressed for 'terraform output -raw' also pollute stdout with 'terraform output -json', breaking 'terraform output -json | jq ...' pipelines. The JSON formatter routes diagnostics through the same writer the human formatter uses, which causes warnings to land on stdout alongside the JSON value. Apply the same fix used for the raw formatter to the JSON formatter: filter to ErrorsOnly() before forwarding to view.Diagnostics. The new helper added in hashicorp#38487 is reused as-is. Adds TestOutputJSON_warningsSuppressed mirroring the existing TestOutputRaw_warningsSuppressed. The new test reuses the output-backend-with-deprecation fixture from hashicorp#38487 and asserts: - exit code 0 - 'deprecated' does not appear on stdout (the parseable-JSON gate) - 'deprecated' does not appear on stderr in this case - stdout contains the value '"bar"' parseable as JSON Mechanical, additive change — no schema or behavior change for any caller that doesn't currently produce backend deprecation warnings, and pipelines like 'terraform output -json | jq' stop breaking on backends that do.
|
Thanks for taking this on so quickly. A backport to 1.15.x would be greatly appreciated. |
|
Thanks @erpel! Backports here are usually triggered by a maintainer adding a |
|
Hi @ChrisJr404, thanks for this submission. Per https://github.com/hashicorp/terraform/blob/main/.github/CONTRIBUTING.md, is there a missing disclosure with regards to GenAI usage? I will raise this in triage as the fix seems similar to a previous PR. |
radeksimko
left a comment
There was a problem hiding this comment.
I'm fine with the solution but the reason behind is different and I think that should be reflected in the comment which I propose updating.
As per @crw's question - Can you also clarify whether AI was used to prepare this PR?
Thanks.
| // filter out warnings as these wouldn't be expected in JSON mode | ||
| // either: pipelines like `terraform output -json | jq` cannot | ||
| // tolerate non-JSON content on stdout, and warnings typically don't | ||
| // influence the exit code so the user cannot expect them in stdout. | ||
| // Mirrors the same suppression added for `-raw` in #38487. |
There was a problem hiding this comment.
| // filter out warnings as these wouldn't be expected in JSON mode | |
| // either: pipelines like `terraform output -json | jq` cannot | |
| // tolerate non-JSON content on stdout, and warnings typically don't | |
| // influence the exit code so the user cannot expect them in stdout. | |
| // Mirrors the same suppression added for `-raw` in #38487. | |
| // filter out warnings as we cannot change the current format | |
| // to introduce them gracefully (without breaking existing usage) |
Changelog WarningCurrently this PR would target a v1.15 release. Please add a changelog entry for in the .changes/v1.15 folder, or discuss which release you'd like to target with your reviewer. If you believe this change does not need a changelog entry, please add the 'no-changelog-needed' label. |
Summary
Closes #38512.
The same backend deprecation warnings that #38487 suppressed for
terraform output -rawalso pollute stdout when runningterraform output -json, breakingterraform output -json | jq …pipelines. As the issue reporter noted, the same fix shape applies.Change
Apply the
ErrorsOnly()filter the raw formatter already uses to the JSON formatter:The
ErrorsOnly()helper added totfdiags/diagnostics.goin #38487 is reused as-is — no new helper.Test
TestOutputJSON_warningsSuppressedmirrorsTestOutputRaw_warningsSuppressedintroduced in #38487. It:output-backend-with-deprecationfixturedeprecatedInmemBackendthat emits a deprecation warning duringPrepareConfigterraform output -json foo"deprecated"does not appear on stdout (the parseable-JSON gate that Deprecation warning pollutes STDOUT withoutput -json#38512 was reporting)"deprecated"does not appear on stderr in this case (no error to surface)\"bar\"Changelog
Added
.changes/v1.16/BUG FIXES-20260504-160000.yamlper the changie flow:Target Release
1.16.x (matches the
target-1.16.xshape #38487 used; happy to add a backport label if you'd like this on 1.15.x as well, since the same break exists there).Rollback Plan
This is a single-method change in
views/output.go. Reverting the commit restores the prior behavior without any state migration concerns.Notes for review
terraform output -raw#38487's exact pattern instead of refactoring the two methods into a shared helper. The twoDiagnosticsimpls are now nearly identical and could be hoisted into anonHumanDiagnosticshelper, but that would be a wider refactor than this bug fix — happy to do it as a follow-up if you'd prefer."bar". If you'd prefer ajson.Unmarshalround-trip assertion, easy to swap.