golang/go · error

internal error: package %s is in the main module (%s), but v

Error message

internal error: package %s is in the main module (%s), but version is not allowed: %w

What it means

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.

Source

Thrown at src/cmd/go/internal/modload/query.go:729

				}
			}
			return m
		}
	}

	var mainModuleMatches []module.Version
	for _, mainModule := range ld.MainModules.Versions() {
		m := match(mainModule, ld.modRoots, true)
		if len(m.Pkgs) > 0 {
			if query != "upgrade" && query != "patch" {
				return nil, nil, &QueryMatchesPackagesInMainModuleError{
					Pattern:  pattern,
					Query:    query,
					Packages: m.Pkgs,
				}
			}
			if err := allowed(ctx, mainModule); err != nil {
				return nil, nil, fmt.Errorf("internal error: package %s is in the main module (%s), but version is not allowed: %w", pattern, mainModule.Path, err)
			}
			return []QueryResult{{
				Mod:      mainModule,
				Rev:      &modfetch.RevInfo{Version: mainModule.Version},
				Packages: m.Pkgs,
			}}, nil, nil
		}
		if err := firstError(m); err != nil {
			return nil, nil, err
		}

		var matchesMainModule bool
		if matchPattern(mainModule.Path) {
			mainModuleMatches = append(mainModuleMatches, mainModule)
			matchesMainModule = true
		}

		if (query == "upgrade" || query == "patch") && matchesMainModule {

View on GitHub (pinned to b6b368adc5)

Solutions

  1. If you maintain the caller, ensure AllowedFunc returns nil for main-module paths (ld.MainModules.Contains).
  2. As an end user, this is a toolchain bug — report at https://go.dev/issue with the module and query.
  3. Retry with GOFLAGS=-mod=mod after 'go clean -modcache' to rule out corrupted cached state.

Example fix

// before (custom caller)
allowed := func(ctx, m) error {
    return errors.New("disallowed") // wrongly applied to main module
}
QueryPattern(ctx, "./...", "upgrade", allowed)
// error: internal error: package ./... is in the main module (...), but version is not allowed

// after
allowed := func(ctx, m) error {
    if ld.MainModules.Contains(m.Path) { return nil }
    return checkAllowed(m)
}
Defensive patterns

Strategy: validation

Validate before calling

// Same safe AllowedFunc pattern as error 1111 — never reject the main module:
//   allowed := func(ctx, m) error {
//       if ld.MainModules.Contains(m.Path) { return nil }
//       return external(ctx, m)
//   }

Type guard

func safeAllowedForQuery(ld *Loader, external AllowedFunc) AllowedFunc {
    return func(ctx context.Context, m module.Version) error {
        if ld.MainModules.Contains(m.Path) { return nil }
        if external == nil { return nil }
        return external(ctx, m)
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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