Skip to content

Commit 12610a1

Browse files
committed
internal/modindex: better behavior in edge cases
Write an index even if there is nothing in the module cache. Change-Id: Ia98f8825d9914a0d4bd2ee9ff1bccf8519b91f37 Reviewed-on: https://go-review.googlesource.com/c/tools/+/626735 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 06a498a commit 12610a1

File tree

3 files changed

+47
-44
lines changed

3 files changed

+47
-44
lines changed

internal/modindex/dir_test.go

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -205,41 +205,42 @@ func TestDirsSinglePath(t *testing.T) {
205205
}
206206
}
207207

208-
/* more data for tests
209-
210-
directories.go:169: WEIRD cloud.google.com/go/iam/admin/apiv1
211-
map[cloud.google.com/go:1 cloud.google.com/go/iam:5]:
212-
[cloud.google.com/go/[email protected]/admin/apiv1
213-
cloud.google.com/go/[email protected]/admin/apiv1
214-
cloud.google.com/go/[email protected]/admin/apiv1
215-
cloud.google.com/go/[email protected]/admin/apiv1
216-
cloud.google.com/go/[email protected]/admin/apiv1
217-
cloud.google.com/[email protected]/iam/admin/apiv1]
218-
directories.go:169: WEIRD cloud.google.com/go/iam
219-
map[cloud.google.com/go:1 cloud.google.com/go/iam:5]:
220-
[cloud.google.com/go/[email protected] cloud.google.com/go/[email protected]
221-
cloud.google.com/go/[email protected] cloud.google.com/go/[email protected]
222-
cloud.google.com/go/[email protected] cloud.google.com/[email protected]/iam]
223-
directories.go:169: WEIRD cloud.google.com/go/compute/apiv1
224-
map[cloud.google.com/go:1 cloud.google.com/go/compute:4]:
225-
[cloud.google.com/go/[email protected]/apiv1
226-
cloud.google.com/go/[email protected]/apiv1
227-
cloud.google.com/go/[email protected]/apiv1
228-
cloud.google.com/go/[email protected]/apiv1
229-
cloud.google.com/[email protected]/compute/apiv1]
230-
directories.go:169: WEIRD cloud.google.com/go/longrunning/autogen
231-
map[cloud.google.com/go:2 cloud.google.com/go/longrunning:2]:
232-
[cloud.google.com/go/[email protected]/autogen
233-
cloud.google.com/go/[email protected]/autogen
234-
cloud.google.com/[email protected]/longrunning/autogen
235-
cloud.google.com/[email protected]/longrunning/autogen]
236-
directories.go:169: WEIRD cloud.google.com/go/iam/credentials/apiv1
237-
map[cloud.google.com/go:1 cloud.google.com/go/iam:5]:
238-
[cloud.google.com/go/[email protected]/credentials/apiv1
239-
cloud.google.com/go/[email protected]/credentials/apiv1
240-
cloud.google.com/go/[email protected]/credentials/apiv1
241-
cloud.google.com/go/[email protected]/credentials/apiv1
242-
cloud.google.com/go/[email protected]/credentials/apiv1
243-
cloud.google.com/[email protected]/iam/credentials/apiv1]
208+
func TestMissingCachedir(t *testing.T) {
209+
// behave properly if the cached dir is empty
210+
dir := testModCache(t)
211+
if err := Create(dir); err != nil {
212+
t.Fatal(err)
213+
}
214+
ixd, err := IndexDir()
215+
if err != nil {
216+
t.Fatal(err)
217+
}
218+
des, err := os.ReadDir(ixd)
219+
if err != nil {
220+
t.Fatal(err)
221+
}
222+
if len(des) != 2 {
223+
t.Errorf("got %d, butexpected two entries in index dir", len(des))
224+
}
225+
}
244226

245-
*/
227+
func TestMissingIndex(t *testing.T) {
228+
// behave properly if there is no existing index
229+
dir := testModCache(t)
230+
if ok, err := Update(dir); err != nil {
231+
t.Fatal(err)
232+
} else if !ok {
233+
t.Error("Update returned !ok")
234+
}
235+
ixd, err := IndexDir()
236+
if err != nil {
237+
t.Fatal(err)
238+
}
239+
des, err := os.ReadDir(ixd)
240+
if err != nil {
241+
t.Fatal(err)
242+
}
243+
if len(des) != 2 {
244+
t.Errorf("got %d, butexpected two entries in index dir", len(des))
245+
}
246+
}

internal/modindex/index.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ package modindex
77
import (
88
"bufio"
99
"encoding/csv"
10+
"errors"
1011
"fmt"
1112
"hash/crc64"
1213
"io"
14+
"io/fs"
1315
"log"
1416
"os"
1517
"path/filepath"
@@ -85,7 +87,8 @@ type Entry struct {
8587

8688
// ReadIndex reads the latest version of the on-disk index
8789
// for the cache directory cd.
88-
// It returns nil if there is none, or if there is an error.
90+
// It returns (nil, nil) if there is no index, but returns
91+
// a non-nil error if the index exists but could not be read.
8992
func ReadIndex(cachedir string) (*Index, error) {
9093
cachedir, err := filepath.Abs(cachedir)
9194
if err != nil {
@@ -100,10 +103,10 @@ func ReadIndex(cachedir string) (*Index, error) {
100103
iname := filepath.Join(dir, base)
101104
buf, err := os.ReadFile(iname)
102105
if err != nil {
103-
if err == os.ErrNotExist {
106+
if errors.Is(err, fs.ErrNotExist) {
104107
return nil, nil
105108
}
106-
return nil, fmt.Errorf("reading %s: %s %T", iname, err, err)
109+
return nil, fmt.Errorf("cannot read %s: %w", iname, err)
107110
}
108111
fname := filepath.Join(dir, string(buf))
109112
fd, err := os.Open(fname)
@@ -235,7 +238,6 @@ func writeIndexToFile(x *Index, fd *os.File) error {
235238
if err := w.Flush(); err != nil {
236239
return err
237240
}
238-
log.Printf("%d Entries %d names", len(x.Entries), cnt)
239241
return nil
240242
}
241243

internal/modindex/modindex.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func modindexTimed(onlyBefore time.Time, cachedir Abspath, clear bool) (bool, er
6767
if clear && err != nil {
6868
return false, err
6969
}
70-
// TODO(pjw): check that most of those directorie still exist
70+
// TODO(pjw): check that most of those directories still exist
7171
}
7272
cfg := &work{
7373
onlyBefore: onlyBefore,
@@ -80,8 +80,8 @@ func modindexTimed(onlyBefore time.Time, cachedir Abspath, clear bool) (bool, er
8080
if err := cfg.buildIndex(); err != nil {
8181
return false, err
8282
}
83-
if len(cfg.newIndex.Entries) == 0 {
84-
// no changes, don't write a new index
83+
if len(cfg.newIndex.Entries) == 0 && curIndex != nil {
84+
// no changes from existing curIndex, don't write a new index
8585
return false, nil
8686
}
8787
if err := cfg.writeIndex(); err != nil {

0 commit comments

Comments
 (0)