hashicorp / vscode-terraform

HashiCorp Terraform VSCode extension
https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform
Mozilla Public License 2.0
911 stars 180 forks source link

Syntax validation throwing incorrect errors for Terraform Test files (`*.tftest.hcl`) #1783

Closed novekm closed 1 week ago

novekm commented 2 weeks ago

Extension Version

v2.30.2

VS Code Version

Version: 1.90.0 (Universal) Commit: 89de5a8d4d6205e5b11647eb6a74844ca23d2573 Date: 2024-06-04T19:34:44.157Z Electron: 29.4.0 ElectronBuildId: 9593362 Chromium: 122.0.6261.156 Node.js: 20.9.0 V8: 12.2.281.27-electron.0 OS: Darwin arm64 23.5.0

Operating System

macOS Sonoma Version 14.5

Terraform Version

Terraform v1.8.2

Steps to Reproduce

  1. Create file ending in *.tftest.hcl
  2. Create a run block
  3. Receive validation error "Blocks of type "run" are not expected here. which is incorrect, as run blocks should be included in test files. So far I've been just ignoring it since it's an incorrect error, but it's a bit annoying and can make troubleshooting more difficult

Expected Behavior

Syntax validation works properly

Actual Behavior

Receive validation error "Blocks of type "run" are not expected here. which is incorrect, as run blocks should be included in test files. So far I've been just ignoring it since it's an incorrect error, but it's a bit annoying and can make troubleshooting more difficult. This issue has persisted for multiple Terraform versions and multiple macOS versions in my testing.

What's odd is that if you quit and re-open VSCode, there is no error until you make any changes in files. Then the incorrect errors appear again. Only way to temporarily resolve is to quit and re-open VSCode, but the issue will continue to reappear.

Terraform Configuration

run "mandatory_plan_basic" {
  command = plan
  module {
    source = "../examples/create-users-and-groups"
  }
}

run "mandatory_apply_basic" {
  command = apply
  module {
    source = "./examples/basic"
  }
}

Project Structure

├── CODEOWNERS
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── LICENSE
├── NOTICE.txt
├── README.md
├── VERSION
├── data.tf
├── examples
│   ├── basic
│   ├── create-users-and-groups
│   ├── existing-users-and-groups
│   ├── google-workspace
│   └── inline-policy
├── locals.tf
├── main.tf
├── outputs.tf
├── providers.tf
├── test
│   ├── go.mod
│   ├── go.sum
│   └── main_test.go
├── tests
│   └── 01_mandatory.tftest.hcl
├── tf.json
├── tf.plan
└── variables.tf

Gist

No response

Anything Else?

No response

Workarounds

If you quit and re-open VSCode, there is no error until you make any changes in files. Then the incorrect errors appear again. Only way to temporarily resolve is to quit and re-open VSCode, but the issue will continue to reappear.

References

No response

Help Wanted

Community Note

dbanck commented 2 weeks ago

Hi @novekm!

Sorry, the VS Code extension and language server don't support Terraform test files today. We are tracking this work in #1534.

By default, the extension ignores *.tftest.hcl files and doesn't send them to the language server, which seems to be the case here. This usually happens because you have manually assigned the Terraform language ID to the test files.

You can check (and change) the language ID in the bottom right corner. It should be HCL for *.tftest.hcl files. To get syntax highlighting for HCL files, you can install the vscode-hcl extension.

Hope this helps! Let me know if it solves your problem.

novekm commented 2 weeks ago

Thanks @dbanck! I've downloaded the extension you mentioned and have now manually changed the language ID to HCL for the test files, however still getting the following error: Unexpected block: Blocks of type "run" are not expected here Terraform. It seems to still be trying to use the Terraform extension in stead of the vscode-hcl extension, even though I've manually set HCL for language ID. Is there anything else I need to do?

dbanck commented 2 weeks ago

Can you please check if your VS Code configuration contains any manual file associations?


{
  "files.associations": {
    ...
  }
}
novekm commented 1 week ago

Hi @dbanck, it looks like this one is set:

  "files.associations": {
    "*.hcl": "terraform"
  },

I don't recall setting this manually. Should this be removed?

dbanck commented 1 week ago

Yes! This can be removed and should solve your issue

novekm commented 1 week ago

Thanks! I commented out those lines and it looks like the syntax validation errors are gone. I'll keep tracking #1534 to see when this is eventually added to Terraform. Thanks again!

dbanck commented 1 week ago

That's great to hear!

Closing this for now. Feel free to open a new issue if something else comes up.