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: provide a way to resolve an import path to a vcs, url, etc. #18387

Open
josharian opened this Issue Dec 20, 2016 · 3 comments

Comments

Projects
None yet
4 participants
@josharian
Contributor

josharian commented Dec 20, 2016

'go get' contains non-trivial logic to figure out how to obtain a package based on its import path, including consulting a list of well-known hosting providers and checking for html meta tags. There is no way to get this information other than actually doing the 'go get', or maintaining a non-trivial fork of a subset of cmd/go.

I'd like a way to get this information from cmd/go. Ideally it would be a library, a la package go/build, but a command line invocation would work as well, perhaps something accepting a format flag:

$ go get -resolve '{{.ImportPath}} {{.URL}} {{.IsCustom}} {{.VCS}} {{.Root}} and probably some vcs commands...' custom.xyz/pkg

or perhaps emitting JSON, with one entry per import path.

I asked for this originally at #18119 (comment). I noted there that @rogpeppe also wants this; see https://go-review.googlesource.com/8725. Another partial copy of this bit of cmd/go can be found at https://github.com/sdboyer/gps (e.g. https://github.com/sdboyer/gps/blob/master/discovery.go).

@josharian josharian added the GoCommand label Dec 20, 2016

@josharian josharian added this to the Proposal milestone Dec 20, 2016

@rakyll

This comment has been minimized.

Member

rakyll commented Dec 20, 2016

I'd love to see a go/get package a la go/build. It is a major blocker for custom tools that want to support go-get and not willing to shell out.

@dmitshur

This comment has been minimized.

Member

dmitshur commented Feb 2, 2017

a way to resolve an import path to a vcs, url, etc.

There is no way to get this information other than actually doing the 'go get', or maintaining a non-trivial fork of a subset of cmd/go. ... Ideally it would be a library

Please correct me if I'm misunderstanding what this issue is about (I'm surprised no one has said anything by now).

This very functionality already exists, and has existed for a long time, in golang.org/x/tools/go/vcs package, under the func named RepoRootForImportPath.

For example, here's output from running vcs.RepoRootForImportPath("rsc.io/pdf/pdfpasswd", false):

&vcs.RepoRoot{
	VCS:  (*vcs.Cmd)(vcs.vcsGit),
	Repo: (string)("https://github.com/rsc/pdf"),
	Root: (string)("rsc.io/pdf"),
}

Note that it correctly resolves a vanity import path to "https://github.com/rsc/pdf", and the repo root to "rsc.io/pdf". It's the same code that cmd/go uses for go get, just copied to another library where it can be imported.

I know this as someone who has sent multiple CLs in order to keep the two in sync (people sometimes send a fix to one, but forget the other).

There's also issue #11490 (/cc @adg) that tracks a possible unification of the two identical sets of code. It wasn't possible before because it was hard to have something both in cmd/go and another library. But I believe that may be changing as of #18653.

@sdboyer

This comment has been minimized.

Member

sdboyer commented Feb 2, 2017

Just seeing this - thanks @shurcooL for the pointer to it.

I'd also like very much to see a package like this. In fact, that's kinda what i wrote gps to be. Though, obviously, it can't be that in any official way unless/until dep makes it into the toolchain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment