go-delve/delve · error

failed to find the named variable

Error message

failed to find the named variable

What it means

When resolving a setVariable target, the server iterates the children of the referenced parent variable looking for a child whose Name matches the requested name. If no child matches, it returns 'failed to find the named variable', meaning the provided name does not correspond to any current child of that variablesReference handle.

Source

Thrown at service/dap/server.go:3598

func (s *Session) onReverseContinueRequest(request *dap.ReverseContinueRequest, allowNextStateChange *syncflag) {
	s.send(&dap.ReverseContinueResponse{
		Response: *s.newResponse(request.Request),
	})
	s.runUntilStopAndNotify(api.Rewind, allowNextStateChange)
}

// computeEvaluateName finds the named child, and computes its evaluate name.
func (s *Session) computeEvaluateName(v *fullyQualifiedVariable, cname string) (string, error) {
	children := s.childrenToDAPVariables(v)
	for _, c := range children {
		if c.Name == cname {
			if c.EvaluateName != "" {
				return c.EvaluateName, nil
			}
			return "", errors.New("cannot set the variable without evaluate name")
		}
	}
	return "", errors.New("failed to find the named variable")
}

// onSetVariableRequest handles 'setVariable' requests.
func (s *Session) onSetVariableRequest(request *dap.SetVariableRequest) {
	arg := request.Arguments

	v, ok := s.variableHandles.get(arg.VariablesReference)
	if !ok {
		s.sendErrorResponse(request.Request, UnableToSetVariable, "Unable to lookup variable", fmt.Sprintf("unknown reference %d", arg.VariablesReference))
		return
	}
	// We need to translate the arg.Name to its evaluateName if the name
	// refers to a field or element of a variable.
	// https://github.com/microsoft/vscode/issues/120774
	evaluateName, err := s.computeEvaluateName(v, arg.Name)
	if err != nil {
		s.sendErrorResponse(request.Request, UnableToSetVariable, "Unable to set variable", err.Error())
		return

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Re-fetch children via the variables request to get fresh handles and exact names before calling setVariable
  2. Match the child Name exactly as returned by delve (including quotes/formatting)
  3. Avoid caching variablesReference values across stopped events; request new scopes after each stop
  4. Use evaluate/setExpression with a real expression as a fallback when the name cannot be matched

Example fix

// before: stale name/cached handle
setVariable({variablesReference: oldRef, name: 'cnt', value: '0'})
// after: refresh then use exact name
vars := requestVariables(newRef) // after latest stopped event
setVariable({variablesReference: newRef, name: vars[0].name, value: '0'})
Defensive patterns

Strategy: validation

Validate before calling

// refresh children and match exact names before setVariable
children := requestVariables(ref) // fresh after latest stop
for _, c := range children {
    if c.Name == wantedName { return setVariable(ref, c.Name, value) }
}
return errors.New("no such child; refresh variables")

Try / catch

if err.Error() == "failed to find the named variable" {
    // invalidate cached variable handles, re-fetch children, retry once
}

Prevention

When it happens

Trigger: setVariable with a name that is not among the parent's children (typo, stale handle after stops, case mismatch); using a variablesReference from before the state changed so children were regenerated with different names; setting a map key or element name formatted differently than the child's display name.

Common situations: IDE caches variable handles across stops; users typing names into a watch/edit box that differ from delve's rendering (e.g. quoted map keys, index formatting); stale variablesReference after resetHandlesForStoppedEvent.

Related errors


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