0

I am trying to install a go package from a local directory (basically I checked out and existing package and applied a pending pull request).

$ # Both commands give a similar output
$ go get -u file:///Users/me/workspaces/go/somepackage
$ go get -u /Users/me/workspaces/go/somepackage
unrecognized import path "[...]" (import path does not begin with hostname)

Since go get is downloading then installing, I tried:

$ go install /Users/me/workspaces/go/somepackage
[] cannot import absolute path

Any experienced go user could give a hand?

AsTeR
  • 7,247
  • 14
  • 60
  • 99
  • Are you trying to install a main package, or a dependency? If it's a `main` package, just cd into it an install it, there's nothing to `go get`. If it's a dependency, it should either already be in GOPATH, or use `replace` in `go.mod`. Packages are never referred to with a local path. – JimB Apr 22 '19 at 15:49

3 Answers3

1

If you just want to use a different branch (your PR branch), you can continue using the original import path.

  • Go get the original package first.
  • Go into the downloaded package in your local file system cd $GOPATH/pkg/<package directory>
  • From your local package cache, switch to the branch you want to pull from. git checkout <PR branch>
  • Now go get -u <package>

If the package is available locally, go get update will just pull the latest code from the branch your local package is checked out to.

svetha.cvl
  • 824
  • 4
  • 8
  • I have ended installing another package as the original package was also broken on install. But this seems to be an answer to my question (+1). – AsTeR Apr 23 '19 at 07:57
0

As far as I know you can't do this with go get, bur you can copy the directory in to GOPATH manually. If you forked example.com/somepackage then copy /Users/me/workspaces/go/somepackage to ~/go/src/example.com/somepackage or ./vendor/example.com/somepackage.

That being said, the best solution is usually to host the forked code somewhere, and then use that import path. Decency tools such as dep and modules support fetching a different origin for packages.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
0

I could be wrong and maybe there is a workaround exists

Go documentantion on cmd/go/#hdr-Remote_import_paths says:

The supported version control systems are:

Bazaar      .bzr
Fossil      .fossil
Git         .git
Mercurial   .hg
Subversion  .svn

And a later:

For example,

import "example.org/pkg/foo"

will result in the following requests:

https://example.org/pkg/foo?go-get=1 (preferred)
http://example.org/pkg/foo?go-get=1  (fallback, only with -insecure)

So I suppose that go get never looks in filesystem repos.

Need to investigate source code of go get to be sure.

I will be glad if someone will prove that I am incorrect in this question.

UPDATE

Maybe I'm mistaken but I suppose that all abilities of go get to work with VCSs are here: https://github.com/golang/go/blob/master/src/cmd/go/internal/get/vcs.go

And I don't see here a possibility to work with git local repos.

Alex Yu
  • 3,412
  • 1
  • 25
  • 38