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

Backport of configs: fix module for_each call bug into v1.2 #31095

Merged

Conversation

teamterraform
Copy link
Contributor

Backport

This PR is auto-generated from #31091 to be assessed for backporting due to the inclusion of the label 1.2-backport.

The below text is copied from the body of the original PR.


This fixes a bug introduced in #30639 in which initialising a module will fail if that module contains both a provider block and a module call using for_each.

This bug was introduced in https://github.com/hashicorp/terraform/pull/30639/files#diff-95e614048e1ba4ba7621e22ba67cfe5c8acd386e5540b8c92c267d2fd1b3d31eR31-R55 by removing a variable scoped to a for loop (var nope) and instead overwriting noProviderConfigRange. This meant that the error at https://github.com/hashicorp/terraform/pull/30639/files#diff-95e614048e1ba4ba7621e22ba67cfe5c8acd386e5540b8c92c267d2fd1b3d31eR181 would be triggered if the parent module contained a provider block, not the child.

Fixes #31081

Test case

There are no tests in code for this behaviour. Highly recommend adding them as follow up. I manually tested this using the example below, kindly provided by @knbnnate.

.
├── barren_child
│   └── main.tf
├── grandchild
│   └── main.tf
└── main.tf

main.tf

provider "azurerm" {
  features {
  }
}
terraform {
  backend "local" {
  }
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 2.99"
    }
  }
}


module "child" {
  source = "./barren_child"
}

barren_child/main.tf

provider "azurerm" {
  alias           = "other"
  subscription_id = var.some_other_sub
  features {
  }
}

module "grandchild" {
  for_each = toset(["bar","baz"])
  source = "../grandchild"
}

grandchild/main.tf

variable "text" {
  type = string
  default = "foo"
  description = "stuff"
}
resource "local_file" "foo" {
  filename = "/dev/null"
  content = jsonencode(var.text)
}

@teamterraform teamterraform force-pushed the backport/kmoe/module-is-incompatible-with-count/newly-artistic-lark branch from 91cad6a to 016ac58 Compare May 20, 2022 15:59
@teamterraform teamterraform requested a review from kmoe May 20, 2022 15:59
@kmoe kmoe merged commit 16bbe8b into v1.2 May 20, 2022
@kmoe kmoe deleted the backport/kmoe/module-is-incompatible-with-count/newly-artistic-lark branch May 20, 2022 16:13
@github-actions
Copy link

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

@github-actions
Copy link

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 Jun 20, 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.

None yet

2 participants