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

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

Closed
GregoryOrciuch opened this issue Nov 30, 2022 · 18 comments
Closed

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

GregoryOrciuch opened this issue Nov 30, 2022 · 18 comments

Comments

@GregoryOrciuch
Copy link

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
Copy link
Owner

dineshba commented Dec 2, 2022

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
Copy link
Author

@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
Copy link
Owner

dineshba commented Dec 2, 2022

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
Copy link

everythings-gonna-be-alright commented Dec 14, 2022

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
Copy link
Owner

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
Copy link

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
Copy link
Owner

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
Copy link

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

@GregoryOrciuch
Copy link
Author

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
Copy link

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

@phoehnel
Copy link

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
Copy link

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

@mdb
Copy link
Contributor

mdb commented Jan 8, 2024

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
Copy link
Contributor

mdb commented Jan 13, 2024

👋 @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 #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

dineshba pushed a commit that referenced this issue Jan 27, 2024
* correct input reading logic

This addresses #20 and
ensures input is read from the plan file -- and not via STDIN -- if a
plan file argument is provided.

This also seeks to improve some of the error messaging to be a bit more
clear.

* demo issue 20 fix via GH Actions

Ideally, tf-summarize would feature a suite of automated tests verifying
its functionality. In absenece of that, this demos the issue #20 fix via
GH Actions.

* use consistent TF version when invoking TF

- ensure the generation of the `example` directory data is done in a
  consistent, reproducible fashion
- ensure GH Actions uses the same version of TF expected by the `example` directory
- add plan and plan JSON files to source control, for testing purposes

* add automated test verifying STDIN vs file-provided input

This adds a basic automated test verifying the validity of the issue #20
fix.

* remove `demo` job from Build GH Actions workflow

Per code review request, @dineshba would prefer this be kept in a
separate file.
@mdb
Copy link
Contributor

mdb commented Mar 3, 2024

@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
Copy link
Owner

dineshba commented Mar 4, 2024

let me release it tomorrow with another PR merged #70

@mdb
Copy link
Contributor

mdb commented Mar 6, 2024

let me release it tomorrow with another PR merged #70

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

@dineshba
Copy link
Owner

dineshba commented Mar 9, 2024

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

@dineshba dineshba closed this as completed Mar 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants