{"record":{"id":"018697f443d23460","repo":"golang/go","slug":"main-module-is-in-repository-q-but-current-direct","errorCode":null,"errorMessage":"main module is in repository %q but current directory is in repository %q","messagePattern":"main module is in repository %q but current directory is in repository %q","errorType":"exception","errorClass":"PackageError","httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/load/pkg.go","lineNumber":2602,"sourceCode":"\t\tif err != nil {\n\t\t\tsetVCSError(err)\n\t\t\treturn\n\t\t}\n\t\tif pkgRepoDir != repoDir {\n\t\t\tif cfg.BuildBuildvcs != \"auto\" {\n\t\t\t\tsetVCSError(fmt.Errorf(\"main package is in repository %q but current directory is in repository %q\", pkgRepoDir, repoDir))\n\t\t\t\treturn\n\t\t\t}\n\t\t\tgoto omitVCS\n\t\t}\n\t\tmodRepoDir, _, err := vcs.FromDir(p.Module.Dir, \"\")\n\t\tif err != nil {\n\t\t\tsetVCSError(err)\n\t\t\treturn\n\t\t}\n\t\tif modRepoDir != repoDir {\n\t\t\tif cfg.BuildBuildvcs != \"auto\" {\n\t\t\t\tsetVCSError(fmt.Errorf(\"main module is in repository %q but current directory is in repository %q\", modRepoDir, repoDir))\n\t\t\t\treturn\n\t\t\t}\n\t\t\tgoto omitVCS\n\t\t}\n\n\t\tst, err := vcsStatusCache.Do(repoDir, func() (vcs.Status, error) {\n\t\t\treturn vcsCmd.Status(vcsCmd, repoDir)\n\t\t})\n\t\tif err != nil {\n\t\t\tsetVCSError(err)\n\t\t\treturn\n\t\t}\n\n\t\tappendSetting(\"vcs\", vcsCmd.Cmd)\n\t\tif st.Revision != \"\" {\n\t\t\tappendSetting(\"vcs.revision\", st.Revision)\n\t\t}\n\t\tif !st.CommitTime.IsZero() {","sourceCodeStart":2584,"sourceCodeEnd":2620,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/load/pkg.go#L2584-L2620","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)."],"exampleFix":"# before: go.work references ~/other-repo/mymod, run from ~/this-repo with -buildvcs=true\n# after: cd ~/other-repo && go build ./mymod  OR  go build -buildvcs=auto","handlingStrategy":"validation","validationCode":"// Same VCS-root check as 891, but compare the MODULE root (dir of go.mod)\n// against cwd. Walk up from the module dir to find its repo root.\npackage vcscheck\n\nfunc BuildVcsModuleConsistent(moduleDir, cwd string) error {\n\tr1, err := sameRepo(moduleDir)\n\tif err != nil {\n\t\treturn err\n\t}\n\tr2, err := sameRepo(cwd)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif r1 != r2 {\n\t\treturn errors.New(\"module repo \" + r1 + \" != cwd repo \" + r2 + \"; use -buildvcs=auto\")\n\t}\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":"// Identical fallback to 891: on \"is in repository\" in stderr, retry\n// with -buildvcs=auto (or -buildvcs=false).","preventionTips":["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."],"tags":["vcs","modules","build","buildvcs","toolchain"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}