pardeike/Harmony · error · ArgumentException

VariableState: variable named

Error message

VariableState: variable named '{name}' not found

What it means

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.

Solutions

  1. Register the local under the name before reading: state[name] = il.DeclareLocal(...)
  2. Make the storing and reading code agree on the exact same string constant (use a const)
  3. List the keys of the 'other' dictionary to verify which names exist

Example fix

// before
var counter = state["loopCounter"];
// after
if (!state.HasName("loopCounter")) state["loopCounter"] = il.DeclareLocal(typeof(int));
var counter = state["loopCounter"];
Defensive patterns

Strategy: validation

Validate before calling

if (!state.Names.Contains(name)) throw new InvalidOperationException($"local '{name}' not registered");
var local = state[name];

Try / catch

try { local = state[name]; } catch (ArgumentException) { log.Error($"Unknown local '{name}' in transpiler"); throw; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of pardeike/Harmony@e7872dc170 (2026-09-15). Data as JSON: /api/errors/5fac216616f80187. Report an issue: GitHub.

Appendix: source

Thrown at Harmony/Internal/VariableState.cs:35

		public LocalBuilder this[InjectionType type]
		{
			get
			{
				if (injected.TryGetValue(type, out var local))
					return local;
				throw new ArgumentException($"VariableState: variable of type {type} not found");
			}
			set => injected[type] = value;
		}

		public LocalBuilder this[string name]
		{
			get
			{
				if (other.TryGetValue(name, out var local))
					return local;
				throw new ArgumentException($"VariableState: variable named '{name}' not found");
			}
			set => other[name] = value;
		}
	}
}

View on GitHub (pinned to e7872dc170)