{"record":{"id":"c6d9e84b01eaca33","repo":"golang/go","slug":"internal-error-package-s-is-in-the-main-module","errorCode":null,"errorMessage":"internal error: package %s is in the main module (%s), but version is not allowed: %w","messagePattern":"internal error: package (.+?) is in the main module \\((.+?)\\), but version is not allowed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/modload/query.go","lineNumber":729,"sourceCode":"\t\t\t\t}\n\t\t\t}\n\t\t\treturn m\n\t\t}\n\t}\n\n\tvar mainModuleMatches []module.Version\n\tfor _, mainModule := range ld.MainModules.Versions() {\n\t\tm := match(mainModule, ld.modRoots, true)\n\t\tif len(m.Pkgs) > 0 {\n\t\t\tif query != \"upgrade\" && query != \"patch\" {\n\t\t\t\treturn nil, nil, &QueryMatchesPackagesInMainModuleError{\n\t\t\t\t\tPattern:  pattern,\n\t\t\t\t\tQuery:    query,\n\t\t\t\t\tPackages: m.Pkgs,\n\t\t\t\t}\n\t\t\t}\n\t\t\tif err := allowed(ctx, mainModule); err != nil {\n\t\t\t\treturn nil, nil, fmt.Errorf(\"internal error: package %s is in the main module (%s), but version is not allowed: %w\", pattern, mainModule.Path, err)\n\t\t\t}\n\t\t\treturn []QueryResult{{\n\t\t\t\tMod:      mainModule,\n\t\t\t\tRev:      &modfetch.RevInfo{Version: mainModule.Version},\n\t\t\t\tPackages: m.Pkgs,\n\t\t\t}}, nil, nil\n\t\t}\n\t\tif err := firstError(m); err != nil {\n\t\t\treturn nil, nil, err\n\t\t}\n\n\t\tvar matchesMainModule bool\n\t\tif matchPattern(mainModule.Path) {\n\t\t\tmainModuleMatches = append(mainModuleMatches, mainModule)\n\t\t\tmatchesMainModule = true\n\t\t}\n\n\t\tif (query == \"upgrade\" || query == \"patch\") && matchesMainModule {","sourceCodeStart":711,"sourceCodeEnd":747,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/modload/query.go#L711-L747","documentation":"Internal invariant error in the pattern-based query path: a package matching the query pattern exists in the main module, the query is 'upgrade'/'patch', but the supplied AllowedFunc rejected the main module's version. Main-module versions should always be allowed for upgrade/patch queries, so this indicates a buggy AllowedFunc in the calling toolchain code rather than a user mistake.","triggerScenarios":"A programmatic caller (or internal toolchain path) issues a pattern query over the main module with an AllowedFunc that returns an error for the main module version while query is upgrade/patch.","commonSituations":"Not seen in normal CLI use; surfaces in custom tooling built atop cmd/go/internal/modload whose allow-list incorrectly filters main-module paths, or during toolchain bugs in retraction/allow logic.","solutions":["If you maintain the caller, ensure AllowedFunc returns nil for main-module paths (ld.MainModules.Contains).","As an end user, this is a toolchain bug — report at https://go.dev/issue with the module and query.","Retry with GOFLAGS=-mod=mod after 'go clean -modcache' to rule out corrupted cached state."],"exampleFix":"// before (custom caller)\nallowed := func(ctx, m) error {\n    return errors.New(\"disallowed\") // wrongly applied to main module\n}\nQueryPattern(ctx, \"./...\", \"upgrade\", allowed)\n// error: internal error: package ./... is in the main module (...), but version is not allowed\n\n// after\nallowed := func(ctx, m) error {\n    if ld.MainModules.Contains(m.Path) { return nil }\n    return checkAllowed(m)\n}","handlingStrategy":"validation","validationCode":"// Same safe AllowedFunc pattern as error 1111 — never reject the main module:\n//   allowed := func(ctx, m) error {\n//       if ld.MainModules.Contains(m.Path) { return nil }\n//       return external(ctx, m)\n//   }","typeGuard":"func safeAllowedForQuery(ld *Loader, external AllowedFunc) AllowedFunc {\n    return func(ctx context.Context, m module.Version) error {\n        if ld.MainModules.Contains(m.Path) { return nil }\n        if external == nil { return nil }\n        return external(ctx, m)\n    }\n}","tryCatchPattern":null,"preventionTips":["Test AllowedFunc against main-module patterns (./..., upgrade, patch) before use.","Keep allow-list logic out of the hot query path; precompute allowed sets.","Log any non-nil AllowedFunc result for main-module paths as a bug."],"tags":["go-modules","internal-error","query","allowed-func","main-module"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}