From 6fa70734e9620d534c56199ea6af249bc60a2a6e Mon Sep 17 00:00:00 2001 From: Audrey Eschright Date: Mon, 7 Dec 2020 14:27:39 -0800 Subject: [PATCH 1/4] Add human readable name to rootmodules command API --- .../handlers/command/rootmodules.go | 7 +++++-- internal/langserver/handlers/service.go | 1 + internal/terraform/rootmodule/root_module.go | 21 +++++++++++++++++++ internal/terraform/rootmodule/types.go | 1 + 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/internal/langserver/handlers/command/rootmodules.go b/internal/langserver/handlers/command/rootmodules.go index 50689a48b..712006d3d 100644 --- a/internal/langserver/handlers/command/rootmodules.go +++ b/internal/langserver/handlers/command/rootmodules.go @@ -22,7 +22,8 @@ type rootmodulesCommandResponse struct { } type rootModuleInfo struct { - URI string `json:"uri"` + URI string `json:"uri"` + Name string `json:"name"` } func RootModulesHandler(ctx context.Context, args cmd.CommandArgs) (interface{}, error) { @@ -44,11 +45,13 @@ func RootModulesHandler(ctx context.Context, args cmd.CommandArgs) (interface{}, } doneLoading := !walker.IsWalking() candidates := cf.RootModuleCandidatesByPath(fh.Dir()) + rootDir, _ := lsctx.RootDirectory(ctx) rootModules := make([]rootModuleInfo, len(candidates)) for i, candidate := range candidates { rootModules[i] = rootModuleInfo{ - URI: uri.FromPath(candidate.Path()), + URI: uri.FromPath(candidate.Path()), + Name: candidate.HumanReadablePath(rootDir), } } sort.SliceStable(rootModules, func(i, j int) bool { diff --git a/internal/langserver/handlers/service.go b/internal/langserver/handlers/service.go index df099a716..4f043dbce 100644 --- a/internal/langserver/handlers/service.go +++ b/internal/langserver/handlers/service.go @@ -270,6 +270,7 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) { ctx = lsctx.WithRootModuleFinder(ctx, svc.modMgr) ctx = lsctx.WithRootModuleWalker(ctx, svc.walker) ctx = lsctx.WithWatcher(ctx, ww) + ctx = lsctx.WithRootDirectory(ctx, &rootDir) return handle(ctx, req, lh.WorkspaceExecuteCommand) }, diff --git a/internal/terraform/rootmodule/root_module.go b/internal/terraform/rootmodule/root_module.go index 1a1f987f0..965fb0596 100644 --- a/internal/terraform/rootmodule/root_module.go +++ b/internal/terraform/rootmodule/root_module.go @@ -385,6 +385,27 @@ func (rm *rootModule) MatchesPath(path string) bool { return filepath.Clean(rm.path) == filepath.Clean(path) } +// HumanReadablePath helps display shorter, but still relevant paths +func (rm *rootModule) HumanReadablePath(rootDir string) string { + if rootDir == "" { + return rm.path + } + + // absolute paths can be too long for UI/messages, + // so we just display relative to root dir + relDir, err := filepath.Rel(rootDir, rm.path) + if err != nil { + return rm.path + } + + if relDir == "." { + // Name of the root dir is more helpful than "." + return filepath.Base(rootDir) + } + + return relDir +} + func (rm *rootModule) UpdateModuleManifest(lockFile File) error { rm.moduleMu.Lock() defer rm.moduleMu.Unlock() diff --git a/internal/terraform/rootmodule/types.go b/internal/terraform/rootmodule/types.go index de47811a4..277152f40 100644 --- a/internal/terraform/rootmodule/types.go +++ b/internal/terraform/rootmodule/types.go @@ -83,6 +83,7 @@ type RootModule interface { IsTerraformAvailable() bool ExecuteTerraformInit(ctx context.Context) error Modules() []ModuleRecord + HumanReadablePath(string) string } type RootModuleFactory func(context.Context, string) (*rootModule, error) From a9dac8243c73dbd9b8a167c3370d8bf4bc1d2192 Mon Sep 17 00:00:00 2001 From: Audrey Eschright Date: Mon, 7 Dec 2020 17:02:41 -0800 Subject: [PATCH 2/4] Fix tests --- .../handlers/execute_command_rootmodules_test.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/internal/langserver/handlers/execute_command_rootmodules_test.go b/internal/langserver/handlers/execute_command_rootmodules_test.go index 563a4b2be..9ea04ef1c 100644 --- a/internal/langserver/handlers/execute_command_rootmodules_test.go +++ b/internal/langserver/handlers/execute_command_rootmodules_test.go @@ -106,11 +106,12 @@ func TestLangServer_workspaceExecuteCommand_rootmodules_basic(t *testing.T) { "doneLoading": true, "rootModules": [ { - "uri": %q + "uri": %q, + "name": %q } ] } - }`, tmpDir.URI())) + }`, tmpDir.URI(), t.Name())) } func TestLangServer_workspaceExecuteCommand_rootmodules_multiple(t *testing.T) { @@ -169,15 +170,18 @@ func TestLangServer_workspaceExecuteCommand_rootmodules_multiple(t *testing.T) { "doneLoading": true, "rootModules": [ { - "uri": %q + "uri": %q, + "name": %q }, { - "uri": %q + "uri": %q, + "name": %q }, { - "uri": %q + "uri": %q, + "name": %q } ] } - }`, dev.URI(), prod.URI(), staging.URI())) + }`, dev.URI(), "env/dev", prod.URI(), "env/prod", staging.URI(), "env/staging")) } From 3d29a5a55b85f576007e41cc6fe974dfeb677037 Mon Sep 17 00:00:00 2001 From: Audrey Eschright Date: Tue, 8 Dec 2020 11:19:59 -0800 Subject: [PATCH 3/4] Fix windows test --- .../langserver/handlers/execute_command_rootmodules_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/langserver/handlers/execute_command_rootmodules_test.go b/internal/langserver/handlers/execute_command_rootmodules_test.go index 9ea04ef1c..23355a6b5 100644 --- a/internal/langserver/handlers/execute_command_rootmodules_test.go +++ b/internal/langserver/handlers/execute_command_rootmodules_test.go @@ -2,6 +2,7 @@ package handlers import ( "fmt" + "path" "path/filepath" "testing" @@ -183,5 +184,5 @@ func TestLangServer_workspaceExecuteCommand_rootmodules_multiple(t *testing.T) { } ] } - }`, dev.URI(), "env/dev", prod.URI(), "env/prod", staging.URI(), "env/staging")) + }`, dev.URI(), path.Join("env", "dev"), prod.URI(), path.Join("env", "prod"), staging.URI(), path.Join("env", "staging"))) } From 36972c9ae411bdefba2b9b897ef7faa0880cad6c Mon Sep 17 00:00:00 2001 From: Audrey Eschright Date: Tue, 8 Dec 2020 11:22:34 -0800 Subject: [PATCH 4/4] Fix test again (vscode autocomplete was too "helpful") --- .../langserver/handlers/execute_command_rootmodules_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/langserver/handlers/execute_command_rootmodules_test.go b/internal/langserver/handlers/execute_command_rootmodules_test.go index 23355a6b5..69d89d09f 100644 --- a/internal/langserver/handlers/execute_command_rootmodules_test.go +++ b/internal/langserver/handlers/execute_command_rootmodules_test.go @@ -2,7 +2,6 @@ package handlers import ( "fmt" - "path" "path/filepath" "testing" @@ -184,5 +183,5 @@ func TestLangServer_workspaceExecuteCommand_rootmodules_multiple(t *testing.T) { } ] } - }`, dev.URI(), path.Join("env", "dev"), prod.URI(), path.Join("env", "prod"), staging.URI(), path.Join("env", "staging"))) + }`, dev.URI(), filepath.Join("env", "dev"), prod.URI(), filepath.Join("env", "prod"), staging.URI(), filepath.Join("env", "staging"))) }