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

command/fmt: Restore some more-opinionated behaviors #26390

Merged
merged 3 commits into from
Sep 28, 2020

Commits on Sep 25, 2020

  1. command/fmt: Factor out the actual formatting

    Previously formatting was just a simple wrapper around hclwrite.Format.
    That remains true here, but the call is factored out into a separate
    method in preparation for making it also do some Terraform-specific
    cleanups in a future commit.
    apparentlymart committed Sep 25, 2020
    Configuration menu
    Copy the full SHA
    a18f665 View commit details
    Browse the repository at this point in the history
  2. command/fmt: Format using the full hclwrite syntax tree

    Previously we were just using hclwrite.Format, a token-only formatting
    pass. Now we'll do that via the full hclwrite parser, getting the
    formatting as a side-effect of the parsing and re-serialization.
    
    This should have no change in observable behavior as-is, but in a future
    commit we'll add some additional processing rules that modify the syntax
    tree before re-serializing it.
    apparentlymart committed Sep 25, 2020
    Configuration menu
    Copy the full SHA
    d564f10 View commit details
    Browse the repository at this point in the history

Commits on Sep 28, 2020

  1. command/fmt: Restore some opinionated behaviors

    In Terraform 0.11 and earlier, the "terraform fmt" command was very
    opinionated in the interests of consistency. While that remains its goal,
    for pragmatic reasons Terraform 0.12 significantly reduced the number
    of formatting behaviors in the fmt command. We've held off on introducing
    0.12-and-later-flavored cleanups out of concern it would make it harder
    to maintain modules that are cross-compatible with both Terraform 0.11
    and 0.12, but with this aimed to land in 0.14 -- two major releases
    later -- our new goal is to help those who find older Terraform language
    examples learn about the more modern idiom.
    
    More rules may follow later, now that the implementation is set up to
    allow modifications to tokens as well as modifications to whitespace, but
    for this initial pass the command will now apply the following formatting
    conventions:
    
     - 0.11-style quoted variable type constraints will be replaced with their
       0.12 syntax equivalents. For example, "string" becomes just string.
       (This change quiets a deprecation warning.)
     - Collection type constraints that don't specify an element type will
       be rewritten to specify the "any" element type explicitly, so
       list becomes list(any).
     - Arguments whose expressions consist of a quoted string template with
       only a single interpolation sequence inside will be "unwrapped" to be
       the naked expression instead, which is functionally equivalent.
       (This change quiets a deprecation warning.)
     - Block labels are given in quotes.
    
    Two of the rules above are coming from a secondary motivation of
    continuing down the deprecation path for two existing warnings, so authors
    can have two active deprecation warnings quieted automatically by
    "terraform fmt", without the need to run any third-party tools.
    
    All of these rules match with current documented idiom as shown in the
    Terraform documentation, so anyone who follows the documented style should
    see no changes as a result of this. Those who have adopted other local
    style will see their configuration files rewritten to the standard
    Terraform style, but it should not make any changes that affect the
    functionality of the configuration.
    
    There are some further similar rewriting rules that could be added in
    future, such as removing 0.11-style quotes around various keyword or
    static reference arguments, but this initial pass focused only on some
    rules that have been proven out in the third-party tool
    terraform-clean-syntax, from which much of this commit is a direct port.
    
    For now this doesn't attempt to re-introduce any rules about vertical
    whitespace, even though the 0.11 "terraform fmt" would previously apply
    such changes. We'll be more cautious about those because the results of
    those rules in Terraform 0.11 were often sub-optimal and so we'd prefer
    to re-introduce those with some care to the implications for those who
    may be using vertical formatting differences for some semantic purpose,
    like grouping together related arguments.
    apparentlymart committed Sep 28, 2020
    Configuration menu
    Copy the full SHA
    5ca8f07 View commit details
    Browse the repository at this point in the history