{"record":{"id":"f20245bfe73835eb","repo":"golang/go","slug":"failed-to-execute-git-version-w","errorCode":null,"errorMessage":"failed to execute git version: %w","messagePattern":"failed to execute git version: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/modfetch/codehost/git.go","lineNumber":995,"sourceCode":"}\n\nfunc (r *gitRepo) runGit(ctx context.Context, cmdline ...any) ([]byte, error) {\n\targs := RunArgs{cmdline: cmdline, dir: r.dir, local: r.local}\n\tif !r.local {\n\t\t// Manually supply GIT_DIR so Git works with safe.bareRepository=explicit set.\n\t\t// This is necessary only for remote repositories as they are initialized with git init --bare.\n\t\targs.env = []string{\"GIT_DIR=\" + r.dir}\n\t}\n\treturn RunWithArgs(ctx, args)\n}\n\n// Capture the major, minor and (optionally) patch version, but ignore anything later\nvar gitVersLineExtract = regexp.MustCompile(`git version\\s+(\\d+\\.\\d+(?:\\.\\d+)?)`)\n\nfunc gitVersion() (string, error) {\n\tgitOut, runErr := exec.Command(\"git\", \"version\").CombinedOutput()\n\tif runErr != nil {\n\t\treturn \"v0\", fmt.Errorf(\"failed to execute git version: %w\", runErr)\n\t}\n\treturn extractGitVersion(gitOut)\n}\n\nfunc extractGitVersion(gitOut []byte) (string, error) {\n\tmatches := gitVersLineExtract.FindSubmatch(gitOut)\n\tif len(matches) < 2 {\n\t\treturn \"v0\", fmt.Errorf(\"git version extraction regexp did not match version line: %q\", gitOut)\n\t}\n\treturn \"v\" + string(matches[1]), nil\n}\n\nfunc hasAtLeastGitVersion(minVers string) (bool, error) {\n\tgitVers, gitVersErr := gitVersion()\n\tif gitVersErr != nil {\n\t\treturn false, gitVersErr\n\t}\n\treturn semver.Compare(minVers, gitVers) <= 0, nil","sourceCodeStart":977,"sourceCodeEnd":1013,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/modfetch/codehost/git.go#L977-L1013","documentation":"gitVersion() shells out to `git version` via exec.Command and this error wraps the exec failure with %w. It means the go command could not even invoke the git binary. The function returns \"v0\" as a fallback version alongside the error.","triggerScenarios":"Any code path that calls hasAtLeastGitVersion or gitVersion when git is not on PATH, not executable, or the process lacks permission to spawn it.","commonSituations":"Minimal containers (distroless, scratch-derived) that omit git; PATH overwritten by a wrapper script; git binary renamed on Windows; CI images that install git only in a later stage.","solutions":["Install git in the environment (apt-get install git, apk add git, choco install git, etc.).","Ensure the git executable is on PATH for the go command process — print `os.Getenv(\"PATH\")` from the launcher to confirm.","On Windows verify git.exe is reachable; the go command looks for bare `git`.","If git truly cannot be provided, switch the dependency to a proxy-served module (GOPROXY) so the codehost git path is never exercised."],"exampleFix":"// before — distroless image with no git\nFROM gcr.io/distroless/static\n// error: failed to execute git version: exec: \"git\": executable file not found in $PATH\n\n// after\nFROM debian:slim\nRUN apt-get update && apt-get install -y git","handlingStrategy":"validation","validationCode":"func gitAvailable() bool {\n    _, err := exec.LookPath(\"git\")\n    return err == nil\n}\n\n// call before invoking any go mod subcommand that may touch a VCS.","typeGuard":null,"tryCatchPattern":"if vers, err := gitVersion(); err != nil {\n    if strings.Contains(err.Error(), \"failed to execute git version\") {\n        // surface a friendlier 'install git' message to the user\n    }\n    return \"v0\", err\n}","preventionTips":["Bake git into CI and container images explicitly — do not assume the base image includes it.","Add a startup check `exec.LookPath(\"git\")` in any wrapper that calls the go command with VCS modules.","Keep GOPROXY=https://proxy.golang.org as a fallback so VCS paths are optional."],"tags":["git","exec","environment","path"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}