{"record":{"id":"37eae823a387e1db","repo":"golang/go","slug":"code-in-directory-s-expects-import-q","errorCode":null,"errorMessage":"code in directory %s expects import %q","messagePattern":"code in directory (.+?) expects import %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/load/pkg.go","lineNumber":1003,"sourceCode":"\t\t\t}\n\t\t\tdata.p, data.err = cfg.BuildContext.Import(r.path, parentDir, buildMode)\n\t\t}\n\t\tdata.p.ImportPath = r.path\n\n\t\t// Set data.p.BinDir in cases where go/build.Context.Import\n\t\t// may give us a path we don't want.\n\t\tif !data.p.Goroot {\n\t\t\tif cfg.GOBIN != \"\" {\n\t\t\t\tdata.p.BinDir = cfg.GOBIN\n\t\t\t} else if cfg.ModulesEnabled {\n\t\t\t\tdata.p.BinDir = modload.BinDir(ld)\n\t\t\t}\n\t\t}\n\n\t\tif !cfg.ModulesEnabled && data.err == nil &&\n\t\t\tdata.p.ImportComment != \"\" && data.p.ImportComment != path &&\n\t\t\t!strings.Contains(path, \"/vendor/\") && !strings.HasPrefix(path, \"vendor/\") {\n\t\t\tdata.err = fmt.Errorf(\"code in directory %s expects import %q\", data.p.Dir, data.p.ImportComment)\n\t\t}\n\t\treturn data.p, data.err\n\t})\n\n\treturn p, loaded, err\n}\n\n// importSpec describes an import declaration in source code. It is used as a\n// cache key for resolvedImportCache.\ntype importSpec struct {\n\tpath                              string\n\tparentPath, parentDir, parentRoot string\n\tparentIsStd                       bool\n\tmode                              int\n}\n\n// resolvedImport holds a canonical identifier for a package. It may also contain\n// a path to the package's directory and an error if one occurred. resolvedImport","sourceCodeStart":985,"sourceCodeEnd":1021,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/load/pkg.go#L985-L1021","documentation":"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/'.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before — mismatched import\nimport \"mylib\"\n// package declares: // import \"github.com/user/mylib\"\n\n// after — canonical import path\nimport \"github.com/user/mylib\"","handlingStrategy":"validation","validationCode":"// In GOPATH mode, verify import path matches canonical import comment.\nfunc checkImportComment(pkgDir, importPath string) error {\n    entries, _ := os.ReadDir(pkgDir)\n    for _, e := range entries {\n        if !strings.HasSuffix(e.Name(), \".go\") {\n            continue\n        }\n        data, _ := os.ReadFile(filepath.Join(pkgDir, e.Name()))\n        // Look for // import \"canonical/path\" comment\n        idx := strings.Index(string(data), \"// import \\\"\")\n        if idx < 0 {\n            continue\n        }\n        start := idx + len(\"// import \\\"\")\n        end := strings.Index(string(data)[start:], \"\\\"\")\n        if end < 0 {\n            continue\n        }\n        canonical := string(data)[start : start+end]\n        if canonical != importPath {\n            return fmt.Errorf(\"package expects import %q, got %q\", canonical, importPath)\n        }\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["go","go-build","gopath","import-path","import-comment"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:17:08.281Z"}