References are no longer detected when using complex types

Consider a module with 2 inputs:

variable "host" {
  type = string
}
variable "user" {
  type = string
}

When I currently view this declaration in VS Code, it will indicate the references through a CodeLens:
image

Now when I refactor this code into a single object instead:

variable "connection" {
  type = object({
    host = string
    user = string
  })
}

Now when I change my code to use var.connection.host and var.connection.user, no CodeLens appears. The “Go to References” feature also no longer works.

Is this a bug? Is my syntax off? Something else?

Hi @oliversalzburg
This looks like a bug or missing feature to me.

I was able to reproduce it using the following snippet:

variable "connection" {
  type = object({
    host = string
    user = string
  })
}

output "name" {
  value = var.connection.host
}

Not only I would expect the reference count code lens to be present, but also completion to provide host and user after var.connection., which also doesn’t seem to be happening.

I have just created https://github.com/hashicorp/terraform-ls/issues/653 to track this.

1 Like

Thanks a lot. I’ll follow that ticket.

Also looks like using certain default values you lose the reference. So far I have noticed true, false, and null cause the reference to be removed. Although after further playing this seems sporadic. Sometimes it works and sometimes it doesn’t.

In this case it works:

variable "root_volume_ebs_optimized" {
  description = "If true, the launched EC2 instance will be EBS-optimized."
  default     = false
}

In this case it doesn’t:

variable "enable_auto_unseal" {
  description = "Enable auto unseal of the Vault cluster"
  default     = false
}

@mmowatt The references currently strictly rely on types of values or variables, and so if the place where you are referencing either one of your “bool” variables doesn’t explicitly accept “bool” then it currently won’t be recognized as a reference by the language server.

We generally recommend types to be aligned whenever possible, even if some types are automatically converted. It makes the resulting config easier to read and follow for everyone.

However we recognize there may be some cases where this isn’t possible and we do have plans to reflect convertible types to better align the LS with Terraform’s own internal behaviour. These efforts are tracked under Reflect convertible types in type comparison · Issue #583 · hashicorp/terraform-ls · GitHub

You are welcomed to subscribe to that issue, upvote it or comment there if you have anything relevant to say on that topic.

@radeksimko Thanks for the explanation, and definitely this applies to my situation.