Functions
Bool
method
#
func (e *encoder) Bool(b bool)
Bytes
method
#
func (e *encoder) Bytes(b []byte)
Error
method
#
func (e *MultiplePackageError) Error() string
Error
method
#
func (e *NoGoError) Error() string
GetModule
function
#
GetModule returns the Module for the given modroot.
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.
func GetModule(modroot string) (*Module, error)
GetPackage
function
#
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.
func GetPackage(modroot string, pkgdir string) (*IndexPackage, error)
Import
method
#
Import is the equivalent of build.Import given the information in Module.
func (rp *IndexPackage) Import(bctxt build.Context, mode build.ImportMode) (p *build.Package, err error)
Int
method
#
Int encodes n. Note that all ints are written to the index as uint32s,
and to avoid problems on 32-bit systems we require fitting into a 32-bit int.
func (e *encoder) Int(n int)
IntAt
method
#
func (e *encoder) IntAt(n int, at int)
IsGoDir
method
#
IsGoDir is the equivalent of fsys.IsGoDir using the information in the index.
func (rp *IndexPackage) IsGoDir() (_ bool, err error)
IsStandardPackage
function
#
IsStandardPackage reports whether path is a standard package
for the goroot and compiler using the module index if possible,
and otherwise falling back to internal/goroot.IsStandardPackage
func IsStandardPackage(goroot_ string, compiler string, path string) bool
Package
method
#
Package and returns finds the package with the given path (relative to the module root).
If the package does not exist, Package returns an IndexPackage that will return an
appropriate error from its methods.
func (m *Module) Package(path string) *IndexPackage
Pos
method
#
func (e *encoder) Pos() int
Position
method
#
func (e *encoder) Position(position token.Position)
ScanDir
method
#
ScanDir implements imports.ScanDir using the information in the index.
func (rp *IndexPackage) ScanDir(tags map[string]bool) (sortedImports []string, sortedTestImports []string, err error)
String
method
#
func (e *encoder) String(s string)
Uint32
method
#
func (e *encoder) Uint32(n uint32)
Walk
method
#
Walk calls f for each package in the index, passing the path to that package relative to the module root.
func (m *Module) Walk(f func(path string))
asString
function
#
func asString(b []byte) string
binaryOnly
method
#
func (sf *sourceFile) binaryOnly() bool
bool
method
#
bool reads the next bool.
func (r *reader) bool() bool
boolAt
method
#
boolAt returns the bool at the given offset in d.data.
func (d *decoder) boolAt(off int) bool
cgoDirectives
method
#
func (sf *sourceFile) cgoDirectives() string
cleanDecls
function
#
func cleanDecls(m map[string][]token.Position) ([]string, map[string][]token.Position)
dirHash
function
#
dirHash returns an ActionID corresponding to the state of the package
located at filesystem path pkgdir.
func dirHash(modroot string, pkgdir string) (cache.ActionID, error)
directives
method
#
func (sf *sourceFile) directives() []build.Directive
directivesOffset
method
#
func (sf *sourceFile) directivesOffset() int
embeds
method
#
func (sf *sourceFile) embeds() []embed
embedsOffset
method
#
func (sf *sourceFile) embedsOffset() int
encodeFile
function
#
func encodeFile(e *encoder, f *rawFile)
encodeModuleBytes
function
#
encodeModuleBytes produces the encoded representation of the module index.
encodeModuleBytes may modify the packages slice.
func encodeModuleBytes(packages []*rawPackage) []byte
encodePackage
function
#
func encodePackage(e *encoder, p *rawPackage)
encodePackageBytes
function
#
func encodePackageBytes(p *rawPackage) []byte
error
method
#
func (sf *sourceFile) error() string
eval
method
#
func (ctxt *Context) eval(x constraint.Expr, allTags map[string]bool) bool
expandSrcDir
function
#
expandSrcDir expands any occurrence of ${SRCDIR}, making sure
the result is safe for the shell.
func expandSrcDir(str string, srcdir string) (string, bool)
fileListForExt
function
#
func fileListForExt(p *build.Package, ext string) *[]string
findEmbed
method
#
findEmbed advances the input reader to the next //go:embed comment.
It reports whether it found a comment.
(Otherwise it found an error or EOF.)
func (r *importReader) findEmbed(first bool) bool
fromBytes
function
#
fromBytes returns a *Module given the encoded representation.
func fromBytes(moddir string, data []byte) (m *Module, err error)
getConstraints
function
#
func getConstraints(content []byte) (goBuild string, plusBuild []string, binaryOnly bool, err error)
getFileInfo
function
#
getFileInfo extracts the information needed from each go file for the module
index.
If Name denotes a Go program, matchFile reads until the end of the
Imports and returns that section of the file in the FileInfo's Header field,
even though it only considers text until the first non-comment
for +build lines.
getFileInfo will return errNonSource if the file is not a source or object
file and shouldn't even be added to IgnoredFiles.
func getFileInfo(dir string, name string, fset *token.FileSet) (*fileInfo, error)
goBuildConstraint
method
#
func (sf *sourceFile) goBuildConstraint() string
goodOSArchFile
method
#
goodOSArchFile returns false if the name contains a $GOOS or $GOARCH
suffix which does not match the current system.
The recognized name formats are:
name_$(GOOS).*
name_$(GOARCH).*
name_$(GOOS)_$(GOARCH).*
name_$(GOOS)_test.*
name_$(GOARCH)_test.*
name_$(GOOS)_$(GOARCH)_test.*
Exceptions:
if GOOS=android, then files with GOOS=linux are also matched.
if GOOS=illumos, then files with GOOS=solaris are also matched.
if GOOS=ios, then files with GOOS=darwin are also matched.
func (ctxt *Context) goodOSArchFile(name string, allTags map[string]bool) bool
gopath
method
#
gopath returns the list of Go path directories.
func (ctxt *Context) gopath() []string
hasSubdir
function
#
hasSubdir reports if dir is within root by performing lexical analysis only.
func hasSubdir(root string, dir string) (rel string, ok bool)
hasSubdir
method
#
hasSubdir calls ctxt.HasSubdir (if not nil) or else uses
the local file system to answer the question.
func (ctxt *Context) hasSubdir(root string, dir string) (rel string, ok bool)
ignoreFile
method
#
func (sf *sourceFile) ignoreFile() bool
importRaw
function
#
importRaw fills the rawPackage from the package files in srcDir.
dir is the package's path relative to the modroot.
func importRaw(modroot string, reldir string) *rawPackage
imports
method
#
func (sf *sourceFile) imports() []rawImport
importsOffset
method
#
func (sf *sourceFile) importsOffset() int
indexModule
function
#
indexModule indexes the module at the given directory and returns its
encoded representation. It returns ErrNotIndexed if the module can't
be indexed because it contains symlinks.
func indexModule(modroot string) ([]byte, error)
indexPackage
function
#
indexPackage indexes the package at the given directory and returns its
encoded representation. It returns ErrNotIndexed if the package can't
be indexed.
func indexPackage(modroot string, pkgdir string) []byte
int
method
#
int reads the next int.
func (r *reader) int() int
intAt
method
#
intAt returns the int at the given offset in d.data.
func (d *decoder) intAt(off int) int
isAbsPath
method
#
isAbsPath calls ctxt.IsAbsPath (if not nil) or else filepath.IsAbs.
func (ctxt *Context) isAbsPath(path string) bool
isDir
function
#
isDir calls ctxt.IsDir (if not nil) or else uses fsys.Stat.
func isDir(path string) bool
isIdent
function
#
func isIdent(c byte) bool
joinPath
method
#
joinPath calls ctxt.JoinPath (if not nil) or else filepath.Join.
func (ctxt *Context) joinPath(elem ...string) string
keys
function
#
func keys(m map[string]bool) []string
makePathsAbsolute
method
#
makePathsAbsolute looks for compiler options that take paths and
makes them absolute. We do this because through the 1.8 release we
ran the compiler in the package directory, so any relative -I or -L
options would be relative to that directory. In 1.9 we changed to
running the compiler in the build directory, to get consistent
build results (issue #19964). To keep builds working, we change any
relative -I or -L options to be absolute.
Using filepath.IsAbs and filepath.Join here means the results will be
different on different systems, but that's OK: -I and -L options are
inherently system-dependent.
func (ctxt *Context) makePathsAbsolute(args []string, srcDir string)
matchAuto
method
#
matchAuto interprets text as either a +build or //go:build expression (whichever works),
reporting whether the expression matches the build context.
matchAuto is only used for testing of tag evaluation
and in #cgo lines, which accept either syntax.
func (ctxt *Context) matchAuto(text string, allTags map[string]bool) bool
matchTag
method
#
matchTag reports whether the name is one of:
cgo (if cgo is enabled)
$GOOS
$GOARCH
boringcrypto
ctxt.Compiler
linux (if GOOS == android)
solaris (if GOOS == illumos)
tag (if tag is listed in ctxt.BuildTags or ctxt.ReleaseTags)
It records all consulted tags in allTags.
func (ctxt *Context) matchTag(name string, allTags map[string]bool) bool
moduleHash
function
#
moduleHash returns an ActionID corresponding to the state of the module
located at filesystem path modroot.
func moduleHash(modroot string, ismodcache bool) (cache.ActionID, error)
moduleWalkErr
function
#
moduleWalkErr returns filepath.SkipDir if the directory isn't relevant
when indexing a module or generating a filehash, ErrNotIndexed,
if the module shouldn't be indexed, and nil otherwise.
func moduleWalkErr(root string, path string, d fs.DirEntry, err error) error
name
method
#
func (sf *sourceFile) name() string
nameExt
function
#
func nameExt(name string) string
newEncoder
function
#
func newEncoder() *encoder
newImportReader
function
#
func newImportReader(name string, r io.Reader) *importReader
nextByte
method
#
nextByte is like peekByte but advances beyond the returned byte.
func (r *importReader) nextByte(skipSpace bool) byte
openIndexModule
function
#
openIndexModule returns the module index for modPath.
It will return ErrNotIndexed if the module can not be read
using the index because it contains symlinks.
func openIndexModule(modroot string, ismodcache bool) (*Module, error)
openIndexPackage
function
#
func openIndexPackage(modroot string, pkgdir string) (*IndexPackage, error)
packageFromBytes
function
#
packageFromBytes returns a *IndexPackage given the encoded representation.
func packageFromBytes(modroot string, data []byte) (p *IndexPackage, err error)
parseError
method
#
func (sf *sourceFile) parseError() string
parseErrorFromString
function
#
parseErrorFromString converts a string produced by parseErrorToString back
to an error. An empty string is converted to a nil error, and all
other strings are expected to be JSON-marshaled parseError structs.
The two functions are meant to preserve the structure of an
error of type scanner.ErrorList in a round trip, but may not preserve the
structure of other errors.
func parseErrorFromString(s string) error
parseErrorToString
function
#
parseErrorToString converts the error from parsing the file into a string
representation. A nil error is converted to an empty string, and all other
errors are converted to a JSON-marshaled parseError struct, with ErrorList
set for errors of type scanner.ErrorList, and ErrorString set to the error's
string representation for all other errors.
func parseErrorToString(err error) string
parseGoEmbed
function
#
parseGoEmbed parses the text following "//go:embed" to extract the glob patterns.
It accepts unquoted space-separated patterns as well as double-quoted and back-quoted Go strings.
This is based on a similar function in cmd/compile/internal/gc/noder.go;
this version calculates position information as well.
func parseGoEmbed(args string, pos token.Position) ([]fileEmbed, error)
parseWord
function
#
parseWord skips any leading spaces or comments in data
and then parses the beginning of data as an identifier or keyword,
returning that word and what remains after the word.
func parseWord(data []byte) (word []byte, rest []byte)
peekByte
method
#
peekByte returns the next byte from the input reader but does not advance beyond it.
If skipSpace is set, peekByte skips leading spaces and comments.
func (r *importReader) peekByte(skipSpace bool) byte
pkg
method
#
pkg returns the i'th IndexPackage in m.
func (m *Module) pkg(i int) *IndexPackage
pkgDir
method
#
pkgDir returns the dir string of the i'th package in the index.
func (m *Module) pkgDir(i int) string
pkgName
method
#
func (sf *sourceFile) pkgName() string
pkgOff
method
#
pkgOff returns the offset of the data for the i'th package in the index.
func (m *Module) pkgOff(i int) int
plusBuildConstraints
method
#
func (sf *sourceFile) plusBuildConstraints() []string
protect
function
#
protect marks the start of a large section of code that accesses the index.
It should be used as:
defer unprotect(protect, &err)
It should not be used for trivial accesses which would be
dwarfed by the overhead of the defer.
func protect() bool
readAt
method
#
readAt returns a reader starting at the given position in d.
func (d *decoder) readAt(pos int) *reader
readByte
method
#
readByte reads the next byte from the input, saves it in buf, and returns it.
If an error occurs, readByte records the error in r.err and returns 0.
func (r *importReader) readByte() byte
readByteNoBuf
method
#
readByteNoBuf is like readByte but doesn't buffer the byte.
It exhausts r.buf before reading from r.b.
func (r *importReader) readByteNoBuf() byte
readGoInfo
function
#
readGoInfo expects a Go file as input and reads the file up to and including the import section.
It records what it learned in *info.
If info.fset is non-nil, readGoInfo parses the file and sets info.parsed, info.parseErr,
info.imports and info.embeds.
It only returns an error if there are problems reading the file,
not for syntax errors in the file itself.
func readGoInfo(f io.Reader, info *fileInfo) error
readIdent
method
#
readIdent reads an identifier from the input.
If an identifier is not present, readIdent records a syntax error.
func (r *importReader) readIdent()
readImport
method
#
readImport reads an import clause - optional identifier followed by quoted string -
from the input.
func (r *importReader) readImport()
readKeyword
method
#
readKeyword reads the given keyword from the input.
If the keyword is not present, readKeyword records a syntax error.
func (r *importReader) readKeyword(kw string)
readString
method
#
readString reads a quoted string literal from the input.
If an identifier is not present, readString records a syntax error.
func (r *importReader) readString()
relPath
function
#
relPath returns the path relative to the module's root.
func relPath(path string, modroot string) string
safeCgoName
function
#
func safeCgoName(s string) bool
saveCgo
method
#
saveCgo saves the information from the #cgo lines in the import "C" comment.
These lines set CFLAGS, CPPFLAGS, CXXFLAGS and LDFLAGS and pkg-config directives
that affect the way cgo's C code is built.
func (ctxt *Context) saveCgo(filename string, di *build.Package, text string) error
shouldBuild
function
#
implements imports.ShouldBuild in terms of an index sourcefile.
func shouldBuild(sf *sourceFile, tags map[string]bool) bool
splitPathList
method
#
splitPathList calls ctxt.SplitPathList (if not nil) or else filepath.SplitList.
func (ctxt *Context) splitPathList(s string) []string
splitQuoted
function
#
splitQuoted splits the string s around each instance of one or more consecutive
white space characters while taking into account quotes and escaping, and
returns an array of substrings of s or an empty list if s contains only white space.
Single quotes and double quotes are recognized to prevent splitting within the
quoted region, and are removed from the resulting substrings. If a quote in s
isn't closed err will be set and r will have the unclosed argument as the
last element. The backslash is used for escaping.
For example, the following string:
a b:"c d" 'e''f' "g\""
Would be parsed as:
[]string{"a", "b:c d", "ef", `g"`}
func splitQuoted(s string) (r []string, err error)
string
method
#
string reads the next string.
func (r *reader) string() string
stringAt
method
#
stringAt returns the string pointed at by the int at the given offset in d.data.
func (d *decoder) stringAt(off int) string
stringTableAt
method
#
stringTableAt returns the string at the given offset in the string table d.str.
func (d *decoder) stringTableAt(off int) string
synopsis
method
#
func (sf *sourceFile) synopsis() string
syntaxError
method
#
syntaxError records a syntax error, but only if an I/O error has not already been recorded.
func (r *importReader) syntaxError()
tokpos
method
#
tokpos reads the next token.Position.
func (r *reader) tokpos() token.Position
unprotect
function
#
unprotect marks the end of a large section of code that accesses the index.
It should be used as:
defer unprotect(protect, &err)
end looks for panics due to errCorrupt or bad mmap accesses.
When it finds them, it adds explanatory text, consumes the panic, and sets *errp instead.
If errp is nil, end adds the explanatory text but then calls base.Fatalf.
func unprotect(old bool, errp *error)