diff --git a/internal/langserver/handlers/did_change_watched_files_test.go b/internal/langserver/handlers/did_change_watched_files_test.go index 9c2262a9..3ab1b1f1 100644 --- a/internal/langserver/handlers/did_change_watched_files_test.go +++ b/internal/langserver/handlers/did_change_watched_files_test.go @@ -701,7 +701,7 @@ func TestLangServer_DidChangeWatchedFiles_pluginChange(t *testing.T) { t.Fatal(err) } - originalTestDir := filepath.Join(testData, "uninitialized-single-submodule") + originalTestDir := filepath.Join(testData, "uninitialized-submodules") testDir := t.TempDir() // Copy test configuration so the test can run in isolation err = copy.Copy(originalTestDir, testDir) @@ -835,7 +835,7 @@ func TestLangServer_DidChangeWatchedFiles_moduleInstalled(t *testing.T) { t.Fatal(err) } - originalTestDir := filepath.Join(testData, "uninitialized-single-submodule") + originalTestDir := filepath.Join(testData, "uninitialized-submodules") testDir := t.TempDir() // Copy test configuration so the test can run in isolation err = copy.Copy(originalTestDir, testDir) diff --git a/internal/langserver/handlers/go_to_ref_target_test.go b/internal/langserver/handlers/go_to_ref_target_test.go index 8763cf36..5efd8da0 100644 --- a/internal/langserver/handlers/go_to_ref_target_test.go +++ b/internal/langserver/handlers/go_to_ref_target_test.go @@ -496,6 +496,89 @@ func TestDefinition_moduleInputToVariable(t *testing.T) { }`, modHandle.URI)) } +func TestDefinition_moduleOutsideWorkspace(t *testing.T) { + testPath, err := filepath.Abs(filepath.Join("testdata", "main-module-multienv")) + if err != nil { + t.Fatal(err) + } + modHandle := document.DirHandleFromPath(filepath.Join(testPath, "main")) + dbModHandle := document.DirHandleFromPath(filepath.Join(testPath, "modules", "database")) + + ss, err := state.NewStateStore() + if err != nil { + t.Fatal(err) + } + wc := walker.NewWalkerCollector() + + ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{ + TerraformCalls: &exec.TerraformMockCalls{ + PerWorkDir: map[string][]*mock.Call{ + modHandle.Path(): validTfMockCalls(), + }, + }, + StateStore: ss, + WalkerCollector: wc, + })) + stop := ls.Start(t) + defer stop() + + ls.Call(t, &langserver.CallRequest{ + Method: "initialize", + ReqParams: fmt.Sprintf(`{ + "capabilities": {}, + "rootUri": %q, + "processId": 12345 + }`, modHandle.URI)}) + waitForWalkerPath(t, ss, wc, modHandle) + ls.Notify(t, &langserver.CallRequest{ + Method: "initialized", + ReqParams: "{}", + }) + ls.Call(t, &langserver.CallRequest{ + Method: "textDocument/didOpen", + ReqParams: fmt.Sprintf(`{ + "textDocument": { + "version": 0, + "languageId": "terraform", + "text": `+fmt.Sprintf("%q", + `module "db" { + source = "../modules/database" +} +`)+`, + "uri": "%s/main.tf" + } + }`, modHandle.URI)}) + ls.CallAndExpectResponse(t, &langserver.CallRequest{ + Method: "textDocument/definition", + ReqParams: fmt.Sprintf(`{ + "textDocument": { + "uri": "%s/main.tf" + }, + "position": { + "line": 1, + "character": 17 + } + }`, modHandle.URI)}, fmt.Sprintf(`{ + "jsonrpc": "2.0", + "id": 3, + "result": [ + { + "uri": "%s/main.tf", + "range": { + "start": { + "line": 0, + "character": 0 + }, + "end": { + "line": 0, + "character": 0 + } + } + } + ] + }`, dbModHandle.URI)) +} + func TestDeclaration_basic(t *testing.T) { tmpDir := TempDir(t) diff --git a/internal/langserver/handlers/testdata/uninitialized-single-submodule/.gitignore b/internal/langserver/handlers/testdata/uninitialized-submodules/.gitignore similarity index 100% rename from internal/langserver/handlers/testdata/uninitialized-single-submodule/.gitignore rename to internal/langserver/handlers/testdata/uninitialized-submodules/.gitignore diff --git a/internal/langserver/handlers/testdata/uninitialized-single-submodule/main.tf b/internal/langserver/handlers/testdata/uninitialized-submodules/main.tf similarity index 69% rename from internal/langserver/handlers/testdata/uninitialized-single-submodule/main.tf rename to internal/langserver/handlers/testdata/uninitialized-submodules/main.tf index d28dd2e2..ba608efd 100644 --- a/internal/langserver/handlers/testdata/uninitialized-single-submodule/main.tf +++ b/internal/langserver/handlers/testdata/uninitialized-submodules/main.tf @@ -1,3 +1,7 @@ module "azure-hcp-consul" { source = "github.com/hashicorp/terraform-azurerm-hcp-consul?ref=v0.2.4" } + +module "local" { + source = "./submodule" +} diff --git a/internal/langserver/handlers/testdata/uninitialized-submodules/submodule/main.tf b/internal/langserver/handlers/testdata/uninitialized-submodules/submodule/main.tf new file mode 100644 index 00000000..e69de29b