Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
adonovan opened this issue Nov 7, 2022 · 2 comments
Open

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

adonovan opened this issue Nov 7, 2022 · 2 comments
Assignees
Labels
Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@adonovan
Copy link
Member

adonovan commented Nov 7, 2022

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.
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/588141 mentions this issue: go/packages: document fields that are part of JSON schema

gopherbot pushed a commit to golang/tools that referenced this issue May 29, 2024
Package is both the "cooked" result data type of a Load
call, and the "raw" JSON schema used by DriverResponse.

This change documents the fields that are part of the
protocol, and ensures that the others are omitted from
the JSON encoding. (They are populated by the post-
processing done by 'refine', if the appropriate Need
bits are set.)

Also
- document that there are a number of open bugs
  in places where it may be likely to help,
  particularly Mode-related issues.
- document that Load returns new Packages,
  using distinct symbol realms (types.Importers).
- document Overlays in slightly more detail.

Fixes golang/go#67614
Fixes golang/go#61418
Fixes golang/go#67601
Fixes golang/go#43850
Updates golang/go#65816
Updates golang/go#58726
Updates golang/go#56677
Updates golang/go#48226
Updates golang/go#63517
Updates golang/go#56633

Change-Id: I2f5f2567baf61512042fc344fca56494f0f5e638
Reviewed-on: https://go-review.googlesource.com/c/tools/+/588141
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/632375 mentions this issue: go/packages: fix behavior of NeedDeps and NeedImports

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

3 participants