{"record":{"id":"1ce77a2e223c99fa","repo":"JetBrains/intellij-community","slug":"variable-0-is-already-declared","errorCode":null,"errorMessage":"Variable ''{0}'' is already declared","messagePattern":"Variable ''(.+?)'' is already declared","errorType":"exception","errorClass":"EvaluateRuntimeException","httpStatus":null,"severity":"error","filePath":"java/debugger/impl/src/com/intellij/debugger/engine/evaluation/expression/CodeFragmentEvaluator.java","lineNumber":100,"sourceCode":"\n  boolean hasValue(String localName) {\n    if (!mySyntheticLocals.containsKey(localName)) {\n      if (myParentFragmentEvaluator != null) {\n        return myParentFragmentEvaluator.hasValue(localName);\n      }\n      else {\n        return false;\n      }\n    }\n    else {\n      return true;\n    }\n  }\n\n  public void setInitialValue(String localName, Object value) {\n    LOG.assertTrue(!(value instanceof Value), \"use setValue for jdi values\");\n    if (hasValue(localName)) {\n      throw new EvaluateRuntimeException(\n        EvaluateExceptionUtil.createEvaluateException(JavaDebuggerBundle.message(\"evaluation.error.variable.already.declared\", localName)));\n    }\n    mySyntheticLocals.put(localName, value);\n  }\n\n  public void setValue(String localName, Value value) throws EvaluateException {\n    if (!mySyntheticLocals.containsKey(localName)) {\n      if (myParentFragmentEvaluator != null) {\n        myParentFragmentEvaluator.setValue(localName, value);\n      }\n      else {\n        throw EvaluateExceptionUtil.createEvaluateException(JavaDebuggerBundle.message(\"evaluation.error.variable.not.declared\", localName));\n      }\n    }\n    else {\n      mySyntheticLocals.put(localName, value);\n    }\n  }","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/JetBrains/intellij-community/blob/be881553f2a76ac8b4ea53950d93f0de78295c19/java/debugger/impl/src/com/intellij/debugger/engine/evaluation/expression/CodeFragmentEvaluator.java#L82-L118","documentation":"Thrown by CodeFragmentEvaluator.setInitialValue when code that builds an evaluation tries to register a synthetic local variable under a name that already exists in the current code fragment evaluator. It guards against silently shadowing an existing evaluation variable with a non-JDI seed value.","triggerScenarios":"Calling setInitialValue(localName, value) twice with the same localName on the same CodeFragmentEvaluator, or registering a name already added by a previous statement; hasValue(localName) returns true and an EvaluateRuntimeException wrapping evaluation.error.variable.already.declared is thrown.","commonSituations":"Debugger internals or plugins (e.g. JVM debugger language injection, Groovy/Kotlin evaluators reusing fragments) that seed the same captured variable name twice; re-evaluating an expression against a reused evaluator instance without resetting synthetic locals.","solutions":["Use a unique variable name for each setInitialValue call","Check hasValue(localName) before calling setInitialValue and use setValue for existing JDI values","Create a fresh CodeFragmentEvaluator per evaluation instead of reusing one"],"exampleFix":"// before\nfragment.setInitialValue(\"x\", 1);\nfragment.setInitialValue(\"x\", 2); // throws\n\n// after\nif (fragment.hasValue(\"x\")) {\n  fragment.setValue(\"x\", debuggerValue);\n} else {\n  fragment.setInitialValue(\"x\", 1);\n}","handlingStrategy":"validation","validationCode":"if (!fragment.hasValue(localName)) {\n  fragment.setInitialValue(localName, value);\n} else {\n  fragment.setValue(localName, jdiValue); // or pick another name\n}","typeGuard":null,"tryCatchPattern":"try {\n  evaluator.setInitialValue(name, value);\n} catch (EvaluateRuntimeException e) {\n  if (e.getMessage().contains(\"already declared\")) { /* rename variable */ }\n}","preventionTips":["Always check hasValue(name) before setInitialValue","Use a fresh CodeFragmentEvaluator per evaluation","Never pass a JDI Value to setInitialValue — use setValue for mirrors"],"tags":["debugger","evaluation","code-fragment","variables"],"backgroundTag":null,"analyzedSha":"be881553f2a76ac8b4ea53950d93f0de78295c19","analyzedAt":"2026-08-14T14:13:06.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}