{"record":{"id":"35cf081faf08b1e5","repo":"go-delve/delve","slug":"failed-to-find-the-named-variable","errorCode":null,"errorMessage":"failed to find the named variable","messagePattern":"failed to find the named variable","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"service/dap/server.go","lineNumber":3598,"sourceCode":"func (s *Session) onReverseContinueRequest(request *dap.ReverseContinueRequest, allowNextStateChange *syncflag) {\n\ts.send(&dap.ReverseContinueResponse{\n\t\tResponse: *s.newResponse(request.Request),\n\t})\n\ts.runUntilStopAndNotify(api.Rewind, allowNextStateChange)\n}\n\n// computeEvaluateName finds the named child, and computes its evaluate name.\nfunc (s *Session) computeEvaluateName(v *fullyQualifiedVariable, cname string) (string, error) {\n\tchildren := s.childrenToDAPVariables(v)\n\tfor _, c := range children {\n\t\tif c.Name == cname {\n\t\t\tif c.EvaluateName != \"\" {\n\t\t\t\treturn c.EvaluateName, nil\n\t\t\t}\n\t\t\treturn \"\", errors.New(\"cannot set the variable without evaluate name\")\n\t\t}\n\t}\n\treturn \"\", errors.New(\"failed to find the named variable\")\n}\n\n// onSetVariableRequest handles 'setVariable' requests.\nfunc (s *Session) onSetVariableRequest(request *dap.SetVariableRequest) {\n\targ := request.Arguments\n\n\tv, ok := s.variableHandles.get(arg.VariablesReference)\n\tif !ok {\n\t\ts.sendErrorResponse(request.Request, UnableToSetVariable, \"Unable to lookup variable\", fmt.Sprintf(\"unknown reference %d\", arg.VariablesReference))\n\t\treturn\n\t}\n\t// We need to translate the arg.Name to its evaluateName if the name\n\t// refers to a field or element of a variable.\n\t// https://github.com/microsoft/vscode/issues/120774\n\tevaluateName, err := s.computeEvaluateName(v, arg.Name)\n\tif err != nil {\n\t\ts.sendErrorResponse(request.Request, UnableToSetVariable, \"Unable to set variable\", err.Error())\n\t\treturn","sourceCodeStart":3580,"sourceCodeEnd":3616,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/service/dap/server.go#L3580-L3616","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Re-fetch children via the variables request to get fresh handles and exact names before calling setVariable","Match the child Name exactly as returned by delve (including quotes/formatting)","Avoid caching variablesReference values across stopped events; request new scopes after each stop","Use evaluate/setExpression with a real expression as a fallback when the name cannot be matched"],"exampleFix":"// before: stale name/cached handle\nsetVariable({variablesReference: oldRef, name: 'cnt', value: '0'})\n// after: refresh then use exact name\nvars := requestVariables(newRef) // after latest stopped event\nsetVariable({variablesReference: newRef, name: vars[0].name, value: '0'})","handlingStrategy":"validation","validationCode":"// refresh children and match exact names before setVariable\nchildren := requestVariables(ref) // fresh after latest stop\nfor _, c := range children {\n    if c.Name == wantedName { return setVariable(ref, c.Name, value) }\n}\nreturn errors.New(\"no such child; refresh variables\")","typeGuard":null,"tryCatchPattern":"if err.Error() == \"failed to find the named variable\" {\n    // invalidate cached variable handles, re-fetch children, retry once\n}","preventionTips":["Never cache variablesReference across stopped events","Use child Name values verbatim as returned by the variables request","Fall back to setExpression when names are hard to match (maps, indices)"],"tags":["dap","set-variable","variables","go"],"backgroundTag":"named-variable-not-found","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}