x/tools/go/packages: on Go 1.19+, explicitly ask for -json fields needed

This change uses the new feature on the go command to explicitly ask
for needed fields on the JSON output to ask for needed fields. Not
requesting most of those fields doesn't make as much of a difference,
but being able to skip Deps and Stale should add decent gains in most
cases.

Change-Id: I30e9d53fbdaf2ab485d73d5d9b9ccd60a78ed10c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/393017
Run-TryBot: Michael Matloob <matloob@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Michael Matloob 2022-03-15 14:44:20 -04:00
parent 00aa68c09b
commit 5bb9c48e96
4 changed files with 81 additions and 4 deletions

View File

@ -446,7 +446,11 @@ func (state *golistState) createDriverResponse(words ...string) (*driverResponse
// Run "go list" for complete
// information on the specified packages.
buf, err := state.invokeGo("list", golistargs(state.cfg, words)...)
goVersion, err := state.getGoVersion()
if err != nil {
return nil, err
}
buf, err := state.invokeGo("list", golistargs(state.cfg, words, goVersion)...)
if err != nil {
return nil, err
}
@ -809,10 +813,70 @@ func absJoin(dir string, fileses ...[]string) (res []string) {
return res
}
func golistargs(cfg *Config, words []string) []string {
func jsonFlag(cfg *Config, goVersion int) string {
if goVersion < 19 {
return "-json"
}
var fields []string
added := make(map[string]bool)
addFields := func(fs ...string) {
for _, f := range fs {
if !added[f] {
added[f] = true
fields = append(fields, f)
}
}
}
addFields("Name", "ImportPath", "Error") // These fields are always needed
if cfg.Mode&NeedFiles != 0 || cfg.Mode&NeedTypes != 0 {
addFields("Dir", "GoFiles", "IgnoredGoFiles", "IgnoredOtherFiles", "CFiles",
"CgoFiles", "CXXFiles", "MFiles", "HFiles", "FFiles", "SFiles",
"SwigFiles", "SwigCXXFiles", "SysoFiles")
if cfg.Tests {
addFields("TestGoFiles", "XTestGoFiles")
}
}
if cfg.Mode&NeedTypes != 0 {
// CompiledGoFiles seems to be required for the test case TestCgoNoSyntax,
// even when -compiled isn't passed in.
// TODO(#52435): Should we make the test ask for -compiled, or automatically
// request CompiledGoFiles in certain circumstances?
addFields("Dir", "CompiledGoFiles")
}
if cfg.Mode&NeedCompiledGoFiles != 0 {
addFields("Dir", "CompiledGoFiles", "Export")
}
if cfg.Mode&NeedImports != 0 {
// When imports are requested, DepOnly is used to distinguish between packages
// explicitly requested and transitive imports of those packages.
addFields("DepOnly", "Imports", "ImportMap")
if cfg.Tests {
addFields("TestImports", "XTestImports")
}
}
if cfg.Mode&NeedDeps != 0 {
addFields("DepOnly")
}
if usesExportData(cfg) {
// Request Dir in the unlikely case Export is not absolute.
addFields("Dir", "Export")
}
if cfg.Mode&needInternalForTest != 0 {
addFields("ForTest")
}
if cfg.Mode&needInternalDepsErrors != 0 {
addFields("DepsErrors")
}
if cfg.Mode&NeedModule != 0 {
addFields("Module")
}
return "-json=" + strings.Join(fields, ",")
}
func golistargs(cfg *Config, words []string, goVersion int) []string {
const findFlags = NeedImports | NeedTypes | NeedSyntax | NeedTypesInfo
fullargs := []string{
"-e", "-json",
"-e", jsonFlag(cfg, goVersion),
fmt.Sprintf("-compiled=%t", cfg.Mode&(NeedCompiledGoFiles|NeedSyntax|NeedTypes|NeedTypesInfo|NeedTypesSizes) != 0),
fmt.Sprintf("-test=%t", cfg.Tests),
fmt.Sprintf("-export=%t", usesExportData(cfg)),

View File

@ -71,6 +71,13 @@ const (
// NeedTypesSizes adds TypesSizes.
NeedTypesSizes
// needInternalDepsErrors adds the internal deps errors field for use by gopls.
needInternalDepsErrors
// needInternalForTest adds the internal forTest field.
// Tests must also be set on the context for this field to be populated.
needInternalForTest
// typecheckCgo enables full support for type checking cgo. Requires Go 1.15+.
// Modifies CompiledGoFiles and Types, and has no effect on its own.
typecheckCgo
@ -403,6 +410,8 @@ func init() {
config.(*Config).modFlag = value
}
packagesinternal.TypecheckCgo = int(typecheckCgo)
packagesinternal.DepsErrors = int(needInternalDepsErrors)
packagesinternal.ForTest = int(needInternalForTest)
}
// An Error describes a problem with a package's metadata, syntax, or types.

View File

@ -231,7 +231,9 @@ func (s *snapshot) config(ctx context.Context, inv *gocommand.Invocation) *packa
packages.NeedImports |
packages.NeedDeps |
packages.NeedTypesSizes |
packages.NeedModule,
packages.NeedModule |
packages.LoadMode(packagesinternal.DepsErrors) |
packages.LoadMode(packagesinternal.ForTest),
Fset: s.FileSet(),
Overlay: s.buildOverlay(),
ParseFile: func(*token.FileSet, string, []byte) (*ast.File, error) {

View File

@ -23,6 +23,8 @@ var GetGoCmdRunner = func(config interface{}) *gocommand.Runner { return nil }
var SetGoCmdRunner = func(config interface{}, runner *gocommand.Runner) {}
var TypecheckCgo int
var DepsErrors int // must be set as a LoadMode to call GetDepsErrors
var ForTest int // must be set as a LoadMode to call GetForTest
var SetModFlag = func(config interface{}, value string) {}
var SetModFile = func(config interface{}, value string) {}