golang/go · error · PackageError

main package is in repository %q but current directory is in

Error message

main package is in repository %q but current directory is in repository %q

What it means

Thrown during VCS stamping (the -buildvcs mechanism) at line 2590. When -buildvcs is explicitly enabled (not 'auto'), the loader requires the main package's directory (p.Dir) and the current working directory to resolve to the SAME VCS repository root via vcs.FromDir. If they differ, embedding VCS revision/status into the binary is ambiguous, so the build errors instead of guessing. With -buildvcs=auto the mismatch silently omits VCS info (goto omitVCS).

Source

Thrown at src/cmd/go/internal/load/pkg.go:2590

				// "-buildvcs=auto" means that we should silently drop the VCS metadata.
				goto omitVCS
			}
		}
	}
	if repoDir != "" && vcsCmd.Status != nil {
		// Check that the current directory, package, and module are in the same
		// repository. vcs.FromDir disallows nested VCS and multiple VCS in the
		// same repository, unless the GODEBUG allowmultiplevcs is set. The
		// current directory may be outside p.Module.Dir when a workspace is
		// used.
		pkgRepoDir, _, err := vcs.FromDir(p.Dir, "")
		if err != nil {
			setVCSError(err)
			return
		}
		if pkgRepoDir != repoDir {
			if cfg.BuildBuildvcs != "auto" {
				setVCSError(fmt.Errorf("main package is in repository %q but current directory is in repository %q", pkgRepoDir, repoDir))
				return
			}
			goto omitVCS
		}
		modRepoDir, _, err := vcs.FromDir(p.Module.Dir, "")
		if err != nil {
			setVCSError(err)
			return
		}
		if modRepoDir != repoDir {
			if cfg.BuildBuildvcs != "auto" {
				setVCSError(fmt.Errorf("main module is in repository %q but current directory is in repository %q", modRepoDir, repoDir))
				return
			}
			goto omitVCS
		}

		st, err := vcsStatusCache.Do(repoDir, func() (vcs.Status, error) {

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Run `go build` from within the same repository that contains the main package.
  2. Use `-buildvcs=auto` (or omit the flag) so a mismatch silently drops VCS metadata instead of erroring.
  3. Use `-buildvcs=false` to disable VCS stamping entirely if you do not need it.
  4. Consolidate the main package and the working directory into one VCS repo.

Example fix

# before
cd ~/repos/workspace && go build -buildvcs=true ~/repos/app/cmd/myapp
# after
cd ~/repos/app && go build ./cmd/myapp   # or use -buildvcs=auto
Defensive patterns

Strategy: validation

Validate before calling

// Before building with -buildvcs=true, confirm the main package dir and
// cwd resolve to the same VCS root.
package vcscheck

import (
	"errors"
	"os/exec"
	"path/filepath"
	"strings"
)

func sameRepo(dir string) (string, error) {
	cmd := exec.Command("git", "rev-parse", "--show-toplevel")
	cmd.Dir = dir
	out, err := cmd.Output()
	if err != nil {
		return "", err
	}
	return strings.TrimSpace(string(out)), nil
}

func BuildVcsConsistent(mainPkgDir, cwd string) error {
	r1, err := sameRepo(mainPkgDir)
	if err != nil {
		return err
	}
	r2, err := sameRepo(cwd)
	if err != nil {
		return err
	}
	if r1 != r2 {
		return errors.New("main package repo " + r1 + " != cwd repo " + r2 + "; use -buildvcs=auto")
	}
	return nil
}

Try / catch

// When shelling out to `go build -buildvcs=true`, fall back to
// -buildvcs=auto on this specific error rather than failing the pipeline:
//
//   out, err := exec.Command("go", "build", "-buildvcs=true", "./...").CombinedOutput()
//   if err != nil && bytes.Contains(out, []byte("is in repository")) {
//       log.Println("VCS mismatch; retrying with -buildvcs=auto")
//       out, err = exec.Command("go", "build", "-buildvcs=auto", "./...").CombinedOutput()
//   }

Prevention

When it happens

Trigger: Building a main package located in one git repo while invoking `go build` from a different repo's working tree, with -buildvcs=true (or any value other than 'auto'). E.g. `cd /repoA && go build -buildvcs=true /repoB/cmd/app`.

Common situations: Monorepo/submodule splits; building a tool from a shared modules workspace rooted elsewhere; CI that checks out the binary's source and the workspace into separate clones; switching to worktrees.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/d1cacdcc9d854b72. Report an issue: GitHub.