Skip to content

x/tools/go/packages: Packages.{Syntax,TypeInfo} fields are empty unless NeedTypes #63517

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

Open
adonovan opened this issue Oct 12, 2023 · 2 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@adonovan
Copy link
Member

adonovan commented Oct 12, 2023

This program, which uses go/packages:

package main

import (
	"fmt"
	"log"
	"os"

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

func main() {
	mode := packages.NeedSyntax | packages.NeedTypesInfo | packages.NeedTypesSizes
	f(mode)
	f(mode | packages.NeedTypes)
}

func f(mode packages.LoadMode) {
	cfg := &packages.Config{Mode: mode}
	pkgs, err := packages.Load(cfg, "fmt")
	if err != nil {
		log.Fatal(err)
	}
	if packages.PrintErrors(pkgs) > 0 {
		os.Exit(1)
	}
	fmt.Printf("Mode: %s\n", mode)
	fmt.Printf("TypesInfo = %p\n", pkgs[0].TypesInfo)
	fmt.Printf("Types = %p\n", pkgs[0].Types)
	fmt.Printf("Syntax = %v\n", pkgs[0].Syntax)
	fmt.Println()
}

produces this output:

Mode: LoadMode(NeedSyntax|NeedTypesInfo|NeedTypesSizes)
TypesInfo = 0x0
Types = 0x0
Syntax = []

Mode: LoadMode(NeedTypes|NeedSyntax|NeedTypesInfo|NeedTypesSizes)
TypesInfo = 0x1400023b770
Types = 0x1400032e000
Syntax = [0x1400064c000 0x140001f0000 0x1400064c090 0x140001f0090 0x140000da090]

Why does absence of the NeedTypes mode flag cause the Syntax and TypesInfo fields, both explicitly "needed", to be missing? I think that's a bug.

This is not the first such bug like this that we've seen, and in my experience the fine-grained mode bits seem to demand a great deal of experimentation before one finds something that works. Also, they create a powerset of paint shades of Package structures of varying degrees of population, which means a function that receives a Package doesn't really know what it's going to get. In practice almost all clients want one of four things:

  • just the metadata for one package +/- its deps;
  • the syntax of one package;
  • syntax and complete type information for one package (with deps from export data);
  • complete typed syntax for everything.

If memory serves a lot of the hair splitting was because of the surprising costs of some fields (e.g. package name?) in Bazel/Blaze. I'm not convinced it was worthwhile, and I wish we could un-deprecate the old compound modes and improve their documentation (perhaps with some better-named aliases). A few compound modes would cover the vast majority of practical cases, with the option of fine-grained bits for the brave and foolhardy.

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Oct 12, 2023
@gopherbot gopherbot added this to the Unreleased milestone Oct 12, 2023
@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 13, 2023
@dmitshur
Copy link
Member

CC @matloob.

@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 <[email protected]>
Reviewed-by: Robert Findley <[email protected]>
Reviewed-by: Michael Matloob <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. 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