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

cmd/doc: 'go doc $pkg' uses GOPATH instead of active module #28992

Open
bcmills opened this Issue Nov 28, 2018 · 6 comments

Comments

Projects
None yet
3 participants
@bcmills
Member

bcmills commented Nov 28, 2018

go doc calls out to cmd/doc, and cmd/doc implicitly uses GOPATH to resolve the imports instead of looking in the active modules. That results in unexpected skew: it produces spurious failures when the package exists but is not in GOPATH, and spurious documentation if GOPATH contains unpublished changes (as in the example in the details below).

This is one instance of #24661, but the tool in question happens to be bundled with cmd/go and released with the Go toolchain: we should fix it sooner rather than later.

CC @robpike @mvdan @ianthehat @rsc

~/src/golang.org/x/build$ export GO111MODULE=on

~/src/golang.org/x/build$ go version
go version devel +d8b7a9286d Wed Nov 28 13:07:32 2018 -0500 linux/amd64

~/src/golang.org/x/build$ go list -m golang.org/x/tools
golang.org/x/tools v0.0.0-20181030000716-a0a13e073c7b

~/src/golang.org/x/build$ go list golang.org/x/tools/cmd/goforward
go: downloading golang.org/x/tools v0.0.0-20181030000716-a0a13e073c7b
go: extracting golang.org/x/tools v0.0.0-20181030000716-a0a13e073c7b
go: finding golang.org/x/tools/cmd/goforward latest
go: finding golang.org/x/tools/cmd latest
go: finding golang.org/x/tools latest
go: downloading golang.org/x/tools v0.0.0-20181127232545-e782529d0ddd
go: extracting golang.org/x/tools v0.0.0-20181127232545-e782529d0ddd
can't load package: package golang.org/x/tools/cmd/goforward: unknown import path "golang.org/x/tools/cmd/goforward": cannot find module providing package golang.org/x/tools/cmd/goforward

~/src/golang.org/x/build$ go doc golang.org/x/tools/cmd/goforward | head -n 2
The goforward command adds forwarding declarations from one Go package to
another.
@mvdan

This comment has been minimized.

Member

mvdan commented Nov 28, 2018

I was under the impression that cmd/doc had been adapted to modules with the rest of the built-in tooling. Thanks for raising the issue.

I presume that we should keep the GOPATH logic for when the command is called outside a module, and introduce module logic otherwise.

I believe that only dirs.go would need changes, as that's the part that scans GOPATH and feeds the packages it finds to the rest of the code.

@bcmills

This comment has been minimized.

Member

bcmills commented Nov 28, 2018

I presume that we should keep the GOPATH logic for when the command is called outside a module, and introduce module logic otherwise.

No, we should only use the GOPATH logic when not in module mode (that is, when go env GOMOD produces the empty string).

In module mode outside of a module (as of CL 148517) we should produce documentation for the latest version of the package, perhaps using go list and/or go mod download.

@mvdan

This comment has been minimized.

Member

mvdan commented Nov 28, 2018

@myitcv rightfully pointed out https://go-review.googlesource.com/c/go/+/126799 - it does seem like support was added.

@bcmills

This comment has been minimized.

Member

bcmills commented Nov 28, 2018

Ah, looks like the bug is here:

usingModules = bytes.Contains(stdout, []byte("go.mod"))

I'm setting GOMOD to os.DevNull in module mode outside a module. Might be a trivial fix — thanks for the reference!

@bcmills

This comment has been minimized.

Member

bcmills commented Nov 28, 2018

Turns out not to be so trivial after all. CL 126799 added module-mode support for everything except packages, which apparently need more than just the source code for the package (i.e. need go/packages).

CC @matloob

@gopherbot

This comment has been minimized.

gopherbot commented Nov 28, 2018

Change https://golang.org/cl/151617 mentions this issue: cmd/doc: treat any non-empty GOMOD as module mode

gopherbot pushed a commit that referenced this issue Nov 29, 2018

cmd/doc: treat any non-empty GOMOD as module mode
Previously, we were looking for the string go.mod specifically, but
the module-mode-outside-a-module logic added in CL 148517 sets GOMOD
to os.DevNull

Updates #28992

Change-Id: I62a4baaa911a495350294d78bae96be3fe4866cb
Reviewed-on: https://go-review.googlesource.com/c/151617
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment