hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io
Other
42.77k stars 9.56k forks source link

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

Open socketbox opened 3 months ago

socketbox commented 3 months ago

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

jbardin commented 3 months ago

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.