How to use 'go get' or 'go mod vendor' for a specific module, without trying to update other modules?
Asked Answered
N

2

11

I'm trying to get a specific package from github for a project.

However, when I use go get [url] or go mod vendor, I get a git fetch error for lack of permissions to one of my company's repos. This repo is vendored, which is how we get around it for go test, go build etc.

This is the error message:

go: private.work.repo.com/project/[email protected]: invalid version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in C:\Users\NICHOLAS.TAN\go\pkg\mod\cache\vcs\37594aeb10b98234e04b4780cf59f32c4ad7bb9da460f552103ae748cea73aa1: exit status 128:
        fatal: remote error: Repository not found
        The requested repository does not exist, or you do not have permission to
        access it.

Is there a way for me to use go get and/or go mod vendor without those commands trying to look at the other module dependencies?

Nancynandor answered 23/7, 2020 at 4:9 Comment(7)
cant you use go get <package name> for packages you need?Millennial
What's your go version? Could you paste the result of go env?Cobos
Using GOPRIVATE=*.corp.com go get [url] would exclude *.corp.com from go modules proxy which is a common reason for fetch problems from private repos.Divergent
@Millennial I will update question to be more clear, but issue is that if my project has a dependent module 'A' that requires access to a private repo, when I try to 'go get B', I will get an error about not being able to access repo A.Nancynandor
@Cobos my go version is 1.14.6, here is my go env (with a little bit edited for work reasons)Nancynandor
@DmitryVerhoturov I tried this but still seem to get the same error, weirdly enough. Not sure if I am using it wrong, this is the repo URL: git.companyname.com/project/.... and I am using command go get [url] GOPRIVATE=*companyname.comNancynandor
I think it should be the other way around: ENV variable then the command. GOPRIVATE=*.companyname.com go get [url] or export GOPRIVATE=*.companyname.com once before doing go get.Divergent
C
5

You can get specific version of package use go get <package>@<version> in your project directory, for example:

% go get github.com/golang/[email protected] 

go: downloading github.com/golang/protobuf v1.4.0

Go download only get github.com/golang/protobuf package required version to local cache ($GOPATH/pkg/mod) and set version to go.mod file.

After all, if you have dependencies from your company's repos in local cache ($GOPATH/pkg/mod), use go mod vendor to create vendor (Go get ones from cache).

Courtnay answered 23/7, 2020 at 5:58 Comment(6)
Thank you, however the issue isn't that I can't get a specific version, but that it tries to fetch dependencies that are already there while trying to go get a new one, which is causing problems because I don't have access to all of those dependencies.Nancynandor
I tried this one (get a specific version) without access to my company's private repo 👉 it works (private repo dependencies was located in my local cache)Courtnay
@Nancynandor - Do you use modules or old approach?Courtnay
@Nancynandor Did you find a solution ?Gantt
@Gantt Sorry, I can't recall clearly any more, but I think I just temporarily manually removed those files from the go.mod/go.sum, ran the commands, and then added them back.Nancynandor
can something similar be done if you want to get a specific branch from a repo? using your example, what if there was a dev branch of protobuf, how would you go along trying to get that specific branch, possibly for testing purposes?Punkah
C
2

This kind of issues will occur while getting private module and please check below configuration already done in your local system

Update git global config url

 git config --global --add url."[email protected]:".insteadOf "https://github.com/"  

Update go env to access private go module

go env -w GOPRIVATE=github.com/abc/*

Then run installation command

Clevey answered 8/11, 2021 at 10:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.