Skip to content

VS Code Extension go to definition seems to prioritize external module over a local definition #2135

@fong-andrew-j

Description

@fong-andrew-j

Extension Version

v2.37.2

VS Code Version

Version: 1.104.2 (Universal)
Commit: e3a5acfb517a443235981655413d566533107e92
Date: 2025-09-24T11:21:37.073Z
Electron: 37.3.1
ElectronBuildId: 12404162
Chromium: 138.0.7204.235
Node.js: 22.18.0
V8: 13.8.258.31-electron.0
OS: Darwin arm64 24.6.0

Operating System

macOS 15.7 (24G222)

Terraform Version

Terraform v1.0.11 on darwin_arm64

Steps to Reproduce

Bug Description

The HashiCorp Terraform VS Code extension's "Go to Definition" feature fails for variables used within module blocks that have external sources, while working correctly for the same variables in other contexts.

Reproduction Steps

  1. Open this directory in VS Code with the HashiCorp Terraform extension enabled

  2. Open main.tf

  3. Try "Go to Definition" (right-click → "Go to Definition" or F12) on:

    These should work:

    • var.test_env on line 11 (in locals block)
    • var.test_name on line 32 (in resource block)
    • var.test_region on line 42 (in data block)

    These will fail:

    • var.test_name on line 18 (in module block)
    • var.test_region on line 20 (in module block)
    • var.test_env on line 26 (in module block)

Root Cause Analysis

The language server appears to get confused by external module sources and tries to resolve variables in the external module context instead of the local scope. When the external module is not accessible (common with Git sources), variable resolution fails entirely instead of falling back to local variable definitions.

Files

  • main.tf - Demonstrates the bug with clear examples
  • variables.tf - Contains the variable definitions that should be found
  • versions.tf - Minimal Terraform configuration
  • README.md - This file

Expected Behavior

"Go to Definition" should work for all variable references (var.variable_name) regardless of the block context, taking you to the variable definition in variables.tf.

Actual Behavior

  • WORKS: Variables in locals, resource, and data blocks
  • FAILS: Variables in module blocks with external sources
  • Error: "no definition found" or "no reference origin found"

Terraform Configuration

# main.tf
# Minimal reproduction case for Terraform Language Server "Go to Definition" bug
#
# BUG DESCRIPTION:
# - "Go to Definition" works for variables in locals and resource blocks
# - "Go to Definition" FAILS for variables in module blocks with external sources
# - Error: "no reference origin found" or "no definition found"

# ✅ WORKS: Variables in locals block
locals {
  # Try "Go to Definition" on var.test_env - should work
  domain = var.test_env == "prod" ? "prod.example.com" : "dev.example.com"
}

# ❌ FAILS: Variables in module block with external source
module "external_module" {
  # The external source causes the language server to get confused
  source = "git::https://github.com/terraform-aws-modules/terraform-aws-vpc.git?ref=v5.0.0"

  # Try "Go to Definition" on these variables - will fail with "no definition found"
  name = var.test_name    # <- BUG: Go to Definition fails here
  cidr = "10.0.0.0/16"

  azs             = ["${var.test_region}a", "${var.test_region}b"]  # <- BUG: Go to Definition fails here
  private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
  public_subnets  = ["10.0.101.0/24", "10.0.102.0/24"]

  enable_nat_gateway = true
  enable_vpn_gateway = false

  tags = {
    Environment = var.test_env  # <- BUG: Go to Definition fails here
  }
}

# ✅ WORKS: Variables in resource blocks
resource "aws_security_group" "test" {
  # Try "Go to Definition" on var.test_name - should work
  name   = "${var.test_name}-sg"
  vpc_id = module.external_module.vpc_id

  tags = {
    Name        = "${var.test_name}-security-group"
    Environment = var.test_env  # <- WORKS: Go to Definition works here
  }
}

# ✅ WORKS: Variables in data blocks
data "aws_availability_zones" "available" {
  state = "available"

  filter {
    name   = "region-name"
    values = [var.test_region]  # <- WORKS: Go to Definition works here
  }
}

# variables.tf
# Minimal reproduction case for Terraform Language Server bug
# Variables that should be accessible via "Go to Definition" in all contexts

variable "test_env" {
  type        = string
  description = "Test environment variable"
  default     = "dev"
}

variable "test_region" {
  type        = string
  description = "Test region variable"
  default     = "us-west-2"
}

variable "test_name" {
  type        = string
  description = "Test name variable"
  default     = "test-app"
}

# versions.tf
terraform {
  required_version = ">= 1.0"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

Project Structure

.
├── main.tf
├── README.md
├── variables.tf
└── versions.tf

Gist

No response

Anything Else?

If there was no variables file with the variable defined, and there was only the external module, I would understand not finding a definition. This behavior is very confusing to me, because even though the module source is external, the variables it uses are defined in the variables.tf and I would expect the extension to find the local file first before attempting to go to a file in the external module and failing if the module wasn't initialized or accessible.

Workarounds

Use manual navigation: Cmd+P → type variables.tfCmd+G → go to line number of the variable definition.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions