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: Package.Fset is nil when using NeedSyntax #48226

Closed
gordonklaus opened this issue Sep 7, 2021 · 5 comments
Closed

x/tools/go/packages: Package.Fset is nil when using NeedSyntax #48226

gordonklaus opened this issue Sep 7, 2021 · 5 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

@gordonklaus
Copy link
Contributor

What did you do?

package main

import (
	"fmt"
	"golang.org/x/tools/go/packages"
)

func main() {
	conf := &packages.Config{
		Mode: packages.NeedSyntax,
	}
	pkgs, err := packages.Load(conf, "fmt")
	if err != nil {
		panic(err)
	}
	fmt.Println(pkgs[0].Fset == nil)
}

What did you expect to see?

false

What did you see instead?

true

Rationale

This is apparently working as intended, as the documentation reads:

	// Fset provides position information for Types, TypesInfo, and Syntax.
	// It is set only when Types is set.
	Fset *token.FileSet

However, I see no reason why Fset should not be exposed when NeedSyntax is requested. In particular, Fset is needed for looking up position information for syntax elements.

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Sep 7, 2021
@gopherbot gopherbot added this to the Unreleased milestone Sep 7, 2021
@thanm thanm added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 7, 2021
@thanm
Copy link
Contributor

thanm commented Sep 7, 2021

@matloob per owners

@adonovan
Copy link
Member

I agree that this is a bug.

In my experience, the packages.Config.Mode API is just hateful. One is forced to choose between the compound Load... modes, whose names are misleading, whose use triggers a deprecation warning in the IDE, and whose current incremental and undocumented declarations do not aid comprehension at all; or the fine-grained Need... modes, which are riddled with bugs such as this one due to the explosion of combinations in the implementation, and which by design invite bugs in other code because of the exponential number of possible "flavors" of Package that result.

Every time I use Mode, I try to do the right thing and specify just the bits I need, and then realize through painful trial and error that I need to enable some extra bit such as NeedTypes just to get access to the FileSet, defeating any possible benefit of the fine-grained interface. Apologies for my part in getting us into this mess.

We should fix the bugs in the fine-grained implementation, but more importantly I think we should un-deprecate the compound modes, creating new ones as needed with appropriate names for the major use cases, and document exactly what every existing mode is good for.

@dominikh
Copy link
Member

Another fun interaction of modes: without NeedModule, go/types.Config.GoVersion won't be set. Which is slightly surprising, because NeedModule is documented as populating the Module field, not as being relevant to type-checking.

@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>
edigaryev added a commit to edigaryev/golang-tools that referenced this issue Jul 27, 2024
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

Successfully merging a pull request may close this issue.

5 participants