Skip to content

Commit d5602d9

Browse files
authored
indexer: refactor & improve/cleanup error handling (#988)
* avoid extra function arguments * indexer: extract module call-related jobs * indexer: improve/cleanup error handling - capture and log any errors from Deferred jobs - capture and log any errors from other places which were previously skipped on error (via multierror) * walker: fix error collection * scheduler/state: avoid passing JobStore via ctx This (hack) is no longer necessary since all indexing logic is now self-contained within one package.
1 parent 14e71fd commit d5602d9

11 files changed

+211
-214
lines changed

internal/indexer/document_change.go

+13-15
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@ func (idx *Indexer) DocumentChanged(modHandle document.DirHandle) (job.IDs, erro
1818
return module.ParseModuleConfiguration(idx.fs, idx.modStore, modHandle.Path())
1919
},
2020
Type: op.OpTypeParseModuleConfiguration.String(),
21-
Defer: func(ctx context.Context, jobErr error) job.IDs {
22-
ids, err := idx.decodeModule(ctx, modHandle)
23-
if err != nil {
24-
idx.logger.Printf("error: %s", err)
25-
}
26-
return ids
21+
Defer: func(ctx context.Context, jobErr error) (job.IDs, error) {
22+
return idx.decodeModule(ctx, modHandle)
2723
},
2824
})
2925
if err != nil {
@@ -37,7 +33,8 @@ func (idx *Indexer) DocumentChanged(modHandle document.DirHandle) (job.IDs, erro
3733
return module.ParseVariables(idx.fs, idx.modStore, modHandle.Path())
3834
},
3935
Type: op.OpTypeParseVariables.String(),
40-
Defer: func(ctx context.Context, jobErr error) (ids job.IDs) {
36+
Defer: func(ctx context.Context, jobErr error) (job.IDs, error) {
37+
ids := make(job.IDs, 0)
4138
id, err := idx.jobStore.EnqueueJob(job.Job{
4239
Dir: modHandle,
4340
Func: func(ctx context.Context) error {
@@ -46,10 +43,10 @@ func (idx *Indexer) DocumentChanged(modHandle document.DirHandle) (job.IDs, erro
4643
Type: op.OpTypeDecodeVarsReferences.String(),
4744
})
4845
if err != nil {
49-
return
46+
return ids, err
5047
}
5148
ids = append(ids, id)
52-
return
49+
return ids, nil
5350
},
5451
})
5552
if err != nil {
@@ -69,7 +66,8 @@ func (idx *Indexer) decodeModule(ctx context.Context, modHandle document.DirHand
6966
return module.LoadModuleMetadata(idx.modStore, modHandle.Path())
7067
},
7168
Type: op.OpTypeLoadModuleMetadata.String(),
72-
Defer: func(ctx context.Context, jobErr error) (ids job.IDs) {
69+
Defer: func(ctx context.Context, jobErr error) (job.IDs, error) {
70+
ids := make(job.IDs, 0)
7371
id, err := idx.jobStore.EnqueueJob(job.Job{
7472
Dir: modHandle,
7573
Func: func(ctx context.Context) error {
@@ -78,7 +76,7 @@ func (idx *Indexer) decodeModule(ctx context.Context, modHandle document.DirHand
7876
Type: op.OpTypeDecodeReferenceTargets.String(),
7977
})
8078
if err != nil {
81-
return
79+
return ids, err
8280
}
8381
ids = append(ids, id)
8482

@@ -90,7 +88,7 @@ func (idx *Indexer) decodeModule(ctx context.Context, modHandle document.DirHand
9088
Type: op.OpTypeDecodeReferenceOrigins.String(),
9189
})
9290
if err != nil {
93-
return
91+
return ids, err
9492
}
9593
ids = append(ids, id)
9694

@@ -104,11 +102,11 @@ func (idx *Indexer) decodeModule(ctx context.Context, modHandle document.DirHand
104102
Type: op.OpTypeGetModuleDataFromRegistry.String(),
105103
})
106104
if err != nil {
107-
return
105+
return ids, err
108106
}
109-
ids = append(ids, id)
110107

111-
return
108+
ids = append(ids, id)
109+
return ids, nil
112110
},
113111
})
114112
if err != nil {

internal/indexer/indexer.go

-126
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
package indexer
22

33
import (
4-
"context"
54
"io/ioutil"
65
"log"
7-
"os"
86

9-
"github.com/hashicorp/terraform-ls/internal/document"
107
"github.com/hashicorp/terraform-ls/internal/job"
118
"github.com/hashicorp/terraform-ls/internal/registry"
129
"github.com/hashicorp/terraform-ls/internal/state"
1310
"github.com/hashicorp/terraform-ls/internal/terraform/exec"
14-
"github.com/hashicorp/terraform-ls/internal/terraform/module"
15-
op "github.com/hashicorp/terraform-ls/internal/terraform/module/operation"
1611
)
1712

1813
type Indexer struct {
@@ -51,124 +46,3 @@ func (idx *Indexer) SetLogger(logger *log.Logger) {
5146
type Collector interface {
5247
CollectJobId(jobId job.ID)
5348
}
54-
55-
func decodeInstalledModuleCalls(fs ReadOnlyFS, modStore *state.ModuleStore, schemaReader state.SchemaReader, modPath string) job.DeferFunc {
56-
return func(ctx context.Context, opErr error) (jobIds job.IDs) {
57-
if opErr != nil {
58-
return
59-
}
60-
61-
moduleCalls, err := modStore.ModuleCalls(modPath)
62-
if err != nil {
63-
return
64-
}
65-
66-
jobStore, err := job.JobStoreFromContext(ctx)
67-
if err != nil {
68-
return
69-
}
70-
71-
for _, mc := range moduleCalls.Installed {
72-
fi, err := os.Stat(mc.Path)
73-
if err != nil || !fi.IsDir() {
74-
continue
75-
}
76-
modStore.Add(mc.Path)
77-
78-
mcHandle := document.DirHandleFromPath(mc.Path)
79-
// copy path for queued jobs below
80-
mcPath := mc.Path
81-
82-
id, err := jobStore.EnqueueJob(job.Job{
83-
Dir: mcHandle,
84-
Func: func(ctx context.Context) error {
85-
return module.ParseModuleConfiguration(fs, modStore, mcPath)
86-
},
87-
Type: op.OpTypeParseModuleConfiguration.String(),
88-
Defer: func(ctx context.Context, jobErr error) (ids job.IDs) {
89-
id, err := jobStore.EnqueueJob(job.Job{
90-
Dir: mcHandle,
91-
Type: op.OpTypeLoadModuleMetadata.String(),
92-
Func: func(ctx context.Context) error {
93-
return module.LoadModuleMetadata(modStore, mcPath)
94-
},
95-
})
96-
if err != nil {
97-
return
98-
}
99-
ids = append(ids, id)
100-
101-
rIds := collectReferences(ctx, mcHandle, modStore, schemaReader)
102-
ids = append(ids, rIds...)
103-
104-
return
105-
},
106-
})
107-
if err != nil {
108-
return
109-
}
110-
jobIds = append(jobIds, id)
111-
112-
id, err = jobStore.EnqueueJob(job.Job{
113-
Dir: mcHandle,
114-
Func: func(ctx context.Context) error {
115-
return module.ParseVariables(fs, modStore, mcPath)
116-
},
117-
Type: op.OpTypeParseVariables.String(),
118-
Defer: func(ctx context.Context, jobErr error) (ids job.IDs) {
119-
id, err = jobStore.EnqueueJob(job.Job{
120-
Dir: mcHandle,
121-
Func: func(ctx context.Context) error {
122-
return module.DecodeVarsReferences(ctx, modStore, schemaReader, mcPath)
123-
},
124-
Type: op.OpTypeDecodeVarsReferences.String(),
125-
})
126-
if err != nil {
127-
return
128-
}
129-
ids = append(ids, id)
130-
return
131-
},
132-
})
133-
if err != nil {
134-
return
135-
}
136-
jobIds = append(jobIds, id)
137-
}
138-
139-
return
140-
}
141-
}
142-
143-
func collectReferences(ctx context.Context, dirHandle document.DirHandle, modStore *state.ModuleStore, schemaReader state.SchemaReader) (ids job.IDs) {
144-
jobStore, err := job.JobStoreFromContext(ctx)
145-
if err != nil {
146-
return
147-
}
148-
149-
id, err := jobStore.EnqueueJob(job.Job{
150-
Dir: dirHandle,
151-
Func: func(ctx context.Context) error {
152-
return module.DecodeReferenceTargets(ctx, modStore, schemaReader, dirHandle.Path())
153-
},
154-
Type: op.OpTypeDecodeReferenceTargets.String(),
155-
})
156-
if err != nil {
157-
return
158-
}
159-
ids = append(ids, id)
160-
161-
id, err = jobStore.EnqueueJob(job.Job{
162-
Dir: dirHandle,
163-
Func: func(ctx context.Context) error {
164-
return module.DecodeReferenceOrigins(ctx, modStore, schemaReader, dirHandle.Path())
165-
},
166-
Type: op.OpTypeDecodeReferenceOrigins.String(),
167-
})
168-
if err != nil {
169-
return
170-
}
171-
ids = append(ids, id)
172-
173-
return
174-
}

internal/indexer/module_calls.go

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package indexer
2+
3+
import (
4+
"context"
5+
"os"
6+
7+
"github.com/hashicorp/go-multierror"
8+
"github.com/hashicorp/terraform-ls/internal/document"
9+
"github.com/hashicorp/terraform-ls/internal/job"
10+
"github.com/hashicorp/terraform-ls/internal/terraform/module"
11+
op "github.com/hashicorp/terraform-ls/internal/terraform/module/operation"
12+
)
13+
14+
func (idx *Indexer) decodeInstalledModuleCalls(modHandle document.DirHandle) job.DeferFunc {
15+
return func(ctx context.Context, opErr error) (job.IDs, error) {
16+
jobIds := make(job.IDs, 0)
17+
if opErr != nil {
18+
return jobIds, opErr
19+
}
20+
21+
moduleCalls, err := idx.modStore.ModuleCalls(modHandle.Path())
22+
if err != nil {
23+
return jobIds, err
24+
}
25+
26+
var errs *multierror.Error
27+
28+
for _, mc := range moduleCalls.Installed {
29+
fi, err := os.Stat(mc.Path)
30+
if err != nil || !fi.IsDir() {
31+
multierror.Append(errs, err)
32+
continue
33+
}
34+
err = idx.modStore.Add(mc.Path)
35+
if err != nil {
36+
multierror.Append(errs, err)
37+
continue
38+
}
39+
40+
mcHandle := document.DirHandleFromPath(mc.Path)
41+
// copy path for queued jobs below
42+
mcPath := mc.Path
43+
44+
id, err := idx.jobStore.EnqueueJob(job.Job{
45+
Dir: mcHandle,
46+
Func: func(ctx context.Context) error {
47+
return module.ParseModuleConfiguration(idx.fs, idx.modStore, mcPath)
48+
},
49+
Type: op.OpTypeParseModuleConfiguration.String(),
50+
Defer: func(ctx context.Context, jobErr error) (job.IDs, error) {
51+
ids := make(job.IDs, 0)
52+
var errs *multierror.Error
53+
54+
id, err := idx.jobStore.EnqueueJob(job.Job{
55+
Dir: mcHandle,
56+
Type: op.OpTypeLoadModuleMetadata.String(),
57+
Func: func(ctx context.Context) error {
58+
return module.LoadModuleMetadata(idx.modStore, mcPath)
59+
},
60+
})
61+
if err != nil {
62+
errs = multierror.Append(errs, err)
63+
} else {
64+
ids = append(ids, id)
65+
}
66+
67+
rIds, err := idx.collectReferences(ctx, mcHandle)
68+
if err != nil {
69+
errs = multierror.Append(errs, err)
70+
} else {
71+
ids = append(ids, rIds...)
72+
}
73+
74+
return ids, errs.ErrorOrNil()
75+
},
76+
})
77+
if err != nil {
78+
multierror.Append(errs, err)
79+
continue
80+
}
81+
jobIds = append(jobIds, id)
82+
83+
id, err = idx.jobStore.EnqueueJob(job.Job{
84+
Dir: mcHandle,
85+
Func: func(ctx context.Context) error {
86+
return module.ParseVariables(idx.fs, idx.modStore, mcPath)
87+
},
88+
Type: op.OpTypeParseVariables.String(),
89+
Defer: func(ctx context.Context, jobErr error) (job.IDs, error) {
90+
ids := make(job.IDs, 0)
91+
id, err = idx.jobStore.EnqueueJob(job.Job{
92+
Dir: mcHandle,
93+
Func: func(ctx context.Context) error {
94+
return module.DecodeVarsReferences(ctx, idx.modStore, idx.schemaStore, mcPath)
95+
},
96+
Type: op.OpTypeDecodeVarsReferences.String(),
97+
})
98+
if err != nil {
99+
return ids, err
100+
}
101+
ids = append(ids, id)
102+
return ids, err
103+
},
104+
})
105+
if err != nil {
106+
multierror.Append(errs, err)
107+
continue
108+
}
109+
jobIds = append(jobIds, id)
110+
}
111+
112+
return jobIds, errs.ErrorOrNil()
113+
}
114+
}
115+
116+
func (idx *Indexer) collectReferences(ctx context.Context, modHandle document.DirHandle) (job.IDs, error) {
117+
ids := make(job.IDs, 0)
118+
119+
var errs *multierror.Error
120+
121+
id, err := idx.jobStore.EnqueueJob(job.Job{
122+
Dir: modHandle,
123+
Func: func(ctx context.Context) error {
124+
return module.DecodeReferenceTargets(ctx, idx.modStore, idx.schemaStore, modHandle.Path())
125+
},
126+
Type: op.OpTypeDecodeReferenceTargets.String(),
127+
})
128+
if err != nil {
129+
errs = multierror.Append(errs, err)
130+
} else {
131+
ids = append(ids, id)
132+
}
133+
134+
id, err = idx.jobStore.EnqueueJob(job.Job{
135+
Dir: modHandle,
136+
Func: func(ctx context.Context) error {
137+
return module.DecodeReferenceOrigins(ctx, idx.modStore, idx.schemaStore, modHandle.Path())
138+
},
139+
Type: op.OpTypeDecodeReferenceOrigins.String(),
140+
})
141+
if err != nil {
142+
errs = multierror.Append(errs, err)
143+
} else {
144+
ids = append(ids, id)
145+
}
146+
147+
return ids, errs.ErrorOrNil()
148+
}

0 commit comments

Comments
 (0)