JanDeDobbeleer/oh-my-posh · error

input slice or array is empty

Error message

input slice or array is empty

What it means

After confirming the input is a slice or array, random() checks v.Len() == 0 and throws 'input slice or array is empty' because there is no element to pick. rand.IntN would panic on a zero length, so this guard converts it to an error.

Source

Thrown at src/template/random.go:18

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. Ensure the list passed to random contains at least one element
  2. Add a fallback default element when the source list may be empty
  3. Conditionally render the random call only when the list is non-empty
  4. Fix the population logic (env var, property) that produced an empty list

Example fix

// before (template)
{{ random .EmptyList }}
// after
{{ if gt (len .EmptyList) 0 }}{{ random .EmptyList }}{{ else }}default{{ end }}
Defensive patterns

Strategy: validation

Validate before calling

// check length before calling random in a template
{{ if gt (len .List) 0 }}{{ random .List }}{{ else }}default{{ end }}

Prevention

When it happens

Trigger: A template calls `random` with a slice/array value that exists but has zero elements — an empty list literal, an empty environment-derived list, or a list built by filtering that removed everything.

Common situations: An empty env var split into a list; a template property that is an initialized but unpopulated array; passing `list` with no elements; config where the list is populated only under conditions that didn't hold.

Related errors


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