Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: build fails when using GIT_DIR environment variable #56862

Open
horacimacias opened this issue Nov 20, 2022 · 7 comments
Open

cmd/go: build fails when using GIT_DIR environment variable #56862

horacimacias opened this issue Nov 20, 2022 · 7 comments
Labels
GoCommand cmd/go NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@horacimacias
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.19.1 darwin/arm64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/...../Library/Caches/go-build"
GOENV="/Users/...../Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/...../go/pkg/mod"
GOOS="darwin"
GOPATH="/Users/..../go"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/opt/homebrew/Cellar/go/1.19.1/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/homebrew/Cellar/go/1.19.1/libexec/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.19.1"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="..../go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/0y/6l073n8s6gj4gzqccdtb009h3rtc0w/T/go-build757008333=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Create a project with the .git folder being renamed as testgit instead of the default .git.
All git operations function normally as long as GIT_DIR=testgit is passed through the environment.
Try to build the project using

env GIT_DIR=testgit go build ./...

What did you expect to see?

Success building project

What did you see instead?

error obtaining VCS status: exit status 128
	Use -buildvcs=false to disable VCS stamping.

Looks like go build is not considering that the git directory may have been overriden using GIT_DIR.
If I rename my testgit folder to the default .git and do not use GIT_DIR environment variable, go build ./... succeeds.

@seankhliao seankhliao changed the title go build fails when using GIT_DIR environment variable cmd/go: build fails when using GIT_DIR environment variable Nov 20, 2022
@seankhliao seankhliao added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. GoCommand cmd/go labels Nov 20, 2022
@seankhliao
Copy link
Member

cc @bcmills @matloob

@bcmills
Copy link
Contributor

bcmills commented Nov 21, 2022

@horacimacias, what was the complete stderr output of the failing go build command?

@bcmills bcmills added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Nov 21, 2022
@bcmills bcmills added this to the Backlog milestone Nov 21, 2022
@horacimacias
Copy link
Author

horacimacias commented Nov 21, 2022

running env GIT_DIR=testgit go build ./... gave me

error obtaining VCS status: exit status 128
	Use -buildvcs=false to disable VCS stamping.

@bcmills
Copy link
Contributor

bcmills commented Nov 21, 2022

So, probably a good starting point would be to fix cmd/go/internal/vcs.gitStatus to include the command's stderr output as part of the error so that this can be diagnosed further.

@horacimacias
Copy link
Author

horacimacias commented Nov 21, 2022

adding -v gave a bit more information:

env GIT_DIR=testgit go build -v ./...

# cd /Users/some_other_folder; git status --porcelain
fatal: not a git repository: 'testgit'
error obtaining VCS status: exit status 128
	Use -buildvcs=false to disable VCS stamping.

looks like that cd /Users/some_other_folder is go trying to "navigate up" until it finds a .git folder? it's assuming .git is the GIT_DIR which is the default, but it's not using the overriden value from env variable.

RootNames: []rootName{
{filename: ".git", isDir: true},
},

In case this is of any help, in my case I have a "root" project git the "normal" .git folder there.
Inside this project, I have some "sample go projects" where a testgit folder is used, to avoid using .git again since this is not the root of the overall project.
In this "sample go projects", I can perform git operations using GIT_DIR and everything is ok.
In fact I can also do env GIT_DIR=testgit git status --porcelain and I get no output or error.

Running env GIT_DIR=testgit git -c log.showsignature=false show -s --format=%H:%ct also succeeds:

1e55531ca86b3e8099149ff7aec173db95c777b0:1669015511

@horacimacias
Copy link
Author

are there any tests on this feature I can have a look at? if somebody points me to some starting point I may be able to contribute with a PR

@bcmills
Copy link
Contributor

bcmills commented Dec 1, 2022

@horacimacias, the existing tests for VCS stamping are in GOROOT/src/cmd/go/testdata/script/*buildvcs*.txt. (See the README in that directory for a description of the script language.)

@seankhliao seankhliao removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Dec 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GoCommand cmd/go NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants