Home > Software design >  Go mod tidy fails on private repository when git clone works
Go mod tidy fails on private repository when git clone works

Time:06-12

I put my ssh key in github so git clone works for a certain private repository; yet go mod tidy fails when trying to access the same repository, with the error message shown below. I could use help troubleshooting this problem. The error message from go mod tidy (redacting the name of the private repository, but otherwise verbatim) is:

    github.com/[private repository path]: cannot find module providing package github.com/[private repository path]: module github.com/[private repository path]: git ls-remote -q origin in /mnt/dependencies/gopath/pkg/mod/cache/vcs/a1f499df6a9855aecdc77bda31504008583a3268fdd403799aade71bb47df7d2: exit status 128:
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository

Please make sure you have the correct access rights
and the repository exists.

The successful git clone command (again redacting the private repository) is:

git clone [email protected]:[private repository path].git

My .gitconfig file is (with my name and email redacted):

[user]
    name = [my name]
    email = [my email]
[url "github.com:"]
    insteadOf = https://github.com/
[core]
    excludesfile = /workdir/.gitignore

Other things that might make a difference:

  • I am running commands as root in a Docker container.
  • I tried to run go mod tidy on my host with the same key in ~/.ssh as I have on the Docker container, and I got login prompts, which github no longer supports (they want SSH keys or personal access tokens).
  • The email in my gitconfig matches email given in the public key that I put on github.com.

A second, related question: Does the fact that I can run git clone rule out keys being uploaded wrong to github, and forgetting to enable SSO for the key on github?

Here is a similar question but it involves personal access tokens rather than ssh keys: `go mod tidy` fails to download private GitHub repository

CodePudding user response:

I am running commands as root in a Docker container.

Make sure then it does access the same keys (in /root/.ssh) as the ones you are using with your regular account.

And check if the Git global config settings actually include your insteadOf directive.

tried to run go mod tidy on my host with the same key in ~/.ssh as I have on the Docker container, and I got login prompts.

That means the public key is not properly registered to the GitHub user profile, or said user does not have the right to access the repository.

CodePudding user response:

I found the answer to my problem, but it turns on a fact that I omitted in the question.

I didn't realize until the next morning that my Docker image had a higher version of golang than was used to populate $GOPATH (or at least I didn't realize how important this was). To fix this, I deleted everything under $GOPATH and re-ran go mod tidy with no issues.

The checklist in VonC's answer is useful and more to the point of the question as stated, so I accepted it. But I'll add to it for anyone that comes across this post:

"Is the version of golang used for go mod tidy different from what populated $GOPATH?"

  • Related