Today I Learned — Fix for “Fatal: could not read Username for ‘https://gitlab.companyname.com’: terminal prompts disabled”

Pavel Gordon
2 min readAug 13, 2020

--

Introduction

I got into new project, which was written in pure Go and was based on a private Gitlab server. When I tried to follow the instructions and set it up, I encountered the following error while installing dependencies:

go: gitlab.companyname.com/inbox/mediaproxy@v0.0.0–20190510132048–77ca670c6dcc: invalid version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in /Users/pavelgordon/go/pkg/mod/cache/vcs/2d951819783281690e044152fbf2405ded1a3254778a249218ea484a3c1ac759: exit status 128:
fatal: could not read Username for ‘https://gitlab.companyname.com’: terminal prompts disabled

There are a couple of solutions to this problem that I found on internet after brief search, but none of them worked for me unfortunately, so I had to come out with my own.

Explanation

There are bunch of problems:

go get disables the “terminal prompt” by default.

This can be changed by setting an environment variable of git for a single command:

env GIT_TERMINAL_PROMPT=1 go get github.com/mycompany/mypackage

or export it for the end of terminal session by running
export GIT_TERMINAL_PROMPT=1 and then running your go get or go mod download -x
This is a minor issue and is not a root cause.

go get can’t establish https connection to gitlab server

The root problem is the connection to the gitlab. go get tries to connect to gitlab.companyname.com using https, and in this type of authentication it requires user to enter username and password via the terminal prompt. While it might work for a single go get <package> execution, this approach has multiple drawbacks, e.g. when you have more dependencies you will have to enter credentials for every package, or security reasons. Better approach is to use ssh, so let’s do it!
Mark that address as a private repo location

go env -w GOPRIVATE=gitlab.companyname.com

Force git client to use ssh over https. This will use our /.ssh, so make sure our ssh key exists and registered in our Gitlab. Here is how you can set up ssh keys for gitlab

git config — global url.”git@git.companyname.com:”.insteadOf “https://gitlab.companyname.com/"

That’s it! Enjoy your day.

Other solution

According to this thread, the winning combination is:

  • create a gitlab Personal Access Token with at least read_api and read_repository scopes.
  • create a ~/.netrc file with following content:
machine machine gitlab.com
login yourname@gitlab.com
password yourpersonalaccesstoken
  • use go get --insecure to get your module
  • do not use the .gitconfig insteadOf thingie

It didn’t work for me, but hey — it is worth a try.

Sources

--

--