hashicorp / vscode-terraform

HashiCorp Terraform VSCode extension
https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform
Mozilla Public License 2.0
919 stars 178 forks source link

Initialised module from GitHub not being recognised #1440

Closed radeksimko closed 1 month ago

radeksimko commented 1 year ago

I'm getting an error after terraform init on v2.26.1 of the plugin (terraform-ls 0.31.2)

With just this HCL:

module "eks_blueprints" {
  source          = "github.com/aws-ia/terraform-aws-eks-blueprints?ref=v4.29.0"
  cluster_version = "1.25"
}
2023/05/21 18:31:15 hover.go:35: Looking for hover data at "main.tf" -> hcl.Pos{Line:3, Column:11, Byte:117}
2023/05/21 18:31:15 hover.go:37: received hover data: (*lang.HoverData)(nil)
2023/05/21 18:31:15 rpc_logger.go:48: Error for "textDocument/hover" (ID 28): [-32098] main.tf (3,11): unknown attribute "cluster_version"
2023/05/21 18:31:15 opts.go:203: Completed 1 requests [2.231417ms elapsed]
[Error - 6:31:15 PM] Request textDocument/hover failed.
  Message: main.tf (3,11): unknown attribute "cluster_version"
  Code: -32098 

But I can see the variable in the locally-downloaded copy as expected:

❯ grep -E 'variable.*cluster_version' .terraform/modules/eks_blueprints/*.tf
.terraform/modules/eks_blueprints/variables.tf:variable "cluster_version" {

Originally posted by @matschaffer-roblox in https://github.com/hashicorp/vscode-terraform/issues/1181#issuecomment-1556381720

radeksimko commented 1 year ago

I suspect this may have something to do with how we compare the modules.json source address with the source address from the configuration:

i.e. it's likely that we fail to treat github.com/aws-ia/terraform-aws-eks-blueprints?ref=v4.29.0 as equal to git::https://github.com/aws-ia/terraform-aws-eks-blueprints.git?ref=v4.29.0.

This probably concerns the following code paths:

I'm not sure if this affects other sources, i.e. whether other sources get encoded under different "canonical" address like GitHub does. If not, we may just get away with an extra edge case for GitHub.

matschaffer-roblox commented 1 year ago

Thanks! I can confirm that for my terraform, git::https://github.com is a viable workaround.

IanMoroney commented 1 year ago

It's also worth pointing out that the official documentation on how to source from github has the address format of github.com/... https://developer.hashicorp.com/terraform/language/modules/sources#github

I found this issue because i'm having the same problem, and my url format is github.com/org/repo... and not git::https://github.com...

IanMoroney commented 1 year ago

And this is the error from my vscode:

2023-06-28 10:01:52.375 [error] main.tf (15,13): unknown attribute "status": Error: main.tf (15,13): unknown attribute "status"
    at c:\Users\user\.vscode\extensions\hashicorp.terraform-2.26.1-win32-x64\out\extension.js:1:387194
    at re (c:\Users\user\.vscode\extensions\hashicorp.terraform-2.26.1-win32-x64\out\extension.js:1:387488)
    at c:\Users\user\.vscode\extensions\hashicorp.terraform-2.26.1-win32-x64\out\extension.js:1:382272
    at Immediate.<anonymous> (c:\Users\user\.vscode\extensions\hashicorp.terraform-2.26.1-win32-x64\out\extension.js:1:382292)
    at processImmediate (node:internal/timers:466:21)
IanMoroney commented 1 year ago

Looking at the terraform init, regardless of what format we put the source in, it always does: Downloading git::https://github.com... modules.json always has "Source":"git::https://github.com If there is a comparator in place between the source address from modules.json and the source address from the configuration, can that be changed so that modules.json becomes the source of truth instead?

Changing the format for the source in main.tf from source = "github.com/ to source = "git::https://github.com/ caused the autocomplete to start working, just as Mat Schaffer pointed out above. If modules.json already has this source address format set, couldn't we just use that as the source address so autocomplete works properly?

ArneRiemann4711 commented 1 year ago

Are there any updates on this? If i include modules like this

module "globals" {
  source = "git@github.com:orga/repo.git//tf_modules//globals"
}

module "this" { 
 account_id = module.globals... # NO AUTOCOMPLETE
}

it wont work, Intellij works just fine (-:

shuan9 commented 11 months ago

I am also having similar problem where I am not able to use "Go to definition" on the variables within the module:

module "my_eks" {
  source = "github.com/aws-ia/terraform-aws-eks-blueprints?ref=v4.32.1"
...
}

the versions:

❯ ~/.vscode/extensions/hashicorp.terraform-2.28.2-darwin-arm64/bin/terraform-ls --version                   
0.32.2
ArneRiemann4711 commented 7 months ago

Any news on this? Would love to swítch from idea to vscode.

TrongBYM commented 6 months ago

Wondering the same thing, is this issue in any way prioritized? 😕

aspectum commented 1 month ago

I see https://github.com/hashicorp/terraform-ls/pull/1760 is supposed to have solved this, but it didn't work for me, though my use case might be a bit different.

We have an iac repo with a lot of tf modules with default parameters and tags, and we reference it with a, for instance, source = "git::https://dev.azure.com/<org>/<project>/_git/<repo-name>//modules/aws/sns/topic_subscription".

terraform init, terraform plan and terraform apply work fine and are able to resolve the modules, but neither the hovering nor the autocomplete from this extension work.

For locally declared modules, everything works fine, including modules referencing these problematic modules.

dbanck commented 1 month ago

@aspectum interesting discovery! I'm not able to reproduce your issue with a module from the public registry, like

module "alias_no_refresh" {
  source = "git::https://github.com/terraform-aws-modules/terraform-aws-lambda.git//modules/alias"

  refresh_alias = false
  name = "current-no-refresh"
}

Do you mind opening a separate issue, so we can dig into this problem?

aspectum commented 1 month ago

@aspectum interesting discovery! I'm not able to reproduce your issue with a module from the public registry, like

module "alias_no_refresh" {
  source = "git::https://github.com/terraform-aws-modules/terraform-aws-lambda.git//modules/alias"

  refresh_alias = false
  name = "current-no-refresh"
}

Do you mind opening a separate issue, so we can dig into this problem?

This snippet also fails in my company laptop, but in my personal machine it works. Right now all the versions of everything are the same (terrafom, vscode and the extension). The most obvious difference is the OS: my company laptop runs Windows while my personal runs Linux. I'll try to investigate this a bit more and if I do find more evidence I'll open an issue.

dbanck commented 1 month ago

We have just released a new version of vscode-terraform: 2.32.0 which adds support for private git repositories (for initialized environments). The update should show up automatically in VS Code.

I'm closing this issue as completed. In case you experience any problems, please do let us know.

github-actions[bot] commented 2 weeks ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.