golang/go · error

import path contains space character

Error message

import path contains space character: %q

What it means

Returned by checkImportPath when allowSpace is false and a rune in the path is unicode whitespace (unicode.IsSpace). Most internal callers disable spaces; spaces in import paths break the build because they are ambiguous in go.mod, source files, and shell quoting.

Solutions

  1. Remove all whitespace characters from the import path.
  2. Rename directories/files that contain spaces before referencing them as import paths.
  3. If you genuinely need a path-with-spaces module, restructure to avoid it; Go import paths should be space-free.

Example fix

// before
 import "example.com/my project"
// after
 import "example.com/myproject"
Defensive patterns

Strategy: validation

Validate before calling

for _, r := range path {
    if unicode.IsSpace(r) {
        return fmt.Errorf("whitespace in import path: %q", path)
    }
}

Prevention

When it happens

Trigger: An import path contains a space, tab, or other unicode space (U+00A0, em-space, etc.) and the caller passed allowSpace=false (the default for source-level checks).

Common situations: Pasting a path from a formatted document that introduced non-breaking spaces; a directory name with spaces being used as an import path; tabs inside an import literal.

Related errors


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

Appendix: source

Thrown at src/cmd/compile/internal/noder/import.go:326

		return errors.New("import path contains NUL")
	}

	for ri := range base.ReservedImports {
		if path == ri {
			return fmt.Errorf("import path %q is reserved and cannot be used", path)
		}
	}

	for _, r := range path {
		switch {
		case r == utf8.RuneError:
			return fmt.Errorf("import path contains invalid UTF-8 sequence: %q", path)
		case r < 0x20 || r == 0x7f:
			return fmt.Errorf("import path contains control character: %q", path)
		case r == '\\':
			return fmt.Errorf("import path contains backslash; use slash: %q", path)
		case !allowSpace && unicode.IsSpace(r):
			return fmt.Errorf("import path contains space character: %q", path)
		case strings.ContainsRune("!\"#$%&'()*,:;<=>?[]^`{|}", r):
			return fmt.Errorf("import path contains invalid character '%c': %q", r, path)
		}
	}

	return nil
}

View on GitHub (pinned to b6b368adc5)