Skip to content

fix: Avoid warnings in 'terraform output -json' (#38512)#38513

Closed
ChrisJr404 wants to merge 1 commit intohashicorp:mainfrom
ChrisJr404:fix/output-json-warnings-38512
Closed

fix: Avoid warnings in 'terraform output -json' (#38512)#38513
ChrisJr404 wants to merge 1 commit intohashicorp:mainfrom
ChrisJr404:fix/output-json-warnings-38512

Conversation

@ChrisJr404
Copy link
Copy Markdown

Summary

Closes #38512.

The same backend deprecation warnings that #38487 suppressed for terraform output -raw also pollute stdout when running terraform output -json, breaking terraform 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:

func (v *OutputJSON) Diagnostics(diags tfdiags.Diagnostics) {
	// 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.
	errsOnly := diags.ErrorsOnly()
	v.view.Diagnostics(errsOnly)
}

The ErrorsOnly() helper added to tfdiags/diagnostics.go in #38487 is reused as-is — no new helper.

Test

TestOutputJSON_warningsSuppressed mirrors TestOutputRaw_warningsSuppressed introduced in #38487. It:

  • reuses the output-backend-with-deprecation fixture
  • registers the deprecatedInmemBackend that emits a deprecation warning during PrepareConfig
  • runs terraform output -json foo
  • asserts exit code 0
  • asserts "deprecated" does not appear on stdout (the parseable-JSON gate that Deprecation warning pollutes STDOUT with output -json #38512 was reporting)
  • asserts "deprecated" does not appear on stderr in this case (no error to surface)
  • asserts the remaining stdout is the parseable JSON value \"bar\"
$ go test ./internal/command -run "TestOutput(Raw|JSON)_warningsSuppressed" -v
=== RUN   TestOutputRaw_warningsSuppressed
--- PASS: TestOutputRaw_warningsSuppressed (0.00s)
=== RUN   TestOutputJSON_warningsSuppressed
--- PASS: TestOutputJSON_warningsSuppressed (0.00s)
PASS

Changelog

Added .changes/v1.16/BUG FIXES-20260504-160000.yaml per the changie flow:

kind: BUG FIXES
body: Avoid warnings on stdout in 'terraform output -json' so the result remains parseable JSON
time: 2026-05-04T16:00:00.000000+00:00
custom:
    Issue: "38512"

Target Release

1.16.x (matches the target-1.16.x shape #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

  • I deliberately mirrored fix: Avoid warnings in terraform output -raw #38487's exact pattern instead of refactoring the two methods into a shared helper. The two Diagnostics impls are now nearly identical and could be hoisted into a nonHumanDiagnostics helper, but that would be a wider refactor than this bug fix — happy to do it as a follow-up if you'd prefer.
  • The new test asserts JSON validity by string-comparing the trimmed stdout to "bar". If you'd prefer a json.Unmarshal round-trip assertion, easy to swap.

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.
@ChrisJr404 ChrisJr404 requested a review from a team as a code owner May 4, 2026 12:24
@hashicorp-cla-app
Copy link
Copy Markdown

hashicorp-cla-app Bot commented May 4, 2026

CLA assistant check
All committers have signed the CLA.

@erpel
Copy link
Copy Markdown

erpel commented May 4, 2026

Thanks for taking this on so quickly. A backport to 1.15.x would be greatly appreciated.

@ChrisJr404
Copy link
Copy Markdown
Author

Thanks @erpel! Backports here are usually triggered by a maintainer adding a backport release/... label once the main PR merges. Happy to open the 1.15.x branch PR by hand if the maintainer prefers that route — let me know either way.

@crw
Copy link
Copy Markdown
Contributor

crw commented May 4, 2026

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.

Copy link
Copy Markdown
Member

@radeksimko radeksimko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +261 to +265
// 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.
Copy link
Copy Markdown
Member

@radeksimko radeksimko May 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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)

@radeksimko radeksimko added the 1.15-backport If you add this label to a PR before merging, backport-assistant will open a new PR once merged label May 5, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 5, 2026

Changelog Warning

Currently 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.

@crw crw added the waiting-response An issue/pull request is waiting for a response from the community label May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

1.15-backport If you add this label to a PR before merging, backport-assistant will open a new PR once merged waiting-response An issue/pull request is waiting for a response from the community

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deprecation warning pollutes STDOUT with output -json

4 participants