Repository: hashicorp/terraform (Go)

Issue:
panic when resource is associated with aliased provider configuration
### Terraform Version

```
> terraform_git -v
Terraform v0.13.0-dev
commit 866449a78a0c50efc2c3b546cb1a4b7e117a0502
```

### Terraform Configuration Files

Take the configuration in the repo:

https://github.com/adamhorden/terraform_bug_001

I have had to strip this down from a large project to a reproducible test case. I added some notes and added the stack trace to the repo. This should be enough to run and produce the error. 

The directory fail shows an `alias` added to a provider. This causes a panic:

**panic: dots are not allowed**

The directory work shows an `alias` added to a provider as above but commented out. This does not cause a stack trace. `kms_key_id` seems to be dependant on making this fail when an `alias` is added to a provider.

I am testing nightly with a build from git for Terraform v0.13-dev.

I decided this required a bug report at this stage of the development cycle. I had a working development infrastructure from a previous git commit. This panic has snuck in but not sure which commit has caused it.

I have dug deep into the code.

I have managed to get a rough idea of the commit to help debugging:

https://github.com/hashicorp/terraform/commit/86f0b5191c9083569e75f572eefd0920f497dab1

This was working with Terraform v0.12 and early on with v0.13.


Discussion:
@adamhorden thank you! This is an incredibly detailed issue report. I really appreciate you trying out 0.13 in master even before our beta is out - super cool.
Thanks for reporting this, @adamhorden!

After some initial study it looks like the bug is here:

https://github.com/hashicorp/terraform/blob/7f91090c5c96e680cc5ea38a643a7b332f994cc8/configs/config.go#L225-L226

`c.Module.ProviderConfigs` is a map from strings like `aws.he_aws_tf_app_rgn_eu` to objects representing `provider` blocks in the configuration. It looks like this code is intending to use just the `aws` portion, which is available in `provider.Name` in that loop.

We'll see about getting this fixed before the final release. Thanks again!

@danieldreier when we know there is a major release of Terraform coming up we light up new pipelines for that version. Before the release these use a `git clone`. This allows us to move fast for testing when the official release happens. It would be a case of switch the pipeline from a `git checkout` to a `curl` of the release. Hence why I have a rolling nightly test pipe line at the moment 🤓 .

I like my issue reports to have as much information and a test case if possible.

@apparentlymart thanks for explaining the code to me. Trying to understand more the internals of Terraform. Been using Terraform now for the last 2.5 years full time so decided it was about time I delve into the internals even if it is a learning exercise 😬.

Thanks for such a quick fix and for all of the hard work that has gone into Terraform v0.13 😬 .


I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.




Patch:
diff --git a/configs/config.go b/configs/config.go
index 39a0b717d413..a99e90cadc8a 100644
--- a/configs/config.go
+++ b/configs/config.go
@@ -222,8 +222,8 @@ func (c *Config) addProviderRequirements(reqs getproviders.Requirements) hcl.Dia
 	}
 
 	// "provider" block can also contain version constraints
-	for name, provider := range c.Module.ProviderConfigs {
-		fqn := c.Module.ProviderForLocalConfig(addrs.LocalProviderConfig{LocalName: name})
+	for _, provider := range c.Module.ProviderConfigs {
+		fqn := c.Module.ProviderForLocalConfig(addrs.LocalProviderConfig{LocalName: provider.Name})
 		if _, ok := reqs[fqn]; !ok {
 			// We'll at least have an unconstrained dependency then, but might
 			// add to this in the loop below.


Test patch:
diff --git a/configs/config_test.go b/configs/config_test.go
index e33e236da401..b2cefe751e42 100644
--- a/configs/config_test.go
+++ b/configs/config_test.go
@@ -162,3 +162,14 @@ func TestConfigProviderForConfigAddr(t *testing.T) {
 		t.Errorf("wrong result\ngot:  %s\nwant: %s", got, want)
 	}
 }
+
+func TestConfigAddProviderRequirements(t *testing.T) {
+	cfg, diags := testModuleConfigFromFile("testdata/valid-files/providers-explicit-implied.tf")
+	assertNoDiagnostics(t, diags)
+
+	reqs := getproviders.Requirements{
+		addrs.NewDefaultProvider("null"): nil,
+	}
+	diags = cfg.addProviderRequirements(reqs)
+	assertNoDiagnostics(t, diags)
+}