Skip to content

x/tools/go/packages: NeedDeps has no effect #56633

@adonovan

Description

@adonovan

The documentation for the NeedDeps field is unclear. It says:

        // NeedDeps adds the fields requested by the LoadMode in the packages in Imports.

I think perhaps the idea might be more clearly expressed as:

       // NeedDeps causes all Packages transitively reachable through
       // the Imports map to also be populated according to the LoadMode.

but that isn't what actually happens. Observe:

xtools$ cat main.go 
package main

import (
	"fmt"
	"log"

	"golang.org/x/tools/go/packages"
)

func main() {
	cfg := &packages.Config{
		Mode: packages.NeedImports |
			packages.NeedCompiledGoFiles |
			//packages.NeedDeps |       // try with and without this line
			0,
	}
	pkgs, err := packages.Load(cfg, "runtime")
	if err != nil {
		log.Fatalf("load: %v", err)
	}
	packages.Visit(pkgs, nil, func(p *packages.Package) {
		fmt.Println(p, len(p.CompiledGoFiles))
	})
}

xtools$ go run  ./main.go  > with
# Now uncomment the NeedDeps line.
xtools$ go run  ./main.go  > without
xtools$ diff with without
xtools$
# nothing

In other words, the NeedDeps flag seems to have no effect when the NeedImports flag is set. And If you remove NeedImports, only a single package is reported, regardless of NeedDeps. It's as if the flag has no effect at all.

I would expect the following behaviors:

  • Imports=true, deps=true: import graph fully populated according to mode bits
  • imports=true, deps=false: import map is populated to depth 1; dependencies may be stubs.
  • imports=false: import map is nil. Deps flag has no effect.

Metadata

Metadata

Assignees

Labels

ToolsThis label describes issues relating to any tools in the x/tools repository.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions