hashicorp/terraform · error

OS portion must not contain whitespace

Error message

OS portion must not contain whitespace

What it means

Returned by ParsePlatform (internal/getproviders/types.go:123) when the OS segment (the part before the underscore) contains any of space/tab/newline/CR, detected via strings.ContainsAny(os, " \t\n\r"). It guards against platform identifiers that have the right underscore count but a polluted OS component that would later break filesystem paths or registry lookups.

Source

Thrown at internal/getproviders/types.go:123

	switch {
	case p.OS != other.OS:
		return p.OS < other.OS
	default:
		return p.Arch < other.Arch
	}
}

// ParsePlatform parses a string representation of a platform, like
// "linux_amd64", or returns an error if the string is not valid.
func ParsePlatform(str string) (Platform, error) {
	parts := strings.Split(str, "_")
	if len(parts) != 2 {
		return Platform{}, fmt.Errorf("must be two words separated by an underscore")
	}

	os, arch := parts[0], parts[1]
	if strings.ContainsAny(os, " \t\n\r") {
		return Platform{}, fmt.Errorf("OS portion must not contain whitespace")
	}
	if strings.ContainsAny(arch, " \t\n\r") {
		return Platform{}, fmt.Errorf("architecture portion must not contain whitespace")
	}

	return Platform{
		OS:   os,
		Arch: arch,
	}, nil
}

// CurrentPlatform is the platform where the current program is running.
//
// If attempting to install providers for use on the same system where the
// installation process is running, this is the right platform to use.
var CurrentPlatform = Platform{
	OS:   runtime.GOOS,
	Arch: runtime.GOARCH,

View on GitHub (pinned to c9def3e214)

Solutions

  1. Trim surrounding/internal whitespace from the input before calling ParsePlatform, or reject it.
  2. Source the OS portion from runtime.GOOS / a controlled constant rather than free-form text.
  3. Validate with regexp ^\S+$ against the OS segment before parsing.
  4. Sanitize config readers (CSV parsers, file reads) for stray tabs/newlines.

Example fix

// before
p, err := ParsePlatform("lin ux_amd64")  // -> OS portion must not contain whitespace

// after
str := strings.TrimSpace(rawStr)
str = strings.Map(func(r rune) rune {
    if r == ' ' || r == '\t' || r == '\n' || r == '\r' { return -1 }
    return r
}, str)
p, err := ParsePlatform(str)
Defensive patterns

Strategy: validation

Validate before calling

func cleanPlatformString(s string) string {
    return strings.Map(func(r rune) rune {
        if r == ' ' || r == '\t' || r == '\n' || r == '\r' { return -1 }
        return r
    }, s)
}
// then ParsePlatform(cleanPlatformString(raw))

Type guard

func osSegmentClean(s string) bool {
    parts := strings.SplitN(s, "_", 2)
    if len(parts) != 2 { return false }
    return !strings.ContainsAny(parts[0], " \t\n\r")
}

Try / catch

p, err := getproviders.ParsePlatform(raw)
if err != nil && strings.Contains(err.Error(), "OS portion") {
    raw = cleanPlatformString(raw)
    p, err = getproviders.ParsePlatform(raw)
}

Prevention

When it happens

Trigger: Calling ParsePlatform on a string like "lin ux_amd64", "linux\tamd64", or a value read from a config/lock file that got line-break contamination (e.g. a value spanning two lines).

Common situations: Copy-paste of platform identifiers with stray spaces, values ingested from CSV/YAML that retained trailing whitespace, Windows CR/LF line endings leaking into a Linux-built binary's runtime.GOOS-derived string, or shell word-splitting bugs that inject spaces.

Related errors


AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07). Data as JSON: /api/errors/65f69fcaa65bc313. Report an issue: GitHub.