muesli/duf · error

'%s' is not valid, must have integer with optional SI prefix

Error message

'%s' is not valid, must have integer with optional SI prefix

What it means

stringToSize parses an SI size string like "10G" into a byte count. The input must match ^\d+([KMGTPE]?)$; if it does not, the function returns "'%s' is not valid, must have integer with optional SI prefix". This validates the shape of threshold size values before applying the prefix multiplier.

Source

Thrown at table.go:508

	case size >= 1<<30:
		str = fmt.Sprintf("%.1fG", b/(1<<30))
	case size >= 1<<20:
		str = fmt.Sprintf("%.1fM", b/(1<<20))
	case size >= 1<<10:
		str = fmt.Sprintf("%.1fK", b/(1<<10))
	default:
		str = fmt.Sprintf("%dB", size)
	}

	return
}

// stringToSize transforms an SI size into a number.
func stringToSize(s string) (size uint64, err error) {
	regex := regexp.MustCompile(`^(\d+)([KMGTPE]?)$`)
	matches := regex.FindStringSubmatch(s)
	if len(matches) == 0 {
		return 0, fmt.Errorf("'%s' is not valid, must have integer with optional SI prefix", s)
	}

	num, err := strconv.ParseUint(matches[1], 10, 64)
	if err != nil {
		return 0, err
	}
	if matches[2] != "" {
		prefix := matches[2]
		switch prefix {
		case "K":
			size = num << 10
		case "M":
			size = num << 20
		case "G":
			size = num << 30
		case "T":
			size = num << 40
		case "P":

View on GitHub (pinned to 4636deb4a7)

Solutions

  1. Use the format <integer><optional uppercase SI prefix>, e.g. "10", "10G", "2T".
  2. Convert decimals to whole units (1.5G -> 1536M).
  3. Use uppercase K/M/G/T/P/E; lowercase k is rejected.

Example fix

// before
stringToSize("1.5G")
// after
stringToSize("1536M")
Defensive patterns

Strategy: validation

Validate before calling

var sizeRe = regexp.MustCompile(`^\d+([KMGTPE]?)$`)
func validSize(s string) bool { return sizeRe.MatchString(s) }

Try / catch

size, err := stringToSize(input)
if err != nil {
	return fmt.Errorf("bad size %q: %w", input, err)
}

Prevention

When it happens

Trigger: Calling stringToSize (directly or via --avail-threshold parsing) with strings like "10Gi", "1.5G", "10k" (lowercase), "G10", or an empty string — anything failing the digit-plus-optional-uppercase-prefix regex.

Common situations: Users write decimal sizes (1.5G), IEC suffixes (Gi, MiB), lowercase prefixes (10k), or reversed order (G10) in threshold flags.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of muesli/duf@4636deb4a7 (2026-09-06). Data as JSON: /api/errors/47801e17481e45c6. Report an issue: GitHub.