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

fix: decode all submodules with the right path #810

Merged
merged 2 commits into from
Mar 1, 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
349 changes: 349 additions & 0 deletions internal/langserver/handlers/complete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,355 @@ output "test" {
}`)
}

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

writeContentToFile(t, filepath.Join(tmpDir.Path(), "submodule-alpha", "main.tf"), `
variable "alpha-var" {
type = string
}

output "alpha-out" {
value = 1
}
`)
writeContentToFile(t, filepath.Join(tmpDir.Path(), "submodule-beta", "main.tf"), `
variable "beta-var" {
type = number
}

output "beta-out" {
value = 2
}
`)
mainCfg := `module "alpha" {
source = "./submodule-alpha"

}
module "beta" {
source = "./submodule-beta"

}

output "test" {

}
`
writeContentToFile(t, filepath.Join(tmpDir.Path(), "main.tf"), mainCfg)
mainCfg = `module "alpha" {
source = "./submodule-alpha"

}
module "beta" {
source = "./submodule-beta"

}

output "test" {
value = module.
}
`

tfExec := tfExecutor(t, tmpDir.Path(), "1.0.2")
err := tfExec.Get(context.Background())
if err != nil {
t.Fatal(err)
}

var testSchema tfjson.ProviderSchemas
err = json.Unmarshal([]byte(testModuleSchemaOutput), &testSchema)
if err != nil {
t.Fatal(err)
}

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
TerraformCalls: &exec.TerraformMockCalls{
PerWorkDir: map[string][]*mock.Call{
tmpDir.Path(): {
{
Method: "Version",
Repeatability: 1,
Arguments: []interface{}{
mock.AnythingOfType(""),
},
ReturnArguments: []interface{}{
version.Must(version.NewVersion("0.12.0")),
nil,
nil,
},
},
{
Method: "GetExecPath",
Repeatability: 1,
ReturnArguments: []interface{}{
"",
},
},
{
Method: "ProviderSchemas",
Repeatability: 1,
Arguments: []interface{}{
mock.AnythingOfType(""),
},
ReturnArguments: []interface{}{
&testSchema,
nil,
},
},
},
},
}}))
stop := ls.Start(t)
defer stop()

ls.Call(t, &langserver.CallRequest{
Method: "initialize",
ReqParams: fmt.Sprintf(`{
"capabilities": {},
"rootUri": %q,
"processId": 12345
}`, tmpDir.URI)})
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": %q,
"uri": "%s/main.tf"
}
}`, mainCfg, tmpDir.URI)})

// first module
ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "textDocument/completion",
ReqParams: fmt.Sprintf(`{
"textDocument": {
"uri": "%s/main.tf"
},
"position": {
"character": 0,
"line": 2
}
}`, tmpDir.URI)}, `{
"jsonrpc": "2.0",
"id": 3,
"result": {
"isIncomplete": false,
"items": [
{
"label": "alpha-var",
"labelDetails": {},
"kind": 10,
"detail": "required, string",
"insertTextFormat": 1,
"textEdit": {
"range": {
"start": {
"line": 2,
"character": 0
},
"end": {
"line": 2,
"character": 0
}
},
"newText": "alpha-var"
}
},
{
"label": "providers",
"labelDetails": {},
"kind": 10,
"detail": "optional, map of provider references",
"documentation": "Explicit mapping of providers which the module uses",
"insertTextFormat": 1,
"textEdit": {
"range": {
"start": {
"line": 2,
"character": 0
},
"end": {
"line": 2,
"character": 0
}
},
"newText": "providers"
}
},
{
"label": "version",
"labelDetails": {},
"kind": 10,
"detail": "optional, string",
"documentation": "Constraint to set the version of the module, e.g. ~\u003e 1.0. Only applicable to modules in a module registry.",
"insertTextFormat": 1,
"textEdit": {
"range": {
"start": {
"line": 2,
"character": 0
},
"end": {
"line": 2,
"character": 0
}
},
"newText": "version"
}
}
]
}
}`)
// second module
ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "textDocument/completion",
ReqParams: fmt.Sprintf(`{
"textDocument": {
"uri": "%s/main.tf"
},
"position": {
"character": 0,
"line": 6
}
}`, tmpDir.URI)}, `{
"jsonrpc": "2.0",
"id": 4,
"result": {
"isIncomplete": false,
"items": [
{
"label": "beta-var",
"labelDetails": {},
"kind": 10,
"detail": "required, number",
"insertTextFormat": 1,
"textEdit": {
"range": {
"start": {
"line": 6,
"character": 0
},
"end": {
"line": 6,
"character": 0
}
},
"newText": "beta-var"
}
},
{
"label": "providers",
"labelDetails": {},
"kind": 10,
"detail": "optional, map of provider references",
"documentation": "Explicit mapping of providers which the module uses",
"insertTextFormat": 1,
"textEdit": {
"range": {
"start": {
"line": 6,
"character": 0
},
"end": {
"line": 6,
"character": 0
}
},
"newText": "providers"
}
},
{
"label": "version",
"labelDetails": {},
"kind": 10,
"detail": "optional, string",
"documentation": "Constraint to set the version of the module, e.g. ~\u003e 1.0. Only applicable to modules in a module registry.",
"insertTextFormat": 1,
"textEdit": {
"range": {
"start": {
"line": 6,
"character": 0
},
"end": {
"line": 6,
"character": 0
}
},
"newText": "version"
}
}
]
}
}`)
// outputs
ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "textDocument/completion",
ReqParams: fmt.Sprintf(`{
"textDocument": {
"uri": "%s/main.tf"
},
"position": {
"character": 17,
"line": 10
}
}`, tmpDir.URI)}, `{
"jsonrpc": "2.0",
"id": 5,
"result": {
"isIncomplete": false,
"items": [
{
"label": "module.alpha",
"labelDetails": {},
"kind": 6,
"detail": "object",
"insertTextFormat": 1,
"textEdit": {
"range": {
"start": {
"line": 10,
"character": 10
},
"end": {
"line": 10,
"character": 17
}
},
"newText": "module.alpha"
}
},
{
"label": "module.beta",
"labelDetails": {},
"kind": 6,
"detail": "object",
"insertTextFormat": 1,
"textEdit": {
"range": {
"start": {
"line": 10,
"character": 10
},
"end": {
"line": 10,
"character": 17
}
},
"newText": "module.beta"
}
}
]
}
}`)
}

func tfExecutor(t *testing.T, workdir, tfVersion string) exec.TerraformExecutor {
ctx := context.Background()
installDir := filepath.Join(t.TempDir(), "hcinstall")
Expand Down
10 changes: 6 additions & 4 deletions internal/terraform/module/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,19 +373,21 @@ func decodeCalledModulesFunc(fs ReadOnlyFS, modStore *state.ModuleStore, schemaR
modStore.Add(mc.Path)

mcHandle := document.DirHandleFromPath(mc.Path)
// copy path for queued jobs below
mcPath := mc.Path

id, err := jobStore.EnqueueJob(job.Job{
Dir: mcHandle,
Func: func(ctx context.Context) error {
return ParseModuleConfiguration(fs, modStore, mc.Path)
return ParseModuleConfiguration(fs, modStore, mcPath)
},
Type: op.OpTypeParseModuleConfiguration.String(),
Defer: func(ctx context.Context, jobErr error) (ids job.IDs) {
id, err := jobStore.EnqueueJob(job.Job{
Dir: mcHandle,
Type: op.OpTypeLoadModuleMetadata.String(),
Func: func(ctx context.Context) error {
return LoadModuleMetadata(modStore, mc.Path)
return LoadModuleMetadata(modStore, mcPath)
},
})
if err != nil {
Expand All @@ -407,14 +409,14 @@ func decodeCalledModulesFunc(fs ReadOnlyFS, modStore *state.ModuleStore, schemaR
id, err = jobStore.EnqueueJob(job.Job{
Dir: mcHandle,
Func: func(ctx context.Context) error {
return ParseVariables(fs, modStore, mc.Path)
return ParseVariables(fs, modStore, mcPath)
},
Type: op.OpTypeParseVariables.String(),
Defer: func(ctx context.Context, jobErr error) (ids job.IDs) {
id, err = jobStore.EnqueueJob(job.Job{
Dir: mcHandle,
Func: func(ctx context.Context) error {
return DecodeVarsReferences(ctx, modStore, schemaReader, mc.Path)
return DecodeVarsReferences(ctx, modStore, schemaReader, mcPath)
},
Type: op.OpTypeDecodeVarsReferences.String(),
})
Expand Down