{"record":{"id":"5fac216616f80187","repo":"pardeike/Harmony","slug":"variablestate-variable-named-name-not-found","errorCode":null,"errorMessage":"VariableState: variable named '{name}' not found","messagePattern":"VariableState: variable named '(.+?)' not found","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Harmony/Internal/VariableState.cs","lineNumber":35,"sourceCode":"\r\n\t\tpublic LocalBuilder this[InjectionType type]\r\n\t\t{\r\n\t\t\tget\r\n\t\t\t{\r\n\t\t\t\tif (injected.TryGetValue(type, out var local))\r\n\t\t\t\t\treturn local;\r\n\t\t\t\tthrow new ArgumentException($\"VariableState: variable of type {type} not found\");\r\n\t\t\t}\r\n\t\t\tset => injected[type] = value;\r\n\t\t}\r\n\r\n\t\tpublic LocalBuilder this[string name]\r\n\t\t{\r\n\t\t\tget\r\n\t\t\t{\r\n\t\t\t\tif (other.TryGetValue(name, out var local))\r\n\t\t\t\t\treturn local;\r\n\t\t\t\tthrow new ArgumentException($\"VariableState: variable named '{name}' not found\");\r\n\t\t\t}\r\n\t\t\tset => other[name] = value;\r\n\t\t}\r\n\t}\r\n}\r\n","sourceCodeStart":17,"sourceCodeEnd":41,"githubUrl":"https://github.com/pardeike/Harmony/blob/e7872dc17008bc0ca2b3de1f53fd4120dbd7a17c/Harmony/Internal/VariableState.cs#L17-L41","documentation":"Lookup of an injected local variable in a VariableState by its name has failed. The string indexer is queried during patching when IL emission code asks for the LocalBuilder previously registered under that name, but the name is absent from the 'other' dictionary — the variable was never stored under it, so patching cannot proceed. This is a generic key-miss guard; the input at fault is the 'name' string with no matching entry.","triggerScenarios":"Reading state[\"myLocal\"] in a transpiler/emitter helper when no prior state[\"myLocal\"] = localBuilder was executed, or when the stored name differs in spelling/case.","commonSituations":"Typo or case mismatch in the local name between the storing and reading code; sharing VariableState across patch stages where one stage never registered the name; refactoring a local's name in one place only.","solutions":["Register the local under the name before reading: state[name] = il.DeclareLocal(...)","Make the storing and reading code agree on the exact same string constant (use a const)","List the keys of the 'other' dictionary to verify which names exist"],"exampleFix":"// before\nvar counter = state[\"loopCounter\"];\n// after\nif (!state.HasName(\"loopCounter\")) state[\"loopCounter\"] = il.DeclareLocal(typeof(int));\nvar counter = state[\"loopCounter\"];","handlingStrategy":"validation","validationCode":"if (!state.Names.Contains(name)) throw new InvalidOperationException($\"local '{name}' not registered\");\nvar local = state[name];","typeGuard":null,"tryCatchPattern":"try { local = state[name]; } catch (ArgumentException) { log.Error($\"Unknown local '{name}' in transpiler\"); throw; }","preventionTips":["Use const strings or an enum-backed name registry for local names","Match casing exactly — lookups are case-sensitive","Dump registered names when a transpiler fails to resolve one"],"tags":["transpiler","emitting","locals","csharp"],"backgroundTag":"record-not-found","analyzedSha":"e7872dc17008bc0ca2b3de1f53fd4120dbd7a17c","analyzedAt":"2026-09-15T22:47:11.550Z","contentChangedAt":"2026-09-15T22:47:11.550Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}