golang/go · error · PackageError
main module is in repository %q but current directory is in
Error message
main module is in repository %q but current directory is in repository %q
What it means
Companion to 891, checked immediately after at line 2602 during VCS stamping. After the main package's repo root matches cwd's, the loader also checks the MAIN MODULE's directory (p.Module.Dir) against cwd's repo root. If the module root is in a different repo than cwd (and -buildvcs != 'auto'), stamping is ambiguous and the build errors. This catches the case where the main package itself is fine but the module it belongs to spans a different repository.
Source
Thrown at src/cmd/go/internal/load/pkg.go:2602
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) {
return vcsCmd.Status(vcsCmd, repoDir)
})
if err != nil {
setVCSError(err)
return
}
appendSetting("vcs", vcsCmd.Cmd)
if st.Revision != "" {
appendSetting("vcs.revision", st.Revision)
}
if !st.CommitTime.IsZero() {View on GitHub (pinned to b6b368adc5)
Solutions
- Run `go build` from inside the same repository that holds the module's go.mod.
- Pass `-buildvcs=auto` to silently skip VCS info on mismatch, or `-buildvcs=false` to disable it.
- Ensure the module directory and your working directory are part of the same VCS checkout (remove nested .git directories or use one clone).
Example fix
# before: go.work references ~/other-repo/mymod, run from ~/this-repo with -buildvcs=true # after: cd ~/other-repo && go build ./mymod OR go build -buildvcs=auto
Defensive patterns
Strategy: validation
Validate before calling
// Same VCS-root check as 891, but compare the MODULE root (dir of go.mod)
// against cwd. Walk up from the module dir to find its repo root.
package vcscheck
func BuildVcsModuleConsistent(moduleDir, cwd string) error {
r1, err := sameRepo(moduleDir)
if err != nil {
return err
}
r2, err := sameRepo(cwd)
if err != nil {
return err
}
if r1 != r2 {
return errors.New("module repo " + r1 + " != cwd repo " + r2 + "; use -buildvcs=auto")
}
return nil
} Try / catch
// Identical fallback to 891: on "is in repository" in stderr, retry // with -buildvcs=auto (or -buildvcs=false).
Prevention
- Keep the main module and your working directory inside one VCS checkout.
- Audit `replace` directives that point at local paths in other repos — they trigger this on -buildvcs=true.
When it happens
Trigger: Building with -buildvcs explicitly enabled when the go.mod's directory is in a different VCS repository than the current working directory — e.g. a `replace` directive points the main module at a local path in another repo, or a workspace pulls a module from a separately-cloned repo.
Common situations: Local replace directives into another checkout; go.work files spanning multiple clones; building inside a nested git worktree while the module root sits in the parent clone.
Related errors
- main package is in repository %q but current directory is in
- use of vendored package not allowed
- ReadZip: encoded file exceeds allowed size
- value is neither 'auto' nor a valid bool
- C compiler %q not found: %v
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/018697f443d23460.
Report an issue: GitHub.