{"record":{"id":"4db2291d7f154d7e","repo":"golang/go","slug":"invalid-input-directory-name-q","errorCode":null,"errorMessage":"invalid input directory name %q","messagePattern":"invalid input directory name %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/load/pkg.go","lineNumber":2048,"sourceCode":"\t\tsetError(fmt.Errorf(\"case-insensitive file name collision: %q and %q\", f1, f2))\n\t\treturn\n\t}\n\n\t// If first letter of input file is ASCII, it must be alphanumeric.\n\t// This avoids files turning into flags when invoking commands,\n\t// and other problems we haven't thought of yet.\n\t// Also, _cgo_ files must be generated by us, not supplied.\n\t// They are allowed to have //go:cgo_ldflag directives.\n\t// The directory scan ignores files beginning with _,\n\t// so we shouldn't see any _cgo_ files anyway, but just be safe.\n\tfor _, file := range inputs {\n\t\tif !SafeArg(file) || strings.HasPrefix(file, \"_cgo_\") {\n\t\t\tsetError(fmt.Errorf(\"invalid input file name %q\", file))\n\t\t\treturn\n\t\t}\n\t}\n\tif name := pathpkg.Base(p.ImportPath); !SafeArg(name) {\n\t\tsetError(fmt.Errorf(\"invalid input directory name %q\", name))\n\t\treturn\n\t}\n\tif strings.ContainsAny(p.Dir, \"\\r\\n\") {\n\t\tsetError(fmt.Errorf(\"invalid package directory %q\", p.Dir))\n\t\treturn\n\t}\n\n\t// Build list of imported packages and full dependency list.\n\timports := make([]*Package, 0, len(p.Imports))\n\tfor i, path := range importPaths {\n\t\tif path == \"C\" {\n\t\t\tcontinue\n\t\t}\n\t\tp1, err := loadImport(ld, ctx, opts, nil, path, p.Dir, p, stk, p.Internal.Build.ImportPos[path], ResolveImport|allowInternalSimdImport)\n\t\tif err != nil && p.Error == nil {\n\t\t\tp.Error = err\n\t\t\tp.Incomplete = true\n\t\t}","sourceCodeStart":2030,"sourceCodeEnd":2066,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/load/pkg.go#L2030-L2066","documentation":"The base name of the package's import path (the last component) must itself be a safe command-line argument. The same SafeArg check applied to input files is applied to pathpkg.Base(p.ImportPath): the first byte must be alphanumeric, '.', '_', '/', or non-ASCII. An import path whose final component starts with '-' could be misinterpreted as a flag by invoked tools.","triggerScenarios":"A module or package whose import path ends in a component starting with '-', '=', or another non-alphanumeric ASCII character. For example, an import path like 'github.com/user/-mypackage' would fail because Base returns '-mypackage' and SafeArg('-') is false.","commonSituations":"Creating a package directory that starts with '-'. Module paths whose final component is a flag-like string. Accidental typos in directory names on case-sensitive filesystems.","solutions":["Rename the package directory so its last path component starts with an alphanumeric character, dot, or underscore.","Update the module path in go.mod and all importers if the directory/module path changes.","Check the import path: go list -f '{{.ImportPath}}' ."],"exampleFix":"# before — directory starts with dash\nmv -- -mypkg mypkg\n# update go.mod\n# module github.com/user/mypkg\n# after — valid import path\n# (update all importers to use the new path)","handlingStrategy":"validation","validationCode":"// Validate that the import path's base component is a safe name.\nfunc validateImportPathBase(importPath string) error {\n    base := path.Base(importPath)\n    if base == \"\" {\n        return fmt.Errorf(\"import path %q has empty base\", importPath)\n    }\n    c := base[0]\n    if !(('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') ||\n        c == '.' || c == '_' || c == '/' || c >= utf8.RuneSelf) {\n        return fmt.Errorf(\"invalid import directory name %q (first char not allowed)\", base)\n    }\n    return nil\n}","typeGuard":"// Check whether the import path's base name is safe.\nfunc isSafeImportPathBase(importPath string) bool {\n    base := path.Base(importPath)\n    if base == \"\" {\n        return false\n    }\n    c := base[0]\n    return '0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' ||\n        c == '.' || c == '_' || c == '/' || c >= 0x80\n}","tryCatchPattern":null,"preventionTips":["Ensure the last component of package/module paths starts with an alphanumeric character.","Avoid directory names starting with '-' or other special characters.","Validate module paths in go.mod during code review."],"tags":["go","go-build","import-path","filenames","validation"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}