Skip to content

Commit cf616fb

Browse files
committed
chore: sync with go1.23.2
1 parent 80be0eb commit cf616fb

File tree

12 files changed

+75
-51
lines changed

12 files changed

+75
-51
lines changed

Diff for: internal/cache/cache.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -162,22 +162,22 @@ var DebugTest = false
162162
// func init() { initEnv() }
163163

164164
// var (
165-
// goCacheVerify = godebug.New("gocacheverify")
166-
// goDebugHash = godebug.New("gocachehash")
167-
// goCacheTest = godebug.New("gocachetest")
165+
// gocacheverify = godebug.New("gocacheverify")
166+
// gocachehash = godebug.New("gocachehash")
167+
// gocachetest = godebug.New("gocachetest")
168168
// )
169169

170170
// func initEnv() {
171-
// if goCacheVerify.Value() == "1" {
172-
// goCacheVerify.IncNonDefault()
171+
// if gocacheverify.Value() == "1" {
172+
// gocacheverify.IncNonDefault()
173173
// verify = true
174174
// }
175-
// if goDebugHash.Value() == "1" {
176-
// goDebugHash.IncNonDefault()
175+
// if gocachehash.Value() == "1" {
176+
// gocachehash.IncNonDefault()
177177
// debugHash = true
178178
// }
179-
// if goCacheTest.Value() == "1" {
180-
// goCacheTest.IncNonDefault()
179+
// if gocachetest.Value() == "1" {
180+
// gocachetest.IncNonDefault()
181181
// DebugTest = true
182182
// }
183183
// }

Diff for: internal/cache/default.go

+33-14
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ import (
1313
)
1414

1515
// Default returns the default cache to use.
16-
func Default() (*DiskCache, error) {
16+
// It never returns nil.
17+
func Default() Cache {
1718
defaultOnce.Do(initDefaultCache)
18-
return defaultCache, defaultDirErr
19+
return defaultCache
1920
}
2021

2122
var (
2223
defaultOnce sync.Once
23-
defaultCache *DiskCache
24+
defaultCache Cache
2425
)
2526

2627
// cacheREADME is a message stored in a README in the cache directory.
@@ -32,7 +33,13 @@ const cacheREADME = `This directory holds cached build artifacts from golangci-l
3233
// initDefaultCache does the work of finding the default cache
3334
// the first time Default is called.
3435
func initDefaultCache() {
35-
dir := DefaultDir()
36+
dir, _ := DefaultDir()
37+
if dir == "off" {
38+
if defaultDirErr != nil {
39+
base.Fatalf("build cache is required, but could not be located: %v", defaultDirErr)
40+
}
41+
base.Fatalf("build cache is disabled by %s=off, but required", envGolangciLintCache)
42+
}
3643
if err := os.MkdirAll(dir, 0744); err != nil {
3744
base.Fatalf("failed to initialize build cache at %s: %s\n", dir, err)
3845
}
@@ -43,44 +50,56 @@ func initDefaultCache() {
4350
}
4451
}
4552

46-
c, err := Open(dir)
53+
diskCache, err := Open(dir)
4754
if err != nil {
4855
base.Fatalf("failed to initialize build cache at %s: %s\n", dir, err)
4956
}
50-
defaultCache = c
57+
58+
if v := os.Getenv(envGolangciLintCacheProg); v != "" {
59+
defaultCache = startCacheProg(v, diskCache)
60+
} else {
61+
defaultCache = diskCache
62+
}
5163
}
5264

5365
var (
54-
defaultDirOnce sync.Once
55-
defaultDir string
56-
defaultDirErr error
66+
defaultDirOnce sync.Once
67+
defaultDir string
68+
defaultDirChanged bool // effective value differs from $GOLANGCI_LINT_CACHE
69+
defaultDirErr error
5770
)
5871

5972
// DefaultDir returns the effective GOLANGCI_LINT_CACHE setting.
60-
func DefaultDir() string {
73+
// It returns "off" if the cache is disabled,
74+
// and reports whether the effective value differs from GOLANGCI_LINT_CACHE.
75+
func DefaultDir() (string, bool) {
6176
// Save the result of the first call to DefaultDir for later use in
6277
// initDefaultCache. cmd/go/main.go explicitly sets GOCACHE so that
6378
// subprocesses will inherit it, but that means initDefaultCache can't
6479
// otherwise distinguish between an explicit "off" and a UserCacheDir error.
6580

6681
defaultDirOnce.Do(func() {
6782
defaultDir = os.Getenv(envGolangciLintCache)
68-
if filepath.IsAbs(defaultDir) {
69-
return
70-
}
7183
if defaultDir != "" {
84+
defaultDirChanged = true
85+
if filepath.IsAbs(defaultDir) || defaultDir == "off" {
86+
return
87+
}
88+
defaultDir = "off"
7289
defaultDirErr = fmt.Errorf("%s is not an absolute path", envGolangciLintCache)
7390
return
7491
}
7592

7693
// Compute default location.
7794
dir, err := os.UserCacheDir()
7895
if err != nil {
96+
defaultDir = "off"
97+
defaultDirChanged = true
7998
defaultDirErr = fmt.Errorf("%s is not defined and %w", envGolangciLintCache, err)
8099
return
81100
}
82101
defaultDir = filepath.Join(dir, "golangci-lint")
83102
})
84103

85-
return defaultDir
104+
return defaultDir, defaultDirChanged
86105
}

Diff for: internal/cache/default_gcil.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
package cache
22

3-
const envGolangciLintCache = "GOLANGCI_LINT_CACHE"
3+
const (
4+
envGolangciLintCache = "GOLANGCI_LINT_CACHE"
5+
envGolangciLintCacheProg = "GOLANGCI_LINT_CACHEPROG"
6+
)

Diff for: internal/cache/prog.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ const (
7676
)
7777

7878
// ProgRequest is the JSON-encoded message that's sent from cmd/go to
79-
// the GOCACHEPROG child process over stdin. Each JSON object is on its
79+
// the GOLANGCI_LINT_CACHEPROG child process over stdin. Each JSON object is on its
8080
// own line. A ProgRequest of Type "put" with BodySize > 0 will be followed
8181
// by a line containing a base64-encoded JSON string literal of the body.
8282
type ProgRequest struct {
@@ -152,7 +152,7 @@ func startCacheProg(progAndArgs string, fuzzDirCache Cache) Cache {
152152
}
153153
args, err := quoted.Split(progAndArgs)
154154
if err != nil {
155-
base.Fatalf("GOCACHEPROG args: %v", err)
155+
base.Fatalf("%s args: %v", envGolangciLintCacheProg, err)
156156
}
157157
var prog string
158158
if len(args) > 0 {
@@ -165,17 +165,17 @@ func startCacheProg(progAndArgs string, fuzzDirCache Cache) Cache {
165165
cmd := exec.CommandContext(ctx, prog, args...)
166166
out, err := cmd.StdoutPipe()
167167
if err != nil {
168-
base.Fatalf("StdoutPipe to GOCACHEPROG: %v", err)
168+
base.Fatalf("StdoutPipe to %s: %v", envGolangciLintCacheProg, err)
169169
}
170170
in, err := cmd.StdinPipe()
171171
if err != nil {
172-
base.Fatalf("StdinPipe to GOCACHEPROG: %v", err)
172+
base.Fatalf("StdinPipe to %s: %v", envGolangciLintCacheProg, err)
173173
}
174174
cmd.Stderr = os.Stderr
175175
cmd.Cancel = in.Close
176176

177177
if err := cmd.Start(); err != nil {
178-
base.Fatalf("error starting GOCACHEPROG program %q: %v", prog, err)
178+
base.Fatalf("error starting %s program %q: %v", envGolangciLintCacheProg, prog, err)
179179
}
180180

181181
pc := &ProgCache{
@@ -206,14 +206,14 @@ func startCacheProg(progAndArgs string, fuzzDirCache Cache) Cache {
206206
for {
207207
select {
208208
case <-timer.C:
209-
log.Printf("# still waiting for GOCACHEPROG %v ...", prog)
209+
log.Printf("# still waiting for %s %v ...", envGolangciLintCacheProg, prog)
210210
case capRes := <-capResc:
211211
can := map[ProgCmd]bool{}
212212
for _, cmd := range capRes.KnownCommands {
213213
can[cmd] = true
214214
}
215215
if len(can) == 0 {
216-
base.Fatalf("GOCACHEPROG %v declared no supported commands", prog)
216+
base.Fatalf("%s %v declared no supported commands", envGolangciLintCacheProg, prog)
217217
}
218218
pc.can = can
219219
return pc
@@ -234,9 +234,9 @@ func (c *ProgCache) readLoop(readLoopDone chan<- struct{}) {
234234
c.mu.Lock()
235235
inFlight := len(c.inFlight)
236236
c.mu.Unlock()
237-
base.Fatalf("GOCACHEPROG exited pre-Close with %v pending requests", inFlight)
237+
base.Fatalf("%s exited pre-Close with %v pending requests", envGolangciLintCacheProg, inFlight)
238238
}
239-
base.Fatalf("error reading JSON from GOCACHEPROG: %v", err)
239+
base.Fatalf("error reading JSON from %s: %v", envGolangciLintCacheProg, err)
240240
}
241241
c.mu.Lock()
242242
ch, ok := c.inFlight[res.ID]
@@ -245,7 +245,7 @@ func (c *ProgCache) readLoop(readLoopDone chan<- struct{}) {
245245
if ok {
246246
ch <- res
247247
} else {
248-
base.Fatalf("GOCACHEPROG sent response for unknown request ID %v", res.ID)
248+
base.Fatalf("%s sent response for unknown request ID %v", envGolangciLintCacheProg, res.ID)
249249
}
250250
}
251251
}
@@ -303,8 +303,8 @@ func (c *ProgCache) writeToChild(req *ProgRequest, resc chan<- *ProgResponse) (e
303303
return nil
304304
}
305305
if wrote != req.BodySize {
306-
return fmt.Errorf("short write writing body to GOCACHEPROG for action %x, object %x: wrote %v; expected %v",
307-
req.ActionID, req.ObjectID, wrote, req.BodySize)
306+
return fmt.Errorf("short write writing body to %s for action %x, object %x: wrote %v; expected %v",
307+
envGolangciLintCacheProg, req.ActionID, req.ObjectID, wrote, req.BodySize)
308308
}
309309
if _, err := c.bw.WriteString("\"\n"); err != nil {
310310
return err
@@ -346,7 +346,7 @@ func (c *ProgCache) Get(a ActionID) (Entry, error) {
346346
e.Time = time.Now()
347347
}
348348
if res.DiskPath == "" {
349-
return Entry{}, &entryNotFoundError{errors.New("GOCACHEPROG didn't populate DiskPath on get hit")}
349+
return Entry{}, &entryNotFoundError{fmt.Errorf("%s didn't populate DiskPath on get hit", envGolangciLintCacheProg)}
350350
}
351351
if copy(e.OutputID[:], res.OutputID) != len(res.OutputID) {
352352
return Entry{}, &entryNotFoundError{errors.New("incomplete ProgResponse OutputID")}
@@ -400,7 +400,7 @@ func (c *ProgCache) Put(a ActionID, file io.ReadSeeker) (_ OutputID, size int64,
400400
return OutputID{}, 0, err
401401
}
402402
if res.DiskPath == "" {
403-
return OutputID{}, 0, errors.New("GOCACHEPROG didn't return DiskPath in put response")
403+
return OutputID{}, 0, fmt.Errorf("%s didn't return DiskPath in put response", envGolangciLintCacheProg)
404404
}
405405
c.noteOutputFile(out, res.DiskPath)
406406
return out, size, err

Diff for: internal/cache/readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Extracted from `go/src/cmd/go/internal/cache/`.
44

5+
- sync with go1.23.2
56
- sync with go1.22.8
67
- sync with go1.21.13
78
- sync with go1.20.14

Diff for: internal/mmap/readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Extracted from `go/src/cmd/go/internal/mmap/` (related to `cache`).
44

5+
- sync with go1.23.2
56
- sync with go1.22.8
67
- sync with go1.21.13
78
- sync with go1.20.14

Diff for: internal/pkgcache/pkgcache.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,27 @@ const (
2828
// Cache is a per-package data cache. A cached data is invalidated when
2929
// package, or it's dependencies change.
3030
type Cache struct {
31-
lowLevelCache *cache.DiskCache
31+
lowLevelCache cache.Cache
3232
pkgHashes sync.Map
3333
sw *timeutils.Stopwatch
3434
log logutils.Log
3535
ioSem chan struct{} // semaphore limiting parallel IO
3636
}
3737

3838
func NewCache(sw *timeutils.Stopwatch, log logutils.Log) (*Cache, error) {
39-
c, err := cache.Default()
40-
if err != nil {
41-
return nil, err
42-
}
4339
return &Cache{
44-
lowLevelCache: c,
40+
lowLevelCache: cache.Default(),
4541
sw: sw,
4642
log: log,
4743
ioSem: make(chan struct{}, runtime.GOMAXPROCS(-1)),
4844
}, nil
4945
}
5046

51-
func (c *Cache) Trim() {
52-
c.sw.TrackStage("trim", func() {
53-
err := c.lowLevelCache.Trim()
47+
func (c *Cache) Close() {
48+
c.sw.TrackStage("close", func() {
49+
err := c.lowLevelCache.Close()
5450
if err != nil {
55-
c.log.Errorf("cache trim: %v", err)
51+
c.log.Errorf("cache close: %v", err)
5652
}
5753
})
5854
}

Diff for: internal/quoted/readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
Extracted from `go/src/cmd/internal/quoted/` (related to `cache`).
44

5+
- sync go1.23.2
56
- sync go1.22.8
67
- sync go1.21.13

Diff for: internal/testenv/readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ Extracted from `go/src/internal/testenv/`.
44

55
Only the function `SyscallIsNotSupported` is extracted (related to `cache`).
66

7+
- sync with go1.23.2
78
- sync with go1.22.8
89
- sync with go1.21.13

Diff for: pkg/commands/cache.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func newCacheCommand() *cacheCommand {
5151
}
5252

5353
func (*cacheCommand) executeClean(_ *cobra.Command, _ []string) error {
54-
cacheDir := cache.DefaultDir()
54+
cacheDir, _ := cache.DefaultDir()
5555

5656
if err := os.RemoveAll(cacheDir); err != nil {
5757
return fmt.Errorf("failed to remove dir %s: %w", cacheDir, err)
@@ -61,7 +61,8 @@ func (*cacheCommand) executeClean(_ *cobra.Command, _ []string) error {
6161
}
6262

6363
func (*cacheCommand) executeStatus(_ *cobra.Command, _ []string) {
64-
cacheDir := cache.DefaultDir()
64+
cacheDir, _ := cache.DefaultDir()
65+
6566
_, _ = fmt.Fprintf(logutils.StdOut, "Dir: %s\n", cacheDir)
6667

6768
cacheSizeBytes, err := dirSizeBytes(cacheDir)

Diff for: pkg/goanalysis/runner.go

-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ func (r *runner) run(analyzers []*analysis.Analyzer, initialPackages []*packages
8484
[]error, map[*analysis.Pass]*packages.Package,
8585
) {
8686
debugf("Analyzing %d packages on load mode %s", len(initialPackages), r.loadMode)
87-
defer r.pkgCache.Trim()
8887

8988
roots := r.analyze(initialPackages, analyzers)
9089

Diff for: pkg/goanalysis/runners.go

+2
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ func saveIssuesToCache(allPkgs []*packages.Package, pkgsFromCache map[*packages.
182182
close(pkgCh)
183183
wg.Wait()
184184

185+
lintCtx.PkgCache.Close()
186+
185187
issuesCacheDebugf("Saved %d issues from %d packages to cache in %s", savedIssuesCount, len(allPkgs), time.Since(startedAt))
186188
}
187189

0 commit comments

Comments
 (0)