Skip to content

fix(terraform): normalize lock file constraint order after provider update#44530

Open
lindeskar wants to merge 1 commit into
renovatebot:mainfrom
lindeskar:fix/terraform-normalize-lock-constraints
Open

fix(terraform): normalize lock file constraint order after provider update#44530
lindeskar wants to merge 1 commit into
renovatebot:mainfrom
lindeskar:fix/terraform-normalize-lock-constraints

Conversation

@lindeskar

Copy link
Copy Markdown

Changes

Terraform and OpenTofu require the constraints field in .terraform.lock.hcl to be written in normalized form. When Renovate updates a provider version, getNewConstraint replaces the version in place but does not re-sort the sub-constraints.

When a provider's constraint combines an exact pin (from required_providers) with a ~> range inherited from a module, bumping the pin can push it past the range boundary — e.g. 5.0.0, ~> 5.0 becomes 5.9.0, ~> 5.0. That is no longer normalized, so terraform/tofu init fails:

The recorded version constraints for provider registry.terraform.io/hashicorp/vault must be written in normalized form: "~> 5.0, 5.9.0".

This adds sortConstraints, which reorders the sub-constraints into Terraform's canonical order — boundary version ascending, ties broken by operator priority — mirroring getproviders.VersionConstraintsString in Terraform. It only reorders existing tokens (it never rewrites operators or version precision) and returns the string untouched if any token cannot be parsed, so it cannot corrupt a valid constraint. It is applied at the two in-place replacement branches of getNewConstraint.

Context

Please select one of the following:

AI assistance disclosure

Did you use AI tools to create any part of this pull request?

  • No — I did not use AI for this contribution.
  • Yes — minimal assistance (e.g., IDE autocomplete, small code completions, grammar fixes).
  • Yes — substantive assistance (AI-generated non‑trivial portions of code, tests, or documentation).
  • Yes — other (please describe):

This PR was written primarily by Claude Code (model: Claude Opus 4.8) — the code, tests, and this description. The approach was directed and reviewed by me, including cross-checking the sort order against Terraform's getproviders.VersionConstraintsString.

Documentation (please check one with an [x])

  • I have updated the documentation, or
  • No documentation update is required

How I've tested my work (please select one)

I have verified these changes via:

  • Code inspection only, or
  • Newly added/modified unit tests, or
  • No unit tests, but ran on a real repository, or
  • Both unit tests + ran on a real repository

The public repository: N/A (unit tests only; not yet run against a live repository)

@cla-assistant

cla-assistant Bot commented Jul 13, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

…pdate

Terraform and OpenTofu reject a .terraform.lock.hcl whose constraints are
not in normalized form. When a provider version is updated in place, a bumped
exact pin can overtake an inherited `~>` range, leaving e.g.
"5.9.0, ~> 5.0" which fails "init" with "must be written in normalized form".
Re-sort the sub-constraints (boundary version ascending, ties broken by
operator priority) to match Terraform's canonical serialization.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@lindeskar lindeskar force-pushed the fix/terraform-normalize-lock-constraints branch from bc807fc to abcb77c Compare July 13, 2026 14:39
@lindeskar lindeskar marked this pull request as ready for review July 13, 2026 20:17
@github-actions github-actions Bot requested a review from viceice July 13, 2026 20:17
@lindeskar

Copy link
Copy Markdown
Author

Hi maintainers,

Sorry I had to force-push once to change the commit message.

Let me know if there's anything missing to get this change through!

/Alex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Updated constraints in Terraform lock file have to be normalized

1 participant