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

Variable Type Validation object() does not report location #27773

Closed
damon-atkins opened this issue Feb 15, 2021 · 2 comments · Fixed by #35465
Closed

Variable Type Validation object() does not report location #27773

damon-atkins opened this issue Feb 15, 2021 · 2 comments · Fixed by #35465
Labels
bug config explained a Terraform Core team member has described the root cause of this issue in code v0.13 Issues (primarily bugs) reported against v0.13 releases

Comments

@damon-atkins
Copy link

Terraform Version

Terraform v0.13.5

Terraform Configuration Files

variables.tf

variable foo {
   type = map(object({
       abc = bool
   }))

  default = {
    "memap" = {
       abc = "i am a string"
       iamme = "one two three"
    }
  }
}

Expected Behavior

The environment variable TF_VAR_foo does not contain a valid value for
variable "foo" -> "memap" -> "abc" : a bool is required.
Error: Invalid default value for variable

  on variables.tf line 8, in variable "foo":
   8:   default = {
   9:     "memap" = {
  10:        abc = "i am a string"
  11:        iamme = "one two three"
  12:     }
  13:   }

This default value is not compatible with the variable's type constraint: a
bool is required within "foo" -> "memap" -> "abc".

Actual Behavior

The environment variable TF_VAR_foo does not contain a valid value for
variable "foo": a bool is required.
Error: Invalid default value for variable

  on variables.tf line 8, in variable "foo":
   8:   default = {
   9:     "memap" = {
  10:        abc = "i am a string"
  11:        iamme = "one two three"
  12:     }
  13:   }

This default value is not compatible with the variable's type constraint: a
bool is required.

Steps to Reproduce

Please list the full steps required to reproduce the issue, for example:

  1. terraform init
  2. terraform apply
@damon-atkins damon-atkins added bug new new issue not yet triaged labels Feb 15, 2021
@damon-atkins damon-atkins changed the title Completed Type Validation object() does not report location Variable Type Validation object() does not report location Feb 15, 2021
@apparentlymart
Copy link
Contributor

Thanks for reporting this, @damon-atkins!

I believe cause of this bug is here:

diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid default value for variable",
Detail: fmt.Sprintf("This default value is not compatible with the variable's type constraint: %s.", err),
Subject: attr.Expr.Range().Ptr(),
})

Just returning err directly here produces only the error message and not the context, because the subsystem that is generating this particular error doesn't know how to write context in Terraform language syntax (it's at a lower layer of abstraction). However, Terraform has a helper function tfdiags.FormatError which knows how to add that extra context in a Terraform-language-like way, so I expect the solution here would be to replace the direct err with a call to tfdiags.FormatError(err), which should make the output look something like this:

Error: Invalid default value for variable

  on variables.tf line 8, in variable "foo":
   8:   default = {
   9:     "memap" = {
  10:        abc = "i am a string"
  11:        iamme = "one two three"
  12:     }
  13:   }

This default value is not compatible with the variable's type constraint:
.memap.abc: a bool is required.

@apparentlymart apparentlymart added config explained a Terraform Core team member has described the root cause of this issue in code and removed new new issue not yet triaged labels Feb 17, 2021
@apparentlymart apparentlymart added the v0.13 Issues (primarily bugs) reported against v0.13 releases label Apr 9, 2021
Copy link
Contributor

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug config explained a Terraform Core team member has described the root cause of this issue in code v0.13 Issues (primarily bugs) reported against v0.13 releases
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants