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

required_providers: warn on legacy version syntax, missing source #64

Merged
merged 3 commits into from
Mar 23, 2023

Conversation

bendrucker
Copy link
Member

@bendrucker bendrucker commented Jan 27, 2023

Tightens enforcement of required_providers:

  • Rejects the legacy string syntax from Terraform 0.12.25 and earlier in favor of the object syntax which includes a source
  • Enforces that source is defined with the object syntax

This is a breaking change, since previously these legacy syntaxes were accepted. It seems appropriate to focus on enforcement for modern TF versions and stop accommodating old/unsupported TF versions. While omitting a version constraint can lead to major issues down the road, omitting source is also a common hazard, albeit more of an immediate one.

https://developer.hashicorp.com/terraform/language/providers/requirements#source-addresses

If you omit the source argument..., Terraform uses an implied source address... This is a backward compatibility feature... in modules that require 0.13 or later, we recommend using explicit source addresses for all providers.

Closes #62

Copy link
Member

@wata727 wata727 left a comment

Choose a reason for hiding this comment

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

Thank you for working on this.

Agreed that we should warn against the legacy syntax and omitted sources, but I wonder if these should be enforced as a default for all users. Perhaps many users only use official providers such as AWS or Google, and explicitly declaring the source can simply feel redundant.

Nonetheless, since the behavior of omitting the source is clearly explained as a backward compatibility feature, the user should originally declare the source. The issue is that when updating TFLint, all users will have to manually fix every required_providers declaration by hand.

I believe that providing an auto-fix is the desired behavior when forcing such a fix for low-impact legacy syntax, as Terraform provided the 0.12upgrade subcommand. See terraform-linters/tflint#266

Another option might be to provide it as another rule or control the behavior with an option of this rule.

What do you think?

@bendrucker
Copy link
Member Author

I'd hesitate to split out too many individual rules, enforcing source and version are things we could make configuration options for this rule that users could disable. That at least allows for a quick way to globally revert to the prior behavior if they don't want to update.

Auto-fixing would be nice if we had the framework to offer it. Arguably it should make a network call to check that the given provider exists in the hashicorp namespace before writing anything out.

The cost of omitting source is that users who go to add a third-party provider don't recognize the need to specify it and get a lengthy error about hashicorp/<my-provider> not existing.

Copy link
Member

@wata727 wata727 left a comment

Choose a reason for hiding this comment

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

OK. Agreed with this change.

@bendrucker
Copy link
Member Author

I'll add config options for source and version before merging this so that users can optionally enforce one or the other. Setting source = false would revert to the previous behavior but version = false has concrete uses that aren't just preserving legacy behavior. In a test module you might want to have the latest version of every provider but still enforce the presence of source.

@bendrucker bendrucker requested a review from wata727 March 22, 2023 22:57
Copy link
Member

@wata727 wata727 left a comment

Choose a reason for hiding this comment

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

👍

@bendrucker bendrucker merged commit 9dfba73 into main Mar 23, 2023
@bendrucker bendrucker deleted the required-providers-legacy-syntax branch March 23, 2023 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

terraform_required_providers: reject legacy source-less version syntax
2 participants