Repository: opentofu/opentofu
File: internal/configs/resource.go (Go)

Code before:
	}
	if r.Alias != "" {
		return fmt.Sprintf("%s.%s", r.Name, r.Alias)
	}
	return r.Name
}

func (r *ProviderConfigRef) InstanceValidation(blockType string, isInstanced bool, hasConfig bool) hcl.Diagnostics {
	var diags hcl.Diagnostics

	summary := fmt.Sprintf("Invalid %s provider configuration", blockType)

	if r.KeyExpression != nil {
		if r.Alias == "" {
			diags = append(diags, &hcl.Diagnostic{
				Severity: hcl.DiagError,
				Summary:  summary,
				Detail:   "An instance key can be specified only for a provider configuration which has an alias and uses for_each.",
				Subject:  r.KeyExpression.Range().Ptr(),
			})
		} else if !isInstanced {
			if !hasConfig {
				diags = append(diags, &hcl.Diagnostic{
					Severity: hcl.DiagError,
					Summary:  summary,
					Detail:   fmt.Sprintf("This module doesn't declare a provider %q block with alias = %q, which is required for use with for_each", r.Name, r.Alias),
					Subject:  r.NameRange.Ptr(),
				})
			} else {
				diags = append(diags, &hcl.Diagnostic{
					Severity: hcl.DiagError,
					Summary:  summary,
					Detail:   "An instance key can be specified only for a provider configuration that uses for_each.",
					Subject:  r.KeyExpression.Range().Ptr(),
				})

			}
		}
	} else if isInstanced {
		diags = append(diags, &hcl.Diagnostic{
			Severity: hcl.DiagError,
			Summary:  summary,
			Detail:   "A reference to a provider configuration which uses for_each requires an instance key. Passing a collection of provider instances into a child module is not allowed.",
			Subject:  r.NameRange.Ptr(),
		})
	}
	return diags
}

var commonResourceAttributes = []hcl.AttributeSchema{
	{

Review comment (bug):
To me the `for_each` part of this at the end is largely irrelevant: it's not valid to refer to an alternative provider configuration that isn't declared _regardless of whether `for_each` is used or not_ so I think mentioning it is likely to cause more confusion than it saves.

As things are currently structured it's true that we'll only be reporting the problem this way when there's an instance key specified, but that's just an implementation detail of how this validation is implemented and not relevant to the end-user.

(Folks reading error messages tend to think that everything they read is directly related to the problem at hand, so including extraneous details tends to cause people to try to solve the wrong problem.)


Code after:
	}
	if r.Alias != "" {
		return fmt.Sprintf("%s.%s", r.Name, r.Alias)
	}
	return r.Name
}

func (r *ProviderConfigRef) InstanceValidation(blockType string, isInstanced bool, hasConfig bool) hcl.Diagnostics {
	var diags hcl.Diagnostics

	summary := fmt.Sprintf("Invalid %s provider configuration", blockType)

	if r.KeyExpression != nil {
		if r.Alias == "" {
			diags = append(diags, &hcl.Diagnostic{
				Severity: hcl.DiagError,
				Summary:  summary,
				Detail:   "An instance key can be specified only for a provider configuration which has an alias and uses for_each.",
				Subject:  r.KeyExpression.Range().Ptr(),
			})
		} else if !isInstanced {
			if !hasConfig {
				diags = append(diags, &hcl.Diagnostic{
					Severity: hcl.DiagError,
					Summary:  summary,
					Detail:   fmt.Sprintf("This module doesn't declare a provider %q block with alias = %q, which is required for use with for_each.", r.Name, r.Alias),
					Subject:  r.NameRange.Ptr(),
				})
			} else {
				diags = append(diags, &hcl.Diagnostic{
					Severity: hcl.DiagError,
					Summary:  summary,
					Detail:   "An instance key can be specified only for a provider configuration that uses for_each.",
					Subject:  r.KeyExpression.Range().Ptr(),
				})

			}
		}
	} else if isInstanced {
		diags = append(diags, &hcl.Diagnostic{
			Severity: hcl.DiagError,
			Summary:  summary,
			Detail:   "A reference to a provider configuration which uses for_each requires an instance key. Passing a collection of provider instances into a child module is not allowed.",
			Subject:  r.NameRange.Ptr(),
		})
	}
	return diags
}

var commonResourceAttributes = []hcl.AttributeSchema{
	{