Skip to content

Commit

Permalink
Add basic module support for variable completion (hashicorp#551)
Browse files Browse the repository at this point in the history
* Add basic module support for variable completion

* Correct debug leftovers and standard file path use default go

* Remove vscode specific debug file
  • Loading branch information
Nepomuceno authored Jul 22, 2021
1 parent 795a256 commit f7e3179
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ internal/schemas/*log*
internal/schemas/providers.tf
internal/schemas/schemas_gen.go
internal/schemas/data/
terraform-ls
20 changes: 20 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug Server",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"args": [
"serve",
"--port",
"30337"
]
}
]
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/hashicorp/terraform-exec v0.14.0
github.com/hashicorp/terraform-json v0.12.0
github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896
github.com/hashicorp/terraform-schema v0.0.0-20210617130010-ab6887832578
github.com/hashicorp/terraform-schema v0.0.0-20210722111916-2c53a3129d11
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.2
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,12 @@ github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2p
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/hashicorp/terraform-exec v0.14.0 h1:UQoUcxKTZZXhyyK68Cwn4mApT4mnFPmEXPiqaHL9r+w=
github.com/hashicorp/terraform-exec v0.14.0/go.mod h1:qrAASDq28KZiMPDnQ02sFS9udcqEkRly002EA2izXTA=
github.com/hashicorp/terraform-json v0.11.0/go.mod h1:pmbq9o4EuL43db5+0ogX10Yofv1nozM+wskr/bGFJpI=
github.com/hashicorp/terraform-json v0.12.0 h1:8czPgEEWWPROStjkWPUnTQDXmpmZPlkQAwYYLETaTvw=
github.com/hashicorp/terraform-json v0.12.0/go.mod h1:pmbq9o4EuL43db5+0ogX10Yofv1nozM+wskr/bGFJpI=
github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896 h1:1FGtlkJw87UsTMg5s8jrekrHmUPUJaMcu6ELiVhQrNw=
github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896/go.mod h1:bzBPnUIkI0RxauU8Dqo+2KrZZ28Cf48s8V6IHt3p4co=
github.com/hashicorp/terraform-schema v0.0.0-20210617130010-ab6887832578 h1:r2zk8WVjkHqFCylDTukWwICfuZDh/5+p0S7A/MbHkeY=
github.com/hashicorp/terraform-schema v0.0.0-20210617130010-ab6887832578/go.mod h1:I401Q0vqx1AvDdh+ND7sC12x+C2sqmY5vTkc7FBjSvM=
github.com/hashicorp/terraform-schema v0.0.0-20210722111916-2c53a3129d11 h1:AmgdvpxCaq1rb90drpXqOafdPSR4084bOBtDthYrd7Y=
github.com/hashicorp/terraform-schema v0.0.0-20210722111916-2c53a3129d11/go.mod h1:2rBfUJC+1kyVzt5R/6pRa+d9HbpZrf8sMjQZdh8rSCM=
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.2 h1:L18LIDzqlW6xN2rEkpdV8+oL/IXWJ1APd+vsdYy4Wdw=
Expand Down
32 changes: 32 additions & 0 deletions internal/state/module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package state

import (
"path/filepath"

"github.com/hashicorp/go-memdb"
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl-lang/lang"
Expand Down Expand Up @@ -247,6 +249,36 @@ func (s *ModuleStore) ModuleByPath(path string) (*Module, error) {
return mod, nil
}

func (s *ModuleStore) ModuleCalls(modPath string) ([]tfmod.ModuleCall, error) {
result := make([]tfmod.ModuleCall, 0)
modList, err := s.List()
for _, mod := range modList {
if mod.ModManifest != nil {
for _, record := range mod.ModManifest.Records {
result = append(result, tfmod.ModuleCall{
SourceAddr: record.SourceAddr,
Path: filepath.Join(modPath, record.Dir),
})
}
}
}
return result, err
}

func (s *ModuleStore) ModuleMeta(modPath string) (*tfmod.Meta, error) {
mod, err := s.ModuleByPath(modPath)
if err != nil {
return nil, err
}
return &tfmod.Meta{
Path: mod.Path,
ProviderReferences: mod.Meta.ProviderReferences,
ProviderRequirements: mod.Meta.ProviderRequirements,
CoreRequirements: mod.Meta.CoreRequirements,
Variables: mod.Meta.Variables,
}, nil
}

func moduleByPath(txn *memdb.Txn, path string) (*Module, error) {
obj, err := txn.First(moduleTableName, "id", path)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions internal/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/hashicorp/go-memdb"
"github.com/hashicorp/go-version"
tfaddr "github.com/hashicorp/terraform-registry-address"
tfmod "github.com/hashicorp/terraform-schema/module"
tfschema "github.com/hashicorp/terraform-schema/schema"
)

Expand Down Expand Up @@ -64,6 +65,11 @@ type ModuleReader interface {
List() ([]*Module, error)
}

type ModuleCallReader interface {
ModuleCalls(modPath string) ([]tfmod.ModuleCall, error)
ModuleMeta(modPath string) (*tfmod.Meta, error)
}

type ProviderSchemaStore struct {
db *memdb.MemDB
tableName string
Expand Down
5 changes: 3 additions & 2 deletions internal/terraform/module/module_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (mm *moduleManager) SchemaForModule(modPath string) (*schema.BodySchema, er
return nil, err
}

return schemaForModule(mod, mm.schemaStore)
return schemaForModule(mod, mm.schemaStore, mm.moduleStore)
}

func (mm *moduleManager) SchemaForVariables(modPath string) (*schema.BodySchema, error) {
Expand All @@ -123,7 +123,7 @@ func (mm *moduleManager) SchemaForVariables(modPath string) (*schema.BodySchema,
return tfschema.SchemaForVariables(mod.Meta.Variables)
}

func schemaForModule(mod *state.Module, schemaReader state.SchemaReader) (*schema.BodySchema, error) {
func schemaForModule(mod *state.Module, schemaReader state.SchemaReader, modReader state.ModuleCallReader) (*schema.BodySchema, error) {
var coreSchema *schema.BodySchema
coreRequirements := make(version.Constraints, 0)
if mod.TerraformVersion != nil {
Expand All @@ -143,6 +143,7 @@ func schemaForModule(mod *state.Module, schemaReader state.SchemaReader) (*schem
sm := tfschema.NewSchemaMerger(coreSchema)
sm.SetSchemaReader(schemaReader)
sm.SetTerraformVersion(mod.TerraformVersion)
sm.SetModuleReader(modReader)

meta := &tfmodule.Meta{
Path: mod.Path,
Expand Down
17 changes: 15 additions & 2 deletions internal/terraform/module/module_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,21 @@ func ParseModuleManifest(fs filesystem.Filesystem, modStore *state.ModuleStore,
}
return err
}
if mm != nil {
for _, modRecord := range mm.Records {
modModulePath := filepath.Join(modPath, modRecord.Dir)
_, err := modStore.ModuleByPath(modModulePath)
if err != nil {
modStore.Add(modModulePath)
ParseModuleConfiguration(fs, modStore, modModulePath)
ParseVariables(fs, modStore, modModulePath)
LoadModuleMetadata(modStore, modModulePath)
}
}
}

sErr := modStore.UpdateModManifest(modPath, mm, err)

if sErr != nil {
return sErr
}
Expand Down Expand Up @@ -361,7 +374,7 @@ func DecodeReferenceTargets(modStore *state.ModuleStore, schemaReader state.Sche
}
}

fullSchema, schemaErr := schemaForModule(mod, schemaReader)
fullSchema, schemaErr := schemaForModule(mod, schemaReader, modStore)
if schemaErr != nil {
sErr := modStore.UpdateReferenceTargets(modPath, lang.ReferenceTargets{}, schemaErr)
if sErr != nil {
Expand Down Expand Up @@ -403,7 +416,7 @@ func DecodeReferenceOrigins(modStore *state.ModuleStore, schemaReader state.Sche
}
}

fullSchema, schemaErr := schemaForModule(mod, schemaReader)
fullSchema, schemaErr := schemaForModule(mod, schemaReader, modStore)
if schemaErr != nil {
sErr := modStore.UpdateReferenceOrigins(modPath, lang.ReferenceOrigins{}, schemaErr)
if sErr != nil {
Expand Down

0 comments on commit f7e3179

Please sign in to comment.