23

I developed a repo on computer A and created a go.mod/go.sum that I checked in.

I pull that repo with the go.mod/go.sum files on computer B, but when I try to build the program, the module constraints can't be satisfied.

$ go build
go: finding github.ibm.com/kms/key-protect-client v0.1.5
go: finding golang.org/x/tools v0.0.0-20180221164845-07fd8470d635
go: github.ibm.com/kms/key-protect-client@v0.1.5: unknown revision v0.1.5
go: error loading module requirements

The repo that is failing is a private repo, and for some reason it doesn't get downloaded to the module cache. On another computer, the dependencies are downloaded and the build succeeds. I am building another private repo in that same domain, so I know that my github credentials give me access to these repos. But for some reason, the go module system can't get to the dependent repo.

I cannot find more information how to debug this.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Ravenwater
  • 735
  • 1
  • 5
  • 14
  • The error isn't that it can't find it, it is `unknown revision v0.1.5` - meaning it did find it, but that git tag doesn't exist. – Adrian Apr 03 '19 at 19:51
  • @adrian, the tag exists in the repo, as its existence made it to the go.mod file on computer A. Just can't get to use it on computer B, and I would like to understand why. That whole private domain (github.ibm.com) does not get created in the mod cache, which I think is the reason why the tag can't be found on computer B. – Ravenwater Apr 03 '19 at 19:54
  • 1
    Not sure if it is possible with IBM's GitHub instance, but a potential solution can be found in the answer to this question on SO: https://stackoverflow.com/questions/53516358/golang-modules-private-repos-and-gopath (using a GITHUB_TOKEN). – retgits Apr 03 '19 at 20:56
  • @retgits it indeed has something to do with access: it is not picking up the right protocol, and I haven't found guidance how to control it inside the go build tool. There are multiple git identities on computer B and my local git config specialized for that repo doesn't seem to control the protocol, so it is using https where it needs to use the token. – Ravenwater Apr 03 '19 at 21:18
  • 1
    Can you clone github.ibm.com/kms/key-protect-client on computer B using git command line? If not, it sounds like it is more of a git problem than a go problem. – Mad Wombat Apr 03 '19 at 21:27
  • @MadWombat I can clone the repo via ssh on computer B. However, when I look at what go build is doing, it is trying to pull via https and that fails. How do I control the go build process to use public and private repos given some particular git identity? – Ravenwater Apr 04 '19 at 13:27
  • @MadWombat inside the private repo I am using a local .git override, so that my git credentials are correct for this tree. However, from what I can tell, go seems to pick up the global git identity. Haven't found any docs yet that explain how to specialize go mod to use a specific git identity. – Ravenwater Apr 04 '19 at 13:32
  • Yep, that was it. The go build tools pick up the global git identity ignoring the local override. When I set the global .gitconfig to the IBM credentials go build downloads all the private repo dependencies and succeeds. – Ravenwater Apr 04 '19 at 13:42

10 Answers10

32

Possible solutions for golang unknown revision if you are using private repository:

  1. git config --global url."ssh://git@yourserver".insteadOf "https://yourserver"

  2. Check your git repository permissions

igonejack
  • 2,366
  • 20
  • 29
6

I had the same problem, please, visit the link .

Make sure you have set GO111MODULES

go env -w GO111MODULE=on

Make sure your git configuration is appropriate for private repositories

git config --global url."ssh://git@github.com:acme-corporation".insteadOf "https://github.com/acme-corporation"

Make sure to create a personal access token if using 2FA

git config --global url."https://:x-oauth-basic@github.com:acme-corporation".insteadOf "https://github.com/acme-corporation"

in my case after setting:

  • ~/.gitconfig
  • ~/.bashrc

I had to do the same with the ~ / .netrc file:

machine gitlab.url.com
    login <gitlab username>
    password <GITLAB_TOKEN>
dixoen
  • 355
  • 4
  • 12
  • 3
    Some people (including me) discourage using `.netrc` for security reasons - adding a plaintext password or auth token is imo bad practice and should only be used to establish the cause of the failure. There are other solutions to this problem - see the above posts. – Remigius Stalder May 23 '21 at 09:43
3

if nothing helps (see the former answers before), use this:

go get -u

The -u flag instructs get to update modules providing dependencies of packages named on the command line to use newer minor or patch releases when available.

Furthermore, I recommend using a versions manager like gobrew. You can easily change between the versions because sometimes it can be a version-related issue too.

If you do so, you can also set the compatibility version for your tidy, e.g:

go mod tidy -compat=1.17

Now you are ready to build:

go clean --modcache
go get -u
go build
2

Problem is related to cmd/go not being aware of git identities and assuming there is only one articulated in the .gitconfig file.

Work-around: have a set of git identities ready to replace global .gitconfig when you need to work with that git identity and are dealing with cmd/go tools that update the module file.

Ravenwater
  • 735
  • 1
  • 5
  • 14
1

In case getting

"Error": "unknown revision ..."

or

"Error": "invalid version ..."

Make sure that you did not specify hash of PR (pull request). Even a merged PR might be wrong.

Look for the commit hash in the github repo under /commits, but not under /pulls.

See more info here: golang/go#31191 explicitly filters out these kind of commits.

Noam Manos
  • 15,216
  • 3
  • 86
  • 85
1

Just in case, did you add "github.ibm.com/kms" to your environment variable GOPRIVATE on computer B? I've had this error occur to me due to exactly this reason.

AMSantiago
  • 11
  • 1
1

Usually the reason can be that the repository is private so you should be authorized at first. Create .netrc file and include there your credentials, the full description you can find here. https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html

if you want to build a docker image you can use this DockerFile example

ENV GOPRIVATE "gitlab.com/yourPrivateRepo"

ARG GO_GET_TOKEN_PASS
ARG GO_GET_TOKEN_LOGIN

RUN touch ~/.netrc && \
    chmod 600 ~/.netrc && \
    echo "machine gitlab.com login ${GO_GET_TOKEN_LOGIN} password ${GO_GET_TOKEN_PASS}" > ~/.netrc
har17bar
  • 728
  • 1
  • 8
  • 22
0

In line with some comments above -- I had this problem because my git was fried due to updating to Mojave... the highly upvoted response here (xcode-select --install) got me past this error message Git is not working after macOS Mojave Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools)

if you are having this problem make sure your go status etc are working as expected.

0

In some cases, in addition to configuring an url-insteadOf configuration in git, you have to disable GOPROXY and GOSUMDB, as follows:

$ go env -w GOPROXY=direct GOSUMDB=off

Running go env before and after shows you the effect. Disabling them using the above command is establishing that this was the reason for the failure. As disabling checksum verification has some security concerns, you might want to keep GOPROXY and GOSUMDB for imported public modules. You can find some more information on how to achieve this here:

https://jfrog.com/blog/why-goproxy-matters-and-which-to-pick/

Remigius Stalder
  • 1,921
  • 2
  • 26
  • 31
0

In my case, it seems like the default location from which Go was trying to download the modules did not include the (old) revision that was required. Setting the Go proxy environment variable like this export GOPROXY=https://proxy.golang.org fixed the problem for me.

La Buse
  • 31
  • 1
  • 3