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

walker: Use memdb for queued paths #839

Merged
merged 4 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 22 additions & 2 deletions internal/cmd/inspect_module_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/hashicorp/go-multierror"
ictx "github.com/hashicorp/terraform-ls/internal/context"
"github.com/hashicorp/terraform-ls/internal/document"
"github.com/hashicorp/terraform-ls/internal/filesystem"
"github.com/hashicorp/terraform-ls/internal/logging"
"github.com/hashicorp/terraform-ls/internal/state"
Expand Down Expand Up @@ -92,16 +93,35 @@ func (c *InspectModuleCommand) inspect(rootPath string) error {

fs := filesystem.NewFilesystem(ss.DocumentStore)

dir := document.DirHandleFromPath(rootPath)

ctx := context.Background()

walker := module.SyncWalker(fs, ss.DocumentStore, ss.Modules, ss.ProviderSchemas, ss.JobStore, exec.NewExecutor)
pa := state.NewPathAwaiter(ss.WalkerPaths, false)
walker := module.NewWalker(fs, pa, ss.Modules, ss.ProviderSchemas, ss.JobStore, exec.NewExecutor)
walker.Collector = module.NewWalkerCollector()
walker.SetLogger(c.logger)
err = ss.WalkerPaths.WaitForDirs(ctx, []document.DirHandle{dir})
if err != nil {
return err
}
err = ss.JobStore.WaitForJobs(ctx, walker.Collector.JobIds()...)
if err != nil {
return err
}
err = walker.Collector.ErrorOrNil()
if err != nil {
return err
}

ctx, cancel := ictx.WithSignalCancel(context.Background(),
c.logger, os.Interrupt, syscall.SIGTERM)
defer cancel()

walker.EnqueuePath(rootPath)
err = ss.WalkerPaths.EnqueueDir(dir)
if err != nil {
return err
}
err = walker.StartWalking(ctx)
if err != nil {
return err
Expand Down
13 changes: 0 additions & 13 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var (
ctxTfExecLogPath = &contextKey{"terraform executor log path"}
ctxTfExecTimeout = &contextKey{"terraform execution timeout"}
ctxWatcher = &contextKey{"watcher"}
ctxModuleWalker = &contextKey{"module walker"}
ctxRootDir = &contextKey{"root directory"}
ctxCommandPrefix = &contextKey{"command prefix"}
ctxDiagsNotifier = &contextKey{"diagnostics notifier"}
Expand Down Expand Up @@ -119,18 +118,6 @@ func CommandPrefix(ctx context.Context) (string, bool) {
return *commandPrefix, true
}

func WithModuleWalker(ctx context.Context, w *module.Walker) context.Context {
return context.WithValue(ctx, ctxModuleWalker, w)
}

func ModuleWalker(ctx context.Context) (*module.Walker, error) {
w, ok := ctx.Value(ctxModuleWalker).(*module.Walker)
if !ok {
return nil, missingContextErr(ctxModuleWalker)
}
return w, nil
}

func WithDiagnosticsNotifier(ctx context.Context, diags *diagnostics.Notifier) context.Context {
return context.WithValue(ctx, ctxDiagsNotifier, diags)
}
Expand Down
11 changes: 11 additions & 0 deletions internal/langserver/handlers/cancel_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ import (
"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/handler"
"github.com/hashicorp/terraform-ls/internal/langserver"
"github.com/hashicorp/terraform-ls/internal/state"
"github.com/hashicorp/terraform-ls/internal/terraform/module"
)

func TestLangServer_cancelRequest(t *testing.T) {
tmpDir := TempDir(t)

ss, err := state.NewStateStore()
if err != nil {
t.Fatal(err)
}
wc := module.NewWalkerCollector()

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
AdditionalHandlers: map[string]handler.Func{
"$/sleepTicker": func(ctx context.Context, req *jrpc2.Request) (interface{}, error) {
Expand Down Expand Up @@ -43,6 +51,8 @@ func TestLangServer_cancelRequest(t *testing.T) {
return nil, ctx.Err()
},
},
StateStore: ss,
WalkerCollector: wc,
}))
stop := ls.Start(t)
defer stop()
Expand All @@ -54,6 +64,7 @@ func TestLangServer_cancelRequest(t *testing.T) {
"rootUri": %q,
"processId": 12345
}`, tmpDir.URI)})
waitForWalkerPath(t, ss, wc, tmpDir)
ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
ReqParams: "{}",
Expand Down
20 changes: 20 additions & 0 deletions internal/langserver/handlers/code_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-ls/internal/langserver"
"github.com/hashicorp/terraform-ls/internal/langserver/session"
"github.com/hashicorp/terraform-ls/internal/state"
"github.com/hashicorp/terraform-ls/internal/terraform/exec"
"github.com/hashicorp/terraform-ls/internal/terraform/module"
"github.com/stretchr/testify/mock"
)

Expand All @@ -31,6 +33,12 @@ func TestLangServer_codeActionWithoutInitialization(t *testing.T) {
func TestLangServer_codeAction_basic(t *testing.T) {
tmpDir := TempDir(t)

ss, err := state.NewStateStore()
if err != nil {
t.Fatal(err)
}
wc := module.NewWalkerCollector()

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
TerraformCalls: &exec.TerraformMockCalls{
PerWorkDir: map[string][]*mock.Call{
Expand Down Expand Up @@ -68,6 +76,8 @@ func TestLangServer_codeAction_basic(t *testing.T) {
}},
},
},
StateStore: ss,
WalkerCollector: wc,
}))
stop := ls.Start(t)
defer stop()
Expand All @@ -79,6 +89,7 @@ func TestLangServer_codeAction_basic(t *testing.T) {
"rootUri": %q,
"processId": 12345
}`, tmpDir.URI)})
waitForWalkerPath(t, ss, wc, tmpDir)
ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
ReqParams: "{}",
Expand Down Expand Up @@ -269,6 +280,12 @@ func TestLangServer_codeAction_no_code_action_requested(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ss, err := state.NewStateStore()
if err != nil {
t.Fatal(err)
}
wc := module.NewWalkerCollector()

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
TerraformCalls: &exec.TerraformMockCalls{
PerWorkDir: map[string][]*mock.Call{
Expand Down Expand Up @@ -306,6 +323,8 @@ func TestLangServer_codeAction_no_code_action_requested(t *testing.T) {
}},
},
},
StateStore: ss,
WalkerCollector: wc,
}))
stop := ls.Start(t)
defer stop()
Expand All @@ -317,6 +336,7 @@ func TestLangServer_codeAction_no_code_action_requested(t *testing.T) {
"rootUri": %q,
"processId": 123456
}`, tmpDir.URI)})
waitForWalkerPath(t, ss, wc, tmpDir)
ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
ReqParams: "{}",
Expand Down
25 changes: 23 additions & 2 deletions internal/langserver/handlers/code_lens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"github.com/hashicorp/terraform-ls/internal/document"
"github.com/hashicorp/terraform-ls/internal/langserver"
"github.com/hashicorp/terraform-ls/internal/langserver/session"
"github.com/hashicorp/terraform-ls/internal/state"
"github.com/hashicorp/terraform-ls/internal/terraform/exec"
"github.com/hashicorp/terraform-ls/internal/terraform/module"
"github.com/stretchr/testify/mock"
)

Expand Down Expand Up @@ -88,6 +90,12 @@ func TestCodeLens_referenceCount(t *testing.T) {
t.Fatal(err)
}

ss, err := state.NewStateStore()
if err != nil {
t.Fatal(err)
}
wc := module.NewWalkerCollector()

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
TerraformCalls: &exec.TerraformMockCalls{
PerWorkDir: map[string][]*mock.Call{
Expand Down Expand Up @@ -124,7 +132,10 @@ func TestCodeLens_referenceCount(t *testing.T) {
},
},
},
}}))
},
StateStore: ss,
WalkerCollector: wc,
}))
stop := ls.Start(t)
defer stop()

Expand All @@ -139,6 +150,7 @@ func TestCodeLens_referenceCount(t *testing.T) {
"rootUri": %q,
"processId": 12345
}`, tmpDir.URI)})
waitForWalkerPath(t, ss, wc, tmpDir)
ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
ReqParams: "{}",
Expand Down Expand Up @@ -215,13 +227,21 @@ func TestCodeLens_referenceCount_crossModule(t *testing.T) {
t.Fatal(err)
}

ss, err := state.NewStateStore()
if err != nil {
t.Fatal(err)
}
wc := module.NewWalkerCollector()
ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
TerraformCalls: &exec.TerraformMockCalls{
PerWorkDir: map[string][]*mock.Call{
submodPath: validTfMockCalls(),
rootModPath: validTfMockCalls(),
},
}}))
},
StateStore: ss,
WalkerCollector: wc,
}))
stop := ls.Start(t)
defer stop()

Expand All @@ -236,6 +256,7 @@ func TestCodeLens_referenceCount_crossModule(t *testing.T) {
"rootUri": %q,
"processId": 12345
}`, rootModUri.URI)})
waitForWalkerPath(t, ss, wc, rootModUri)
ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
ReqParams: "{}",
Expand Down
Loading