Skip to content

Commit 94aa1d8

Browse files
authored
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
1 parent c30f329 commit 94aa1d8

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

internal/langserver/handlers/command/module_calls.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,6 @@ func getModuleDocumentationLink(record datadir.ModuleRecord) string {
129129
return ""
130130
}
131131

132-
return fmt.Sprintf(`https://registry.terraform.io/modules/%s/%s`, record.SourceAddr, record.VersionStr)
132+
shortName := strings.TrimPrefix(record.SourceAddr, "registry.terraform.io/")
133+
return fmt.Sprintf(`https://registry.terraform.io/modules/%s/%s`, shortName, record.VersionStr)
133134
}

internal/langserver/handlers/command/module_calls_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,42 @@ func Test_parseModuleRecords(t *testing.T) {
8787
})
8888
}
8989
}
90+
91+
// With the release of Terraform 1.1.0 module source addresses are now stored normalized
92+
func Test_parseModuleRecords_v1_1(t *testing.T) {
93+
tests := []struct {
94+
name string
95+
records []datadir.ModuleRecord
96+
want []moduleCall
97+
}{
98+
{
99+
name: "detects terraform module types",
100+
records: []datadir.ModuleRecord{
101+
{
102+
Key: "ec2_instances",
103+
SourceAddr: "registry.terraform.io/terraform-aws-modules/ec2-instance/aws",
104+
VersionStr: "2.12.0",
105+
Dir: ".terraform\\modules\\ec2_instances",
106+
},
107+
},
108+
want: []moduleCall{
109+
{
110+
Name: "ec2_instances",
111+
SourceAddr: "registry.terraform.io/terraform-aws-modules/ec2-instance/aws",
112+
Version: "2.12.0",
113+
SourceType: "tfregistry",
114+
DocsLink: "https://registry.terraform.io/modules/terraform-aws-modules/ec2-instance/aws/2.12.0",
115+
DependentModules: []moduleCall{},
116+
},
117+
},
118+
},
119+
}
120+
for _, tt := range tests {
121+
t.Run(tt.name, func(t *testing.T) {
122+
got := parseModuleRecords(tt.records)
123+
if diff := cmp.Diff(tt.want, got); diff != "" {
124+
t.Fatalf("module mismatch: %s", diff)
125+
}
126+
})
127+
}
128+
}

internal/terraform/datadir/module_types.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ func (r *ModuleRecord) GetModuleType() ModuleType {
3434
// the authorative source: hashicorp/terraform/internal/addrs/module_source.go.
3535
// However this works enough for now to identify module types for display in vscode-terraform.
3636
// Example: terraform-aws-modules/ec2-instance/aws
37-
if _, err := tfregistry.ParseRawProviderSourceString(r.SourceAddr); err == nil {
37+
_, err := tfregistry.ParseRawProviderSourceString(r.SourceAddr)
38+
// Example: registry.terraform.io/terraform-aws-modules/vpc/aws
39+
if err == nil || strings.HasPrefix(r.SourceAddr, "registry.terraform.io/") {
3840
return TFREGISTRY
3941
}
4042

0 commit comments

Comments
 (0)