golang/go · error
invalid input file name %q
Error message
invalid input file name %q
What it means
Every input file in a Go package must have a name that is safe to pass as a subprocess command-line argument. The SafeArg check requires the first byte to be alphanumeric (0-9, A-Z, a-z), '.', '_', '/', or a non-ASCII byte (>= 0x80 / utf8.RuneSelf). Files starting with '-' could be misinterpreted as flags by invoked compilers. Additionally, files starting with '_cgo_' are rejected because that prefix is reserved for toolchain-generated cgo outputs.
Source
Thrown at src/cmd/go/internal/load/pkg.go:2043
// where two different input files have equal names under a case-insensitive
// comparison.
inputs := p.AllFiles()
f1, f2 := str.FoldDup(inputs)
if f1 != "" {
setError(fmt.Errorf("case-insensitive file name collision: %q and %q", f1, f2))
return
}
// If first letter of input file is ASCII, it must be alphanumeric.
// This avoids files turning into flags when invoking commands,
// and other problems we haven't thought of yet.
// Also, _cgo_ files must be generated by us, not supplied.
// They are allowed to have //go:cgo_ldflag directives.
// The directory scan ignores files beginning with _,
// so we shouldn't see any _cgo_ files anyway, but just be safe.
for _, file := range inputs {
if !SafeArg(file) || strings.HasPrefix(file, "_cgo_") {
setError(fmt.Errorf("invalid input file name %q", file))
return
}
}
if name := pathpkg.Base(p.ImportPath); !SafeArg(name) {
setError(fmt.Errorf("invalid input directory name %q", name))
return
}
if strings.ContainsAny(p.Dir, "\r\n") {
setError(fmt.Errorf("invalid package directory %q", p.Dir))
return
}
// Build list of imported packages and full dependency list.
imports := make([]*Package, 0, len(p.Imports))
for i, path := range importPaths {
if path == "C" {
continue
}View on GitHub (pinned to b6b368adc5)
Solutions
- Rename the file so its first character is alphanumeric, a dot, underscore, or slash.
- Remove any '_cgo_' prefixed files that were not generated by the Go toolchain's cgo tool.
- If the file is not a source file needed for the build, move it out of the package directory.
- Check for files created by accident: ls -la | grep '^-'
Example fix
# before — filename starts with dash mv -- -options.go options.go # after — valid first character # (file is now named 'options.go')
Defensive patterns
Strategy: validation
Validate before calling
// Validate that all package input files have safe names.
func safeArg(name string) bool {
if name == "" {
return false
}
c := name[0]
return '0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' ||
c == '.' || c == '_' || c == '/' || c >= utf8.RuneSelf
}
func validatePackageFiles(files []string) error {
for _, f := range files {
if !safeArg(f) {
return fmt.Errorf("invalid input file name %q (first char not allowed)", f)
}
if strings.HasPrefix(f, "_cgo_") {
return fmt.Errorf("invalid input file name %q (_cgo_ prefix reserved)", f)
}
}
return nil
} Type guard
// Check whether a filename is safe for the Go build system.
func isSafeFileName(name string) bool {
if name == "" || strings.HasPrefix(name, "_cgo_") {
return false
}
c := name[0]
return '0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' ||
c == '.' || c == '_' || c == '/' || c >= 0x80
} Prevention
- Ensure filenames start with alphanumeric characters, dots, or underscores.
- Never manually create _cgo_ prefixed files — let the cgo toolchain generate them.
- Add a pre-commit hook that rejects filenames starting with special characters.
- Audit scripts and code generators that produce files in package directories.
When it happens
Trigger: A file in the package directory starts with '-', '=', or another special ASCII character (e.g., a file named '-helper.go'). Or a file starts with '_cgo_' (e.g., '_cgo_main.c') that was not generated by the Go toolchain's cgo tool. The SafeArg function at pkg.go:2667 defines the allowed first-character set.
Common situations: Accidentally creating files with names starting with '-' or other special characters via scripts or typos. Manually adding files with '_cgo_' prefix instead of letting cgo generate them. Files created by external tools with unusual naming conventions. Renaming files and introducing problematic first characters.
Related errors
- invalid input directory name %q
- invalid package directory %q
- crypto/dsa: invalid public key
- crypto/ecdh: private key and public key curves do not match
- crypto/ecdh: invalid private key
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/bf7a75c776ac8cfd.
Report an issue: GitHub.