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

Preserve comments #36

Open
nikolaik opened this issue Apr 16, 2023 · 5 comments
Open

Preserve comments #36

nikolaik opened this issue Apr 16, 2023 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@nikolaik
Copy link

Hi and thanks for this nice little tool. Would love it if could also preserve comments

@AlexNabokikh AlexNabokikh added the enhancement New feature or request label Apr 17, 2023
@AlexNabokikh AlexNabokikh self-assigned this Apr 17, 2023
@AlexNabokikh
Copy link
Owner

Hey @nikolaik,
Thank you for your feedback.
I appreciate your insights on preserving comments in outputs and variable files. However, it may be challenging to maintain the order of these comments when sorting. As a result, a comment that was added to a specific variable might end up at the top or bottom of the file after sorting, rendering it less useful.

I understand that people often use comments to logically group variable blocks based on their purposes, such as RDS-related vars or SQS-related vars. However, when we decide to sort variables alphabetically - using comments doesn't make much sense anymore. Terraform's description argument is a suitable alternative that serves the purpose of comments and documentation.

I would be interested in hearing more about how you envision this implementation. It would be helpful to see some code examples before and after the proposed changes. Thank you again for your feedback!

@timduhenchanter
Copy link

timduhenchanter commented Apr 17, 2023

If the comments were declared inside of the variable block I imagine this would be pretty easy to implement. I would support and request this addition. description provides insight for any consumer of the variable but comments are more effective for giving wider definitions to the variable especially with complex modules.

@AlexNabokikh
Copy link
Owner

@timduhenchanter Well, I think I don't understand the request. If the comment is declared within the variable or outputs block, tfsort will preserve them. Did you mean some specific scenario?

@nikolaik
Copy link
Author

Thanks for the reply @AlexNabokikh.

I understand that people often use comments to logically group variable blocks based on their purposes, such as RDS-related vars or SQS-related vars. However, when we decide to sort variables alphabetically - using comments doesn't make much sense anymore. Terraform's description argument is a suitable alternative that serves the purpose of comments and documentation.

That is our main use case for comments in variables.tf files too, grouping a list of variable blocks. I don't necessarily think grouping variables like that is the way to go. Alternatives that come to mind are:

  • Move them into variables_group_N.tf files.
  • Introducing a common prefix to the group of variables, f.ex `variable group_.

One grouping I often see are required variables, but I'm not sure if that is worth the hassle.

Moving all of the comments to the top of the file might be less surprising, than stripping them completely

@rswrz
Copy link

rswrz commented Jan 21, 2025

I use tfsort in combination with tflint. Unfortunately, I noticed that tfsort removes comments that are used to ignore specific TFLint rules. For example:

# tflint-ignore: terraform_naming_convention
output "EXAMPLE" {
  value       = "example"
  description = "example"
}

It would be great if tfsort could preserve such comments directly above outputs/variables and ensure they are moved along with the respective blocks during sorting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants