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 · 1 comment
Open

Print retryLogHook messages to stderr #35159

ben-z opened this issue May 15, 2024 · 1 comment
Labels
bug cloud Related to Terraform Cloud's integration with Terraform new new issue not yet triaged

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
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 new new issue not yet triaged
Projects
None yet
Development

No branches or pull requests

2 participants