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

Can't run terraform fmt because file is not formatted correctly #35517

Open
socketbox opened this issue Jul 30, 2024 · 1 comment
Open

Can't run terraform fmt because file is not formatted correctly #35517

socketbox opened this issue Jul 30, 2024 · 1 comment
Labels
enhancement new new issue not yet triaged

Comments

@socketbox
Copy link

Terraform Version

Terraform v1.9.3                                              
on linux_amd64                                                
+ provider registry.terraform.io/hashicorp/archive v2.4.2     
+ provider registry.terraform.io/hashicorp/aws v5.59.0        
+ provider registry.terraform.io/hashicorp/awscc v1.1.0       
+ provider registry.terraform.io/hashicorp/local v2.5.1       
+ provider registry.terraform.io/hashicorp/template v2.2.0

Terraform Configuration Files

  ordered_cache_behavior {
    path_pattern             = "/show*"
    allowed_methods          = ["GET", "HEAD", "OPTIONS"]
    cached_methods           = ["GET", "HEAD"]
    target_origin_id         = "next-staging"
    origin_request_policy_id = aws_cloudfront_origin_request_policy.all_viewer_plus_geo.id
    cache_policy_id          = aws_cloudfront_cache_policy.pbsorg_cacheall_qa.id
    viewer_protocol_policy   = "redirect-to-https"
    compress                 = true
  lambda_function_association {
      event_type = "viewer-request"
      lambda_arn = "${aws_lambda_function.next_localization_request.arn}:${aws_lambda_function.next_localization_request.version}"
    }

    lambda_function_association {
      event_type = "viewer-response"
      lambda_arn = "${aws_lambda_function.next_localization_response.arn}:${aws_lambda_function.next_localization_response.version}"
    }}

Debug Output

https://gist.github.com/socketbox/f079c37e0219673706e3a8e54b4c4915

Expected Behavior

The file should be formatted, adding a line break as necessary.

Actual Behavior

terraform fmt errors out and won't even run because of the very formatting that it should fix.

Steps to Reproduce

  1. terraform fmt

Additional Context

I've read the comments in the issue I link to (#23223) and I think that the justification given for not dealing with "vertical whitespace" isn't very sound.

References

#23223

@socketbox socketbox added bug new new issue not yet triaged labels Jul 30, 2024
@jbardin
Copy link
Member

jbardin commented Jul 31, 2024

Hi @socketbox,

The fmt command is designed to reformat valid hcl, otherwise it cannot parse and tokenize the source to complete that task. While the required change in the source here appears mostly cosmetic to us, the parser is looking for the newline to signify the end of the block declaration and finds an unexpected character before the block is complete.

I don't think we'd ever want fmt to use a different parser, since divergence there could lead to other bugs, but perhaps it would be possible to implement some method allowing fmt to be more lenient in its parsing and accept and fix common mistakes. The fmt command is also guaranteed to transform the source with no change to the structures in that source, and altering syntax in this way changes that guarantee somewhat.

My guess is that the most likely improvement here would be for a better error output to help pinpoint the mistake, rather than creating a new fmt command to handle it.

@jbardin jbardin added enhancement and removed bug labels Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement new new issue not yet triaged
Projects
None yet
Development

No branches or pull requests

2 participants