hashicorp / terraform-ls

Terraform Language Server
Mozilla Public License 2.0
970 stars 132 forks source link

Terraform Language Server doesn't recognize variables defined in other file. #1729

Open crazyoptimist opened 1 month ago

crazyoptimist commented 1 month ago

Language Server Version

0.33.1

Terraform Version

Terraform v1.8.4 on windows_amd64

Client Version

Vim (coc.nvim) 9.1

Terraform Configuration

terraform {
  backend "azurerm" {
    storage_account_name = "redacted"
    container_name       = "redacted"
    key                  = "redacted/terraform.tfstate"
    use_azuread_auth     = true
  }

  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.106.1"
    }
  }
  required_version = ">= 1.5"
}

provider "azurerm" {
  features {}
}

locals {
  location = "westus"
}

resource "azurerm_resource_group" "main" {
  # Here! It complains: `No declaration found for "var.namespace" (Terraform)`
  name     = "${var.namespace}-tfbackend-test"
  location = local.location
}

Steps to Reproduce

  1. Create a very simple terraform project as shown above, you can even skip the remote backend configuration.
  2. Create variables.tf and define a variable namespace of string type.
  3. Run terraform init and terraform apply -auto-approve
  4. It runs successfully, but terraform-ls complains that the variable is not defined.

Expected Behavior

It should not complain because it's not a bug at all.

Actual Behavior

terraform-ls complains No declaration found for "var.namespace" (Terraform)

It shows up as an error.

Gist

No response

Workarounds

No workarounds I can find.

References

No response

Help Wanted

Community Note

dbanck commented 1 month ago

Hi @crazyoptimist! Thanks for the report. Unfortunately I can't reproduce the bug.

Can you please share the full language server log output? Does this bug occur all the time when you open the file?

crazyoptimist commented 1 month ago

image

It's that simple example. And here's my variables.tf:

variable "namespace" {
  description = "Project naming prefix"
  type        = string
  default     = "dev"
}

I think above things are better than language server log, and I don't know how I can grab the server log yet. I use terraform-ls.exe on windows, with vim 9.1 and coc.nvim.

Thank you!

crazyoptimist commented 1 month ago

An interesting thing is that if I open the variables.tf on a vim's split window, the error goes away. Afterwards, even if I close the variables.tf buffer, the error doesn't appear again. The error is reproduced when I shutdown vim and reopen the file. Even if I open both file buffers, the error persists unless I open them on the same screen using split windows (using :split command).

dbanck commented 3 weeks ago

Interesting find, thank you.

You can write the language server log to a file by passing an additional argument in your coc.vim configuration: https://github.com/hashicorp/terraform-ls/blob/main/docs/TROUBLESHOOTING.md#logging-to-files

crazyoptimist commented 3 weeks ago

terraform-ls.log

Attached is a minimal log file. I believe you can ignore Error: Backend initialization required, please run "terraform init" , because I originally found it when I ran it. Now I've moved to another machine and can't find the same error on Mac/Linux.

Thank you for your guidance on logging!