]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/go/internal/modindex/read.go
cmd/go: do not install .a files for packages in std
[gostls13.git] / src / cmd / go / internal / modindex / read.go
1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package modindex
6
7 import (
8         "bytes"
9         "encoding/binary"
10         "errors"
11         "fmt"
12         "go/build"
13         "go/build/constraint"
14         "go/token"
15         "internal/godebug"
16         "internal/goroot"
17         "path"
18         "path/filepath"
19         "runtime"
20         "runtime/debug"
21         "sort"
22         "strings"
23         "sync"
24         "time"
25         "unsafe"
26
27         "cmd/go/internal/base"
28         "cmd/go/internal/cache"
29         "cmd/go/internal/cfg"
30         "cmd/go/internal/fsys"
31         "cmd/go/internal/imports"
32         "cmd/go/internal/par"
33         "cmd/go/internal/str"
34 )
35
36 // enabled is used to flag off the behavior of the module index on tip.
37 // It will be removed before the release.
38 // TODO(matloob): Remove enabled once we have more confidence on the
39 // module index.
40 var enabled = godebug.New("goindex").Value() != "0"
41
42 // Module represents and encoded module index file. It is used to
43 // do the equivalent of build.Import of packages in the module and answer other
44 // questions based on the index file's data.
45 type Module struct {
46         modroot string
47         d       *decoder
48         n       int // number of packages
49 }
50
51 // moduleHash returns an ActionID corresponding to the state of the module
52 // located at filesystem path modroot.
53 func moduleHash(modroot string, ismodcache bool) (cache.ActionID, error) {
54         // We expect modules stored within the module cache to be checksummed and
55         // immutable, and we expect released modules within GOROOT to change only
56         // infrequently (when the Go version changes).
57         if !ismodcache {
58                 // The contents of this module may change over time. We don't want to pay
59                 // the cost to detect changes and re-index whenever they occur, so just
60                 // don't index it at all.
61                 //
62                 // Note that this is true even for modules in GOROOT/src: non-release builds
63                 // of the Go toolchain may have arbitrary development changes on top of the
64                 // commit reported by runtime.Version, or could be completly artificial due
65                 // to lacking a `git` binary (like "devel gomote.XXXXX", as synthesized by
66                 // "gomote push" as of 2022-06-15). (Release builds shouldn't have
67                 // modifications, but we don't want to use a behavior for releases that we
68                 // haven't tested during development.)
69                 return cache.ActionID{}, ErrNotIndexed
70         }
71
72         h := cache.NewHash("moduleIndex")
73         // TODO(bcmills): Since modules in the index are checksummed, we could
74         // probably improve the cache hit rate by keying off of the module
75         // path@version (perhaps including the checksum?) instead of the module root
76         // directory.
77         fmt.Fprintf(h, "module index %s %s %v\n", runtime.Version(), indexVersion, modroot)
78         return h.Sum(), nil
79 }
80
81 const modTimeCutoff = 2 * time.Second
82
83 // dirHash returns an ActionID corresponding to the state of the package
84 // located at filesystem path pkgdir.
85 func dirHash(modroot, pkgdir string) (cache.ActionID, error) {
86         h := cache.NewHash("moduleIndex")
87         fmt.Fprintf(h, "modroot %s\n", modroot)
88         fmt.Fprintf(h, "package %s %s %v\n", runtime.Version(), indexVersion, pkgdir)
89         entries, err := fsys.ReadDir(pkgdir)
90         if err != nil {
91                 // pkgdir might not be a directory. give up on hashing.
92                 return cache.ActionID{}, ErrNotIndexed
93         }
94         cutoff := time.Now().Add(-modTimeCutoff)
95         for _, info := range entries {
96                 if info.IsDir() {
97                         continue
98                 }
99
100                 if !info.Mode().IsRegular() {
101                         return cache.ActionID{}, ErrNotIndexed
102                 }
103                 // To avoid problems for very recent files where a new
104                 // write might not change the mtime due to file system
105                 // mtime precision, reject caching if a file was read that
106                 // is less than modTimeCutoff old.
107                 //
108                 // This is the same strategy used for hashing test inputs.
109                 // See hashOpen in cmd/go/internal/test/test.go for the
110                 // corresponding code.
111                 if info.ModTime().After(cutoff) {
112                         return cache.ActionID{}, ErrNotIndexed
113                 }
114
115                 fmt.Fprintf(h, "file %v %v %v\n", info.Name(), info.ModTime(), info.Size())
116         }
117         return h.Sum(), nil
118 }
119
120 var modrootCache par.Cache
121
122 var ErrNotIndexed = errors.New("not in module index")
123
124 var (
125         errDisabled           = fmt.Errorf("%w: module indexing disabled", ErrNotIndexed)
126         errNotFromModuleCache = fmt.Errorf("%w: not from module cache", ErrNotIndexed)
127 )
128
129 // GetPackage returns the IndexPackage for the package at the given path.
130 // It will return ErrNotIndexed if the directory should be read without
131 // using the index, for instance because the index is disabled, or the packgae
132 // is not in a module.
133 func GetPackage(modroot, pkgdir string) (*IndexPackage, error) {
134         mi, err := GetModule(modroot)
135         if err == nil {
136                 return mi.Package(relPath(pkgdir, modroot)), nil
137         }
138         if !errors.Is(err, errNotFromModuleCache) {
139                 return nil, err
140         }
141         if cfg.BuildContext.Compiler == "gccgo" && str.HasPathPrefix(modroot, cfg.GOROOTsrc) {
142                 return nil, err // gccgo has no sources for GOROOT packages.
143         }
144         return openIndexPackage(modroot, pkgdir)
145 }
146
147 // GetModule returns the Module for the given modroot.
148 // It will return ErrNotIndexed if the directory should be read without
149 // using the index, for instance because the index is disabled, or the packgae
150 // is not in a module.
151 func GetModule(modroot string) (*Module, error) {
152         if !enabled || cache.DefaultDir() == "off" {
153                 return nil, errDisabled
154         }
155         if modroot == "" {
156                 panic("modindex.GetPackage called with empty modroot")
157         }
158         if cfg.BuildMod == "vendor" {
159                 // Even if the main module is in the module cache,
160                 // its vendored dependencies are not loaded from their
161                 // usual cached locations.
162                 return nil, errNotFromModuleCache
163         }
164         modroot = filepath.Clean(modroot)
165         if !str.HasFilePathPrefix(modroot, cfg.GOMODCACHE) {
166                 return nil, errNotFromModuleCache
167         }
168         return openIndexModule(modroot, true)
169 }
170
171 var mcache par.Cache
172
173 // openIndexModule returns the module index for modPath.
174 // It will return ErrNotIndexed if the module can not be read
175 // using the index because it contains symlinks.
176 func openIndexModule(modroot string, ismodcache bool) (*Module, error) {
177         type result struct {
178                 mi  *Module
179                 err error
180         }
181         r := mcache.Do(modroot, func() any {
182                 fsys.Trace("openIndexModule", modroot)
183                 id, err := moduleHash(modroot, ismodcache)
184                 if err != nil {
185                         return result{nil, err}
186                 }
187                 data, _, err := cache.Default().GetMmap(id)
188                 if err != nil {
189                         // Couldn't read from modindex. Assume we couldn't read from
190                         // the index because the module hasn't been indexed yet.
191                         data, err = indexModule(modroot)
192                         if err != nil {
193                                 return result{nil, err}
194                         }
195                         if err = cache.Default().PutBytes(id, data); err != nil {
196                                 return result{nil, err}
197                         }
198                 }
199                 mi, err := fromBytes(modroot, data)
200                 if err != nil {
201                         return result{nil, err}
202                 }
203                 return result{mi, nil}
204         }).(result)
205         return r.mi, r.err
206 }
207
208 var pcache par.Cache
209
210 func openIndexPackage(modroot, pkgdir string) (*IndexPackage, error) {
211         type result struct {
212                 pkg *IndexPackage
213                 err error
214         }
215         r := pcache.Do([2]string{modroot, pkgdir}, func() any {
216                 fsys.Trace("openIndexPackage", pkgdir)
217                 id, err := dirHash(modroot, pkgdir)
218                 if err != nil {
219                         return result{nil, err}
220                 }
221                 data, _, err := cache.Default().GetMmap(id)
222                 if err != nil {
223                         // Couldn't read from index. Assume we couldn't read from
224                         // the index because the package hasn't been indexed yet.
225                         data = indexPackage(modroot, pkgdir)
226                         if err = cache.Default().PutBytes(id, data); err != nil {
227                                 return result{nil, err}
228                         }
229                 }
230                 pkg, err := packageFromBytes(modroot, data)
231                 if err != nil {
232                         return result{nil, err}
233                 }
234                 return result{pkg, nil}
235         }).(result)
236         return r.pkg, r.err
237 }
238
239 var errCorrupt = errors.New("corrupt index")
240
241 // protect marks the start of a large section of code that accesses the index.
242 // It should be used as:
243 //
244 //      defer unprotect(protect, &err)
245 //
246 // It should not be used for trivial accesses which would be
247 // dwarfed by the overhead of the defer.
248 func protect() bool {
249         return debug.SetPanicOnFault(true)
250 }
251
252 var isTest = false
253
254 // unprotect marks the end of a large section of code that accesses the index.
255 // It should be used as:
256 //
257 //      defer unprotect(protect, &err)
258 //
259 // end looks for panics due to errCorrupt or bad mmap accesses.
260 // When it finds them, it adds explanatory text, consumes the panic, and sets *errp instead.
261 // If errp is nil, end adds the explanatory text but then calls base.Fatalf.
262 func unprotect(old bool, errp *error) {
263         // SetPanicOnFault's errors _may_ satisfy this interface. Even though it's not guaranteed
264         // that all its errors satisfy this interface, we'll only check for these errors so that
265         // we don't suppress panics that could have been produced from other sources.
266         type addrer interface {
267                 Addr() uintptr
268         }
269
270         debug.SetPanicOnFault(old)
271
272         if e := recover(); e != nil {
273                 if _, ok := e.(addrer); ok || e == errCorrupt {
274                         // This panic was almost certainly caused by SetPanicOnFault or our panic(errCorrupt).
275                         err := fmt.Errorf("error reading module index: %v", e)
276                         if errp != nil {
277                                 *errp = err
278                                 return
279                         }
280                         if isTest {
281                                 panic(err)
282                         }
283                         base.Fatalf("%v", err)
284                 }
285                 // The panic was likely not caused by SetPanicOnFault.
286                 panic(e)
287         }
288 }
289
290 // fromBytes returns a *Module given the encoded representation.
291 func fromBytes(moddir string, data []byte) (m *Module, err error) {
292         if !enabled {
293                 panic("use of index")
294         }
295
296         defer unprotect(protect(), &err)
297
298         if !bytes.HasPrefix(data, []byte(indexVersion+"\n")) {
299                 return nil, errCorrupt
300         }
301
302         const hdr = len(indexVersion + "\n")
303         d := &decoder{data: data}
304         str := d.intAt(hdr)
305         if str < hdr+8 || len(d.data) < str {
306                 return nil, errCorrupt
307         }
308         d.data, d.str = data[:str], d.data[str:]
309         // Check that string table looks valid.
310         // First string is empty string (length 0),
311         // and we leave a marker byte 0xFF at the end
312         // just to make sure that the file is not truncated.
313         if len(d.str) == 0 || d.str[0] != 0 || d.str[len(d.str)-1] != 0xFF {
314                 return nil, errCorrupt
315         }
316
317         n := d.intAt(hdr + 4)
318         if n < 0 || n > (len(d.data)-8)/8 {
319                 return nil, errCorrupt
320         }
321
322         m = &Module{
323                 moddir,
324                 d,
325                 n,
326         }
327         return m, nil
328 }
329
330 // packageFromBytes returns a *IndexPackage given the encoded representation.
331 func packageFromBytes(modroot string, data []byte) (p *IndexPackage, err error) {
332         m, err := fromBytes(modroot, data)
333         if err != nil {
334                 return nil, err
335         }
336         if m.n != 1 {
337                 return nil, fmt.Errorf("corrupt single-package index")
338         }
339         return m.pkg(0), nil
340 }
341
342 // pkgDir returns the dir string of the i'th package in the index.
343 func (m *Module) pkgDir(i int) string {
344         if i < 0 || i >= m.n {
345                 panic(errCorrupt)
346         }
347         return m.d.stringAt(12 + 8 + 8*i)
348 }
349
350 // pkgOff returns the offset of the data for the i'th package in the index.
351 func (m *Module) pkgOff(i int) int {
352         if i < 0 || i >= m.n {
353                 panic(errCorrupt)
354         }
355         return m.d.intAt(12 + 8 + 8*i + 4)
356 }
357
358 // Walk calls f for each package in the index, passing the path to that package relative to the module root.
359 func (m *Module) Walk(f func(path string)) {
360         defer unprotect(protect(), nil)
361         for i := 0; i < m.n; i++ {
362                 f(m.pkgDir(i))
363         }
364 }
365
366 // relPath returns the path relative to the module's root.
367 func relPath(path, modroot string) string {
368         return str.TrimFilePathPrefix(filepath.Clean(path), filepath.Clean(modroot))
369 }
370
371 var installgorootAll = godebug.New("installgoroot").Value() == "all"
372
373 // Import is the equivalent of build.Import given the information in Module.
374 func (rp *IndexPackage) Import(bctxt build.Context, mode build.ImportMode) (p *build.Package, err error) {
375         defer unprotect(protect(), &err)
376
377         ctxt := (*Context)(&bctxt)
378
379         p = &build.Package{}
380
381         p.ImportPath = "."
382         p.Dir = filepath.Join(rp.modroot, rp.dir)
383
384         var pkgerr error
385         switch ctxt.Compiler {
386         case "gccgo", "gc":
387         default:
388                 // Save error for end of function.
389                 pkgerr = fmt.Errorf("import %q: unknown compiler %q", p.Dir, ctxt.Compiler)
390         }
391
392         if p.Dir == "" {
393                 return p, fmt.Errorf("import %q: import of unknown directory", p.Dir)
394         }
395
396         // goroot and gopath
397         inTestdata := func(sub string) bool {
398                 return strings.Contains(sub, "/testdata/") || strings.HasSuffix(sub, "/testdata") || str.HasPathPrefix(sub, "testdata")
399         }
400         var pkga string
401         if !inTestdata(rp.dir) {
402                 // In build.go, p.Root should only be set in the non-local-import case, or in
403                 // GOROOT or GOPATH. Since module mode only calls Import with path set to "."
404                 // and the module index doesn't apply outside modules, the GOROOT case is
405                 // the only case where p.Root needs to be set.
406                 if ctxt.GOROOT != "" && str.HasFilePathPrefix(p.Dir, cfg.GOROOTsrc) && p.Dir != cfg.GOROOTsrc {
407                         p.Root = ctxt.GOROOT
408                         p.Goroot = true
409                         modprefix := str.TrimFilePathPrefix(rp.modroot, cfg.GOROOTsrc)
410                         p.ImportPath = rp.dir
411                         if modprefix != "" {
412                                 p.ImportPath = filepath.Join(modprefix, p.ImportPath)
413                         }
414
415                         // Set GOROOT-specific fields (sometimes for modules in a GOPATH directory).
416                         // The fields set below (SrcRoot, PkgRoot, BinDir, PkgTargetRoot, and PkgObj)
417                         // are only set in build.Import if p.Root != "".
418                         var pkgtargetroot string
419                         suffix := ""
420                         if ctxt.InstallSuffix != "" {
421                                 suffix = "_" + ctxt.InstallSuffix
422                         }
423                         switch ctxt.Compiler {
424                         case "gccgo":
425                                 pkgtargetroot = "pkg/gccgo_" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix
426                                 dir, elem := path.Split(p.ImportPath)
427                                 pkga = pkgtargetroot + "/" + dir + "lib" + elem + ".a"
428                         case "gc":
429                                 pkgtargetroot = "pkg/" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix
430                                 pkga = pkgtargetroot + "/" + p.ImportPath + ".a"
431                         }
432                         p.SrcRoot = ctxt.joinPath(p.Root, "src")
433                         p.PkgRoot = ctxt.joinPath(p.Root, "pkg")
434                         p.BinDir = ctxt.joinPath(p.Root, "bin")
435                         if pkga != "" {
436                                 // Always set PkgTargetRoot. It might be used when building in shared
437                                 // mode.
438                                 p.PkgTargetRoot = ctxt.joinPath(p.Root, pkgtargetroot)
439
440                                 // Set the install target if applicable.
441                                 if !p.Goroot || (installgorootAll && p.ImportPath != "unsafe" && p.ImportPath != "builtin") {
442                                         p.PkgObj = ctxt.joinPath(p.Root, pkga)
443                                 }
444                         }
445                 }
446         }
447
448         if rp.error != nil {
449                 if errors.Is(rp.error, errCannotFindPackage) && ctxt.Compiler == "gccgo" && p.Goroot {
450                         return p, nil
451                 }
452                 return p, rp.error
453         }
454
455         if mode&build.FindOnly != 0 {
456                 return p, pkgerr
457         }
458
459         // We need to do a second round of bad file processing.
460         var badGoError error
461         badGoFiles := make(map[string]bool)
462         badGoFile := func(name string, err error) {
463                 if badGoError == nil {
464                         badGoError = err
465                 }
466                 if !badGoFiles[name] {
467                         p.InvalidGoFiles = append(p.InvalidGoFiles, name)
468                         badGoFiles[name] = true
469                 }
470         }
471
472         var Sfiles []string // files with ".S"(capital S)/.sx(capital s equivalent for case insensitive filesystems)
473         var firstFile string
474         embedPos := make(map[string][]token.Position)
475         testEmbedPos := make(map[string][]token.Position)
476         xTestEmbedPos := make(map[string][]token.Position)
477         importPos := make(map[string][]token.Position)
478         testImportPos := make(map[string][]token.Position)
479         xTestImportPos := make(map[string][]token.Position)
480         allTags := make(map[string]bool)
481         for _, tf := range rp.sourceFiles {
482                 name := tf.name()
483                 // Check errors for go files and call badGoFiles to put them in
484                 // InvalidGoFiles if they do have an error.
485                 if strings.HasSuffix(name, ".go") {
486                         if error := tf.error(); error != "" {
487                                 badGoFile(name, errors.New(tf.error()))
488                                 continue
489                         } else if parseError := tf.parseError(); parseError != "" {
490                                 badGoFile(name, parseErrorFromString(tf.parseError()))
491                                 // Fall through: we still want to list files with parse errors.
492                         }
493                 }
494
495                 var shouldBuild = true
496                 if !ctxt.goodOSArchFile(name, allTags) && !ctxt.UseAllFiles {
497                         shouldBuild = false
498                 } else if goBuildConstraint := tf.goBuildConstraint(); goBuildConstraint != "" {
499                         x, err := constraint.Parse(goBuildConstraint)
500                         if err != nil {
501                                 return p, fmt.Errorf("%s: parsing //go:build line: %v", name, err)
502                         }
503                         shouldBuild = ctxt.eval(x, allTags)
504                 } else if plusBuildConstraints := tf.plusBuildConstraints(); len(plusBuildConstraints) > 0 {
505                         for _, text := range plusBuildConstraints {
506                                 if x, err := constraint.Parse(text); err == nil {
507                                         if !ctxt.eval(x, allTags) {
508                                                 shouldBuild = false
509                                         }
510                                 }
511                         }
512                 }
513
514                 ext := nameExt(name)
515                 if !shouldBuild || tf.ignoreFile() {
516                         if ext == ".go" {
517                                 p.IgnoredGoFiles = append(p.IgnoredGoFiles, name)
518                         } else if fileListForExt((*Package)(p), ext) != nil {
519                                 p.IgnoredOtherFiles = append(p.IgnoredOtherFiles, name)
520                         }
521                         continue
522                 }
523
524                 // Going to save the file. For non-Go files, can stop here.
525                 switch ext {
526                 case ".go":
527                         // keep going
528                 case ".S", ".sx":
529                         // special case for cgo, handled at end
530                         Sfiles = append(Sfiles, name)
531                         continue
532                 default:
533                         if list := fileListForExt((*Package)(p), ext); list != nil {
534                                 *list = append(*list, name)
535                         }
536                         continue
537                 }
538
539                 pkg := tf.pkgName()
540                 if pkg == "documentation" {
541                         p.IgnoredGoFiles = append(p.IgnoredGoFiles, name)
542                         continue
543                 }
544                 isTest := strings.HasSuffix(name, "_test.go")
545                 isXTest := false
546                 if isTest && strings.HasSuffix(tf.pkgName(), "_test") && p.Name != tf.pkgName() {
547                         isXTest = true
548                         pkg = pkg[:len(pkg)-len("_test")]
549                 }
550
551                 if !isTest && tf.binaryOnly() {
552                         p.BinaryOnly = true
553                 }
554
555                 if p.Name == "" {
556                         p.Name = pkg
557                         firstFile = name
558                 } else if pkg != p.Name {
559                         // TODO(#45999): The choice of p.Name is arbitrary based on file iteration
560                         // order. Instead of resolving p.Name arbitrarily, we should clear out the
561                         // existing Name and mark the existing files as also invalid.
562                         badGoFile(name, &MultiplePackageError{
563                                 Dir:      p.Dir,
564                                 Packages: []string{p.Name, pkg},
565                                 Files:    []string{firstFile, name},
566                         })
567                 }
568                 // Grab the first package comment as docs, provided it is not from a test file.
569                 if p.Doc == "" && !isTest && !isXTest {
570                         if synopsis := tf.synopsis(); synopsis != "" {
571                                 p.Doc = synopsis
572                         }
573                 }
574
575                 // Record Imports and information about cgo.
576                 isCgo := false
577                 imports := tf.imports()
578                 for _, imp := range imports {
579                         if imp.path == "C" {
580                                 if isTest {
581                                         badGoFile(name, fmt.Errorf("use of cgo in test %s not supported", name))
582                                         continue
583                                 }
584                                 isCgo = true
585                         }
586                 }
587                 if directives := tf.cgoDirectives(); directives != "" {
588                         if err := ctxt.saveCgo(name, (*Package)(p), directives); err != nil {
589                                 badGoFile(name, err)
590                         }
591                 }
592
593                 var fileList *[]string
594                 var importMap, embedMap map[string][]token.Position
595                 switch {
596                 case isCgo:
597                         allTags["cgo"] = true
598                         if ctxt.CgoEnabled {
599                                 fileList = &p.CgoFiles
600                                 importMap = importPos
601                                 embedMap = embedPos
602                         } else {
603                                 // Ignore Imports and Embeds from cgo files if cgo is disabled.
604                                 fileList = &p.IgnoredGoFiles
605                         }
606                 case isXTest:
607                         fileList = &p.XTestGoFiles
608                         importMap = xTestImportPos
609                         embedMap = xTestEmbedPos
610                 case isTest:
611                         fileList = &p.TestGoFiles
612                         importMap = testImportPos
613                         embedMap = testEmbedPos
614                 default:
615                         fileList = &p.GoFiles
616                         importMap = importPos
617                         embedMap = embedPos
618                 }
619                 *fileList = append(*fileList, name)
620                 if importMap != nil {
621                         for _, imp := range imports {
622                                 importMap[imp.path] = append(importMap[imp.path], imp.position)
623                         }
624                 }
625                 if embedMap != nil {
626                         for _, e := range tf.embeds() {
627                                 embedMap[e.pattern] = append(embedMap[e.pattern], e.position)
628                         }
629                 }
630         }
631
632         p.EmbedPatterns, p.EmbedPatternPos = cleanDecls(embedPos)
633         p.TestEmbedPatterns, p.TestEmbedPatternPos = cleanDecls(testEmbedPos)
634         p.XTestEmbedPatterns, p.XTestEmbedPatternPos = cleanDecls(xTestEmbedPos)
635
636         p.Imports, p.ImportPos = cleanDecls(importPos)
637         p.TestImports, p.TestImportPos = cleanDecls(testImportPos)
638         p.XTestImports, p.XTestImportPos = cleanDecls(xTestImportPos)
639
640         for tag := range allTags {
641                 p.AllTags = append(p.AllTags, tag)
642         }
643         sort.Strings(p.AllTags)
644
645         if len(p.CgoFiles) > 0 {
646                 p.SFiles = append(p.SFiles, Sfiles...)
647                 sort.Strings(p.SFiles)
648         } else {
649                 p.IgnoredOtherFiles = append(p.IgnoredOtherFiles, Sfiles...)
650                 sort.Strings(p.IgnoredOtherFiles)
651         }
652
653         if badGoError != nil {
654                 return p, badGoError
655         }
656         if len(p.GoFiles)+len(p.CgoFiles)+len(p.TestGoFiles)+len(p.XTestGoFiles) == 0 {
657                 return p, &build.NoGoError{Dir: p.Dir}
658         }
659         return p, pkgerr
660 }
661
662 // IsStandardPackage reports whether path is a standard package
663 // for the goroot and compiler using the module index if possible,
664 // and otherwise falling back to internal/goroot.IsStandardPackage
665 func IsStandardPackage(goroot_, compiler, path string) bool {
666         if !enabled || compiler != "gc" {
667                 return goroot.IsStandardPackage(goroot_, compiler, path)
668         }
669
670         reldir := filepath.FromSlash(path) // relative dir path in module index for package
671         modroot := filepath.Join(goroot_, "src")
672         if str.HasFilePathPrefix(reldir, "cmd") {
673                 reldir = str.TrimFilePathPrefix(reldir, "cmd")
674                 modroot = filepath.Join(modroot, "cmd")
675         }
676         if _, err := GetPackage(modroot, filepath.Join(modroot, reldir)); err == nil {
677                 // Note that goroot.IsStandardPackage doesn't check that the directory
678                 // actually contains any go files-- merely that it exists. GetPackage
679                 // returning a nil error is enough for us to know the directory exists.
680                 return true
681         } else if errors.Is(err, ErrNotIndexed) {
682                 // Fall back because package isn't indexable. (Probably because
683                 // a file was modified recently)
684                 return goroot.IsStandardPackage(goroot_, compiler, path)
685         }
686         return false
687 }
688
689 // IsDirWithGoFiles is the equivalent of fsys.IsDirWithGoFiles using the information in the index.
690 func (rp *IndexPackage) IsDirWithGoFiles() (_ bool, err error) {
691         defer func() {
692                 if e := recover(); e != nil {
693                         err = fmt.Errorf("error reading module index: %v", e)
694                 }
695         }()
696         for _, sf := range rp.sourceFiles {
697                 if strings.HasSuffix(sf.name(), ".go") {
698                         return true, nil
699                 }
700         }
701         return false, nil
702 }
703
704 // ScanDir implements imports.ScanDir using the information in the index.
705 func (rp *IndexPackage) ScanDir(tags map[string]bool) (sortedImports []string, sortedTestImports []string, err error) {
706         // TODO(matloob) dir should eventually be relative to indexed directory
707         // TODO(matloob): skip reading raw package and jump straight to data we need?
708
709         defer func() {
710                 if e := recover(); e != nil {
711                         err = fmt.Errorf("error reading module index: %v", e)
712                 }
713         }()
714
715         imports_ := make(map[string]bool)
716         testImports := make(map[string]bool)
717         numFiles := 0
718
719 Files:
720         for _, sf := range rp.sourceFiles {
721                 name := sf.name()
722                 if strings.HasPrefix(name, "_") || strings.HasPrefix(name, ".") || !strings.HasSuffix(name, ".go") || !imports.MatchFile(name, tags) {
723                         continue
724                 }
725
726                 // The following section exists for backwards compatibility reasons:
727                 // scanDir ignores files with import "C" when collecting the list
728                 // of imports unless the "cgo" tag is provided. The following comment
729                 // is copied from the original.
730                 //
731                 // import "C" is implicit requirement of cgo tag.
732                 // When listing files on the command line (explicitFiles=true)
733                 // we do not apply build tag filtering but we still do apply
734                 // cgo filtering, so no explicitFiles check here.
735                 // Why? Because we always have, and it's not worth breaking
736                 // that behavior now.
737                 imps := sf.imports() // TODO(matloob): directly read import paths to avoid the extra strings?
738                 for _, imp := range imps {
739                         if imp.path == "C" && !tags["cgo"] && !tags["*"] {
740                                 continue Files
741                         }
742                 }
743
744                 if !shouldBuild(sf, tags) {
745                         continue
746                 }
747                 numFiles++
748                 m := imports_
749                 if strings.HasSuffix(name, "_test.go") {
750                         m = testImports
751                 }
752                 for _, p := range imps {
753                         m[p.path] = true
754                 }
755         }
756         if numFiles == 0 {
757                 return nil, nil, imports.ErrNoGo
758         }
759         return keys(imports_), keys(testImports), nil
760 }
761
762 func keys(m map[string]bool) []string {
763         list := make([]string, 0, len(m))
764         for k := range m {
765                 list = append(list, k)
766         }
767         sort.Strings(list)
768         return list
769 }
770
771 // implements imports.ShouldBuild in terms of an index sourcefile.
772 func shouldBuild(sf *sourceFile, tags map[string]bool) bool {
773         if goBuildConstraint := sf.goBuildConstraint(); goBuildConstraint != "" {
774                 x, err := constraint.Parse(goBuildConstraint)
775                 if err != nil {
776                         return false
777                 }
778                 return imports.Eval(x, tags, true)
779         }
780
781         plusBuildConstraints := sf.plusBuildConstraints()
782         for _, text := range plusBuildConstraints {
783                 if x, err := constraint.Parse(text); err == nil {
784                         if !imports.Eval(x, tags, true) {
785                                 return false
786                         }
787                 }
788         }
789
790         return true
791 }
792
793 // IndexPackage holds the information needed to access information in the
794 // index needed to load a package in a specific directory.
795 type IndexPackage struct {
796         error error
797         dir   string // directory of the package relative to the modroot
798
799         modroot string
800
801         // Source files
802         sourceFiles []*sourceFile
803 }
804
805 var errCannotFindPackage = errors.New("cannot find package")
806
807 // Package and returns finds the package with the given path (relative to the module root).
808 // If the package does not exist, Package returns an IndexPackage that will return an
809 // appropriate error from its methods.
810 func (m *Module) Package(path string) *IndexPackage {
811         defer unprotect(protect(), nil)
812
813         i, ok := sort.Find(m.n, func(i int) int {
814                 return strings.Compare(path, m.pkgDir(i))
815         })
816         if !ok {
817                 return &IndexPackage{error: fmt.Errorf("%w %q in:\n\t%s", errCannotFindPackage, path, filepath.Join(m.modroot, path))}
818         }
819         return m.pkg(i)
820 }
821
822 // pkgAt returns the i'th IndexPackage in m.
823 func (m *Module) pkg(i int) *IndexPackage {
824         r := m.d.readAt(m.pkgOff(i))
825         p := new(IndexPackage)
826         if errstr := r.string(); errstr != "" {
827                 p.error = errors.New(errstr)
828         }
829         p.dir = r.string()
830         p.sourceFiles = make([]*sourceFile, r.int())
831         for i := range p.sourceFiles {
832                 p.sourceFiles[i] = &sourceFile{
833                         d:   m.d,
834                         pos: r.int(),
835                 }
836         }
837         p.modroot = m.modroot
838         return p
839 }
840
841 // sourceFile represents the information of a given source file in the module index.
842 type sourceFile struct {
843         d               *decoder // encoding of this source file
844         pos             int      // start of sourceFile encoding in d
845         onceReadImports sync.Once
846         savedImports    []rawImport // saved imports so that they're only read once
847 }
848
849 // Offsets for fields in the sourceFile.
850 const (
851         sourceFileError = 4 * iota
852         sourceFileParseError
853         sourceFileSynopsis
854         sourceFileName
855         sourceFilePkgName
856         sourceFileIgnoreFile
857         sourceFileBinaryOnly
858         sourceFileCgoDirectives
859         sourceFileGoBuildConstraint
860         sourceFileNumPlusBuildConstraints
861 )
862
863 func (sf *sourceFile) error() string {
864         return sf.d.stringAt(sf.pos + sourceFileError)
865 }
866 func (sf *sourceFile) parseError() string {
867         return sf.d.stringAt(sf.pos + sourceFileParseError)
868 }
869 func (sf *sourceFile) synopsis() string {
870         return sf.d.stringAt(sf.pos + sourceFileSynopsis)
871 }
872 func (sf *sourceFile) name() string {
873         return sf.d.stringAt(sf.pos + sourceFileName)
874 }
875 func (sf *sourceFile) pkgName() string {
876         return sf.d.stringAt(sf.pos + sourceFilePkgName)
877 }
878 func (sf *sourceFile) ignoreFile() bool {
879         return sf.d.boolAt(sf.pos + sourceFileIgnoreFile)
880 }
881 func (sf *sourceFile) binaryOnly() bool {
882         return sf.d.boolAt(sf.pos + sourceFileBinaryOnly)
883 }
884 func (sf *sourceFile) cgoDirectives() string {
885         return sf.d.stringAt(sf.pos + sourceFileCgoDirectives)
886 }
887 func (sf *sourceFile) goBuildConstraint() string {
888         return sf.d.stringAt(sf.pos + sourceFileGoBuildConstraint)
889 }
890
891 func (sf *sourceFile) plusBuildConstraints() []string {
892         pos := sf.pos + sourceFileNumPlusBuildConstraints
893         n := sf.d.intAt(pos)
894         pos += 4
895         ret := make([]string, n)
896         for i := 0; i < n; i++ {
897                 ret[i] = sf.d.stringAt(pos)
898                 pos += 4
899         }
900         return ret
901 }
902
903 func (sf *sourceFile) importsOffset() int {
904         pos := sf.pos + sourceFileNumPlusBuildConstraints
905         n := sf.d.intAt(pos)
906         // each build constraint is 1 uint32
907         return pos + 4 + n*4
908 }
909
910 func (sf *sourceFile) embedsOffset() int {
911         pos := sf.importsOffset()
912         n := sf.d.intAt(pos)
913         // each import is 5 uint32s (string + tokpos)
914         return pos + 4 + n*(4*5)
915 }
916
917 func (sf *sourceFile) imports() []rawImport {
918         sf.onceReadImports.Do(func() {
919                 importsOffset := sf.importsOffset()
920                 r := sf.d.readAt(importsOffset)
921                 numImports := r.int()
922                 ret := make([]rawImport, numImports)
923                 for i := 0; i < numImports; i++ {
924                         ret[i] = rawImport{r.string(), r.tokpos()}
925                 }
926                 sf.savedImports = ret
927         })
928         return sf.savedImports
929 }
930
931 func (sf *sourceFile) embeds() []embed {
932         embedsOffset := sf.embedsOffset()
933         r := sf.d.readAt(embedsOffset)
934         numEmbeds := r.int()
935         ret := make([]embed, numEmbeds)
936         for i := range ret {
937                 ret[i] = embed{r.string(), r.tokpos()}
938         }
939         return ret
940 }
941
942 func asString(b []byte) string {
943         return unsafe.String(unsafe.SliceData(b), len(b))
944 }
945
946 // A decoder helps decode the index format.
947 type decoder struct {
948         data []byte // data after header
949         str  []byte // string table
950 }
951
952 // intAt returns the int at the given offset in d.data.
953 func (d *decoder) intAt(off int) int {
954         if off < 0 || len(d.data)-off < 4 {
955                 panic(errCorrupt)
956         }
957         i := binary.LittleEndian.Uint32(d.data[off : off+4])
958         if int32(i)>>31 != 0 {
959                 panic(errCorrupt)
960         }
961         return int(i)
962 }
963
964 // boolAt returns the bool at the given offset in d.data.
965 func (d *decoder) boolAt(off int) bool {
966         return d.intAt(off) != 0
967 }
968
969 // stringTableAt returns the string pointed at by the int at the given offset in d.data.
970 func (d *decoder) stringAt(off int) string {
971         return d.stringTableAt(d.intAt(off))
972 }
973
974 // stringTableAt returns the string at the given offset in the string table d.str.
975 func (d *decoder) stringTableAt(off int) string {
976         if off < 0 || off >= len(d.str) {
977                 panic(errCorrupt)
978         }
979         s := d.str[off:]
980         v, n := binary.Uvarint(s)
981         if n <= 0 || v > uint64(len(s[n:])) {
982                 panic(errCorrupt)
983         }
984         return asString(s[n : n+int(v)])
985 }
986
987 // A reader reads sequential fields from a section of the index format.
988 type reader struct {
989         d   *decoder
990         pos int
991 }
992
993 // readAt returns a reader starting at the given position in d.
994 func (d *decoder) readAt(pos int) *reader {
995         return &reader{d, pos}
996 }
997
998 // int reads the next int.
999 func (r *reader) int() int {
1000         i := r.d.intAt(r.pos)
1001         r.pos += 4
1002         return i
1003 }
1004
1005 // string reads the next string.
1006 func (r *reader) string() string {
1007         return r.d.stringTableAt(r.int())
1008 }
1009
1010 // bool reads the next bool.
1011 func (r *reader) bool() bool {
1012         return r.int() != 0
1013 }
1014
1015 // tokpos reads the next token.Position.
1016 func (r *reader) tokpos() token.Position {
1017         return token.Position{
1018                 Filename: r.string(),
1019                 Offset:   r.int(),
1020                 Line:     r.int(),
1021                 Column:   r.int(),
1022         }
1023 }