Skip to content

command/fmt: support formatting multiple files#28191

Merged
alisdair merged 1 commit intohashicorp:mainfrom
zimbatm:terraform-fmt-manyargs
Aug 12, 2022
Merged

command/fmt: support formatting multiple files#28191
alisdair merged 1 commit intohashicorp:mainfrom
zimbatm:terraform-fmt-manyargs

Conversation

@zimbatm
Copy link
Copy Markdown
Contributor

@zimbatm zimbatm commented Mar 24, 2021

All the code infrastructure was there to support formatting multiple
files already.

This makes terraform fmt more flexible and also compliant with the
treefmt formatted spec

Fixes numtide/treefmt#97

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 24, 2021

Codecov Report

Merging #28191 (a648ade) into main (bc38623) will decrease coverage by 1.29%.
The diff coverage is 16.66%.

Impacted Files Coverage Δ
command/fmt.go 58.85% <16.66%> (-14.80%) ⬇️
terraform/eval_error.go 33.33% <0.00%> (-16.67%) ⬇️
terraform/transform_vertex.go 45.45% <0.00%> (-11.69%) ⬇️
terraform/transform_local.go 63.63% <0.00%> (-11.37%) ⬇️
terraform/node_provisioner.go 71.42% <0.00%> (-10.39%) ⬇️
states/statefile/version0.go 33.33% <0.00%> (-9.53%) ⬇️
helper/hashcode/hashcode.go 66.66% <0.00%> (-8.34%) ⬇️
backend/remote/colorize.go 55.55% <0.00%> (-8.09%) ⬇️
plans/plan.go 40.00% <0.00%> (-8.00%) ⬇️
registry/response/terraform_provider.go 37.50% <0.00%> (-7.96%) ⬇️
... and 654 more

@zimbatm
Copy link
Copy Markdown
Contributor Author

zimbatm commented Mar 24, 2021

codecov is complaining because the added doc is untested :)

@zimbatm
Copy link
Copy Markdown
Contributor Author

zimbatm commented Apr 10, 2021

ping?

@mloughran
Copy link
Copy Markdown

Some more context and justification for this PR from my perspective: treefmt allows dramatically speeding up repository formatting by only attempting to format files that have changed. The current interface to terraform fmt, while working great standalone, is too restrictive to support this use-case. This seems like a shame!

@zimbatm zimbatm force-pushed the terraform-fmt-manyargs branch from a648ade to 0f331a6 Compare May 7, 2021 20:39
@zimbatm
Copy link
Copy Markdown
Contributor Author

zimbatm commented May 7, 2021

rebased to fix the merge conflict

@zimbatm zimbatm force-pushed the terraform-fmt-manyargs branch from 0f331a6 to 559137f Compare June 24, 2021 13:52
@zimbatm
Copy link
Copy Markdown
Contributor Author

zimbatm commented Jun 24, 2021

rebased to fix the merge conflicts

@flokli
Copy link
Copy Markdown

flokli commented Nov 8, 2021

Ping @jbardin. It'd be really nice if this could go in!

@zimbatm
Copy link
Copy Markdown
Contributor Author

zimbatm commented Dec 12, 2021

/cc @teamterraform

@zimbatm zimbatm force-pushed the terraform-fmt-manyargs branch 2 times, most recently from b070abd to 905b2c0 Compare January 28, 2022 10:28
@hashicorp-cla
Copy link
Copy Markdown

hashicorp-cla commented Mar 12, 2022

CLA assistant check
All committers have signed the CLA.

@aldoborrero
Copy link
Copy Markdown

This feature is useful to have!

@mloughran
Copy link
Copy Markdown

mloughran commented Jul 22, 2022

Some more justification for this PR: Currently the only way to run terraform via treefmt is it to use a wrapper script (e.g. see https://github.com/numtide/treefmt/blob/master/treefmt.toml#L77)

Unfortunately, the startup speed of terraform fmt means that running it separately for each file is very slow. On my project (64 terraform files) this is already 23s. Output from treefmt:

[INFO]: #terraform: 64 files processed in 23.24s

Whereas

terraform fmt -recursive  0.48s user 0.90s system 83% cpu 1.659 total

This makes treefmt + terraform just about acceptable for local use cases (hopefully the files don't change much, and treefmt only runs on changed files), but it's a deal-breaker for use cases such as checking formatting in CI.

@crw
Copy link
Copy Markdown
Contributor

crw commented Jul 29, 2022

Hi @zimbatm, apologies for the late reply. We should be able to review this within a few weeks. Thanks for your patience.

Copy link
Copy Markdown
Contributor

@alisdair alisdair left a comment

Choose a reason for hiding this comment

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

Thanks for this contribution! This looks like a nice improvement. There are a couple of things to fix before we can merge:

  • There's a merge conflict due to some recent help text changes. Can you rebase and fix that?
  • We need to update the website docs to note the new functionality. Can you update the docs file here to describe how terraform fmt works after your changes?

@zimbatm zimbatm force-pushed the terraform-fmt-manyargs branch from 905b2c0 to 492a8be Compare August 7, 2022 12:44
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Aug 7, 2022

Codecov Report

Merging #28191 (b070abd) into main (21228e1) will increase coverage by 1.46%.
The diff coverage is 16.66%.

❗ Current head b070abd differs from pull request most recent head 7a9ccc0. Consider uploading reports for the commit 7a9ccc0 to get more accurate results

Impacted Files Coverage Δ
command/fmt.go 59.78% <16.66%> (-16.61%) ⬇️
terraform/graph_builder_eval.go 0.00% <0.00%> (-100.00%) ⬇️
terraform/node_provider_eval.go 0.00% <0.00%> (-100.00%) ⬇️
terraform/ui_output_callback.go 0.00% <0.00%> (-100.00%) ⬇️
repl/format.go 11.42% <0.00%> (-87.63%) ⬇️
configs/provider_meta.go 0.00% <0.00%> (-83.34%) ⬇️
command/version.go 0.00% <0.00%> (-71.24%) ⬇️
internal/getproviders/http_mirror_source.go 0.00% <0.00%> (-68.93%) ⬇️
internal/providercache/installer_events.go 42.85% <0.00%> (-57.15%) ⬇️
command/console_interactive.go 0.00% <0.00%> (-48.15%) ⬇️
... and 776 more

All the code infrastructure was there to support formatting multiple
files already.

This makes `terraform fmt` more flexible and also compliant with the
[treefmt formatter
spec](https://numtide.github.io/treefmt/docs/formatters-spec.html)
@zimbatm zimbatm force-pushed the terraform-fmt-manyargs branch from 492a8be to 7a9ccc0 Compare August 7, 2022 13:04
@zimbatm
Copy link
Copy Markdown
Contributor Author

zimbatm commented Aug 7, 2022

rebased!

  • The code didn't change
  • I re-did the doc, copying some of the text from the website
  • Renamed PATH... to [target...] to better follow the other commands.
  • Updated the cmd and website descriptions to be more in sync.

Copy link
Copy Markdown
Contributor

@alisdair alisdair left a comment

Choose a reason for hiding this comment

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

Thanks, this is great!

@alisdair alisdair merged commit 2796604 into hashicorp:main Aug 12, 2022
@github-actions
Copy link
Copy Markdown
Contributor

Reminder for the merging maintainer: if this is a user-visible change, please update the changelog on the appropriate release branch.

@zimbatm zimbatm deleted the terraform-fmt-manyargs branch August 13, 2022 09:27
@github-actions
Copy link
Copy Markdown
Contributor

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 contributions.
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 Sep 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

terraform fmt doesn't take files

8 participants