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

Completions for variables include the var. (unless you don't type the dot) #1746

Open robbruce opened 2 months ago

robbruce commented 2 months ago

Extension Version

v2.30.1

VS Code Version

Version: 1.87.2 (Universal) Commit: 863d2581ecda6849923a2118d93a088b0745d9d6 Date: 2024-03-08T15:21:31.043Z (1 mo ago) Electron: 27.3.2 ElectronBuildId: 26836302 Chromium: 118.0.5993.159 Node.js: 18.17.1 V8: 11.8.172.18-electron.0 OS: Darwin arm64 23.1.0

Operating System

macOS Sonoma Version 14.1.1

Terraform Version

Terraform v1.8.1 on darwin_arm64

Steps to Reproduce

  1. Copy terraform code
  2. Type my_v the after var., wait for auto complete
  3. Press enter

Expected Behavior

Within a for loop, when you type var.my_var, it completes to var.var.my_var

Actual Behavior

Within a for loop, when you type var.my_var, it should complete to var.my_var

Terraform Configuration

variable "my_var" {
  type = map(string)
}

locals {
  test = { for key, value in var.}
}

Project Structure

No response

Gist

No response

Anything Else?

No response

Workarounds

No response

References

No response

Help Wanted

Community Note

robbruce commented 2 months ago

Continuation of https://github.com/hashicorp/vscode-terraform/issues/1624, but that issue was closed

robbruce commented 2 months ago

terraform-bug

dbanck commented 2 months ago

Thanks for the report @robbruce! I'm able to reproduce the bug.

I want to treat this as a separate issue from #1624, because we're in the context of a for expression. Depending on the type of expression (e.g. list, tuple, for, ...) the HCL parser behaves differently when it encounters incomplete configuration. Sometimes we can recover enough information to provide meaningful and correct completions, and sometimes the parser is thrown off by the impartial expression and we end up with a bug like this.

A fix for this will require upstream changes to the HCL parser. https://github.com/hashicorp/hcl-lang/issues/370 has some more context on this issue.