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

indexer: Account for module calls pointing outside of the workspace #1000

Closed
wants to merge 2 commits into from
Closed
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
11 changes: 11 additions & 0 deletions internal/indexer/document_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package indexer
import (
"context"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-ls/internal/document"
"github.com/hashicorp/terraform-ls/internal/job"
"github.com/hashicorp/terraform-ls/internal/terraform/module"
Expand Down Expand Up @@ -68,6 +69,16 @@ func (idx *Indexer) decodeModule(ctx context.Context, modHandle document.DirHand
Type: op.OpTypeLoadModuleMetadata.String(),
Defer: func(ctx context.Context, jobErr error) (job.IDs, error) {
ids := make(job.IDs, 0)

var errs *multierror.Error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to return this further down?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch! 😅


mcIds, err := idx.decodeDeclaredModuleCalls(modHandle)(ctx, jobErr)
if err != nil {
errs = multierror.Append(errs, err)
} else {
ids = append(ids, mcIds...)
}

id, err := idx.jobStore.EnqueueJob(job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
Expand Down
103 changes: 103 additions & 0 deletions internal/indexer/module_calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package indexer
import (
"context"
"os"
"path/filepath"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-ls/internal/document"
"github.com/hashicorp/terraform-ls/internal/job"
"github.com/hashicorp/terraform-ls/internal/terraform/module"
op "github.com/hashicorp/terraform-ls/internal/terraform/module/operation"
tfmod "github.com/hashicorp/terraform-schema/module"
)

func (idx *Indexer) decodeInstalledModuleCalls(modHandle document.DirHandle) job.DeferFunc {
Expand Down Expand Up @@ -108,6 +110,107 @@ func (idx *Indexer) decodeInstalledModuleCalls(modHandle document.DirHandle) job
}
}

func (idx *Indexer) decodeDeclaredModuleCalls(modHandle document.DirHandle) job.DeferFunc {
return func(ctx context.Context, opErr error) (job.IDs, error) {
jobIds := make(job.IDs, 0)
if opErr != nil {
return jobIds, opErr
}

moduleCalls, err := idx.modStore.ModuleCalls(modHandle.Path())
if err != nil {
return jobIds, err
}

var errs *multierror.Error

for _, mc := range moduleCalls.Declared {
sourceAddr, ok := mc.SourceAddr.(tfmod.LocalSourceAddr)
if !ok {
continue
}
modPath := filepath.Join(modHandle.Path(), sourceAddr.String())

fi, err := os.Stat(modPath)
if err != nil || !fi.IsDir() {
multierror.Append(errs, err)
continue
}
err = idx.modStore.Add(modPath)
if err != nil {
multierror.Append(errs, err)
continue
}

mcHandle := document.DirHandleFromPath(modPath)

id, err := idx.jobStore.EnqueueJob(job.Job{
Dir: mcHandle,
Func: func(ctx context.Context) error {
return module.ParseModuleConfiguration(idx.fs, idx.modStore, mcHandle.Path())
},
Type: op.OpTypeParseModuleConfiguration.String(),
Defer: func(ctx context.Context, jobErr error) (job.IDs, error) {
ids := make(job.IDs, 0)

id, err := idx.jobStore.EnqueueJob(job.Job{
Dir: mcHandle,
Type: op.OpTypeLoadModuleMetadata.String(),
Func: func(ctx context.Context) error {
return module.LoadModuleMetadata(idx.modStore, mcHandle.Path())
},
Defer: func(ctx context.Context, jobErr error) (job.IDs, error) {
return idx.collectReferences(ctx, mcHandle)
},
})
if err != nil {
return ids, err
} else {
ids = append(ids, id)
}

return ids, nil
},
})
if err != nil {
multierror.Append(errs, err)
continue
}
jobIds = append(jobIds, id)

id, err = idx.jobStore.EnqueueJob(job.Job{
Dir: mcHandle,
Func: func(ctx context.Context) error {
return module.ParseVariables(idx.fs, idx.modStore, mcHandle.Path())
},
Type: op.OpTypeParseVariables.String(),
Defer: func(ctx context.Context, jobErr error) (job.IDs, error) {
ids := make(job.IDs, 0)
id, err = idx.jobStore.EnqueueJob(job.Job{
Dir: mcHandle,
Func: func(ctx context.Context) error {
return module.DecodeVarsReferences(ctx, idx.modStore, idx.schemaStore, mcHandle.Path())
},
Type: op.OpTypeDecodeVarsReferences.String(),
})
if err != nil {
return ids, err
}
ids = append(ids, id)
return ids, err
},
})
if err != nil {
multierror.Append(errs, err)
continue
}
jobIds = append(jobIds, id)
}

return jobIds, errs.ErrorOrNil()
}
}

func (idx *Indexer) collectReferences(ctx context.Context, modHandle document.DirHandle) (job.IDs, error) {
ids := make(job.IDs, 0)

Expand Down
1 change: 1 addition & 0 deletions internal/indexer/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (idx *Indexer) WalkedModule(ctx context.Context, modHandle document.DirHand
Func: func(ctx context.Context) error {
return module.LoadModuleMetadata(idx.modStore, modHandle.Path())
},
Defer: idx.decodeDeclaredModuleCalls(modHandle),
})
if err != nil {
return ids, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
83 changes: 83 additions & 0 deletions internal/langserver/handlers/go_to_ref_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module "azure-hcp-consul" {
source = "github.com/hashicorp/terraform-azurerm-hcp-consul?ref=v0.2.4"
}

module "local" {
source = "./submodule"
}