Skip to content

Commit af738e0

Browse files
authored
deps: Remove github.com/hashicorp/go-multierror direct dependency (#1247)
Reference: hashicorp/terraform-plugin-testing#99 Similar to terraform-plugin-testing, now that this Go module is Go 1.20+, we can use native `errors.Join()` functionality for joining multiple errors. To fully remove the dependency, will need to update some other dependencies as well, e.g. ``` # github.com/hashicorp/go-multierror github.com/hashicorp/terraform-plugin-sdk/v2/internal/plugintest github.com/hashicorp/hc-install github.com/hashicorp/go-multierror ```
1 parent 6b58e7f commit af738e0

File tree

8 files changed

+35
-39
lines changed

8 files changed

+35
-39
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require (
66
github.com/google/go-cmp v0.5.9
77
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
88
github.com/hashicorp/go-hclog v1.5.0
9-
github.com/hashicorp/go-multierror v1.1.1
109
github.com/hashicorp/go-plugin v1.5.1
1110
github.com/hashicorp/go-uuid v1.0.3
1211
github.com/hashicorp/go-version v1.6.0
@@ -35,6 +34,7 @@ require (
3534
github.com/hashicorp/errwrap v1.0.0 // indirect
3635
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
3736
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
37+
github.com/hashicorp/go-multierror v1.1.1 // indirect
3838
github.com/hashicorp/terraform-registry-address v0.2.2 // indirect
3939
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
4040
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect

helper/customdiff/compose.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ package customdiff
55

66
import (
77
"context"
8-
9-
"github.com/hashicorp/go-multierror"
8+
"errors"
109

1110
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1211
)
@@ -48,14 +47,14 @@ import (
4847
// }
4948
func All(funcs ...schema.CustomizeDiffFunc) schema.CustomizeDiffFunc {
5049
return func(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error {
51-
var err error
50+
var errs []error
5251
for _, f := range funcs {
5352
thisErr := f(ctx, d, meta)
5453
if thisErr != nil {
55-
err = multierror.Append(err, thisErr)
54+
errs = append(errs, thisErr)
5655
}
5756
}
58-
return err
57+
return errors.Join(errs...)
5958
}
6059
}
6160

helper/customdiff/compose_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,30 @@ package customdiff
66
import (
77
"context"
88
"errors"
9-
"strings"
109
"testing"
1110

1211
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1312
)
1413

1514
func TestAll(t *testing.T) {
1615
var aCalled, bCalled, cCalled bool
16+
aErr := errors.New("A bad")
17+
cErr := errors.New("C bad")
1718

1819
provider := testProvider(
1920
map[string]*schema.Schema{},
2021
All(
2122
func(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
2223
aCalled = true
23-
return errors.New("A bad")
24+
return aErr
2425
},
2526
func(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
2627
bCalled = true
2728
return nil
2829
},
2930
func(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
3031
cCalled = true
31-
return errors.New("C bad")
32+
return cErr
3233
},
3334
),
3435
)
@@ -46,11 +47,11 @@ func TestAll(t *testing.T) {
4647
if err == nil {
4748
t.Fatal("Diff succeeded; want error")
4849
}
49-
if s, sub := err.Error(), "* A bad"; !strings.Contains(s, sub) {
50-
t.Errorf("Missing substring %q in error message %q", sub, s)
50+
if !errors.Is(err, aErr) {
51+
t.Errorf("Missing substring %q in error message %q", aErr, err)
5152
}
52-
if s, sub := err.Error(), "* C bad"; !strings.Contains(s, sub) {
53-
t.Errorf("Missing substring %q in error message %q", sub, s)
53+
if !errors.Is(err, cErr) {
54+
t.Errorf("Missing substring %q in error message %q", cErr, err)
5455
}
5556

5657
if !aCalled {

helper/resource/testing.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"strings"
1616
"time"
1717

18-
"github.com/hashicorp/go-multierror"
1918
"github.com/mitchellh/go-testing-interface"
2019

2120
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
@@ -836,15 +835,15 @@ func ComposeTestCheckFunc(fs ...TestCheckFunc) TestCheckFunc {
836835
// TestCheckFuncs and aggregates failures.
837836
func ComposeAggregateTestCheckFunc(fs ...TestCheckFunc) TestCheckFunc {
838837
return func(s *terraform.State) error {
839-
var result *multierror.Error
838+
var result []error
840839

841840
for i, f := range fs {
842841
if err := f(s); err != nil {
843-
result = multierror.Append(result, fmt.Errorf("Check %d/%d error: %s", i+1, len(fs), err))
842+
result = append(result, fmt.Errorf("Check %d/%d error: %w", i+1, len(fs), err))
844843
}
845844
}
846845

847-
return result.ErrorOrNil()
846+
return errors.Join(result...)
848847
}
849848
}
850849

helper/resource/testing_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"strings"
1414
"testing"
1515

16-
"github.com/hashicorp/go-multierror"
1716
testinginterface "github.com/mitchellh/go-testing-interface"
1817

1918
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -88,26 +87,27 @@ func TestParallelTest(t *testing.T) {
8887
}
8988

9089
func TestComposeAggregateTestCheckFunc(t *testing.T) {
90+
err1 := errors.New("Error 1")
9191
check1 := func(s *terraform.State) error {
92-
return errors.New("Error 1")
92+
return err1
9393
}
9494

95+
err2 := errors.New("Error 2")
9596
check2 := func(s *terraform.State) error {
96-
return errors.New("Error 2")
97+
return err2
9798
}
9899

99100
f := ComposeAggregateTestCheckFunc(check1, check2)
100101
err := f(nil)
101102
if err == nil {
102-
t.Fatalf("Expected errors")
103+
t.Fatal("expected error, got none")
103104
}
104105

105-
multi := err.(*multierror.Error)
106-
if !strings.Contains(multi.Errors[0].Error(), "Error 1") {
107-
t.Fatalf("Expected Error 1, Got %s", multi.Errors[0])
106+
if !errors.Is(err, err1) {
107+
t.Errorf("expected %s, got: %s", err1, err)
108108
}
109-
if !strings.Contains(multi.Errors[1].Error(), "Error 2") {
110-
t.Fatalf("Expected Error 2, Got %s", multi.Errors[1])
109+
if !errors.Is(err, err2) {
110+
t.Errorf("expected %s, got: %s", err2, err)
111111
}
112112
}
113113

helper/schema/provider.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
"sort"
1313
"strings"
1414

15-
"github.com/hashicorp/go-multierror"
16-
1715
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
1816
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1917
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/configs/configschema"
@@ -130,10 +128,10 @@ func (p *Provider) InternalValidate() error {
130128
return errors.New("ConfigureFunc and ConfigureContextFunc must not both be set")
131129
}
132130

133-
var validationErrors error
131+
var validationErrors []error
134132
sm := schemaMap(p.Schema)
135133
if err := sm.InternalValidate(sm); err != nil {
136-
validationErrors = multierror.Append(validationErrors, err)
134+
validationErrors = append(validationErrors, err)
137135
}
138136

139137
// Provider-specific checks
@@ -145,17 +143,17 @@ func (p *Provider) InternalValidate() error {
145143

146144
for k, r := range p.ResourcesMap {
147145
if err := r.InternalValidate(nil, true); err != nil {
148-
validationErrors = multierror.Append(validationErrors, fmt.Errorf("resource %s: %s", k, err))
146+
validationErrors = append(validationErrors, fmt.Errorf("resource %s: %s", k, err))
149147
}
150148
}
151149

152150
for k, r := range p.DataSourcesMap {
153151
if err := r.InternalValidate(nil, false); err != nil {
154-
validationErrors = multierror.Append(validationErrors, fmt.Errorf("data source %s: %s", k, err))
152+
validationErrors = append(validationErrors, fmt.Errorf("data source %s: %s", k, err))
155153
}
156154
}
157155

158-
return validationErrors
156+
return errors.Join(validationErrors...)
159157
}
160158

161159
func isReservedProviderFieldName(name string) bool {

internal/diagutils/diagutils.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"errors"
88
"fmt"
99

10-
"github.com/hashicorp/go-multierror"
1110
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1211
)
1312

@@ -28,7 +27,7 @@ func (diags ErrorDiags) Errors() []error {
2827
}
2928

3029
func (diags ErrorDiags) Error() string {
31-
return multierror.ListFormatFunc(diags.Errors())
30+
return errors.Join(diags.Errors()...).Error()
3231
}
3332

3433
type WarningDiags diag.Diagnostics

terraform/state.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"bufio"
88
"bytes"
99
"encoding/json"
10+
"errors"
1011
"fmt"
1112
"log"
1213
"os"
@@ -17,7 +18,6 @@ import (
1718
"sync"
1819

1920
"github.com/hashicorp/go-cty/cty"
20-
"github.com/hashicorp/go-multierror"
2121
"github.com/hashicorp/go-uuid"
2222
"github.com/mitchellh/copystructure"
2323

@@ -287,7 +287,7 @@ func (s *State) Validate() error {
287287
s.Lock()
288288
defer s.Unlock()
289289

290-
var result error
290+
var result []error
291291

292292
// !!!! FOR DEVELOPERS !!!!
293293
//
@@ -309,7 +309,7 @@ func (s *State) Validate() error {
309309

310310
key := strings.Join(ms.Path, ".")
311311
if _, ok := found[key]; ok {
312-
result = multierror.Append(result, fmt.Errorf(
312+
result = append(result, fmt.Errorf(
313313
strings.TrimSpace(stateValidateErrMultiModule), key))
314314
continue
315315
}
@@ -318,7 +318,7 @@ func (s *State) Validate() error {
318318
}
319319
}
320320

321-
return result
321+
return errors.Join(result...)
322322
}
323323

324324
// Remove removes the item in the state at the given address, returning

0 commit comments

Comments
 (0)