-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/go: add go get -printdir #18119
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
Comments
Is there a good reason for it other than explanatory docs? We've always written cd $GOPATH before without worrying about this. Most people don't know it's a list or don't care. There's also very few times I can think of when you definitively want the first GOPATH entry. Usually you want the source for a particular package, which is
|
I often seen docs where people include $GOPATH in bash scripts exactly in ways where it would fail if my GOPATH actually were a list, encouraging people to not make it a list. I'd like there to be some official answer people could use in docs instead.
|
Maybe this can wait for Go 1.9? |
Indeed. I didn't intend it for Go 1.8. |
In general it would be good to express the question we want answered instead of the mechanism. In this case we want to know where a particular package will be installed, without really caring about the fact that GOPATH is how we get there. Maybe |
Other possibilities: |
Another possibility is accept a format string. Then this feature could also be used to learn the VCS system that go get would use, the remote url, the destination on disk, the clone command, whether it is a custom import path, etc. I currently maintain a modified fork of cmd/go just for doing this kind of import path resolution. @rogpeppe does too; see https://go-review.googlesource.com/8725. |
|
OK, so -printdir never does anything but only prints the absolute path of the directory where 'go get' would check out the named package, or where one is already checked out that 'go get -u' would update. If there are no arguments (packages on command line), then that's an error. There must be at least one. If there are multiple arguments, it prints one line for each. Must print to standard output. |
@bradfitz in your example, |
The idea was this would be done purely lexically. It would not hit the network or attempt to parse the arguments in any way (including using repoRootForImportPath) |
Ok, but to be super clear, then, it is definitely not true that:
|
"would attempt to check out" ? Are you just saying we're missing the words "attempt to" or "try to"? |
I guess? In the case of What would To handle the the "would update" case, does |
As an aside, for the specific |
Verbatim verbatim. The code would literally be: first := filepath.SplitList(goroot)[0]
for _, arg := range args {
fmt.Println(filepath.Join(first, arg))
} |
No, that's the wrong implementation. The whole point of going down this 'go get -printdir' path was because the answer varies by import path. If the target already exists in a later entry in GOPATH, go get will use that one. Josh is right that you really need to give it the actual package you care about. The script is wrong. It should be:
(note dot at end of final command) |
Ah, okay. You can tell how often I use multiple entries in my $GOPATH. |
What about a separate command, go dir. (The suggested semantics below apply to a -printdir flag as well but it being on go get seems weird to me, since it never gets anything) Given no arguments it prints the elements of $GOPATH, one per line (or the default if not set). Given arguments it assumes they're packages and prints their absolute paths (including /src/), one per line. Failing if any do not exist. This also would allow it work with /... or any of the special names. If you only care about the first pipe through head -n 1 or sed 1q. You can redirect to /dev/null if you just want to check the exit code. If it's a separate command these could be flags on the command, though there's no need for them on unixy systems, at least. That seems like it would cover all the cases for shell scripting. The only problem would be something like go dir $EMPTY_VAR, but if it's a separate command no args could be an error and go dir -gopath could print $GOPATH. |
Honestly, I think this is a non-issue and we should do nothing at all. But I would argue strongly against any command that exposes what GOPATH is directly. Obviously code that cares can find out. But that's almost always the wrong question to be asking. The appropriate question is "where is the code for this particular import path?" The reason this is on 'go get' is that if you've already got the code you can use 'go list -f whatever-you-need'. The one case that doesn't handle is where the code is yet to be downloaded, and 'go get -printdir' will tell you where it would put the code after the download. |
The one question I often find myself trying to reliably answer with multiple entries in my |
@myitcv, there's already an answer for that question:
This bug is about a question for which there's no current way to extract an answer. |
@bradfitz thanks, and apologies for the noise. |
CL https://golang.org/cl/48410 mentions this issue. |
@rsc, could you review https://golang.org/cl/48410 ? |
@rsc said on the CL:
|
Is this still an issue, particularly given module mode and |
ping @rsc |
Timed out in state WaitingForInfo. Closing. (I am just a bot, though. Please speak up if this is a mistake or you have the requested information.) |
I'd like a succinct and portable way to get the first element of the user's GOPATH.
Currently:
Obviously we can't change that.
I propose a
-1
flag togo env
to only print the first GOPATH element, using https://golang.org/pkg/path/filepath/#SplitListThe text was updated successfully, but these errors were encountered: