From 94aa1d8d66f28441fbf8e3273eefcc5554b9b817 Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Thu, 14 Apr 2022 16:25:53 +0200 Subject: [PATCH] Fix module calls command for remote modules (#872) * use HasPrefix instead of ParseRawProviderSourceString to check for remote module sources * strip registry prefix for docs links * account for older terraform versions * new test case for terraform 1.1.0 module sources --- .../handlers/command/module_calls.go | 3 +- .../handlers/command/module_calls_test.go | 39 +++++++++++++++++++ internal/terraform/datadir/module_types.go | 4 +- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/internal/langserver/handlers/command/module_calls.go b/internal/langserver/handlers/command/module_calls.go index a1e5c029e..df251a25d 100644 --- a/internal/langserver/handlers/command/module_calls.go +++ b/internal/langserver/handlers/command/module_calls.go @@ -129,5 +129,6 @@ func getModuleDocumentationLink(record datadir.ModuleRecord) string { return "" } - return fmt.Sprintf(`https://registry.terraform.io/modules/%s/%s`, record.SourceAddr, record.VersionStr) + shortName := strings.TrimPrefix(record.SourceAddr, "registry.terraform.io/") + return fmt.Sprintf(`https://registry.terraform.io/modules/%s/%s`, shortName, record.VersionStr) } diff --git a/internal/langserver/handlers/command/module_calls_test.go b/internal/langserver/handlers/command/module_calls_test.go index 05ced5615..1469ba8b3 100644 --- a/internal/langserver/handlers/command/module_calls_test.go +++ b/internal/langserver/handlers/command/module_calls_test.go @@ -87,3 +87,42 @@ func Test_parseModuleRecords(t *testing.T) { }) } } + +// With the release of Terraform 1.1.0 module source addresses are now stored normalized +func Test_parseModuleRecords_v1_1(t *testing.T) { + tests := []struct { + name string + records []datadir.ModuleRecord + want []moduleCall + }{ + { + name: "detects terraform module types", + records: []datadir.ModuleRecord{ + { + Key: "ec2_instances", + SourceAddr: "registry.terraform.io/terraform-aws-modules/ec2-instance/aws", + VersionStr: "2.12.0", + Dir: ".terraform\\modules\\ec2_instances", + }, + }, + want: []moduleCall{ + { + Name: "ec2_instances", + SourceAddr: "registry.terraform.io/terraform-aws-modules/ec2-instance/aws", + Version: "2.12.0", + SourceType: "tfregistry", + DocsLink: "https://registry.terraform.io/modules/terraform-aws-modules/ec2-instance/aws/2.12.0", + DependentModules: []moduleCall{}, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := parseModuleRecords(tt.records) + if diff := cmp.Diff(tt.want, got); diff != "" { + t.Fatalf("module mismatch: %s", diff) + } + }) + } +} diff --git a/internal/terraform/datadir/module_types.go b/internal/terraform/datadir/module_types.go index 68a1111c7..c0e95b3e1 100644 --- a/internal/terraform/datadir/module_types.go +++ b/internal/terraform/datadir/module_types.go @@ -34,7 +34,9 @@ func (r *ModuleRecord) GetModuleType() ModuleType { // the authorative source: hashicorp/terraform/internal/addrs/module_source.go. // However this works enough for now to identify module types for display in vscode-terraform. // Example: terraform-aws-modules/ec2-instance/aws - if _, err := tfregistry.ParseRawProviderSourceString(r.SourceAddr); err == nil { + _, err := tfregistry.ParseRawProviderSourceString(r.SourceAddr) + // Example: registry.terraform.io/terraform-aws-modules/vpc/aws + if err == nil || strings.HasPrefix(r.SourceAddr, "registry.terraform.io/") { return TFREGISTRY }