Skip to content

Commit

Permalink
internal/modindex: better behavior in edge cases
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
pjweinbgo committed Nov 12, 2024
1 parent 06a498a commit 12610a1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 44 deletions.
75 changes: 38 additions & 37 deletions internal/modindex/dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,41 +205,42 @@ func TestDirsSinglePath(t *testing.T) {
}
}

/* more data for tests
directories.go:169: WEIRD cloud.google.com/go/iam/admin/apiv1
map[cloud.google.com/go:1 cloud.google.com/go/iam:5]:
[cloud.google.com/go/[email protected]/admin/apiv1
cloud.google.com/go/[email protected]/admin/apiv1
cloud.google.com/go/[email protected]/admin/apiv1
cloud.google.com/go/[email protected]/admin/apiv1
cloud.google.com/go/[email protected]/admin/apiv1
cloud.google.com/[email protected]/iam/admin/apiv1]
directories.go:169: WEIRD cloud.google.com/go/iam
map[cloud.google.com/go:1 cloud.google.com/go/iam:5]:
[cloud.google.com/go/[email protected] cloud.google.com/go/[email protected]
cloud.google.com/go/[email protected] cloud.google.com/go/[email protected]
cloud.google.com/go/[email protected] cloud.google.com/[email protected]/iam]
directories.go:169: WEIRD cloud.google.com/go/compute/apiv1
map[cloud.google.com/go:1 cloud.google.com/go/compute:4]:
[cloud.google.com/go/[email protected]/apiv1
cloud.google.com/go/[email protected]/apiv1
cloud.google.com/go/[email protected]/apiv1
cloud.google.com/go/[email protected]/apiv1
cloud.google.com/[email protected]/compute/apiv1]
directories.go:169: WEIRD cloud.google.com/go/longrunning/autogen
map[cloud.google.com/go:2 cloud.google.com/go/longrunning:2]:
[cloud.google.com/go/[email protected]/autogen
cloud.google.com/go/[email protected]/autogen
cloud.google.com/[email protected]/longrunning/autogen
cloud.google.com/[email protected]/longrunning/autogen]
directories.go:169: WEIRD cloud.google.com/go/iam/credentials/apiv1
map[cloud.google.com/go:1 cloud.google.com/go/iam:5]:
[cloud.google.com/go/[email protected]/credentials/apiv1
cloud.google.com/go/[email protected]/credentials/apiv1
cloud.google.com/go/[email protected]/credentials/apiv1
cloud.google.com/go/[email protected]/credentials/apiv1
cloud.google.com/go/[email protected]/credentials/apiv1
cloud.google.com/[email protected]/iam/credentials/apiv1]
func TestMissingCachedir(t *testing.T) {
// behave properly if the cached dir is empty
dir := testModCache(t)
if err := Create(dir); err != nil {
t.Fatal(err)
}
ixd, err := IndexDir()
if err != nil {
t.Fatal(err)
}
des, err := os.ReadDir(ixd)
if err != nil {
t.Fatal(err)
}
if len(des) != 2 {
t.Errorf("got %d, butexpected two entries in index dir", len(des))
}
}

*/
func TestMissingIndex(t *testing.T) {
// behave properly if there is no existing index
dir := testModCache(t)
if ok, err := Update(dir); err != nil {
t.Fatal(err)
} else if !ok {
t.Error("Update returned !ok")
}
ixd, err := IndexDir()
if err != nil {
t.Fatal(err)
}
des, err := os.ReadDir(ixd)
if err != nil {
t.Fatal(err)
}
if len(des) != 2 {
t.Errorf("got %d, butexpected two entries in index dir", len(des))
}
}
10 changes: 6 additions & 4 deletions internal/modindex/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ package modindex
import (
"bufio"
"encoding/csv"
"errors"
"fmt"
"hash/crc64"
"io"
"io/fs"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -85,7 +87,8 @@ type Entry struct {

// ReadIndex reads the latest version of the on-disk index
// for the cache directory cd.
// It returns nil if there is none, or if there is an error.
// It returns (nil, nil) if there is no index, but returns
// a non-nil error if the index exists but could not be read.
func ReadIndex(cachedir string) (*Index, error) {
cachedir, err := filepath.Abs(cachedir)
if err != nil {
Expand All @@ -100,10 +103,10 @@ func ReadIndex(cachedir string) (*Index, error) {
iname := filepath.Join(dir, base)
buf, err := os.ReadFile(iname)
if err != nil {
if err == os.ErrNotExist {
if errors.Is(err, fs.ErrNotExist) {
return nil, nil
}
return nil, fmt.Errorf("reading %s: %s %T", iname, err, err)
return nil, fmt.Errorf("cannot read %s: %w", iname, err)
}
fname := filepath.Join(dir, string(buf))
fd, err := os.Open(fname)
Expand Down Expand Up @@ -235,7 +238,6 @@ func writeIndexToFile(x *Index, fd *os.File) error {
if err := w.Flush(); err != nil {
return err
}
log.Printf("%d Entries %d names", len(x.Entries), cnt)
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions internal/modindex/modindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func modindexTimed(onlyBefore time.Time, cachedir Abspath, clear bool) (bool, er
if clear && err != nil {
return false, err
}
// TODO(pjw): check that most of those directorie still exist
// TODO(pjw): check that most of those directories still exist
}
cfg := &work{
onlyBefore: onlyBefore,
Expand All @@ -80,8 +80,8 @@ func modindexTimed(onlyBefore time.Time, cachedir Abspath, clear bool) (bool, er
if err := cfg.buildIndex(); err != nil {
return false, err
}
if len(cfg.newIndex.Entries) == 0 {
// no changes, don't write a new index
if len(cfg.newIndex.Entries) == 0 && curIndex != nil {
// no changes from existing curIndex, don't write a new index
return false, nil
}
if err := cfg.writeIndex(); err != nil {
Expand Down

0 comments on commit 12610a1

Please sign in to comment.