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 loader in 1.14 broke go doc examples? #38376

Closed
posener opened this issue Apr 11, 2020 · 3 comments
Closed

x/tools/go/packages: package loader in 1.14 broke go doc examples? #38376

posener opened this issue Apr 11, 2020 · 3 comments
Labels
FrozenDueToAge 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

@posener
Copy link

posener commented Apr 11, 2020

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

$ go version
go version go1.14.1 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
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="0"
GOMOD="/test/go.mod"
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build760461752=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I try to load package examples. In 1.13 I get the right example, in 1.14 I get the entire file and replaces the example code with func main() { ... }.
Look at pkg.go.dev or go doc examples.

What did you expect to see?

What was shown in go 1.13 - only the example code.

What did you see instead?

The entire test file with the example code replaced with func main() { ... }.

More details

The code below behaves differently in 1.13 and 1.14:

package main

import (
	"fmt"
	"go/doc"
	"os"

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

func main() {
	cfg := &packages.Config{Mode:  packages.LoadAllSyntax, Tests: true}
	pkgs, err := packages.Load(cfg, "github.com/posener/fuzzing")
	if err != nil {
		os.Exit(1)
	}
	if packages.PrintErrors(pkgs) > 0 {
		os.Exit(1)
	}

	for _, pkg := range pkgs {
		fmt.Println(pkg.Name)
		examples := doc.Examples(pkg.Syntax...)
		if len(examples) > 0 {
			fmt.Println(examples[0].Play)
		}
	}
}

In 1.13 fmt.Println(examples[0].Play) prints nil for the fuzzing package, though in 1.14 it prints non-nil object. This breaks what gddo expects in https://github.com/golang/gddo/blob/master/doc/builder.go#L233. I think.

@andybons
Copy link
Member

@matloob

@andybons andybons changed the title Package loader in 1.14 broke go doc examples? x/tools/go/packages: package loader in 1.14 broke go doc examples? Apr 13, 2020
@gopherbot gopherbot added this to the Unreleased milestone Apr 13, 2020
@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Apr 13, 2020
@andybons andybons added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. and removed Tools This label describes issues relating to any tools in the x/tools repository. labels Apr 13, 2020
@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Apr 13, 2020
@antong
Copy link
Contributor

antong commented Apr 13, 2020

@dmitshur
Copy link
Contributor

Thanks for reporting. The bug is in doc.Examples, not in the package loader. See issue #38409.

@golang golang locked and limited conversation to collaborators Apr 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge 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

5 participants