vscode-terraform: "Unexpected attribute" false alarms
Extension Version
v2.29.3
VS Code Version
Version: 1.85.2 (user setup) Commit: 8b3775030ed1a69b13e4f4c628c612102e30a681 Date: 2024-01-18T06:40:10.514Z Electron: 25.9.7 ElectronBuildId: 26354273 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
Edition Windows 11 Enterprise Version 22H2 Installed on 13/12/2023 OS build 22621.3007 Experience Windows Feature Experience Pack 1000.22681.1000.0
Terraform Version
Terraform v1.7.1 on windows_amd64 + provider registry.terraform.io/hashicorp/aws v5.32.1
Steps to Reproduce
- Open vscode
- Create a file named vpc.tf
- Configure as follows:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.5.1"
name = var.vpc_name
cidr = var.vpc_cidr
azs = local.azs
intra_subnets = local.intra_subnets
private_subnets = local.private_subnets
create_igw = false
enable_nat_gateway = false
enable_dns_hostnames = true
enable_dns_support = true
tags = merge(
var.default_tags
)
}
- Install and enable the official Hashicorp terraform for visual studio code.
Expected Behavior
I don’t expect the Hashicorp terraform extension to highlight the above code as having any ‘problems’.
Actual Behavior
- The Hashicorp Terraform extension incorrectly identifies many of the parameters supplied to the VPC module configuration as being unexpected e.g.
Unexpected attribute: An attribute named "name" is not expected here Terraform
-
The parameters that are highlighted as problems are all valid according to the Inputs section of the VPC module documentation. Also performing
terraform validateon the command line confirms that the code is fine. -
Finally - and this is difficult to reproduce as it’s not consistent - hovering over the highlighted problems in the code mostly shows the error:
Unexpected attribute: An attribute named "name" is not expected here Terraform.
However, occasionally it simultaneously display the ‘unexpected’ error as well as ‘usage’ message for the parameter it says should be there. See the attached image.
Terraform Configuration
No response
Project Structure
No response
Gist
No response
Anything Else?
The AWS toolkit extension was enable when I first encountered this issue. I disabled the AWS extension and reloaded - so no other extensions were enabled during my steps to reproduce for this report.
Workarounds
None.
References
No response
Help Wanted
- I’m interested in contributing a fix myself
Community Note
- Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
- Please do not leave “+1” or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
- If you are interested in working on this issue or have submitted a pull request, please leave a comment
About this issue
- Original URL
- State: open
- Created 5 months ago
- Reactions: 21
- Comments: 27 (3 by maintainers)
After this information I found the autodetect schema is assuming you are working in a single project. I have a folder with multiple projects and the modules folder. The extension reads the first folder with
.terraform/providers/registry.terraform.io/hashicorp/aws, in my case I have providers versions 3.75.2 and 4.27.0. The extension takes 4.27.0. However the module I`m working is for a project with version 5.40.0 that is in a later folder.If I specify the provider version in the module folder the false alarms disappear.
Hope this helps.
macOS for me (M1 chip), Terraform v1.7.3
AWS EKS module also.
Hi @borrell, this sounds like a different issue. Would you mind creating a new one with more details?
Hi @TinaHeiligers, this sounds like a different issue. Would you mind creating a new one with more details?
Hi @paul-civitas, we bundle the latest provider schema with the language server at build time to provide a better out-of-the-box experience. If a user has Terraform installed and has run
terraform init, we prefer the schema of the locally installed provider instead to ensure a better match. The schema contains all resources, datasources and their attributes.For modules, we can get the schema from the registry, which should always ensure a matching version. Here it looks like there is an edge case when fetching module schemas on an unstable connection.
I’m seeing this with “terraform-google-modules/network/google” as well. It seems like repos with both a top-level module and submodules are where it fails.
I have a file where I use both the
terraform-google-modules/network/googlemodule and theterraform-google-modules/network/google//modules/network-peeringmodule. Vscode is attempting to validate the first one as though it wasnetwork-peering. When I remove my instance ofnetwork-peeringfrom the file, then validation works for the first oneNote to self (or anyone else picking this up):
I was able to accidentally reproduce the problem while investigating something else. It looks like when the registry request fails with a timeout (other errors seem fine), we are making incorrect assumptions about the module schema.
Running VS Code on PopOS 22 and then Remote-SSH Plugin to Ubuntu 22.04.03 machine. Terraform v1.7.3 installed on that machine if it matters. Did try
terraform init -upgradewith no help. Feel free to let me know anything else I can do to get you more debug info…