Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix module calls command for remote modules #872

Merged
merged 4 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/langserver/handlers/command/module_calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 4 additions & 4 deletions internal/langserver/handlers/command/module_calls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Test_parseModuleRecords(t *testing.T) {
records: []datadir.ModuleRecord{
{
Key: "ec2_instances",
SourceAddr: "terraform-aws-modules/ec2-instance/aws",
SourceAddr: "registry.terraform.io/terraform-aws-modules/ec2-instance/aws",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding this as a separate test case, e.g. Test_parseModuleRecords_v1_1 ? Just to make sure we keep testing for both old and new format.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have an entry in our test table below for terraform-aws-modules/eks/aws,but I can create a separate test case as well.

VersionStr: "2.12.0",
Dir: ".terraform\\modules\\ec2_instances",
},
Expand All @@ -30,7 +30,7 @@ func Test_parseModuleRecords(t *testing.T) {
},
{
Key: "eks",
SourceAddr: "terraform-aws-modules/eks/aws",
SourceAddr: "registry.terraform.io/terraform-aws-modules/eks/aws",
VersionStr: "17.20.0",
Dir: ".terraform\\modules\\eks",
},
Expand All @@ -44,15 +44,15 @@ func Test_parseModuleRecords(t *testing.T) {
want: []moduleCall{
{
Name: "ec2_instances",
SourceAddr: "terraform-aws-modules/ec2-instance/aws",
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{},
},
{
Name: "eks",
SourceAddr: "terraform-aws-modules/eks/aws",
SourceAddr: "registry.terraform.io/terraform-aws-modules/eks/aws",
Version: "17.20.0",
SourceType: "tfregistry",
DocsLink: "https://registry.terraform.io/modules/terraform-aws-modules/eks/aws/17.20.0",
Expand Down
12 changes: 2 additions & 10 deletions internal/terraform/datadir/module_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package datadir

import (
"strings"

tfregistry "github.com/hashicorp/terraform-registry-address"
)

type ModuleType string
Expand All @@ -27,14 +25,8 @@ var moduleSourceLocalPrefixes = []string{
// from. It currently supports detecting Terraform Registry modules, GitHub modules, Git modules, and
// local file paths
func (r *ModuleRecord) GetModuleType() ModuleType {
// TODO: It is technically incorrect to use the package hashicorp/terraform-registry-address
// here as it is written to parse Terraform provider addresses and may not work correctly on
// Terraform module addresses. The proper approach is to create a new parsing library that is
// dedicated to parsing these kinds of addresses correctly, by re-using the logic defined in
// 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 {
// Example: registry.terraform.io/terraform-aws-modules/vpc/aws
if strings.HasPrefix(r.SourceAddr, "registry.terraform.io/") {
return TFREGISTRY
}

Expand Down