Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task
crazyoptimist opened this issue Jun 6, 2024 · 7 comments
Open
1 task
Labels
bug Something isn't working

Comments

@crazyoptimist
Copy link

crazyoptimist commented Jun 6, 2024

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

  • 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
@crazyoptimist crazyoptimist added the bug Something isn't working label Jun 6, 2024
@dbanck
Copy link
Member

dbanck commented Jun 7, 2024

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
Copy link
Author

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
Copy link
Author

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
Copy link
Member

dbanck commented Jun 11, 2024

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
Copy link
Author

crazyoptimist commented Jun 12, 2024

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!

@windowsrefund
Copy link

FYI, I can't reproduce that issue on terraform-ls v0.34.2 and nvim v0.10.1 on GNU/Linux

@crazyoptimist
Copy link
Author

Thanks for investigation. It only happens on Windows. The terminal in my screenshot was Alacritty with Git bash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants