Skip to content

Commit

Permalink
remove native watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Jun 16, 2022
1 parent a5e839e commit 274657e
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 716 deletions.
14 changes: 0 additions & 14 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/hashicorp/terraform-ls/internal/langserver/diagnostics"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
"github.com/hashicorp/terraform-ls/internal/settings"
"github.com/hashicorp/terraform-ls/internal/terraform/module"
)

type contextKey struct {
Expand All @@ -22,7 +21,6 @@ var (
ctxTfExecPath = &contextKey{"terraform executable path"}
ctxTfExecLogPath = &contextKey{"terraform executor log path"}
ctxTfExecTimeout = &contextKey{"terraform execution timeout"}
ctxWatcher = &contextKey{"watcher"}
ctxRootDir = &contextKey{"root directory"}
ctxCommandPrefix = &contextKey{"command prefix"}
ctxDiagsNotifier = &contextKey{"diagnostics notifier"}
Expand Down Expand Up @@ -53,18 +51,6 @@ func TerraformExecTimeout(ctx context.Context) (time.Duration, bool) {
return path, ok
}

func WithWatcher(ctx context.Context, w module.Watcher) context.Context {
return context.WithValue(ctx, ctxWatcher, w)
}

func Watcher(ctx context.Context) (module.Watcher, error) {
w, ok := ctx.Value(ctxWatcher).(module.Watcher)
if !ok {
return nil, missingContextErr(ctxWatcher)
}
return w, nil
}

func WithTerraformExecPath(ctx context.Context, path string) context.Context {
return context.WithValue(ctx, ctxTfExecPath, path)
}
Expand Down
12 changes: 0 additions & 12 deletions internal/langserver/handlers/did_change_workspace_folders.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ func (svc *service) indexNewModule(ctx context.Context, modURI string) {
})
return
}

err = svc.watcher.AddModule(modHandle.Path())
if err != nil {
svc.logger.Printf("failed to add module to watcher: %s", err)
return
}
}

func (svc *service) removeIndexedModule(ctx context.Context, modURI string) {
Expand All @@ -54,12 +48,6 @@ func (svc *service) removeIndexedModule(ctx context.Context, modURI string) {
return
}

err = svc.watcher.RemoveModule(modHandle.Path())
if err != nil {
svc.logger.Printf("failed to remove module from watcher: %s", err)
return
}

err = svc.stateStore.JobStore.DequeueJobsForDir(modHandle)
if err != nil {
svc.logger.Printf("failed to dequeue jobs for module: %s", err)
Expand Down
13 changes: 0 additions & 13 deletions internal/langserver/handlers/did_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package handlers
import (
"context"

lsctx "github.com/hashicorp/terraform-ls/internal/context"
"github.com/hashicorp/terraform-ls/internal/document"
"github.com/hashicorp/terraform-ls/internal/job"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
Expand Down Expand Up @@ -65,25 +64,13 @@ func (svc *service) TextDocumentDidOpen(ctx context.Context, params lsp.DidOpenT
jobIds = append(jobIds, jobId)
}

watcher, err := lsctx.Watcher(ctx)
if err != nil {
return err
}

if svc.singleFileMode {
err = svc.stateStore.WalkerPaths.EnqueueDir(modHandle)
if err != nil {
return err
}
}

if !watcher.IsModuleWatched(mod.Path) {
err := watcher.AddModule(mod.Path)
if err != nil {
return err
}
}

return svc.stateStore.JobStore.WaitForJobs(ctx, jobIds...)
}

Expand Down
21 changes: 0 additions & 21 deletions internal/langserver/handlers/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,27 +281,6 @@ func (svc *service) setupWalker(ctx context.Context, params lsp.InitializeParams
svc.openDirWalker.SetIgnoreDirectoryNames(options.IgnoreDirectoryNames)
svc.openDirWalker.SetExcludeModulePaths(excludeModulePaths)

if len(options.ModulePaths) > 0 {
svc.logger.Printf("Attempting to add %d static module paths", len(options.ModulePaths))
for _, rawPath := range options.ModulePaths {
modPath, err := resolvePath(root.Path(), rawPath)
if err != nil {
jrpc2.ServerFromContext(ctx).Notify(ctx, "window/showMessage", &lsp.ShowMessageParams{
Type: lsp.Warning,
Message: fmt.Sprintf("Ignoring module path %s: %s", rawPath, err),
})
continue
}

err = svc.watcher.AddModule(modPath)
if err != nil {
return err
}
}

return nil
}

return nil
}

Expand Down
1 change: 0 additions & 1 deletion internal/langserver/handlers/initialize_benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ func BenchmarkInitializeFolder_basic(b *testing.B) {
srvCtx: ctx,
sessCtx: sessCtx,
stopSession: stopSession,
newWatcher: module.MockWatcher(),
tfDiscoFunc: d.LookPath,
tfExecFactory: exec.NewExecutor,
walkerCollector: wc,
Expand Down
28 changes: 0 additions & 28 deletions internal/langserver/handlers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ type service struct {
fs *filesystem.Filesystem
modStore *state.ModuleStore
schemaStore *state.ProviderSchemaStore
watcher module.Watcher
newWatcher module.WatcherFactory
tfDiscoFunc discovery.DiscoveryFunc
tfExecFactory exec.ExecutorFactory
tfExecOpts *exec.ExecutorOpts
Expand Down Expand Up @@ -79,7 +77,6 @@ func NewSession(srvCtx context.Context) session.Session {
srvCtx: srvCtx,
sessCtx: sessCtx,
stopSession: stopSession,
newWatcher: module.NewWatcher,
tfDiscoFunc: d.LookPath,
tfExecFactory: exec.NewExecutor,
telemetry: &telemetry.NoopSender{},
Expand Down Expand Up @@ -153,7 +150,6 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) {
if err != nil {
return nil, err
}
ctx = lsctx.WithWatcher(ctx, svc.watcher)
return handle(ctx, req, svc.TextDocumentDidOpen)
},
"textDocument/didClose": func(ctx context.Context, req *jrpc2.Request) (interface{}, error) {
Expand Down Expand Up @@ -288,8 +284,6 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) {
return nil, err
}

ctx = lsctx.WithWatcher(ctx, svc.watcher)

return handle(ctx, req, svc.DidChangeWorkspaceFolders)
},
"workspace/didChangeWatchedFiles": func(ctx context.Context, req *jrpc2.Request) (interface{}, error) {
Expand All @@ -315,7 +309,6 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) {
}

ctx = lsctx.WithCommandPrefix(ctx, &commandPrefix)
ctx = lsctx.WithWatcher(ctx, svc.watcher)
ctx = lsctx.WithRootDirectory(ctx, &rootDir)
ctx = lsctx.WithDiagnosticsNotifier(ctx, svc.diagsNotifier)
ctx = ilsp.ContextWithClientName(ctx, &clientName)
Expand Down Expand Up @@ -501,17 +494,6 @@ func (svc *service) configureSessionDependencies(ctx context.Context, cfgOpts *s
svc.closedDirWalker.Collector = svc.walkerCollector
svc.openDirWalker.SetLogger(svc.logger)

ww, err := svc.newWatcher(svc.fs, svc.modStore, svc.stateStore.ProviderSchemas, svc.stateStore.JobStore, svc.tfExecFactory)
if err != nil {
return err
}
svc.watcher = ww
svc.watcher.SetLogger(svc.logger)
err = svc.watcher.Start(ctx)
if err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -546,16 +528,6 @@ func (svc *service) shutdown() {
svc.logger.Printf("openDirWalker stopped")
}

if svc.watcher != nil {
svc.logger.Println("stopping watcher for session ...")
err := svc.watcher.Stop()
if err != nil {
svc.logger.Println("unable to stop watcher for session:", err)
} else {
svc.logger.Println("watcher stopped")
}
}

if svc.closedDirIndexer != nil {
svc.closedDirIndexer.Stop()
}
Expand Down
1 change: 0 additions & 1 deletion internal/langserver/handlers/session_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func (ms *mockSession) new(srvCtx context.Context) session.Session {
srvCtx: srvCtx,
sessCtx: sessCtx,
stopSession: ms.stop,
newWatcher: module.MockWatcher(),
tfDiscoFunc: d.LookPath,
tfExecFactory: exec.NewMockExecutor(tfCalls),
additionalHandlers: handlers,
Expand Down
21 changes: 21 additions & 0 deletions internal/terraform/module/module_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
tfjson "github.com/hashicorp/terraform-json"
"github.com/hashicorp/terraform-ls/internal/document"
"github.com/hashicorp/terraform-ls/internal/filesystem"
"github.com/hashicorp/terraform-ls/internal/job"
"github.com/hashicorp/terraform-ls/internal/scheduler"
"github.com/hashicorp/terraform-ls/internal/state"
"github.com/hashicorp/terraform-ls/internal/terraform/exec"
Expand Down Expand Up @@ -351,3 +352,23 @@ func testLogger() *log.Logger {

return log.New(ioutil.Discard, "", 0)
}

type closedJobStore struct {
js *state.JobStore
}

func (js *closedJobStore) EnqueueJob(newJob job.Job) (job.ID, error) {
return js.js.EnqueueJob(newJob)
}

func (js *closedJobStore) AwaitNextJob(ctx context.Context) (job.ID, job.Job, error) {
return js.js.AwaitNextJob(ctx, false)
}

func (js *closedJobStore) FinishJob(id job.ID, jobErr error, deferredJobIds ...job.ID) error {
return js.js.FinishJob(id, jobErr, deferredJobIds...)
}

func (js *closedJobStore) WaitForJobs(ctx context.Context, jobIds ...job.ID) error {
return js.js.WaitForJobs(ctx, jobIds...)
}
Loading

0 comments on commit 274657e

Please sign in to comment.