{"record":{"id":"6eeacf3b60c38296","repo":"go-delve/delve","slug":"could-not-find-variable-q-v","errorCode":null,"errorMessage":"could not find variable %q: %v","messagePattern":"could not find variable %q: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/terminal/starbind/conv.go","lineNumber":744,"sourceCode":"}\n\nfunc (starlarkTargetObject) Truth() starlark.Bool {\n\treturn true\n}\n\nfunc (starlarkTargetObject) Type() string {\n\treturn \"<target variables>\"\n}\n\nfunc (tgt starlarkTargetObject) AttrNames() []string {\n\treturn nil\n}\n\nfunc (tgt starlarkTargetObject) Attr(name string) (starlark.Value, error) {\n\tenv := tgt.env\n\tv, err := env.ctx.Client().EvalVariable(env.ctx.Scope(), name, env.ctx.LoadConfig())\n\tif err != nil {\n\t\treturn starlark.None, fmt.Errorf(\"could not find variable %q: %v\", name, err)\n\t}\n\treturn env.variableValueToStarlarkValue(v, true)\n}\n\ntype starlarkUnhashable struct {\n}\n\nfunc (starlarkUnhashable) Freeze() {\n}\n\nfunc (starlarkUnhashable) Hash() (uint32, error) {\n\treturn 0, errors.New(\"not hashable\")\n}\n","sourceCodeStart":726,"sourceCodeEnd":758,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/terminal/starbind/conv.go#L726-L758","documentation":"starlarkTargetObject.Attr resolves a named attribute by evaluating it as a variable expression in the debuggee via Client().EvalVariable; when the debugger cannot evaluate the expression (undefined symbol, wrong scope, optimized-out variable) the underlying error is wrapped as `could not find variable %q`.","triggerScenarios":"A Starlark script accesses `target_object.someVar` where someVar is not a variable in the current evaluation scope (env.ctx.Scope()), or the name is a function/local that has been optimized out or belongs to another goroutine/frame.","commonSituations":"Scripts run with a scope pointing at a frame where the local no longer exists, accessing package-level names that need package qualification, or typos in variable names.","solutions":["Verify the variable exists in the current scope (use `locals`/`print` in the CLI first)","Qualify package variables with their package path (e.g. main.myVar)","Switch the evaluation scope/goroutine before accessing the attribute","Check the inner error (%v) for details like 'could not find symbol' or 'optimized out'"],"exampleFix":"# before\nx = tgt.counter      # counter not in current frame scope\n# after\nx = tgt[\"main.globalCounter\"]        # qualified name, or\n# switch scope to the frame holding the local first","handlingStrategy":"try-catch","validationCode":"# pre-check the name exists in scope via a plain evaluation before use\nout = eval_in_debugger(name)\nif out is None or 'could not find' in out:\n    fail(\"variable %r not in scope\" % name)","typeGuard":null,"tryCatchPattern":"try:\n    v = tgt.someVar\nexcept Exception as e:\n    if \"could not find variable\" in str(e):\n        v = None  # or re-qualify the name and retry\n    else:\n        raise","preventionTips":["Confirm the variable exists in the current frame with `locals`/`print` first","Qualify package-level names (pkg.var)","Account for compiler optimizations removing locals","Set the correct goroutine/scope before attribute access"],"tags":["starlark","delve","scripting","variable-evaluation"],"backgroundTag":"variable-not-in-scope","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}