Skip to content

Commit f072d55

Browse files
authored
dev: add doc about internal package extracted from Go (#3204)
1 parent b1cec47 commit f072d55

File tree

12 files changed

+65
-44
lines changed

12 files changed

+65
-44
lines changed

.golangci.yml

+4-5
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,8 @@ issues:
138138

139139
run:
140140
timeout: 5m
141-
go: '1.17' # TODO(ldez): we force to use an old version of Go for the CI and the tests.
142141
skip-dirs:
143-
- test/testdata_etc
144-
- internal/cache
145-
- internal/renameio
146-
- internal/robustio
142+
- test/testdata_etc # test files
143+
- internal/cache # extracted from Go code
144+
- internal/renameio # extracted from Go code
145+
- internal/robustio # extracted from Go code

internal/cache/cache.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ type Cache struct {
5151
// to share a cache directory (for example, if the directory were stored
5252
// in a network file system). File locking is notoriously unreliable in
5353
// network file systems and may not suffice to protect the cache.
54-
//
5554
func Open(dir string) (*Cache, error) {
5655
info, err := os.Stat(dir)
5756
if err != nil {
@@ -159,7 +158,7 @@ func (c *Cache) get(id ActionID) (Entry, error) {
159158
defer f.Close()
160159
entry := make([]byte, entrySize+1) // +1 to detect whether f is too long
161160
if n, readErr := io.ReadFull(f, entry); n != entrySize || readErr != io.ErrUnexpectedEOF {
162-
return failed(fmt.Errorf("read %d/%d bytes from %s with error %s", n, entrySize, fileName, readErr))
161+
return failed(fmt.Errorf("read %d/%d bytes from %s with error %w", n, entrySize, fileName, readErr))
163162
}
164163
if entry[0] != 'v' || entry[1] != '1' || entry[2] != ' ' || entry[3+hexSize] != ' ' || entry[3+hexSize+1+hexSize] != ' ' || entry[3+hexSize+1+hexSize+1+20] != ' ' || entry[entrySize-1] != '\n' {
165164
return failed(fmt.Errorf("bad data in %s", fileName))
@@ -181,15 +180,15 @@ func (c *Cache) get(id ActionID) (Entry, error) {
181180
}
182181
size, err := strconv.ParseInt(string(esize[i:]), 10, 64)
183182
if err != nil || size < 0 {
184-
return failed(fmt.Errorf("failed to parse esize int from %s with error %s", fileName, err))
183+
return failed(fmt.Errorf("failed to parse esize int from %s with error %w", fileName, err))
185184
}
186185
i = 0
187186
for i < len(etime) && etime[i] == ' ' {
188187
i++
189188
}
190189
tm, err := strconv.ParseInt(string(etime[i:]), 10, 64)
191190
if err != nil || tm < 0 {
192-
return failed(fmt.Errorf("failed to parse etime int from %s with error %s", fileName, err))
191+
return failed(fmt.Errorf("failed to parse etime int from %s with error %w", fileName, err))
193192
}
194193

195194
if err = c.used(fileName); err != nil {
@@ -498,7 +497,7 @@ func (c *Cache) copyFile(file io.ReadSeeker, out OutputID, size int64) error {
498497
return err
499498
}
500499
if n, wErr := h.Write(buf); n != len(buf) {
501-
return fmt.Errorf("wrote to hash %d/%d bytes with error %s", n, len(buf), wErr)
500+
return fmt.Errorf("wrote to hash %d/%d bytes with error %w", n, len(buf), wErr)
502501
}
503502

504503
sum := h.Sum(nil)

internal/cache/readme.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# cache
2+
3+
Extracted from go/src/cmd/go/internal/cache/
4+
I don't know what version of Go this package was pulled from.
5+
6+
Adapted for golangci-lint:
7+
- https://github.com/golangci/golangci-lint/pull/699
8+
- https://github.com/golangci/golangci-lint/pull/779
9+
- https://github.com/golangci/golangci-lint/pull/788
10+
- https://github.com/golangci/golangci-lint/pull/808
11+
- https://github.com/golangci/golangci-lint/pull/1063
12+
- https://github.com/golangci/golangci-lint/pull/1070
13+
- https://github.com/golangci/golangci-lint/pull/1162
14+
- https://github.com/golangci/golangci-lint/pull/2318
15+
- https://github.com/golangci/golangci-lint/pull/2352
16+
- https://github.com/golangci/golangci-lint/pull/3012
17+
- https://github.com/golangci/golangci-lint/pull/3096
18+
- https://github.com/golangci/golangci-lint/pull/3204

internal/renameio/readme.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# renameio
2+
3+
Extracted from go/src/cmd/go/internal/renameio/
4+
I don't know what version of Go this package was pulled from.
5+
6+
Adapted for golangci-lint:
7+
- https://github.com/golangci/golangci-lint/pull/699
8+
- https://github.com/golangci/golangci-lint/pull/808
9+
- https://github.com/golangci/golangci-lint/pull/1063
10+
- https://github.com/golangci/golangci-lint/pull/3204

internal/renameio/umask_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build !plan9,!windows,!js
5+
//go:build !plan9 && !windows && !js
66

77
package renameio
88

internal/robustio/readme.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# robustio
2+
3+
Extracted from go1.19.1/src/cmd/go/internal/robustio
4+
5+
There is only one modification:
6+
- ERROR_SHARING_VIOLATION extracted from go1.19.1/src/internal/syscall/windows/syscall_windows.go to remove the dependencies to `internal/syscall/windows`

internal/robustio/robustio.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ func RemoveAll(path string) error {
4242
// in this package attempt to mitigate.
4343
//
4444
// Errors considered ephemeral include:
45-
// - syscall.ERROR_ACCESS_DENIED
46-
// - syscall.ERROR_FILE_NOT_FOUND
47-
// - internal/syscall/windows.ERROR_SHARING_VIOLATION
45+
// - syscall.ERROR_ACCESS_DENIED
46+
// - syscall.ERROR_FILE_NOT_FOUND
47+
// - internal/syscall/windows.ERROR_SHARING_VIOLATION
4848
//
4949
// This set may be expanded in the future; programs must not rely on the
5050
// non-ephemerality of any given error.

internal/robustio/robustio_darwin.go

+3-11
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,16 @@
55
package robustio
66

77
import (
8-
"os"
8+
"errors"
99
"syscall"
1010
)
1111

1212
const errFileNotFound = syscall.ENOENT
1313

1414
// isEphemeralError returns true if err may be resolved by waiting.
1515
func isEphemeralError(err error) bool {
16-
switch werr := err.(type) {
17-
case *os.PathError:
18-
err = werr.Err
19-
case *os.LinkError:
20-
err = werr.Err
21-
case *os.SyscallError:
22-
err = werr.Err
23-
24-
}
25-
if errno, ok := err.(syscall.Errno); ok {
16+
var errno syscall.Errno
17+
if errors.As(err, &errno) {
2618
return errno == errFileNotFound
2719
}
2820
return false

internal/robustio/robustio_flaky.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build windows darwin
5+
//go:build windows || darwin
66

77
package robustio
88

99
import (
10+
"errors"
1011
"math/rand"
1112
"os"
1213
"syscall"
1314
"time"
1415
)
1516

16-
const arbitraryTimeout = 500 * time.Millisecond
17-
18-
const ERROR_SHARING_VIOLATION = 32
17+
const arbitraryTimeout = 2000 * time.Millisecond
1918

2019
// retry retries ephemeral errors from f up to an arbitrary timeout
2120
// to work around filesystem flakiness on Windows and Darwin.
@@ -32,7 +31,8 @@ func retry(f func() (err error, mayRetry bool)) error {
3231
return err
3332
}
3433

35-
if errno, ok := err.(syscall.Errno); ok && (lowestErrno == 0 || errno < lowestErrno) {
34+
var errno syscall.Errno
35+
if errors.As(err, &errno) && (lowestErrno == 0 || errno < lowestErrno) {
3636
bestErr = err
3737
lowestErrno = errno
3838
} else if bestErr == nil {
@@ -78,8 +78,7 @@ func readFile(filename string) ([]byte, error) {
7878
// Unlike in rename, we do not retry errFileNotFound here: it can occur
7979
// as a spurious error, but the file may also genuinely not exist, so the
8080
// increase in robustness is probably not worth the extra latency.
81-
82-
return err, isEphemeralError(err) && err != errFileNotFound
81+
return err, isEphemeralError(err) && !errors.Is(err, errFileNotFound)
8382
})
8483
return b, err
8584
}

internal/robustio/robustio_other.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//+build !windows,!darwin
5+
//go:build !windows && !darwin
66

77
package robustio
88

internal/robustio/robustio_windows.go

+7-10
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,20 @@
55
package robustio
66

77
import (
8-
"os"
8+
"errors"
99
"syscall"
1010
)
1111

1212
const errFileNotFound = syscall.ERROR_FILE_NOT_FOUND
1313

14+
// ERROR_SHARING_VIOLATION (ldez) extract from go1.19.1/src/internal/syscall/windows/syscall_windows.go.
15+
// This is the only modification of this file.
16+
const ERROR_SHARING_VIOLATION syscall.Errno = 32
17+
1418
// isEphemeralError returns true if err may be resolved by waiting.
1519
func isEphemeralError(err error) bool {
16-
switch werr := err.(type) {
17-
case *os.PathError:
18-
err = werr.Err
19-
case *os.LinkError:
20-
err = werr.Err
21-
case *os.SyscallError:
22-
err = werr.Err
23-
}
24-
if errno, ok := err.(syscall.Errno); ok {
20+
var errno syscall.Errno
21+
if errors.As(err, &errno) {
2522
switch errno {
2623
case syscall.ERROR_ACCESS_DENIED,
2724
syscall.ERROR_FILE_NOT_FOUND,

pkg/golinters/goanalysis/runner_action.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package goanalysis
33
import (
44
"fmt"
55
"go/types"
6+
"io"
67
"reflect"
78
"runtime/debug"
89
"time"
@@ -331,7 +332,7 @@ func (act *action) loadPersistedFacts() bool {
331332
var facts []Fact
332333
key := fmt.Sprintf("%s/facts", act.a.Name)
333334
if err := act.r.pkgCache.Get(act.pkg, pkgcache.HashModeNeedAllDeps, key, &facts); err != nil {
334-
if err != pkgcache.ErrMissing {
335+
if !errors.Is(err, pkgcache.ErrMissing) && !errors.Is(err, io.EOF) {
335336
act.r.log.Warnf("Failed to get persisted facts: %s", err)
336337
}
337338

0 commit comments

Comments
 (0)