Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print retryLogHook messages to stderr #35159

Open
ben-z opened this issue May 15, 2024 · 3 comments · Fixed by #35929
Open

Print retryLogHook messages to stderr #35159

ben-z opened this issue May 15, 2024 · 3 comments · Fixed by #35929
Assignees
Labels
bug cloud Related to Terraform Cloud's integration with Terraform json-output v1.8 Issues (primarily bugs) reported against v1.8 releases

Comments

@ben-z
Copy link

ben-z commented May 15, 2024

Terraform Version

Terraform v1.8.2
on linux_amd64

Terraform Configuration Files

terraform {
  cloud {
    organization = "..."

    workspaces {
      name = "..."
    }
  }
}

// ...

Debug Output

Not relevant

Expected Behavior

terraform -output json should output only JSON.

Actual Behavior

The output is not pure json. It contains some error messages from intermittent (retriable) errors.

> terraform -output json
There was an error connecting to HCP Terraform. Please do not exit Terraform to prevent data loss!
Trying to restore the connection...

Still trying to restore the connection... (3s elapsed)
{}

These are the culprit messages:

const initialRetryError = `
There was an error connecting to HCP Terraform. Please do not exit
Terraform to prevent data loss! Trying to restore the connection...
`
const repeatedRetryError = "Still trying to restore the connection... (%s elapsed)"

This makes it nontrivial for downstream applications to parse the output.

Steps to Reproduce

  1. terraform -output json
  2. When the connection to the backend is suboptimal, additional messages show up.

Additional Context

No response

References

No response

@ben-z ben-z added bug new new issue not yet triaged labels May 15, 2024
@ben-z
Copy link
Author

ben-z commented May 15, 2024

I'm currently using a quick and dirty workaround: next(extract_json_objects(stdout))

def extract_json_objects(text, decoder=JSONDecoder()):
    """
    Find JSON objects in text, and yield the decoded JSON data

    Does not attempt to look for JSON arrays, text, or other JSON types outside
    of a parent JSON object.

    Derived from https://stackoverflow.com/a/54235803

    This is useful for handling JSON objects embedded in a string, such as
    when this bug pollutes the output:
    https://github.com/hashicorp/terraform/issues/35159
    """
    pos = 0
    while True:
        match = text.find("{", pos)
        if match == -1:
            break
        try:
            result, index = decoder.raw_decode(text[match:])
            yield result
            pos = match + index
        except ValueError:
            pos = match + 1

ben-z added a commit to unicorns/infra that referenced this issue May 15, 2024
Terraform has [a
bug](hashicorp/terraform#35159) where the JSON
output of `terraform output -json` can be polluted by error messages.
This is a workaround to parse the output json properly.
@jbardin jbardin added the cloud Related to Terraform Cloud's integration with Terraform label May 15, 2024
@apparentlymart apparentlymart added the v1.8 Issues (primarily bugs) reported against v1.8 releases label Jun 17, 2024
@gawbul
Copy link

gawbul commented Oct 28, 2024

Any updates on this at all. We are seeing the issue with our Terraform Cloud workflows as well.
It doesn't cause failure in our Terraform Apply pipelines, but the final output steps sometimes fail and the deployments are therefore reported as failures in Jira.

@bschaatsbergen bschaatsbergen self-assigned this Oct 30, 2024
@bschaatsbergen bschaatsbergen added json-output and removed new new issue not yet triaged labels Oct 30, 2024
@bschaatsbergen
Copy link
Member

bschaatsbergen commented Oct 30, 2024

Hey @ben-z, thank you for reporting this.

It seems like the initial and repeated retry error log messages are not rendered through a view yet, which causes them to default to a human-readable output. Ideally, there should always be a HumanView and JsonView implementation for such operations in the views package.

I'll be picking up this issue and will share more information there as soon as it becomes available.

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug cloud Related to Terraform Cloud's integration with Terraform json-output v1.8 Issues (primarily bugs) reported against v1.8 releases
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants