larksuite/cli · error

array default for %s requires exactly %d items, got %d

Error message

array default for %s requires exactly %d items, got %d

What it means

valueAssignableTo checks that a default value can be assigned to the target Go type, and for fixed-size reflect.Array targets it requires the JSON representation of the default to have exactly the same length. This error reports the required length versus the actual length so declaration mistakes are caught at compile/validation time.

Source

Thrown at shortcuts/common/typed_compile_args.go:632

	case typedObjectShape:
		return base.Kind() == reflect.Struct || base.Kind() == reflect.Map || base.Kind() == reflect.Interface
	default:
		return false
	}
}

func valueAssignableTo(value any, target reflect.Type) error {
	base := indirectType(target)
	if base.Kind() == reflect.Array && value != nil {
		source := reflect.ValueOf(value)
		for source.Kind() == reflect.Pointer || source.Kind() == reflect.Interface {
			if source.IsNil() {
				break
			}
			source = source.Elem()
		}
		if (source.Kind() == reflect.Array || source.Kind() == reflect.Slice) && source.Len() != base.Len() {
			return fmt.Errorf("array default for %s requires exactly %d items, got %d", target, base.Len(), source.Len())
		}
	}
	encoded, err := json.Marshal(value)
	if err != nil {
		return err
	}
	decoded := reflect.New(target)
	if err := json.Unmarshal(encoded, decoded.Interface()); err != nil {
		return fmt.Errorf("value is incompatible with %s: %w", target, err)
	}
	return nil
}

View on GitHub (pinned to 7fd6ef3c07)

Solutions

  1. Make the default JSON array contain exactly N items matching the declared array length
  2. If the count is variable, change the field to a slice []T instead of a fixed array [N]T
  3. Re-run the input validation test after resizing either side

Example fix

// before
type in struct{ Names [3]string `cli:"default=[a,b]"` }
// after
type in struct{ Names [3]string `cli:"default=[a,b,c]"` }
Defensive patterns

Strategy: validation

Validate before calling

func defaultFitsArray(def any, n int) error {
	items, ok := def.([]any)
	if !ok { return fmt.Errorf("default is not a list") }
	if len(items) != n { return fmt.Errorf("need exactly %d items, got %d", n, len(items)) }
	return nil
}

Prevention

When it happens

Trigger: Declaring an [N]T (fixed array) field with a default value whose marshaled JSON array has fewer or more than N elements, e.g. default of 2 items for a [3]string field.

Common situations: Changing the array size in the struct but not the default literal; copy-pasting a default between fields with different array lengths.

Related errors


AI-assisted analysis of larksuite/cli@7fd6ef3c07 (2026-09-04). Data as JSON: /api/errors/ddd1127837bef1fe. Report an issue: GitHub.