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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Peter Weinberger 2024-11-08 15:18:07 -05:00
Родитель 06a498a7a3
Коммит 12610a1270
3 изменённых файлов: 47 добавлений и 44 удалений

Просмотреть файл

@ -205,41 +205,42 @@ func TestDirsSinglePath(t *testing.T) {
}
}
/* more data for tests
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))
}
}
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/iam@v0.12.0/admin/apiv1
cloud.google.com/go/iam@v0.13.0/admin/apiv1
cloud.google.com/go/iam@v0.3.0/admin/apiv1
cloud.google.com/go/iam@v0.7.0/admin/apiv1
cloud.google.com/go/iam@v1.0.1/admin/apiv1
cloud.google.com/go@v0.94.0/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/iam@v0.12.0 cloud.google.com/go/iam@v0.13.0
cloud.google.com/go/iam@v0.3.0 cloud.google.com/go/iam@v0.7.0
cloud.google.com/go/iam@v1.0.1 cloud.google.com/go@v0.94.0/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/compute@v1.12.1/apiv1
cloud.google.com/go/compute@v1.18.0/apiv1
cloud.google.com/go/compute@v1.19.0/apiv1
cloud.google.com/go/compute@v1.7.0/apiv1
cloud.google.com/go@v0.94.0/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/longrunning@v0.3.0/autogen
cloud.google.com/go/longrunning@v0.4.1/autogen
cloud.google.com/go@v0.104.0/longrunning/autogen
cloud.google.com/go@v0.94.0/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/iam@v0.12.0/credentials/apiv1
cloud.google.com/go/iam@v0.13.0/credentials/apiv1
cloud.google.com/go/iam@v0.3.0/credentials/apiv1
cloud.google.com/go/iam@v0.7.0/credentials/apiv1
cloud.google.com/go/iam@v1.0.1/credentials/apiv1
cloud.google.com/go@v0.94.0/iam/credentials/apiv1]
*/
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))
}
}

Просмотреть файл

@ -7,9 +7,11 @@ package modindex
import (
"bufio"
"encoding/csv"
"errors"
"fmt"
"hash/crc64"
"io"
"io/fs"
"log"
"os"
"path/filepath"
@ -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 {
@ -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)
@ -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
}

Просмотреть файл

@ -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,
@ -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 {