Skip to content

Commit

Permalink
internal/mod/modpkgload: skip fs.Stat before fs.ReadDir
Browse files Browse the repository at this point in the history
Calling fs.Stat before fs.ReadDir used to be crucial to catch cases
where a local package being loaded was a regular file rather than a
directory, and we have had test cases for that scenario for some time.

Since the recent cue/load and modules refactors, this extra io/fs work
appears to no longer be necessary, as we just capture missing files,
which can be done with fs.ReadDir easily.

This reduces the number of syscalls for a single and all packages from

    $ strace -f -t -e trace=file cue fmt ./internal/ci |& grep '/home/mvdan/src' | wc -l
    96
    $ strace -f -t -e trace=file cue fmt ./... |& grep '/home/mvdan/src' | wc -l
    5256

by about 5% and 0.5% to

    $ strace -f -t -e trace=file cue fmt ./internal/ci |& grep '/home/mvdan/src' | wc -l
    90
    $ strace -f -t -e trace=file cue fmt ./... |& grep '/home/mvdan/src' | wc -l
    5224

Updates #3155.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Id706a0ef83e7bf29869c8a99faeb7ae5db1537e9
  • Loading branch information
mvdan committed May 29, 2024
1 parent 3c82fa0 commit 91e6db3
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions internal/mod/modpkgload/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,22 +237,15 @@ func (pkgs *Packages) findLocalPackage(pkgPath string) ([]module.SourceLoc, erro
}

func isDirWithCUEFiles(loc module.SourceLoc) (bool, error) {
// It would be nice if we could inspect the error returned from
// ReadDir to see if it's failing because it's not a directory,
// but unfortunately that doesn't seem to be something defined
// by the Go fs interface.
fi, err := fs.Stat(loc.FS, loc.Dir)
if err != nil {
if !errors.Is(err, fs.ErrNotExist) {
return false, err
}
return false, nil
}
if !fi.IsDir() {
return false, nil
}
// It would be nice if we could inspect the error returned from ReadDir to see
// if it's failing because it's not a directory, but unfortunately that doesn't
// seem to be something defined by the Go fs interface.
// For now, catching fs.ErrNotExist seems to be enough.
entries, err := fs.ReadDir(loc.FS, loc.Dir)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return false, nil
}
return false, err
}
for _, e := range entries {
Expand Down

0 comments on commit 91e6db3

Please sign in to comment.