{"record":{"id":"d1cacdcc9d854b72","repo":"golang/go","slug":"main-package-is-in-repository-q-but-current-direc","errorCode":null,"errorMessage":"main package is in repository %q but current directory is in repository %q","messagePattern":"main package 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":2590,"sourceCode":"\t\t\t\t// \"-buildvcs=auto\" means that we should silently drop the VCS metadata.\n\t\t\t\tgoto omitVCS\n\t\t\t}\n\t\t}\n\t}\n\tif repoDir != \"\" && vcsCmd.Status != nil {\n\t\t// Check that the current directory, package, and module are in the same\n\t\t// repository. vcs.FromDir disallows nested VCS and multiple VCS in the\n\t\t// same repository, unless the GODEBUG allowmultiplevcs is set. The\n\t\t// current directory may be outside p.Module.Dir when a workspace is\n\t\t// used.\n\t\tpkgRepoDir, _, err := vcs.FromDir(p.Dir, \"\")\n\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) {","sourceCodeStart":2572,"sourceCodeEnd":2608,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/load/pkg.go#L2572-L2608","documentation":"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).","triggerScenarios":"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`.","commonSituations":"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.","solutions":["Run `go build` from within the same repository that contains the main package.","Use `-buildvcs=auto` (or omit the flag) so a mismatch silently drops VCS metadata instead of erroring.","Use `-buildvcs=false` to disable VCS stamping entirely if you do not need it.","Consolidate the main package and the working directory into one VCS repo."],"exampleFix":"# before\ncd ~/repos/workspace && go build -buildvcs=true ~/repos/app/cmd/myapp\n# after\ncd ~/repos/app && go build ./cmd/myapp   # or use -buildvcs=auto","handlingStrategy":"validation","validationCode":"// Before building with -buildvcs=true, confirm the main package dir and\n// cwd resolve to the same VCS root.\npackage vcscheck\n\nimport (\n\t\"errors\"\n\t\"os/exec\"\n\t\"path/filepath\"\n\t\"strings\"\n)\n\nfunc sameRepo(dir string) (string, error) {\n\tcmd := exec.Command(\"git\", \"rev-parse\", \"--show-toplevel\")\n\tcmd.Dir = dir\n\tout, err := cmd.Output()\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\treturn strings.TrimSpace(string(out)), nil\n}\n\nfunc BuildVcsConsistent(mainPkgDir, cwd string) error {\n\tr1, err := sameRepo(mainPkgDir)\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(\"main package repo \" + r1 + \" != cwd repo \" + r2 + \"; use -buildvcs=auto\")\n\t}\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":"// When shelling out to `go build -buildvcs=true`, fall back to\n// -buildvcs=auto on this specific error rather than failing the pipeline:\n//\n//   out, err := exec.Command(\"go\", \"build\", \"-buildvcs=true\", \"./...\").CombinedOutput()\n//   if err != nil && bytes.Contains(out, []byte(\"is in repository\")) {\n//       log.Println(\"VCS mismatch; retrying with -buildvcs=auto\")\n//       out, err = exec.Command(\"go\", \"build\", \"-buildvcs=auto\", \"./...\").CombinedOutput()\n//   }","preventionTips":["Default to `-buildvcs=auto` in CI to avoid hard failures on repo mismatches.","Build from the repository root that contains the main package."],"tags":["vcs","build","buildvcs","toolchain"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}