go-delve/delve · error

invalid argument %s (type %s) for cap

Error message

invalid argument %s (type %s) for cap

What it means

Delve's expression evaluator throws this when the `cap()` builtin is called on an expression whose type has no capacity: only arrays, slices and channels are valid, and a pointer is accepted only if it points to an array. The error embeds the source text of the argument and its type so you can see exactly which expression was rejected.

Source

Thrown at pkg/proc/eval.go:1898

}

var supportedBuiltins = map[string]func([]*Variable, []ast.Expr) (*Variable, error){
	"cap":     capBuiltin,
	"len":     lenBuiltin,
	"complex": complexBuiltin,
	"imag":    imagBuiltin,
	"real":    realBuiltin,
	"min":     minBuiltin,
	"max":     maxBuiltin,
}

func capBuiltin(args []*Variable, nodeargs []ast.Expr) (*Variable, error) {
	if len(args) != 1 {
		return nil, fmt.Errorf("wrong number of arguments to cap: %d", len(args))
	}

	arg := args[0]
	invalidArgErr := fmt.Errorf("invalid argument %s (type %s) for cap", astutil.ExprToString(nodeargs[0]), arg.TypeString())

	switch arg.Kind {
	case reflect.Ptr:
		arg = arg.maybeDereference()
		if arg.Kind != reflect.Array {
			return nil, invalidArgErr
		}
		fallthrough
	case reflect.Array:
		return newConstant(constant.MakeInt64(arg.Len), arg.bi, arg.mem), nil
	case reflect.Slice:
		return newConstant(constant.MakeInt64(arg.Cap), arg.bi, arg.mem), nil
	case reflect.Chan:
		arg.loadValue(LoadFullValue())
		if arg.Unreadable != nil {
			return nil, arg.Unreadable
		}
		if arg.Base == 0 {

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Check the argument's type with `ptype <expr>`; cap only accepts slice, channel or array (or pointer-to-array).
  2. For strings use `len <expr>` instead of cap.
  3. If the value is a pointer you expected to be an array, dereference explicitly and verify the pointee type.
  4. Re-check the variable was not shadowed or optimized away into a differently-typed register value.

Example fix

// before (debugger console)
cap(myString)        // invalid argument myString (type string) for cap
// after
len(myString)        // strings: use len
Defensive patterns

Strategy: validation

Validate before calling

// before evaluating cap(x) in the debugger
// cap is only valid for slice, chan, array (or ptr-to-array)
if kind := v.Kind; kind != reflect.Slice && kind != reflect.Chan && kind != reflect.Array {
    // for pointers: only proceed if maybeDereference yields an array
    return fmt.Errorf("cap requires slice/chan/array, got %s", v.TypeString())
}

Type guard

func isCapable(v *proc.Variable) bool {
    if v.Kind == reflect.Slice || v.Kind == reflect.Chan || v.Kind == reflect.Array {
        return true
    }
    if v.Kind == reflect.Ptr {
        d := v.MaybeDereference()
        return d.Kind == reflect.Array
    }
    return false
}

Try / catch

val, err := evalCap(expr)
if err != nil && strings.Contains(err.Error(), "invalid argument") {
    // fall back to len() or report the argument type to the user
}

Prevention

When it happens

Trigger: Evaluating `cap(x)` in the debugger where x's reflect.Kind is not Slice, Chan or Array — e.g. cap of a string, a map, an int, or a pointer that dereferences to something other than an array (eval.go:1898, capBuiltin).

Common situations: Typing `cap(myString)` (strings have len, not cap), `cap(ptrToSlice)` (pointer to slice is not auto-dereferenced to an array), or evaluating cap on a variable whose DWARF type resolved to something unexpected during optimization.

Related errors


AI-assisted analysis of go-delve/delve@a23773e6c3 (2026-08-31). Data as JSON: /api/errors/928d91d2c3ee2f50. Report an issue: GitHub.