-
Notifications
You must be signed in to change notification settings - Fork 182
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
locals throwing a declaration error after updating to v2.28.0 and v2.28.1 #1586
Comments
looks like the workaround is to disable enhanced validation instead of downgrading according to the post here:
But that doesn't solve this bug with the Enhanced validation and it not being able to validate set of objects, lists etc. |
Thank you for the report, I was able to reproduce it. I suspect this is similar to hashicorp/hcl-lang#330 I will look into it. |
It turns out this is after all a little different to hashicorp/hcl-lang#330 For added clarity - As far as I can tell, it is an edge case involving mismatch between an The configuration in the example is - type-wise speaking - a little unusual in that That said I cannot confidently say this would be unusual from usage perspective. For various reasons, many users and configurations use types in one unexpected way or another. 😅 You can also verify this by converting the types more explicitly:
Also as shown above, you only get away with the conversion in the first local because the primitive types (string and number) happen to be convertible between each other. Boolean or tuple cannot be converted into string or number. It's not entirely clear to me how Terraform (CLI) internally deals with such a case. I suspect there's some further special handling of |
After looking through the codebase, I can tell that objects are still considered valid types for The under-documentation is somewhat intentional. The support for objects is primarily there for historical/backwards-compatible reasons. The problem with iterating over an object is that you cannot be sure about the type (and validity) of For most (not necessarily 100%) cases there are better solutions which do not involve objects. For example, you can iterate over the keys only: locals {
testing = {
one = {
name = "one"
number = 1
},
two = {
description = "two"
number = 2
}
}
testing_with_list = {
one = {
name = "one"
number = 1
},
two = {
description = "two"
number = 2
list = ["one", "two", "three"]
}
}
}
resource "null_resource" "declaration_working_as_expected" {
for_each = keys(local.testing)
}
resource "null_resource" "declaration_not_working_as_expected" {
for_each = keys(local.testing_with_list)
} It is still unusual to even begin with objects like that if you only need to be looping over the keys (object attribute names), but at least this prevents you from making the mistake of accessing |
A PR fixing the issue is now pending review: hashicorp/hcl-lang#333 Once that is merged and released, you should no longer see the diagnostic error. However, I would still advise you to review the choices of types and data structures in that example, for the reasons mentioned. |
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. |
Extension Version
v2.28.1
VS Code Version
Version: 1.83.0 (user setup)
Commit: e7e037083ff4455cf320e344325dacb480062c3c
Date: 2023-10-03T16:12:16.321Z
Electron: 25.8.4
ElectronBuildId: 24154031
Chromium: 114.0.5735.289
Node.js: 18.15.0
V8: 11.4.183.29-electron.0
OS: Windows_NT x64 10.0.22621
Operating System
Windows 11 22H2 Enterprise Version
Terraform Version
Terraform v1.6.0 on windows_amd64
Steps to Reproduce
locals.tf
filelocals.tf
terraform.tf
fileterraform.tf
When having a list inside the object, or even set of objects you get the error
No declaration found for "local.testing_with_list" Terraform
Expected Behavior
Expected Behavior:
![image](https://private-user-images.githubusercontent.com/26624010/274138970-b96e5369-ea16-4e6d-81c4-4c67fc127183.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkwMzAwMDYsIm5iZiI6MTczOTAyOTcwNiwicGF0aCI6Ii8yNjYyNDAxMC8yNzQxMzg5NzAtYjk2ZTUzNjktZWExNi00ZTZkLTgxYzQtNGM2N2ZjMTI3MTgzLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMDglMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjA4VDE1NDgyNlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWY5MmQ2MjRlYjJjYTJjNzA5ZmQyYzM5N2ZjOWRjZmQwODhmZDg0NGYwZDg1OWIyNWIzZTIzY2Q5ODQwZWMzM2ImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.kyqxK_Uqr7bXWiJ8wK9j9wD6fiH7MYEEfT1LPA1BLV4)
Actual Behavior
Terraform Configuration
locals.tf
terraform.tf
Project Structure
. ├── terraform │ ├── locals.tf │ └── terraform.tf
Gist
No response
Anything Else?
No response
Workarounds
No response
References
Help Wanted
Community Note
The text was updated successfully, but these errors were encountered: