golang/go · error

invalid input directory name %q

Error message

invalid input directory name %q

What it means

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.

Source

Thrown at src/cmd/go/internal/load/pkg.go:2048

		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
		}
		p1, err := loadImport(ld, ctx, opts, nil, path, p.Dir, p, stk, p.Internal.Build.ImportPos[path], ResolveImport|allowInternalSimdImport)
		if err != nil && p.Error == nil {
			p.Error = err
			p.Incomplete = true
		}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Rename the package directory so its last path component starts with an alphanumeric character, dot, or underscore.
  2. Update the module path in go.mod and all importers if the directory/module path changes.
  3. Check the import path: go list -f '{{.ImportPath}}' .

Example fix

# before — directory starts with dash
mv -- -mypkg mypkg
# update go.mod
# module github.com/user/mypkg
# after — valid import path
# (update all importers to use the new path)
Defensive patterns

Strategy: validation

Validate before calling

// Validate that the import path's base component is a safe name.
func validateImportPathBase(importPath string) error {
    base := path.Base(importPath)
    if base == "" {
        return fmt.Errorf("import path %q has empty base", importPath)
    }
    c := base[0]
    if !(('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') ||
        c == '.' || c == '_' || c == '/' || c >= utf8.RuneSelf) {
        return fmt.Errorf("invalid import directory name %q (first char not allowed)", base)
    }
    return nil
}

Type guard

// Check whether the import path's base name is safe.
func isSafeImportPathBase(importPath string) bool {
    base := path.Base(importPath)
    if base == "" {
        return false
    }
    c := base[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 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.

Common situations: 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.

Related errors


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