golang/go · error

without -mod=vendor, directory %s has no package path

Error message

without -mod=vendor, directory %s has no package path

What it means

Thrown by resolveLocalPackage (load.go:612) when the resolved absolute directory is under a main module's 'vendor/' subdirectory but the build mode (cfg.BuildMod) is not 'vendor'. The vendor directory only has importable package paths in vendor mode. Without -mod=vendor, the directory suffix like 'vendor/foo/bar' has no resolvable package path, so an error is returned before the vendor list is even read.

Source

Thrown at src/cmd/go/internal/modload/load.go:612

			if absDir == cfg.GOROOTsrc {
				return "", errPkgIsGorootSrc
			}
			return ld.MainModules.PathPrefix(mod), nil
		}
	}

	// Note: The checks for @ here are just to avoid misinterpreting
	// the module cache directories (formerly GOPATH/src/mod/foo@v1.5.2/bar).
	// It's not strictly necessary but helpful to keep the checks.
	var pkgNotFoundErr error
	pkgNotFoundLongestPrefix := ""
	for _, mainModule := range ld.MainModules.Versions() {
		modRoot := ld.MainModules.ModRoot(mainModule)
		if modRoot != "" && str.HasFilePathPrefix(absDir, modRoot) && !strings.Contains(absDir[len(modRoot):], "@") {
			suffix := filepath.ToSlash(str.TrimFilePathPrefix(absDir, modRoot))
			if pkg, found := strings.CutPrefix(suffix, "vendor/"); found {
				if cfg.BuildMod != "vendor" {
					return "", fmt.Errorf("without -mod=vendor, directory %s has no package path", absDir)
				}

				readVendorList(VendorDir(ld))
				if _, ok := vendorPkgModule[pkg]; !ok {
					return "", fmt.Errorf("directory %s is not a package listed in vendor/modules.txt", absDir)
				}
				return pkg, nil
			}

			mainModulePrefix := ld.MainModules.PathPrefix(mainModule)
			if mainModulePrefix == "" {
				pkg := suffix
				if pkg == "builtin" {
					// "builtin" is a pseudo-package with a real source file.
					// It's not included in "std", so it shouldn't resolve from "."
					// within module "std" either.
					return "", errPkgIsBuiltin
				}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Set the build to vendor mode: run 'go build -mod=vendor' or add 'GOFLAGS=-mod=vendor' to your environment.
  2. Alternatively, reference the package by its import path rather than by its vendor filesystem path.
  3. Remove the vendor/ directory if you intend to use network/module-cache mode instead.

Example fix

# before
go build ./vendor/example.com/pkg

# after
go build -mod=vendor ./...
# or import by path, not by vendor filesystem location:
go build example.com/pkg
Defensive patterns

Strategy: validation

Validate before calling

// Check build mode before pointing at vendor paths.
func ensureVendorMode() error {
    if cfg.BuildMod != "vendor" {
        return fmt.Errorf("vendor directory requires -mod=vendor")
    }
    return nil
}

Prevention

When it happens

Trigger: Running 'go build ./vendor/...' or importing a path that resolves into a vendor/ directory when the module is not in vendor mode (cfg.BuildMod != "vendor"). This typically happens when -mod=mod or -mod=readonly is active and the user points directly at a vendored package.

Common situations: A project has a vendor/ directory (from 'go mod vendor') but the build is running without -mod=vendor, or GOFLAGS does not include -mod=vendor. Pointing tooling or IDE at a vendor subdirectory directly.

Related errors


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