errNotFromModuleCache = fmt.Errorf("%w: not from module cache", ErrNotIndexed)
)
-// GetPackage returns the IndexPackage for the package at the given path.
+// GetPackage returns the IndexPackage for the directory at the given path.
// It will return ErrNotIndexed if the directory should be read without
// using the index, for instance because the index is disabled, or the package
// is not in a module.
reldir = str.TrimFilePathPrefix(reldir, "cmd")
modroot = filepath.Join(modroot, "cmd")
}
- if _, err := GetPackage(modroot, filepath.Join(modroot, reldir)); err == nil {
- // Note that goroot.IsStandardPackage doesn't check that the directory
- // actually contains any go files-- merely that it exists. GetPackage
- // returning a nil error is enough for us to know the directory exists.
- return true
+ if pkg, err := GetPackage(modroot, filepath.Join(modroot, reldir)); err == nil {
+ hasGo, err := pkg.IsDirWithGoFiles()
+ return err == nil && hasGo
} else if errors.Is(err, ErrNotIndexed) {
// Fall back because package isn't indexable. (Probably because
// a file was modified recently)
return true
}
-// IndexPackage holds the information needed to access information in the
-// index needed to load a package in a specific directory.
+// IndexPackage holds the information in the index
+// needed to load a package in a specific directory.
type IndexPackage struct {
error error
dir string // directory of the package relative to the modroot
--- /dev/null
+# Issue 65406. The testdata directory in GOROOT/src
+# shouldn't be treated as a standard package.
+
+go list -f '{{.ImportPath}} {{.Dir}}' testdata
+! stderr 'found package testdata in multiple modules'
+stdout 'testdata '$WORK${/}'gopath'${/}'src'
+
+-- go.mod --
+module testdata
+-- p.go --
+package p
\ No newline at end of file
# Module loader does not interfere with list -e (golang.org/issue/24149).
go list -e -f '{{.Error.Err}}' database
-stdout 'no Go files in '
+stdout 'package database is not in std'
! go list database
-stderr 'no Go files in '
+stderr 'package database is not in std'
-- go.mod --
module x
switch compiler {
case "gc":
dir := filepath.Join(goroot, "src", path)
- info, err := os.Stat(dir)
- return err == nil && info.IsDir()
+ dirents, err := os.ReadDir(dir)
+ if err != nil {
+ return false
+ }
+ for _, dirent := range dirents {
+ if strings.HasSuffix(dirent.Name(), ".go") {
+ return true
+ }
+ }
+ return false
case "gccgo":
return gccgoSearch.isStandard(path)
default:
import (
"os"
"path/filepath"
+ "strings"
)
// IsStandardPackage reports whether path is a standard package,
switch compiler {
case "gc":
dir := filepath.Join(goroot, "src", path)
- _, err := os.Stat(dir)
- return err == nil
+ dirents, err := os.ReadDir(dir)
+ if err != nil {
+ return false
+ }
+ for _, dirent := range dirents {
+ if strings.HasSuffix(dirent.Name(), ".go") {
+ return true
+ }
+ }
+ return false
case "gccgo":
return stdpkg[path]
default: