Skip to content

Commit 7c0cd41

Browse files
authored
Merge pull request #68 from knz/20210413-go116
Fix support for Go 1.16.
2 parents c7e33ef + e0af124 commit 7c0cd41

18 files changed

+181
-73
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ go:
44
- 1.13.x
55
- 1.14.x
66
- 1.15.x
7+
- 1.16.x

errbase/adapters.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,11 @@ func init() {
191191
ws := pkgErr.WithStack(baseErr)
192192
RegisterWrapperEncoder(GetTypeKey(ws), encodePkgWithStack)
193193

194+
registerOsPathErrorMigration() // Needed for Go 1.16.
194195
pKey := GetTypeKey(&os.PathError{})
195196
RegisterWrapperEncoder(pKey, encodePathError)
196197
RegisterWrapperDecoder(pKey, decodePathError)
198+
197199
pKey = GetTypeKey(&os.LinkError{})
198200
RegisterWrapperEncoder(pKey, encodeLinkError)
199201
RegisterWrapperDecoder(pKey, decodeLinkError)

errbase/adapters_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,13 @@ func TestAdaptOsErrors(t *testing.T) {
180180

181181
origErr = &os.PathError{Op: "hello", Path: "world", Err: goErr.New("woo")}
182182
newErr := network(t, origErr)
183-
tt.Check(reflect.DeepEqual(newErr, origErr))
183+
tt.CheckDeepEqual(newErr, origErr)
184184

185185
origErr = &os.LinkError{Op: "hello", Old: "world", New: "universe", Err: goErr.New("woo")}
186186
newErr = network(t, origErr)
187-
tt.Check(reflect.DeepEqual(newErr, origErr))
187+
tt.CheckDeepEqual(newErr, origErr)
188188

189189
origErr = &os.SyscallError{Syscall: "hello", Err: goErr.New("woo")}
190190
newErr = network(t, origErr)
191-
tt.Check(reflect.DeepEqual(newErr, origErr))
191+
tt.CheckDeepEqual(newErr, origErr)
192192
}

errbase/oserror_go116.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2021 The Cockroach Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
// implied. See the License for the specific language governing
13+
// permissions and limitations under the License.
14+
15+
// +build go1.16
16+
17+
package errbase
18+
19+
import "io/fs"
20+
21+
func registerOsPathErrorMigration() {
22+
// The os.PathError type was migrated to io.fs.PathError in Go 1.16.
23+
RegisterTypeMigration("os", "*os.PathError", &fs.PathError{})
24+
}

errbase/oserror_pre116.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2021 The Cockroach Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
// implied. See the License for the specific language governing
13+
// permissions and limitations under the License.
14+
15+
// +build !go1.16
16+
17+
package errbase
18+
19+
func registerOsPathErrorMigration() {}

fmttests/datadriven_test_go116.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2021 The Cockroach Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
// implied. See the License for the specific language governing
13+
// permissions and limitations under the License.
14+
15+
// +build go1.16
16+
17+
package fmttests
18+
19+
func fakeGo116(s string) string {
20+
// We are at 1.16 already. Nothing to fake.
21+
return s
22+
}

fmttests/datadriven_test_pre116.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2021 The Cockroach Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
// implied. See the License for the specific language governing
13+
// permissions and limitations under the License.
14+
15+
// +build !go1.16
16+
17+
package fmttests
18+
19+
import "strings"
20+
21+
func fakeGo116(s string) string {
22+
// In Go 1.16, the canonical type for os.PathError is io/fs/*fs.PathError.
23+
// So when running the tests with a pre-1.16 runtime, the strings
24+
// emitted by printing out the error objects don't match the output
25+
// expected in the tests, which was generated with go 1.16.
26+
s = strings.ReplaceAll(s, "os/*os.PathError (*::)", "io/fs/*fs.PathError (os/*os.PathError::)")
27+
s = strings.ReplaceAll(s, " *os.PathError", " *fs.PathError")
28+
s = strings.ReplaceAll(s, "\n*os.PathError", "\n*fs.PathError")
29+
s = strings.ReplaceAll(s, "&os.PathError", "&fs.PathError")
30+
return s
31+
}

fmttests/format_error_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,15 @@ func fmtClean(spv string) string {
367367
spv = stackref.ReplaceAllString(spv, `&stack{...}`)
368368
spv = hexref.ReplaceAllString(spv, "0xAAAABBBB")
369369
spv = strings.ReplaceAll(spv, "\t", "<tab>")
370+
371+
// When running the tests with a Go version before 1.16,
372+
// the reference test output wrt fs.PathError will not match what the
373+
// Go runtime thinks (os.PathError was a separate type).
374+
//
375+
// In that case, we translate the test output as per the old
376+
// runtime into the shape expected in 1.16 or later.
377+
spv = fakeGo116(spv)
378+
370379
return spv
371380
}
372381

fmttests/testdata/format/wrap-fmt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,7 +2330,7 @@ accept %\#v via Formattable.*IRREGULAR: not same as %\#v
23302330

23312331
require (?s)
23322332
----
2333-
&os.PathError{
2333+
&fs.PathError{
23342334
Op: "link",
23352335
Path: "/path/to/file",
23362336
Err: &fmttests.errFmt{msg:"innerone\ninnertwo"},
@@ -2339,7 +2339,7 @@ require (?s)
23392339
===== non-redactable formats
23402340
=====
23412341
== %#v
2342-
&os.PathError{Op:"link", Path:"/path/to/file", Err:&fmttests.errFmt{msg:"innerone\ninnertwo"}}
2342+
&fs.PathError{Op:"link", Path:"/path/to/file", Err:&fmttests.errFmt{msg:"innerone\ninnertwo"}}
23432343
== Error()
23442344
link /path/to/file: innerone
23452345
innertwo
@@ -2350,7 +2350,7 @@ innertwo
23502350
== %X = HEX Error(), good
23512351
== %+v = Error(), ok
23522352
== %#v via Formattable() (IRREGULAR: not same as %#v)
2353-
&os.PathError{
2353+
&fs.PathError{
23542354
Op: "link",
23552355
Path: "/path/to/file",
23562356
Err: &fmttests.errFmt{msg:"innerone\ninnertwo"},
@@ -2366,7 +2366,7 @@ Wraps: (2) innerone
23662366
| -- this is innerone
23672367
| innertwo's
23682368
| multi-line leaf payload
2369-
Error types: (1) *os.PathError (2) *fmttests.errFmt
2369+
Error types: (1) *fs.PathError (2) *fmttests.errFmt
23702370
=====
23712371
===== redactable formats
23722372
=====
@@ -2386,7 +2386,7 @@ Wraps: (2) ‹innerone›‹›
23862386
‹ | -- this is innerone›
23872387
‹ | innertwo's›
23882388
‹ | multi-line leaf payload›
2389-
Error types: (1) *os.PathError (2) *fmttests.errFmt
2389+
Error types: (1) *fs.PathError (2) *fmttests.errFmt
23902390
=====
23912391
===== Sentry reporting
23922392
=====
@@ -2395,10 +2395,10 @@ link ×: ×
23952395
×
23962396
--
23972397
*fmttests.errFmt
2398-
*os.PathError
2398+
*fs.PathError
23992399
== Extra "error types"
24002400
github.com/cockroachdb/errors/fmttests/*fmttests.errFmt (*::)
2401-
os/*os.PathError (*::)
2401+
io/fs/*fs.PathError (os/*os.PathError::)
24022402
== Exception 1 (Module: "error domain: <none>")
24032403
Type: "*fmttests.errFmt"
24042404
Title: "link ×: ×\n×"

fmttests/testdata/format/wrap-fmt-via-network

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2992,7 +2992,7 @@ accept %\#v via Formattable.*IRREGULAR: not same as %\#v
29922992

29932993
require (?s)innerone.*innertwo
29942994
----
2995-
&os.PathError{
2995+
&fs.PathError{
29962996
Op: "link",
29972997
Path: "/path/to/file",
29982998
Err: &errbase.opaqueLeaf{
@@ -3009,7 +3009,7 @@ require (?s)innerone.*innertwo
30093009
===== non-redactable formats
30103010
=====
30113011
== %#v
3012-
&os.PathError{Op:"link", Path:"/path/to/file", Err:&errbase.opaqueLeaf{
3012+
&fs.PathError{Op:"link", Path:"/path/to/file", Err:&errbase.opaqueLeaf{
30133013
msg: "innerone\ninnertwo",
30143014
details: errorspb.EncodedErrorDetails{
30153015
OriginalTypeName: "github.com/cockroachdb/errors/fmttests/*fmttests.errFmt",
@@ -3028,7 +3028,7 @@ innertwo
30283028
== %X = HEX Error(), good
30293029
== %+v = Error(), ok
30303030
== %#v via Formattable() (IRREGULAR: not same as %#v)
3031-
&os.PathError{
3031+
&fs.PathError{
30323032
Op: "link",
30333033
Path: "/path/to/file",
30343034
Err: &errbase.opaqueLeaf{
@@ -3052,7 +3052,7 @@ Wraps: (2) innerone
30523052
|
30533053
| (opaque error leaf)
30543054
| type name: github.com/cockroachdb/errors/fmttests/*fmttests.errFmt
3055-
Error types: (1) *os.PathError (2) *errbase.opaqueLeaf
3055+
Error types: (1) *fs.PathError (2) *errbase.opaqueLeaf
30563056
=====
30573057
===== redactable formats
30583058
=====
@@ -3072,7 +3072,7 @@ Wraps: (2) ‹innerone›
30723072
|
30733073
| (opaque error leaf)
30743074
| type name: github.com/cockroachdb/errors/fmttests/*fmttests.errFmt
3075-
Error types: (1) *os.PathError (2) *errbase.opaqueLeaf
3075+
Error types: (1) *fs.PathError (2) *errbase.opaqueLeaf
30763076
=====
30773077
===== Sentry reporting
30783078
=====
@@ -3081,10 +3081,10 @@ link ×: ×
30813081
×
30823082
--
30833083
*fmttests.errFmt
3084-
*os.PathError
3084+
*fs.PathError
30853085
== Extra "error types"
30863086
github.com/cockroachdb/errors/fmttests/*fmttests.errFmt (*::)
3087-
os/*os.PathError (*::)
3087+
io/fs/*fs.PathError (os/*os.PathError::)
30883088
== Exception 1 (Module: "error domain: <none>")
30893089
Type: "*fmttests.errFmt"
30903090
Title: "link ×: ×\n×"

0 commit comments

Comments
 (0)