{"record":{"id":"2879928ce1202f2e","repo":"golang/go","slug":"invalid-package-name-q","errorCode":null,"errorMessage":"invalid package name: %q","messagePattern":"invalid package name: %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/compile/internal/types2/resolver.go","lineNumber":164,"sourceCode":"\t\t// ordinary import\n\t\tvar err error\n\t\tif importer := check.conf.Importer; importer == nil {\n\t\t\terr = fmt.Errorf(\"Config.Importer not installed\")\n\t\t} else if importerFrom, ok := importer.(ImporterFrom); ok {\n\t\t\timp, err = importerFrom.ImportFrom(path, dir, 0)\n\t\t\tif imp == nil && err == nil {\n\t\t\t\terr = fmt.Errorf(\"Config.Importer.ImportFrom(%s, %s, 0) returned nil but no error\", path, dir)\n\t\t\t}\n\t\t} else {\n\t\t\timp, err = importer.Import(path)\n\t\t\tif imp == nil && err == nil {\n\t\t\t\terr = fmt.Errorf(\"Config.Importer.Import(%s) returned nil but no error\", path)\n\t\t\t}\n\t\t}\n\t\t// make sure we have a valid package name\n\t\t// (errors here can only happen through manipulation of packages after creation)\n\t\tif err == nil && imp != nil && (imp.name == \"_\" || imp.name == \"\") {\n\t\t\terr = fmt.Errorf(\"invalid package name: %q\", imp.name)\n\t\t\timp = nil // create fake package below\n\t\t}\n\t\tif err != nil {\n\t\t\tcheck.errorf(pos, BrokenImport, \"could not import %s (%s)\", path, err)\n\t\t\tif imp == nil {\n\t\t\t\t// create a new fake package\n\t\t\t\t// come up with a sensible package name (heuristic)\n\t\t\t\tname := strings.TrimSuffix(path, \"/\")\n\t\t\t\tif i := strings.LastIndex(name, \"/\"); i >= 0 {\n\t\t\t\t\tname = name[i+1:]\n\t\t\t\t}\n\t\t\t\timp = NewPackage(path, name)\n\t\t\t}\n\t\t\t// continue to use the package as best as we can\n\t\t\timp.fake = true // avoid follow-up lookup failures\n\t\t}\n\t}\n","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/compile/internal/types2/resolver.go#L146-L182","documentation":"Thrown by types2's resolver after a successful import when the returned package has an invalid name — either \"_\" (blank import-only) or \"\" (empty). The comment notes this can normally only happen through post-creation manipulation of package objects, since well-formed importers always set a real name. The package is set to nil so a synthetic fake package gets created and the BrokenImport is reported as 'could not import %s'.","triggerScenarios":"Produced when imp is non-nil, err is nil, and imp.name is \"_\" or \"\". Reachable via a buggy importer that constructs packages via NewPackage with a blank/empty name, or via code that mutates a package's name field after creation.","commonSituations":"Custom importer that calls types.NewPackage(path, \"\") or types.NewPackage(path, \"_\"). A test harness or code-generation tool that fabricates package objects with placeholder names. Tampered/corrupted export data feeding an importer.","solutions":["Ensure any custom importer sets a valid, non-blank package name (call NewPackage with a real name derived from the path)","Audit code that constructs *types.Package objects and verify name is never \"_\" or empty","If importing real packages, use importer.ForDefault / gcimporter rather than hand-building packages"],"exampleFix":"// before\npkg := types.NewPackage(path, \"_\")\n// after\nname := path[strings.LastIndex(path, \"/\")+1:]\npkg := types.NewPackage(path, name)","handlingStrategy":"validation","validationCode":"// Before exposing a *types.Package from an importer, validate its name:\nfunc validPkg(p *types.Package) error {\n    if p == nil { return fmt.Errorf(\"nil package\") }\n    if p.Name() == \"\" || p.Name() == \"_\" {\n        return fmt.Errorf(\"invalid package name %q for %s\", p.Name(), p.Path())\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass a concrete non-blank name to types.NewPackage","Derive the name from the path tail rather than hard-coding \"_\"","Do not mutate pkg names after creation"],"tags":["go-toolchain","type-checker","importer","package-name"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}