JanDeDobbeleer/oh-my-posh · error

input must be a slice or array

Error message

input must be a slice or array

What it means

The template function random(list) picks a random element from a list passed in a template. It uses reflection to inspect the argument and throws 'input must be a slice or array' when the value's Kind is neither slice nor array — e.g. a string, map, or scalar was passed.

Source

Thrown at src/template/random.go:14

package template

import (
	"errors"
	"fmt"
	"math/rand/v2"
	"reflect"
)

func random(list any) (string, error) {
	v := reflect.ValueOf(list)

	if v.Kind() != reflect.Slice && v.Kind() != reflect.Array {
		return "", errors.New("input must be a slice or array")
	}

	if v.Len() == 0 {
		return "", errors.New("input slice or array is empty")
	}

	return fmt.Sprintf("%v", v.Index(rand.IntN(v.Len()))), nil
}

View on GitHub (pinned to 0976794618)

Solutions

  1. Pass an actual slice/array to random in the template
  2. Fix the template property name so it resolves to the intended list
  3. Convert string input to a list before calling random
  4. Guard the call with a conditional on the value being a non-empty list

Example fix

// before (template)
{{ random "a,b,c" }}
// after
{{ random (list "a" "b" "c") }}
Defensive patterns

Strategy: type-guard

Validate before calling

// in template logic, ensure the argument is a list
{{ if and (not (eq (typeOf .Value) "string")) (gt (len .Value) 0) }}{{ random .Value }}{{ end }}

Type guard

func isList(v any) bool {
    k := reflect.ValueOf(v).Kind()
    return k == reflect.Slice || k == reflect.Array
}

Prevention

When it happens

Trigger: A template calls `random` with a non-list argument, such as a bare string, an int, a map, or a nil value produced by an unset template property.

Common situations: Typo in the template property name yielding nil; passing a comma-separated string instead of an actual slice; passing a map of options instead of a slice; calling random with no argument.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of JanDeDobbeleer/oh-my-posh@0976794618 (2026-08-31). Data as JSON: /api/errors/8e419681e30c5d0c. Report an issue: GitHub.