From 8d71ae8c775129161fda64e77b2de2276e7362e2 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Mon, 16 Jan 2023 08:02:34 -0500 Subject: [PATCH] cmd/go/internal/modindex: remove copy of build.Package modindex defines its own type Package that must be exactly the same as build.Package (there are conversions between them). The normal reason to do this is to provide a different method set, but there aren't any different methods. And if we needed to do that, we could write type Package build.Package instead of repeating the struct definition. Remove the type entirely in favor of direct use of build.Package. This makes the modindex package not break when fields are added to go/build.Package. Change-Id: I8ffe9f8832bbc62be93a72e6a09d807ddbce216b Reviewed-on: https://go-review.googlesource.com/c/go/+/462255 Reviewed-by: Bryan Mills Auto-Submit: Russ Cox TryBot-Result: Gopher Robot Run-TryBot: Russ Cox --- src/cmd/go/internal/modindex/build.go | 81 ++------------------------- src/cmd/go/internal/modindex/read.go | 6 +- src/cmd/go/internal/modindex/scan.go | 2 +- 3 files changed, 8 insertions(+), 81 deletions(-) diff --git a/src/cmd/go/internal/modindex/build.go b/src/cmd/go/internal/modindex/build.go index e4380973e0..afc976521d 100644 --- a/src/cmd/go/internal/modindex/build.go +++ b/src/cmd/go/internal/modindex/build.go @@ -13,6 +13,7 @@ import ( "errors" "fmt" "go/ast" + "go/build" "go/build/constraint" "go/token" "io" @@ -210,80 +211,6 @@ func (ctxt *Context) gopath() []string { var defaultToolTags, defaultReleaseTags []string -// A Package describes the Go package found in a directory. -type Package struct { - Dir string // directory containing package sources - Name string // package name - ImportComment string // path in import comment on package statement - Doc string // documentation synopsis - ImportPath string // import path of package ("" if unknown) - Root string // root of Go tree where this package lives - SrcRoot string // package source root directory ("" if unknown) - PkgRoot string // package install root directory ("" if unknown) - PkgTargetRoot string // architecture dependent install root directory ("" if unknown) - BinDir string // command install directory ("" if unknown) - Goroot bool // package found in Go root - PkgObj string // installed .a file - AllTags []string // tags that can influence file selection in this directory - ConflictDir string // this directory shadows Dir in $GOPATH - BinaryOnly bool // cannot be rebuilt from source (has //go:binary-only-package comment) - - // Source files - GoFiles []string // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles) - CgoFiles []string // .go source files that import "C" - IgnoredGoFiles []string // .go source files ignored for this build (including ignored _test.go files) - InvalidGoFiles []string // .go source files with detected problems (parse error, wrong package name, and so on) - IgnoredOtherFiles []string // non-.go source files ignored for this build - CFiles []string // .c source files - CXXFiles []string // .cc, .cpp and .cxx source files - MFiles []string // .m (Objective-C) source files - HFiles []string // .h, .hh, .hpp and .hxx source files - FFiles []string // .f, .F, .for and .f90 Fortran source files - SFiles []string // .s source files - SwigFiles []string // .swig files - SwigCXXFiles []string // .swigcxx files - SysoFiles []string // .syso system object files to add to archive - - // Cgo directives - CgoCFLAGS []string // Cgo CFLAGS directives - CgoCPPFLAGS []string // Cgo CPPFLAGS directives - CgoCXXFLAGS []string // Cgo CXXFLAGS directives - CgoFFLAGS []string // Cgo FFLAGS directives - CgoLDFLAGS []string // Cgo LDFLAGS directives - CgoPkgConfig []string // Cgo pkg-config directives - - // Test information - TestGoFiles []string // _test.go files in package - XTestGoFiles []string // _test.go files outside package - - // Dependency information - Imports []string // import paths from GoFiles, CgoFiles - ImportPos map[string][]token.Position // line information for Imports - TestImports []string // import paths from TestGoFiles - TestImportPos map[string][]token.Position // line information for TestImports - XTestImports []string // import paths from XTestGoFiles - XTestImportPos map[string][]token.Position // line information for XTestImports - - // //go:embed patterns found in Go source files - // For example, if a source file says - // //go:embed a* b.c - // then the list will contain those two strings as separate entries. - // (See package embed for more details about //go:embed.) - EmbedPatterns []string // patterns from GoFiles, CgoFiles - EmbedPatternPos map[string][]token.Position // line information for EmbedPatterns - TestEmbedPatterns []string // patterns from TestGoFiles - TestEmbedPatternPos map[string][]token.Position // line information for TestEmbedPatterns - XTestEmbedPatterns []string // patterns from XTestGoFiles - XTestEmbedPatternPos map[string][]token.Position // line information for XTestEmbedPatternPos -} - -// IsCommand reports whether the package is considered a -// command to be installed (not just a library). -// Packages named "main" are treated as commands. -func (p *Package) IsCommand() bool { - return p.Name == "main" -} - // NoGoError is the error used by Import to describe a directory // containing no buildable Go source files. (It may still contain // test files, files hidden by build tags, and so on.) @@ -316,7 +243,7 @@ func nameExt(name string) string { return name[i:] } -func fileListForExt(p *Package, ext string) *[]string { +func fileListForExt(p *build.Package, ext string) *[]string { switch ext { case ".c": return &p.CFiles @@ -448,7 +375,7 @@ func parseWord(data []byte) (word, rest []byte) { return word, rest } -var dummyPkg Package +var dummyPkg build.Package // fileInfo records information learned about a file included in a build. type fileInfo struct { @@ -685,7 +612,7 @@ Lines: // 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 *Package, text string) error { +func (ctxt *Context) saveCgo(filename string, di *build.Package, text string) error { for _, line := range strings.Split(text, "\n") { orig := line diff --git a/src/cmd/go/internal/modindex/read.go b/src/cmd/go/internal/modindex/read.go index c83000c4a1..2d40201408 100644 --- a/src/cmd/go/internal/modindex/read.go +++ b/src/cmd/go/internal/modindex/read.go @@ -515,7 +515,7 @@ func (rp *IndexPackage) Import(bctxt build.Context, mode build.ImportMode) (p *b if !shouldBuild || tf.ignoreFile() { if ext == ".go" { p.IgnoredGoFiles = append(p.IgnoredGoFiles, name) - } else if fileListForExt((*Package)(p), ext) != nil { + } else if fileListForExt(p, ext) != nil { p.IgnoredOtherFiles = append(p.IgnoredOtherFiles, name) } continue @@ -530,7 +530,7 @@ func (rp *IndexPackage) Import(bctxt build.Context, mode build.ImportMode) (p *b Sfiles = append(Sfiles, name) continue default: - if list := fileListForExt((*Package)(p), ext); list != nil { + if list := fileListForExt(p, ext); list != nil { *list = append(*list, name) } continue @@ -585,7 +585,7 @@ func (rp *IndexPackage) Import(bctxt build.Context, mode build.ImportMode) (p *b } } if directives := tf.cgoDirectives(); directives != "" { - if err := ctxt.saveCgo(name, (*Package)(p), directives); err != nil { + if err := ctxt.saveCgo(name, p, directives); err != nil { badGoFile(name, err) } } diff --git a/src/cmd/go/internal/modindex/scan.go b/src/cmd/go/internal/modindex/scan.go index 712257ac21..dce8e09a23 100644 --- a/src/cmd/go/internal/modindex/scan.go +++ b/src/cmd/go/internal/modindex/scan.go @@ -181,7 +181,7 @@ func importRaw(modroot, reldir string) *rawPackage { // We still haven't checked // that p.dir directory exists. This is the right time to do that check. // We can't do it earlier, because we want to gather partial information for the - // non-nil *Package returned when an error occurs. + // non-nil *build.Package returned when an error occurs. // We need to do this before we return early on FindOnly flag. if !isDir(absdir) { // package was not found -- 2.48.1