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

  1. Rename the file so its first character is alphanumeric, a dot, underscore, or slash.
  2. Remove any '_cgo_' prefixed files that were not generated by the Go toolchain's cgo tool.
  3. If the file is not a source file needed for the build, move it out of the package directory.
  4. 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

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


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/bf7a75c776ac8cfd. Report an issue: GitHub.