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: passing NeedSyntax to Load() doesn't populate Syntax field unless NeedTypes is passed #35331

Closed
nevkontakte opened this issue Nov 3, 2019 · 4 comments

Comments

@nevkontakte
Copy link

@nevkontakte nevkontakte commented Nov 3, 2019

What version of Go are you using (go version)?

$ go version
go version go1.12.10 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
 $ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/aleks/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/aleks/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build037965907=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Calling packages.Load() with packages.NeedSyntax doesn't actually populate package's Syntax field, unless packages.NeedTypes is specified at the same time. This behavior seems to contradict documentation:

 // Syntax is the package's syntax trees, for the files listed in CompiledGoFiles.
//
// The NeedSyntax LoadMode bit populates this field for packages matching the patterns.
// If NeedDeps and NeedImports are also set, this field will also be populated
// for dependencies.
Syntax []*ast.File

This makes it impossible to load and parse a package without simultaneously type-checking it, which may not always be desirable.

Minimal example to reproduce
package main

import (
        "go/token"
        "log"

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

func main() {
        cfg := &packages.Config{
                Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles |
                   packages.NeedImports | packages.NeedDeps | packages.NeedSyntax,
                Fset: token.NewFileSet(),
        }
        pkgs, err := packages.Load(cfg, "strconv")
        if err != nil {
                log.Fatalf("package.Load() returned error: %s", err)
                return
        }
        if count := packages.PrintErrors(pkgs); count > 0 {
                log.Fatalf("Encountered errors while loading, can't continue.")
                return
        }

        packages.Visit(pkgs, func(pkg *packages.Package) bool {
                log.Printf("Package %q has %d files parsed.", pkg, len(pkg.Syntax))
                return true
        }, nil)
}

What did you expect to see?

 $ go run main.go
2019/11/03 19:36:07 Package "strconv" has 10 files parsed.
2019/11/03 19:36:07 Package "errors" has 1 files parsed.
2019/11/03 19:36:07 Package "internal/bytealg" has 7 files parsed.
2019/11/03 19:36:07 Package "internal/cpu" has 3 files parsed.
2019/11/03 19:36:07 Package "unsafe" has 0 files parsed.
2019/11/03 19:36:07 Package "math" has 45 files parsed.
2019/11/03 19:36:07 Package "math/bits" has 2 files parsed.
2019/11/03 19:36:07 Package "unicode/utf8" has 1 files parsed.

What did you see instead?

 $ go run main.go
2019/11/03 19:35:26 Package "strconv" has 0 files parsed.
2019/11/03 19:35:26 Package "errors" has 0 files parsed.
2019/11/03 19:35:26 Package "internal/bytealg" has 0 files parsed.
2019/11/03 19:35:26 Package "internal/cpu" has 0 files parsed.
2019/11/03 19:35:26 Package "unsafe" has 0 files parsed.
2019/11/03 19:35:26 Package "math" has 0 files parsed.
2019/11/03 19:35:26 Package "math/bits" has 0 files parsed.
2019/11/03 19:35:26 Package "unicode/utf8" has 0 files parsed.

@dmitshur

This comment has been minimized.

Copy link
Member

@dmitshur dmitshur commented Nov 3, 2019

/cc @matloob per owners.

@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Nov 4, 2019

Change https://golang.org/cl/205160 mentions this issue: go/packages: fix loading of syntax when types not requested

@nevkontakte

This comment has been minimized.

Copy link
Author

@nevkontakte nevkontakte commented Nov 4, 2019

Thanks for a super-quick fix!

@matloob

This comment has been minimized.

Copy link
Contributor

@matloob matloob commented Nov 4, 2019

Thanks for finding the issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.