Including privately hosted go module

I’m new to golang and inherited a codebase that I am trying to run. In the source code I’m executing go build ., but that complains aboouot missing go.sum entry, and helpfully it tells me what to do: go mod download private-host.net/org/harbor-client. Now the problem is that private-host is a bitbucket server running on default port 7990 and using port number in module name/require is not allowed. Next attempt to download source code and put it under $GOPATH/src/private-host.net/org/harbor-client directory, but that doesn’t help, go is still trying to download from private bitbucket server and failing because there is no way to include port number. How can I use that module (it is auto-generated from swagger.yaml file) in my other source code? It seems like the issue might because there is a tag(?) added after module name in go.mod file (private-host.net/org/harbor-client v0.0.0-20220613162241-f7f8a7118946), but I have no idea where is it coming from and how can I edit it to match what is in $GOPATH/src/ path…

Hi @Rapolas_K,

You could try telling Git (that Go uses under the hood unless you use another VCS) to use ssh instead of HTTP by adding an url insteadOf to your Git config:

[url "ssh://git@private-host.net:7990/"]
	insteadOf = https://private-host.net/

(To be honest, I never tried that option on a private Git server.)

If the ssh fallback does not work, try pre-downloading the code into $GOPATH/pkg/mod/private-host.net/org/harbor-client. (The go command does not search the $GOPATH/src path for cached modules AFAIK.)

As a last resort, you could install a Go proxy server, but this is the most involved option.

The funny tag is an auto-generated version number. Go adds this when the module has no tagged version number. This should not cause any issue with the download, but you would want to add the correct version to go.mod (like v1.2.3).

Thanks for your reply. Changing git config works for git, but Go doesn’t seem to care about it. Tried both http (replacing with http with port number) and ssh versions.

Cloning repo into $GOPATH/pkg/mod/private-host.net/org/harbor-client also didn’t help. Any attempt to use harbor-client immediately makes Go attempt to download and complain about “unrecognized import path private-host.net/org/harbor-client?go-get=1: dial 10.60.122.36:80 connect: connection refused”, that makes sense, as the server is not listening on port 80 (nor 443).

I’ll see if I can figure out how to setup Go proxy server.

You might also try setting up a simple HTTP server that maps an import path to a VCS URL, as described here: Go Modules Reference > Version control systems

TL;DR: (shortened and with added emphasizes)


If the module path has a VCS qualifier (one of .bzr, .fossil, .git, .hg, .svn) at the end of a path component, the go command will [use the respective VCS to download the repo]

If the module path does not have a qualifier, the go command sends an HTTP GET request to a URL derived from the module path with a ?go-get=1 query string. For example, for the module golang.org/x/mod, the go command may send the following requests:

https://golang.org/x/mod?go-get=1 (preferred)
http://golang.org/x/mod?go-get=1  (fallback, only with GOINSECURE)

The go command follows redirects but otherwise ignores response status codes, so the server may respond with a 404 or any other error status. The GOINSECURE environment variable may be set to allow fallback and redirects to unencrypted HTTP for specific modules.

The server must respond with an HTML document containing a <meta> tag in the document’s <head>. The <meta> tag should appear early in the document to avoid confusing the go command’s restricted parser. In particular, it should appear before any raw JavaScript or CSS. The <meta> tag must have the form:

<meta name="go-import" content="root-path vcs repo-url">

[For example:]

<meta name="go-import" content="golang.org/x/mod git https://go.googlesource.com/mod">

From this response, the go command will use the Git repository at the remote URL https://go.googlesource.com/mod.

[…]

After the repository URL is found, the go command will clone the repository into the module cache.


In your case, if you are able to host the HTTP server directly at private-host.net on port 80 or 443, the meta tag to be returned would look like this:

<meta name="go-import" content="private-host.net/org/harbor-client git https://private-host.net:7990/org/harbor-client">

Thanks again. Unfortunately, I can’t spin a proxy server on the same host where bitbucket is running, so trying to run it on different server. Renaming module to use other-host.net/org/harbor-client and re-runing go build gives similar output as before:

go: downloading other-host.net/org/harbor-client v0.0.0-20220613162241-f7f8a7118946
labeller.go:11:2: other-host.net/org/harbor-client@v0.0.0-20220613162241-f7f8a7118946: invalid version: git ls-remote -q origin in $GOPATH/pkg/mod/cache/vcs/b71575aafda93a8fbd9cd241334506eb4dfca3833bf87653e766ff3bcb0ab801: exit status 128:
        fatal: remote error: Repository not found
        The requested repository does not exist, or you do not have permission to
        access it.

Even if manually executing git ls-remote -q origin in $GOPATH/pkg/mod/cache/vcs/b71575aafda93a8fbd9cd241334506eb4dfca3833bf87653e766ff3bcb0ab801 gives no output and exit status of 0.

If I change module to have .git at the end, it complains about missing go.sum entry:

labeller.go:11:2: other-host.net/org/harbor-client.git@v0.0.0-20220613162241-f7f8a7118946: missing go.sum entry; to add it:
        go mod download other-host.net/org/harbor-client.git

Executing go mod download other-host.net/org/harbor-client.git gives another error:

go: other-host.net/org/harbor-client.git@v0.0.0-20220613162241-f7f8a7118946: verifying go.mod: other-host.net/org/harbor-client.git@v0.0.0-20220613162241-f7f8a7118946/go.mod: reading https://sum.golang.org/lookup/other-host.net/org/harbor-client.git@v0.0.0-20220613162241-f7f8a7118946: 404 Not Found
        server response:
        not found: other-host.net/org/harbor-client.git@v0.0.0-20220613162241-f7f8a7118946: invalid version: git ls-remote -q origin in /tmp/gopath/pkg/mod/cache/vcs/48c1bdabe3cdfb3202eaeb030dd0ca5f558a9b11751613f733f01661d5814cab: exit status 128:
                fatal: transport 'git' not allowed

Note, cloning with git via proxy server using http works fine.

Apologies, for some reasons, I did not receive a notification about your previous update.

I realize I did not ask whether you have set the GOPRIVATE env variable to include the module import URL. This should suppress queries to the sun database that resulted in the 404 error. (see Go Modules Reference - The Go Programming Language)

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.