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

fmt: print parse errors on error #12870

Merged
merged 1 commit into from
Mar 7, 2024
Merged

Conversation

lbajolet-hashicorp
Copy link
Contributor

The fmt command reformats HCL2 templates, provided it can parse the file and reformat its contents according to the standards set by the HCL library's formatters.

However, if the file is malformed for some reason, the command will fail with a parse error, but while the parse error message is shown, the actual errors in the template(s) are not forwarded, making it hard for users to understand what went wrong with the contents of the file they're trying to format.

In order to be more helpful with those errors, we now forward those parsing errors to the UI.

@lbajolet-hashicorp lbajolet-hashicorp added enhancement backport/1.10.x Backport PR changes to `release/1.10.x` labels Mar 7, 2024
@lbajolet-hashicorp lbajolet-hashicorp requested a review from a team as a code owner March 7, 2024 15:14
@JenGoldstrich
Copy link
Contributor

Tested it and read the code, LGTM!

Before:

packer fmt ~/malformed.pkr.hcl
Error: encountered an error while formatting /Users/()/malformed.pkr.hcl

failed to parse HCL /Users/()/malformed.pkr.hcl

After:

packer fmt ~/malformed.pkr.hcl
Error: encountered an error while formatting /Users/()/malformed.pkr.hcl

2 errors occurred:
        * failed to parse HCL /Users/()/malformed.pkr.hcl
        * /Users/()/malformed.pkr.hcl:5,1-8: Argument or block definition
required; An argument or block definition is required here. To set an argument,
use the equals sign "=" to introduce the argument value.

Copy link
Contributor

@nywilken nywilken left a comment

Choose a reason for hiding this comment

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

Nice improvement. I left a small suggestion.

hcl2template/formatter.go Outdated Show resolved Hide resolved
Copy link
Contributor

@nywilken nywilken left a comment

Choose a reason for hiding this comment

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

Approving for when you are ready to merge.

Should this be backported to 1.10.x ?

The fmt command reformats HCL2 templates, provided it can parse the file
and reformat its contents according to the standards set by the HCL
library's formatters.

However, if the file is malformed for some reason, the command will fail
with a parse error, but while the parse error message is shown, the
actual errors in the template(s) are not forwarded, making it hard for
users to understand what went wrong with the contents of the file
they're trying to format.

In order to be more helpful with those errors, we now forward those
parsing errors to the UI.
@lbajolet-hashicorp lbajolet-hashicorp merged commit ea1e798 into main Mar 7, 2024
11 checks passed
@lbajolet-hashicorp lbajolet-hashicorp deleted the fmt_fwd_parse_errors branch March 7, 2024 19:43
Copy link

github-actions bot commented Apr 7, 2024

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
backport/1.10.x Backport PR changes to `release/1.10.x` enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants