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.34k stars 9.49k forks source link

terraform fmt should allow for a min position for = sign #31130

Open DonBower opened 2 years ago

DonBower commented 2 years ago

Current Terraform Version

1.0.8

...

Use-cases

Formating .tf and .tfvars files to have consistent separation between key and value provides easier to read code.

current behavior will reset the = sign column with every block.

Attempted Solutions

Proposal

add parm like -min-column=35 which would line up all the = signs on column 35 unless it would clobber the key.

References

kmoe commented 2 years ago

Thanks for the enhancement request. What is your use case for this?

apparentlymart commented 2 years ago

As a general rule we do not offer any customization options for terraform fmt, because its purpose is to represent the standard formatting that all Terraform configuration should ideally follow. Its purpose is not to represent anyone's particular tastes for layout (it does several things I wouldn't personally have chosen, too) but rather to encourage Terraform configuration to look the same everywhere, so people don't need to relearn a new style each time they start working on a different codebase.

Given that, I don't expect we would add an option like this. We could potentially change the default rule so that all configuration has a minimum indentation, but we've also committed that we will not add new formatting rules in later versions that are not supported by the long-standing examples in our documentation, to avoid creating churn for those who already used fmt in previous Terraform versions, and so I don't think such a change would be consistent with that promise.

For those who are particularly opposed to what fmt does there is of course the option of not using it at all and potentially of writing your own formatter that better meets your personal tastes. I would not typically recommend that, but it is a decision you are free to make if you wish.

DonBower commented 2 years ago

to turn something like this:

provider "aws" {
  access_key = var.aws_access_key_id
  secret_key = var.aws_secret_access_key
  token      = var.aws_session_token
  region     = var.aws_region
  endpoints {
    sts = "https://sts.${var.aws_region}.amazonaws.com"
  }
  assume_role {
    role_arn = "arn:aws:iam::${var.aws_account_id}:role/terraform"
  }
}

into something like this:

provider "aws" {
  access_key      = var.aws_access_key_id
  secret_key      = var.aws_secret_access_key
  token           = var.aws_session_token
  region          = var.aws_region
  endpoints {
    sts           = "https://sts.${var.aws_region}.amazonaws.com"
  }
  assume_role {
    role_arn      = "arn:aws:iam::${var.aws_account_id}:role/terraform"
  }
}

the original code is output from the current terraform fmt command.

DonBower commented 2 years ago

I'm not saying making an offset mandatory, I am asking for an option. At the very least it should provide consistent results within a given resource.

sodul commented 1 year ago

I have the opposite pet peeve about fmt adding useless spaces that lead to larger diffs than necessary on PRs. It causes useless additional storage to be used on SCM backends and when reviewing a PR, one has to go out of their way to disable whitespace difference to really view only the meaningful changes.

As demonstrated above the current implementation of the equal alignments has limited impact on improving vertical readability as it jumps around with sub-scopes.

Some of our devs have resorted to use sed to trim the extra spaces but this is dangerous as it can do that for embedded data where spaces might mean something.

If anyone knows about a tool that can reliably set the spaces before = to just one space in .tf files I would appreciate it!