Skip to content

Commit

Permalink
bump tfschema to 08c17f04e2bc & account for upstream changes
Browse files Browse the repository at this point in the history
Checking MetaState in (ModuleStore).LocalModuleMeta prevents a possible race condition where terraform-schema would otherwise build out module schema just because the module entry is present, but metadata was not parsed yet.
  • Loading branch information
radeksimko committed Jul 12, 2022
1 parent d0fa017 commit decc54b
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 37 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/hashicorp/terraform-exec v0.17.2
github.com/hashicorp/terraform-json v0.14.0
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c
github.com/hashicorp/terraform-schema v0.0.0-20220708122804-408b2aefd4d6
github.com/hashicorp/terraform-schema v0.0.0-20220712135911-08c17f04e2bc
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5
github.com/mitchellh/cli v1.1.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ github.com/hashicorp/terraform-json v0.14.0 h1:sh9iZ1Y8IFJLx+xQiKHGud6/TSUCM0N8e
github.com/hashicorp/terraform-json v0.14.0/go.mod h1:5A9HIWPkk4e5aeeXIBbkcOvaZbIYnAIkEyqP2pNSckM=
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c h1:D8aRO6+mTqHfLsK/BC3j5OAoogv1WLRWzY1AaTo3rBg=
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c/go.mod h1:Wn3Na71knbXc1G8Lh+yu/dQWWJeFQEpDeJMtWMtlmNI=
github.com/hashicorp/terraform-schema v0.0.0-20220708122804-408b2aefd4d6 h1:nyYG4twPkXiAUnLSgF5HfhKUGNp3rErv2sYOS2bDsZY=
github.com/hashicorp/terraform-schema v0.0.0-20220708122804-408b2aefd4d6/go.mod h1:GL+zHwXHrTc/MfKnQP8K2Z4hmaTck85ndiIbvZueMng=
github.com/hashicorp/terraform-schema v0.0.0-20220712135911-08c17f04e2bc h1:08Pp12m9Rv4cQgk2GOlkoG6njaiAyCR+rKDpdrgvlVs=
github.com/hashicorp/terraform-schema v0.0.0-20220712135911-08c17f04e2bc/go.mod h1:GL+zHwXHrTc/MfKnQP8K2Z4hmaTck85ndiIbvZueMng=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg=
github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/inspect_module_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func formatModuleRecords(mds []datadir.ModuleRecord) []string {
continue
}
if m.IsExternal() {
out = append(out, "EXTERNAL(%s)", m.SourceAddr)
out = append(out, "EXTERNAL(%s)", m.SourceAddr.String())
continue
}
out = append(out, fmt.Sprintf("%s (%s)", m.Dir, m.SourceAddr))
Expand Down
4 changes: 4 additions & 0 deletions internal/state/module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package state

import (
"fmt"
"path/filepath"

"github.com/hashicorp/go-memdb"
Expand Down Expand Up @@ -374,6 +375,9 @@ func (s *ModuleStore) LocalModuleMeta(modPath string) (*tfmod.Meta, error) {
if err != nil {
return nil, err
}
if mod.MetaState != op.OpStateLoaded {
return nil, fmt.Errorf("%s: module data not available", modPath)
}
return &tfmod.Meta{
Path: mod.Path,
ProviderReferences: mod.Meta.ProviderReferences,
Expand Down
10 changes: 5 additions & 5 deletions internal/state/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestModuleStore_CallersOfModule(t *testing.T) {
[]datadir.ModuleRecord{
{
Key: "web_server_sg1",
SourceAddr: "terraform-aws-modules/security-group/aws//modules/http-80",
SourceAddr: tfmod.ParseModuleSourceAddr("terraform-aws-modules/security-group/aws//modules/http-80"),
VersionStr: "3.10.0",
Version: version.Must(version.NewVersion("3.10.0")),
Dir: filepath.Join(".terraform", "modules", "web_server_sg", "terraform-aws-security-group-3.10.0", "modules", "http-80"),
Expand All @@ -102,7 +102,7 @@ func TestModuleStore_CallersOfModule(t *testing.T) {
},
{
Key: "local-x",
SourceAddr: "../nested/submodule",
SourceAddr: tfmod.ParseModuleSourceAddr("../nested/submodule"),
Dir: filepath.Join("..", "nested", "submodule"),
},
},
Expand All @@ -115,7 +115,7 @@ func TestModuleStore_CallersOfModule(t *testing.T) {
},
{
Key: "local-foo",
SourceAddr: "../another/submodule",
SourceAddr: tfmod.ParseModuleSourceAddr("../another/submodule"),
Dir: filepath.Join("..", "another", "submodule"),
},
},
Expand All @@ -125,7 +125,7 @@ func TestModuleStore_CallersOfModule(t *testing.T) {
[]datadir.ModuleRecord{
{
Key: "web_server_sg2",
SourceAddr: "terraform-aws-modules/security-group/aws//modules/http-80",
SourceAddr: tfmod.ParseModuleSourceAddr("terraform-aws-modules/security-group/aws//modules/http-80"),
VersionStr: "3.10.0",
Version: version.Must(version.NewVersion("3.10.0")),
Dir: filepath.Join(".terraform", "modules", "web_server_sg", "terraform-aws-security-group-3.10.0", "modules", "http-80"),
Expand All @@ -135,7 +135,7 @@ func TestModuleStore_CallersOfModule(t *testing.T) {
},
{
Key: "local-y",
SourceAddr: "../nested/submodule",
SourceAddr: tfmod.ParseModuleSourceAddr("../nested/submodule"),
Dir: filepath.Join("..", "nested", "submodule"),
},
},
Expand Down
17 changes: 12 additions & 5 deletions internal/terraform/datadir/module_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

version "github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-ls/internal/pathcmp"
tfmod "github.com/hashicorp/terraform-schema/module"
)

func ModuleManifestFilePath(fs fs.StatFS, modulePath string) (string, bool) {
Expand All @@ -34,11 +35,12 @@ type ModuleRecord struct {
// position within the static module tree.
Key string `json:"Key"`

// SourceAddr is the source address given for this module in configuration.
// This is used only to detect if the source was changed in configuration
// since the module was last installed, which means that the installer
// must re-install it.
SourceAddr string `json:"Source"`
// SourceAddr is the source address for the module.
SourceAddr tfmod.ModuleSourceAddr `json:"-"`

// RawSourceAddr is the raw source address for the module
// as it appears in the manifest.
RawSourceAddr string `json:"Source"`

// Version is the exact version of the module, which results from parsing
// VersionStr. nil for un-versioned modules.
Expand All @@ -61,6 +63,11 @@ func (r *ModuleRecord) UnmarshalJSON(b []byte) error {
if err != nil {
return err
}

if record.RawSourceAddr != "" {
record.SourceAddr = tfmod.ParseModuleSourceAddr(record.RawSourceAddr)
}

if record.VersionStr != "" {
record.Version, err = version.NewVersion(record.VersionStr)
if err != nil {
Expand Down
30 changes: 17 additions & 13 deletions internal/terraform/datadir/module_manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/go-version"
tfmod "github.com/hashicorp/terraform-schema/module"
)

func TestParseModuleManifestFromFile(t *testing.T) {
Expand All @@ -22,26 +23,29 @@ func TestParseModuleManifestFromFile(t *testing.T) {
rootDir: modPath,
Records: []ModuleRecord{
{
Key: "web_server_sg1",
SourceAddr: "terraform-aws-modules/security-group/aws//modules/http-80",
VersionStr: "3.10.0",
Version: version.Must(version.NewVersion("3.10.0")),
Dir: filepath.Join(".terraform", "modules", "web_server_sg", "terraform-aws-security-group-3.10.0", "modules", "http-80"),
Key: "web_server_sg1",
SourceAddr: tfmod.ParseModuleSourceAddr("terraform-aws-modules/security-group/aws//modules/http-80"),
RawSourceAddr: "terraform-aws-modules/security-group/aws//modules/http-80",
VersionStr: "3.10.0",
Version: version.Must(version.NewVersion("3.10.0")),
Dir: filepath.Join(".terraform", "modules", "web_server_sg", "terraform-aws-security-group-3.10.0", "modules", "http-80"),
},
{
Key: "web_server_sg2",
SourceAddr: "terraform-aws-modules/security-group/aws//modules/http-80",
VersionStr: "3.10.0",
Version: version.Must(version.NewVersion("3.10.0")),
Dir: filepath.Join(".terraform", "modules", "web_server_sg", "terraform-aws-security-group-3.10.0", "modules", "http-80"),
Key: "web_server_sg2",
SourceAddr: tfmod.ParseModuleSourceAddr("terraform-aws-modules/security-group/aws//modules/http-80"),
RawSourceAddr: "terraform-aws-modules/security-group/aws//modules/http-80",
VersionStr: "3.10.0",
Version: version.Must(version.NewVersion("3.10.0")),
Dir: filepath.Join(".terraform", "modules", "web_server_sg", "terraform-aws-security-group-3.10.0", "modules", "http-80"),
},
{
Dir: ".",
},
{
Key: "local",
SourceAddr: "./nested/path",
Dir: filepath.Join("nested", "path"),
Key: "local",
SourceAddr: tfmod.ParseModuleSourceAddr("./nested/path"),
RawSourceAddr: "./nested/path",
Dir: filepath.Join("nested", "path"),
},
},
}
Expand Down
12 changes: 7 additions & 5 deletions internal/terraform/datadir/module_manifest_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/go-version"
tfmod "github.com/hashicorp/terraform-schema/module"
)

func TestRecord_UnmarshalJSON_basic(t *testing.T) {
Expand All @@ -23,11 +24,12 @@ func TestRecord_UnmarshalJSON_basic(t *testing.T) {
t.Fatal(err)
}
expectedRecord := ModuleRecord{
Key: "web_server_sg",
SourceAddr: "terraform-aws-modules/security-group/aws//modules/http-80",
VersionStr: "3.10.0",
Version: expectedVersion,
Dir: ".terraform/modules/web_server_sg/terraform-aws-security-group-3.10.0/modules/http-80",
Key: "web_server_sg",
SourceAddr: tfmod.ParseModuleSourceAddr("terraform-aws-modules/security-group/aws//modules/http-80"),
RawSourceAddr: "terraform-aws-modules/security-group/aws//modules/http-80",
VersionStr: "3.10.0",
Version: expectedVersion,
Dir: ".terraform/modules/web_server_sg/terraform-aws-security-group-3.10.0/modules/http-80",
}
if diff := cmp.Diff(expectedRecord, record); diff != "" {
t.Fatalf("version mismatch: %s", diff)
Expand Down
12 changes: 7 additions & 5 deletions internal/terraform/datadir/module_manifest_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/go-version"
tfmod "github.com/hashicorp/terraform-schema/module"
)

func TestRecord_UnmarshalJSON_basic(t *testing.T) {
Expand All @@ -20,11 +21,12 @@ func TestRecord_UnmarshalJSON_basic(t *testing.T) {
t.Fatal(err)
}
expectedRecord := ModuleRecord{
Key: "web_server_sg",
SourceAddr: "terraform-aws-modules/security-group/aws//modules/http-80",
VersionStr: "3.10.0",
Version: expectedVersion,
Dir: `.terraform\modules\web_server_sg\terraform-aws-security-group-3.10.0\modules\http-80`,
Key: "web_server_sg",
SourceAddr: tfmod.ParseModuleSourceAddr("terraform-aws-modules/security-group/aws//modules/http-80"),
RawSourceAddr: "terraform-aws-modules/security-group/aws//modules/http-80",
VersionStr: "3.10.0",
Version: expectedVersion,
Dir: `.terraform\modules\web_server_sg\terraform-aws-security-group-3.10.0\modules\http-80`,
}
if diff := cmp.Diff(expectedRecord, record); diff != "" {
t.Fatalf("version mismatch: %s", diff)
Expand Down

0 comments on commit decc54b

Please sign in to comment.