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

  1. Open vscode
  2. Create a file named vpc.tf
  3. 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
  )

}
  1. 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

  1. 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

  1. The parameters that are highlighted as problems are all valid according to the Inputs section of the VPC module documentation. Also performing terraform validate on the command line confirms that the code is fine.

  2. 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.

Screenshot 2024-01-26 170007

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)

Most upvoted comments

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.

terraform {
  required_version = ">= 1.3"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">=5.40"
    }
  }
}

Hope this helps.

Thanks for all the reports! Unfortunately, I haven’t been able to reproduce the problem yet 😕

@TarekAS @aderuelle @snowzach Are you all running Windows? Can you share more info about your environment? Like OS, Terraform version, did you run terraform init? Maybe we can find a common denominator.

macOS for me (M1 chip), Terraform v1.7.3

Having the same error with an AWS resource

Hi @borrell, this sounds like a different issue. Would you mind creating a new one with more details?

I see the same issue with locals that are defined in a different .tf file

Hi @TinaHeiligers, this sounds like a different issue. Would you mind creating a new one with more details?

How is the code getting what attributes are expected with each resources? Because that can vary depending on the provider versions, which are different per project.

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.

Weird that it’s happening only on the aws vpc module, any idea why?

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/google module and the terraform-google-modules/network/google//modules/network-peering module. Vscode is attempting to validate the first one as though it was network-peering. When I remove my instance of network-peering from the file, then validation works for the first one

Note 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.

2024/02/27 14:36:57 jobs.go:461: JOBS: Finishing job "11": "OpTypeGetModuleDataFromRegistry" for {"file:///Users/dbanck/tmp/modules2"} (err = 1 error occurred:
	* Get "https://registry.terraform.io/v1/modules/terraform-aws-modules/eks/aws/versions": context deadline exceeded (Client.Timeout exceeded while awaiting headers)

, deferredJobs: [])

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 -upgrade with no help. Feel free to let me know anything else I can do to get you more debug info…