github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/go/internal/modindex/read.go (about) 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 "github.com/shogo82148/std/errors" 9 "github.com/shogo82148/std/go/build" 10 ) 11 12 // Module represents and encoded module index file. It is used to 13 // do the equivalent of build.Import of packages in the module and answer other 14 // questions based on the index file's data. 15 type Module struct { 16 modroot string 17 d *decoder 18 n int 19 } 20 21 var ErrNotIndexed = errors.New("not in module index") 22 23 // GetPackage returns the IndexPackage for the directory at the given path. 24 // It will return ErrNotIndexed if the directory should be read without 25 // using the index, for instance because the index is disabled, or the package 26 // is not in a module. 27 func GetPackage(modroot, pkgdir string) (*IndexPackage, error) 28 29 // GetModule returns the Module for the given modroot. 30 // It will return ErrNotIndexed if the directory should be read without 31 // using the index, for instance because the index is disabled, or the package 32 // is not in a module. 33 func GetModule(modroot string) (*Module, error) 34 35 // Walk calls f for each package in the index, passing the path to that package relative to the module root. 36 func (m *Module) Walk(f func(path string)) 37 38 // Import is the equivalent of build.Import given the information in Module. 39 func (rp *IndexPackage) Import(bctxt build.Context, mode build.ImportMode) (p *build.Package, err error) 40 41 // IsStandardPackage reports whether path is a standard package 42 // for the goroot and compiler using the module index if possible, 43 // and otherwise falling back to internal/goroot.IsStandardPackage 44 func IsStandardPackage(goroot_, compiler, path string) bool 45 46 // IsDirWithGoFiles is the equivalent of fsys.IsDirWithGoFiles using the information in the index. 47 func (rp *IndexPackage) IsDirWithGoFiles() (_ bool, err error) 48 49 // ScanDir implements imports.ScanDir using the information in the index. 50 func (rp *IndexPackage) ScanDir(tags map[string]bool) (sortedImports []string, sortedTestImports []string, err error) 51 52 // IndexPackage holds the information in the index 53 // needed to load a package in a specific directory. 54 type IndexPackage struct { 55 error error 56 dir string 57 58 modroot string 59 60 // Source files 61 sourceFiles []*sourceFile 62 } 63 64 // Package and returns finds the package with the given path (relative to the module root). 65 // If the package does not exist, Package returns an IndexPackage that will return an 66 // appropriate error from its methods. 67 func (m *Module) Package(path string) *IndexPackage