golang/go · error
code in directory %s expects import %q
Error message
code in directory %s expects import %q
What it means
In GOPATH mode (not module-aware mode), a package can declare its canonical import path using an '// import "path"' comment in its source. This error fires when the declared import comment does not match the import path that was actually used, indicating the package is being imported by a non-canonical path. This check only applies when !cfg.ModulesEnabled, data.err is nil, the path doesn't contain '/vendor/', and doesn't start with 'vendor/'.
Source
Thrown at src/cmd/go/internal/load/pkg.go:1003
}
data.p, data.err = cfg.BuildContext.Import(r.path, parentDir, buildMode)
}
data.p.ImportPath = r.path
// Set data.p.BinDir in cases where go/build.Context.Import
// may give us a path we don't want.
if !data.p.Goroot {
if cfg.GOBIN != "" {
data.p.BinDir = cfg.GOBIN
} else if cfg.ModulesEnabled {
data.p.BinDir = modload.BinDir(ld)
}
}
if !cfg.ModulesEnabled && data.err == nil &&
data.p.ImportComment != "" && data.p.ImportComment != path &&
!strings.Contains(path, "/vendor/") && !strings.HasPrefix(path, "vendor/") {
data.err = fmt.Errorf("code in directory %s expects import %q", data.p.Dir, data.p.ImportComment)
}
return data.p, data.err
})
return p, loaded, err
}
// importSpec describes an import declaration in source code. It is used as a
// cache key for resolvedImportCache.
type importSpec struct {
path string
parentPath, parentDir, parentRoot string
parentIsStd bool
mode int
}
// resolvedImport holds a canonical identifier for a package. It may also contain
// a path to the package's directory and an error if one occurred. resolvedImportView on GitHub (pinned to b6b368adc5)
Solutions
- Update the import statement in the importing code to match the canonical path declared in the package's '// import' comment.
- If the package has moved, update the '// import' comment to the new canonical path.
- Switch to module mode (Go modules, GO111MODULE=on) where canonical import comment enforcement is relaxed.
Example fix
// before — mismatched import import "mylib" // package declares: // import "github.com/user/mylib" // after — canonical import path import "github.com/user/mylib"
Defensive patterns
Strategy: validation
Validate before calling
// In GOPATH mode, verify import path matches canonical import comment.
func checkImportComment(pkgDir, importPath string) error {
entries, _ := os.ReadDir(pkgDir)
for _, e := range entries {
if !strings.HasSuffix(e.Name(), ".go") {
continue
}
data, _ := os.ReadFile(filepath.Join(pkgDir, e.Name()))
// Look for // import "canonical/path" comment
idx := strings.Index(string(data), "// import \"")
if idx < 0 {
continue
}
start := idx + len("// import \"")
end := strings.Index(string(data)[start:], "\"")
if end < 0 {
continue
}
canonical := string(data)[start : start+end]
if canonical != importPath {
return fmt.Errorf("package expects import %q, got %q", canonical, importPath)
}
}
return nil
} Prevention
- Match import statements to canonical import comments declared in source.
- Switch to module mode (GO111MODULE=on) to avoid GOPATH import comment enforcement.
- Update import comments when moving packages between repositories.
When it happens
Trigger: A package at $GOPATH/src/github.com/user/lib contains '// import "github.com/user/lib"' in its source, but another package imports it as "lib" or with a different non-canonical path. The ImportComment field from build.Context.Import is compared against the actual import path.
Common situations: Working in GOPATH mode with packages that have import comments. Moving a package to a new path without updating importers. Forking a repository and importing it by the fork's path when the canonical comment still references the original upstream. Shortened imports that don't match the declared canonical path.
Related errors
- disallowed import path %q
- unknown import path %q: internal error: module loader did no
- invalid input directory name %q
- can only use path@version syntax with 'go get' and 'go insta
- binary-only packages are no longer supported
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/37eae823a387e1db.
Report an issue: GitHub.