Skip to content

Commit

Permalink
Use memdb-driven state for modules and schema
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Apr 14, 2021
1 parent daf7d2d commit ee41b5e
Show file tree
Hide file tree
Showing 29 changed files with 631 additions and 866 deletions.
8 changes: 7 additions & 1 deletion internal/cmd/completion_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/terraform-ls/internal/logging"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
"github.com/hashicorp/terraform-ls/internal/state"
"github.com/hashicorp/terraform-ls/internal/terraform/module"
"github.com/mitchellh/cli"
)
Expand Down Expand Up @@ -104,7 +105,12 @@ func (c *CompletionCommand) Run(args []string) int {
}

ctx := context.Background()
modMgr := module.NewSyncModuleManager(ctx, fs)
ss, err := state.NewStateStore()
if err != nil {
c.Ui.Error(err.Error())
return 1
}
modMgr := module.NewSyncModuleManager(ctx, fs, ss.Modules, ss.ProviderSchemas)

mod, err := modMgr.AddModule(fh.Dir())
if err != nil {
Expand Down
36 changes: 20 additions & 16 deletions internal/cmd/inspect_module_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
ictx "github.com/hashicorp/terraform-ls/internal/context"
"github.com/hashicorp/terraform-ls/internal/filesystem"
"github.com/hashicorp/terraform-ls/internal/logging"
"github.com/hashicorp/terraform-ls/internal/state"
"github.com/hashicorp/terraform-ls/internal/terraform/datadir"
"github.com/hashicorp/terraform-ls/internal/terraform/module"
"github.com/mitchellh/cli"
Expand Down Expand Up @@ -86,7 +87,11 @@ func (c *InspectModuleCommand) inspect(rootPath string) error {
fs := filesystem.NewFilesystem()

ctx := context.Background()
modMgr := module.NewSyncModuleManager(ctx, fs)
ss, err := state.NewStateStore()
if err != nil {
return err
}
modMgr := module.NewSyncModuleManager(ctx, fs, ss.Modules, ss.ProviderSchemas)
modMgr.SetLogger(c.logger)

walker := module.SyncWalker(fs, modMgr)
Expand All @@ -101,34 +106,33 @@ func (c *InspectModuleCommand) inspect(rootPath string) error {
return err
}

modules := modMgr.ListModules()
modules, err := modMgr.ListModules()
if err != nil {
return err
}
c.Ui.Output(fmt.Sprintf("%d modules found in total at %s", len(modules), rootPath))
for _, mod := range modules {
errs := &multierror.Error{}

_, err = mod.TerraformVersion()
if err != nil {
multierror.Append(errs, err)
if mod.TerraformVersionErr != nil {
multierror.Append(errs, mod.TerraformVersionErr)
}

_, err := mod.ProviderSchema()
if err != nil {
multierror.Append(errs, err)
if mod.ProviderSchemaErr != nil {
multierror.Append(errs, mod.ProviderSchemaErr)
}

_, err = mod.ModuleManifest()
if err != nil {
multierror.Append(errs, err)
if mod.ModManifestErr != nil {
multierror.Append(errs, mod.ModManifestErr)
}

_, err = mod.ParsedFiles()
if err != nil {
multierror.Append(errs, err)
if mod.ParsingErr != nil {
multierror.Append(errs, mod.ParsingErr)
}

errs.ErrorFormat = formatErrors

modules := formatModuleRecords(mod.ModuleCalls())
modules := formatModuleRecords(mod.ModManifest.Records)
subModules := fmt.Sprintf("%d modules", len(modules))
if len(modules) > 0 {
subModules += "\n"
Expand All @@ -139,7 +143,7 @@ func (c *InspectModuleCommand) inspect(rootPath string) error {

c.Ui.Output(fmt.Sprintf(` - %s
- %s
- %s`, mod.Path(), errs, subModules))
- %s`, mod.Path, errs, subModules))
}
c.Ui.Output("")

Expand Down
7 changes: 1 addition & 6 deletions internal/decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ func DecoderForModule(ctx context.Context, mod module.Module) (*decoder.Decoder,
d.SetUtmMedium(clientName)
}

pf, err := mod.ParsedFiles()
if err != nil {
return nil, err
}

for name, f := range pf {
for name, f := range mod.ParsedFiles {
err := d.LoadFile(name, f)
if err != nil {
return nil, fmt.Errorf("failed to load a file: %w", err)
Expand Down
25 changes: 23 additions & 2 deletions internal/langserver/handlers/command/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package command
import (
"context"
"fmt"
"path/filepath"
"sort"

"github.com/creachadair/jrpc2/code"
Expand Down Expand Up @@ -68,8 +69,8 @@ func ModulesHandler(ctx context.Context, args cmd.CommandArgs) (interface{}, err
modules := make([]moduleInfo, len(sources))
for i, source := range sources {
modules[i] = moduleInfo{
URI: uri.FromPath(source.Path()),
Name: source.HumanReadablePath(rootDir),
URI: uri.FromPath(source.Path),
Name: humanReadablePath(rootDir, source.Path),
}
}
sort.SliceStable(modules, func(i, j int) bool {
Expand All @@ -81,3 +82,23 @@ func ModulesHandler(ctx context.Context, args cmd.CommandArgs) (interface{}, err
Modules: modules,
}, nil
}

func humanReadablePath(rootDir, modPath string) string {
if rootDir == "" {
return modPath
}

// absolute paths can be too long for UI/messages,
// so we just display relative to root dir
relDir, err := filepath.Rel(rootDir, modPath)
if err != nil {
return modPath
}

if relDir == "." {
// Name of the root dir is more helpful than "."
return filepath.Base(rootDir)
}

return relDir
}
2 changes: 1 addition & 1 deletion internal/langserver/handlers/command/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TerraformValidateHandler(ctx context.Context, args cmd.CommandArgs) (interf

diags := diagnostics.HCLDiagsFromJSON(jsonDiags)

notifier.PublishHCLDiags(ctx, mod.Path(), diags, "terraform validate")
notifier.PublishHCLDiags(ctx, mod.Path, diags, "terraform validate")

return nil, nil
}
10 changes: 7 additions & 3 deletions internal/langserver/handlers/did_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
lsctx "github.com/hashicorp/terraform-ls/internal/context"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
"github.com/hashicorp/terraform-ls/internal/terraform/module"
op "github.com/hashicorp/terraform-ls/internal/terraform/module/operation"
)

func TextDocumentDidChange(ctx context.Context, params lsp.DidChangeTextDocumentParams) error {
Expand Down Expand Up @@ -59,7 +59,11 @@ func TextDocumentDidChange(ctx context.Context, params lsp.DidChangeTextDocument
return err
}

err = modMgr.EnqueueModuleOpWait(mod.Path(), module.OpTypeParseConfiguration)
err = modMgr.EnqueueModuleOpWait(mod.Path, op.OpTypeParseConfiguration)
if err != nil {
return err
}
err = modMgr.EnqueueModuleOpWait(mod.Path, op.OpTypeLoadModuleMetadata)
if err != nil {
return err
}
Expand All @@ -68,7 +72,7 @@ func TextDocumentDidChange(ctx context.Context, params lsp.DidChangeTextDocument
if err != nil {
return err
}
diags.PublishHCLDiags(ctx, mod.Path(), mod.Diagnostics(), "HCL")
diags.PublishHCLDiags(ctx, mod.Path, mod.Diagnostics, "HCL")

return nil
}
Loading

0 comments on commit ee41b5e

Please sign in to comment.