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/go: go list should return more information about relative import paths #14348
Comments
I don't think I understand what is being requested. As I understand it, all the information being requested is already easily derived from what is reported already:
|
The raw import path is necessary, IMHO, to correctly report relative paths to source files, as it is done by the golint command. |
Sorry, bear with me, but I still don't understand. It sounds like you want to be able to distinguish the output of the two
and also that you want
to report about a package "./ioutil" instead of "io/ioutil". I don't really see why that's useful. The standard name of the package is io/ioutil, not ./ioutil. The fact that you can refer to it as "./ioutil" or "./..." from within the $GOROOT/src/io directory is a convenience only. It shouldn't propagate into the rest of the toolchain. I don't know what golint does for reporting relative paths to source files, but I can tell you what the go command itself does, and I would suggest golint do the same. The go command uses this code, which works very well in practice. At least I can't remember a single complaint about the decisions it makes:
|
On Tue, Feb 16, 2016 at 6:31 PM, Russ Cox notifications@github.com wrote:
No, I don't want to change how standard packages are handled.
That is, all the paths are prefixed with "..", since this is how I but I can tell you what the go command itself does, and I would suggest
I prefer the golint solution, since it seems more coherent. Thanks! |
On golang-dev you asked:
The default for imports in Go is to be "absolute" or "fully-qualified" in the sense that import "X" has a fixed meaning regardless of the directory in which it appears (ignoring vendoring). Within GOROOT and GOPATH, this is the only permitted import form. Outside GOROOT and GOPATH, mainly to allow simple experiments and throwaway programs, the go command permits an import to use a relative import path like "./X", which obviously changes meaning based on the directory in which it appears. But directory a/b/c might import "./d" and directory a/b might import "./c/d" and directory a/b/c/x might import "../d" and those all refer to the same directory, so internally the go command must construct one canonical import path for that directory. The path it constructs is _/, which you see, for example, in the output of go list. The go command refers to packages found outside GOROOT/GOPATH as "local", in the sense that they are not part of the GOROOT/GOPATH space. Code in GOROOT/GOPATH needs to be self-contained, so it cannot import local packages, although of course imports in the other direction are fine. All that concerns import paths found in import statements in source code. The go command line processes import paths as well, and there it is convenient to allow mentioning a directory (for example, in GOROOT/src/io, saying . or ./ioutil) as a shorthand for the standard, absolute import path of that directory (for that example, io or io/ioutil). The shorthand is limited to command line argument processing. The code in GOROOT/src/io cannot import "./ioutil". |
OK, I guess it comes down to this:
That's right, that's how the go command works today:
The error message refers to Now I understand what you want from the go command, but I don't see a good way to get it to you. The fact is, the model you're asking for is contrary to how the go command thinks about import paths, about packages, and about files. It could go out of its way to do something it doesn't need, for clients that want to be different, but the tool ecosystem would be more consistent if clients followed the go command's lead. I don't think it's worth adding complexity to the go command just to allow golint to (continue to?) print file names in an idiosyncratic way. |
On Tue, Feb 16, 2016 at 8:20 PM, Russ Cox notifications@github.com wrote:
Thanks. This makes things more clear. By the way: is this documented somewhere? I don't remember reading a |
On Tue, Feb 16, 2016 at 8:27 PM, Russ Cox notifications@github.com wrote:
$ cd $GOROOT/src/io/ioutil should print ioutil, not ../ioutil
I agree. I found this issue only because I was trying to do the "right" thing in my |
The go command prints source file (*.go) paths are printed relative to the current directory (when that makes them shorter). The basic motivation here is that when you run 'go build' (to build the code in the current directory) you want to see a compiler error reported in 'foo.go' and not 'c:\the\directory\you\are\already\in\foo.go'. Import paths are different: they are always printed fully qualified (even including unvendoring them). And go list is really just shorthand for go list -f '{{.ImportPath}}'. |
This is my latest informal patch to add Path and Cmdline fields to the Package struct: I don't know if it is worth the additional complexity; probably not. |
Given that we're approaching three years since the last reply on this issue, let's do the latter. |
Suppose you want to implement a tool like
golint
usinggo list
.When reporting a problem,
golint
prints the full path to a source file, but when the user specified a relative import path,golint
prints a relative path.Unfortunately the
Package
struct returned fromgo list
does not allow this.The only way to obtain a full path to a .go source file is to use the
Dir
field, but this is always an absolute path.The
Package
struct used bygo list
should have the following additional fields:The text was updated successfully, but these errors were encountered: