dineshba / tf-summarize

A command-line utility to print the summary of the terraform plan
MIT License
534 stars 38 forks source link

error when parsing input: unexpected end of JSON input #20

Closed GregoryOrciuch closed 8 months ago

GregoryOrciuch commented 1 year ago

I'm trying to fire it from CICD where I install version 0.2.2 (and tried 0.2.5 with same effect). I took the one for linux_amd64 release build, but it fail to kind of run:

Plan: 0 to add, 5 to change, 0 to destroy.
─────────────────────────────────────────────────────────────────────────────
Saved the plan to: tf.plan
To perform exactly these actions, run the following command to apply:
    terraform apply "tf.plan"
$ tf-summarize tf.plan
error when parsing input: unexpected end of JSON input
Cleaning up project directory and file based variables
ERROR: Job failed: exit code 1

but that is very legit tf.plan file.

If I run it locally (when tf-summarize is installed by macos brew it works well), so it has to be something with a release thing.

Any suggestions?

dineshba commented 1 year ago

May I know which CICD you use ? Also, how terraform is installed ?

Reason for asking:

For Github actions,

If you are using hashicorp/setup-terraform github action to setup terraform, ensure terraform_wrapper is set to false. Because without it, each terraform output will some extra lines. Example

Check if there is similar thing happen in your CICD setup.

GregoryOrciuch commented 1 year ago

@dineshba it's a just a installation of latest terraform into the amazonlinux official docker image. It's something like FROM amazonlinux2 RUN wget terraform_zip, unzip and copy to /usr/bin/ nothing there about wrapper.

I run in GitLab CI.

dineshba commented 1 year ago

Could you try this once?

terraform show -json tfplan > output.json
tf-summarize output.json

# if we still get the json parse error,
# cat output.json # see if the output.json is valid json
everythings-gonna-be-alright commented 1 year ago

https://docs.gitlab.com/runner/shells/

echo 'tf-summarize -tree tfplan.json' | bash
error when parsing input: unexpected end of JSON input

Temporary workaround 🏄‍♂️:

echo 'cat tfplan.json | tf-summarize' | bash
+--------+------+
| CHANGE | NAME |
+--------+------+
+--------+------+

cat tfplan.json | tf-summarize

dineshba commented 1 year ago

Glad that we have a workaround.

I will try to reproduce this issue and understand the reason behind it. This will help me to fix this issue.

kayman-mk commented 1 year ago

I had the same problem. But I figured out that I used a non-existing file calling tf-summarize. Would be better to see file not found in case the file doesn't exist.

dineshba commented 1 year ago

Hi @kayman-mk. Thanks for confirming the issue.

For the non-existing file, I am getting below error

$ tf-summarize invalid.json
error reading from input: error when opening file invalid.json: open invalid.json: no such file or directory

and I am in 0.2.6 version (latest version). Could you please confirm the version once ?

Terraformadillo commented 1 year ago

@dineshba I am seeing a similar error using the latest version tag v0.3.2 installed via go.

GregoryOrciuch commented 1 year ago

yes, still same with 0.3.2, however point above workaround works pretty well. So you output a json plan from terraform then pipe it to tf-summarize, e.g.: cat tfplan.json | tf-summarize -tree'

Terraformadillo commented 1 year ago

@GregoryOrciuch Yeah workaround is now working for me now. I was doing something wrong with terraform show step.

phoehnel commented 1 year ago

Got the same error with error when parsing input: unexpected end of JSON input when running in GitLab CI.

When i download the exact plan file from the pipeline and run it locally using the same tf-summarize version, it works though. Seems to be either about the CI Environment or the .deb installation method.

stevehipwell commented 1 year ago

I'm seeing this same issue on a GitHub actions runner, I've fixed it with jq '.' "${json_file_path}" | tf-summarize.

mdb commented 10 months ago

I will try to reproduce this issue and understand the reason behind it. This will help me to fix this issue.

As a data point, I am unable to reproduce this locally on Mac OS (Sonoma 14.2.1) using tf-summarize v0.3.6:

$ make summarize WORKSPACE=default
terraform show \
                -json \
                default.plan > default-plan.json
tf-summarize -v
Version: 0.3.6
tf-summarize \
                -out default-summary.md \
                -md \
                default-plan.json

Written plan summary to default-summary.md

However, the same command(s) and plan result in the following error when run from GitHub Actions's Ubuntu runner:

make summarize WORKSPACE=default
terraform show \
    -json \
    default.plan > default-plan.json
tf-summarize -v
Version: 0.3.6
tf-summarize \
    -out default-summary.md \
    -md \
    default-plan.json
error when parsing input: unexpected end of JSON input

However, passing raw JSON via STDIN works as expected both locally on Mac OS and in GitHub Actions's Ubuntu runner:

make summarize WORKSPACE=default
terraform show \
    -json \
    default.plan > default-plan.json
tf-summarize -v
Version: 0.3.6
jq '.' "default-plan.json" | tf-summarize \
    -md > default-summary.md

This suggests to me the possibility of an OS-specific bug/inconsistently related to how tf-summarize reads the plan JSON file if/when it's passed a plan JSON file (as opposed to when it's passed raw JSON via STDIN). Or am I barking up the wrong tree?

mdb commented 10 months ago

👋 @dineshba I've created a quick fix for this bug via PR #64.

I've also managed to create a reproducible demo for you over in https://github.com/dineshba/tf-summarize/pull/63, whose GitHub Actions runs encounter the error when parsing input: unexpected end of JSON input error: https://github.com/dineshba/tf-summarize/actions/runs/7514179482/job/20456763616#step:9:6

mdb commented 8 months ago

@dineshba This issue was addressed in PR #64 (now merged), but never included in a tf-summarize GitHub release (tf-summarize 0.3.7 is currently the latest release). Would you be willing to publish a 0.3.8 release that includes the PR #64 fix to this issue, then closing this issue? Thanks!

dineshba commented 8 months ago

let me release it tomorrow with another PR merged https://github.com/dineshba/tf-summarize/pull/70

mdb commented 8 months ago

let me release it tomorrow with another PR merged https://github.com/dineshba/tf-summarize/pull/70

@dineshba 👋 😄 Gentle nudge: A 0.3.8 release including PR #64 and #70 would be great!

dineshba commented 8 months ago

Created the new release v0.3.8 @mdb. Thanks for ur PRs. Really appreciated.