{"record":{"id":"973acd084841cbde","repo":"clojure/clojure","slug":"can-t-create-defs-outside-of-current-ns","errorCode":null,"errorMessage":"Can't create defs outside of current ns","messagePattern":"Can't create defs outside of current ns","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/jvm/clojure/lang/Compiler.java","lineNumber":554,"sourceCode":"\t\t\telse if(RT.count(form) < 2)\n\t\t\t\tthrow Util.runtimeException(\"Too few arguments to def\");\n\t\t\telse if(!(RT.second(form) instanceof Symbol))\n\t\t\t\t\tthrow Util.runtimeException(\"First argument to def must be a Symbol\");\n\t\t\tSymbol sym = (Symbol) RT.second(form);\n\t\t\tVar v = lookupVar(sym, true);\n\t\t\tif(v == null)\n\t\t\t\tthrow Util.runtimeException(\"Can't refer to qualified var that doesn't exist\");\n\t\t\tif(!v.ns.equals(currentNS()))\n\t\t\t\t{\n\t\t\t\tif(sym.ns == null)\n\t\t\t\t\t{\n\t\t\t\t\tv = currentNS().intern(sym);\n\t\t\t\t\tregisterVar(v);\n\t\t\t\t\t}\n//\t\t\t\t\tthrow Util.runtimeException(\"Name conflict, can't def \" + sym + \" because namespace: \" + currentNS().name +\n//\t\t\t\t\t                    \" refers to:\" + v);\n\t\t\t\telse\n\t\t\t\t\tthrow Util.runtimeException(\"Can't create defs outside of current ns\");\n\t\t\t\t}\n\t\t\tIPersistentMap mm = sym.meta();\n\t\t\tboolean isDynamic = RT.booleanCast(RT.get(mm,dynamicKey));\n\t\t\tif(isDynamic)\n\t\t\t   v.setDynamic();\n            if(!isDynamic && sym.name.startsWith(\"*\") && sym.name.endsWith(\"*\") && sym.name.length() > 2)\n                {\n                RT.errPrintWriter().format(\"Warning: %1$s not declared dynamic and thus is not dynamically rebindable, \"\n                                          +\"but its name suggests otherwise. Please either indicate ^:dynamic %1$s or change the name. (%2$s:%3$d)\\n\",\n                                           sym, SOURCE_PATH.get(), LINE.get());\n                }\n\t\t\tif(RT.booleanCast(RT.get(mm, arglistsKey)))\n\t\t\t\t{\n\t\t\t\tIPersistentMap vm = v.meta();\n\t\t\t\t//vm = (IPersistentMap) RT.assoc(vm,staticKey,RT.T);\n\t\t\t\t//drop quote\n\t\t\t\tvm = (IPersistentMap) RT.assoc(vm,arglistsKey,RT.second(mm.valAt(arglistsKey)));\n\t\t\t\tv.setMeta(vm);","sourceCodeStart":536,"sourceCodeEnd":572,"githubUrl":"https://github.com/clojure/clojure/blob/f3b143341d6efc6428b523b2eaa099a6cc99156e/src/jvm/clojure/lang/Compiler.java#L536-L572","documentation":"def always creates the var in the current namespace. If a def names a qualified symbol that resolves to an existing var belonging to a different namespace, interning would mutate someone else's namespace, so the compiler refuses with this error (the old \"name conflict\" message is commented out in the source).","triggerScenarios":"Evaluating (def some.other.ns/existing-var v) where the var exists but its namespace is not the current *ns* and the symbol carries an explicit ns qualifier (the sym.ns == null intern path is skipped).","commonSituations":"Attempting to override a library var via its fully qualified name; generated code that emits qualified def names; after renaming/moving code so an old qualified def now points at a foreign ns.","solutions":["Drop the qualifier and def an unqualified symbol in the current namespace, or use a new name to avoid confusion.","If you truly need to set a var in another namespace, use (intern 'other.ns 'var-name value) or eval inside (in-ns 'other.ns).","If you wanted to change the value of the existing var, use alter-var-root (e.g. (alter-var-root #'other.ns/v (constantly new-v))) instead of def."],"exampleFix":"// before\n(def clojure.test/*report-counters* {}) ; foreign ns\n// after\n(alter-var-root #'clojure.test/*report-counters* (constantly {}))","handlingStrategy":"fallback","validationCode":"(let [v (resolve sym)]\n  (when (and v (not= (ns-name (:ns (meta v))) (ns-name *ns*)))\n    (println \"var belongs to another ns; use intern or alter-var-root\")))","typeGuard":null,"tryCatchPattern":"try {\n  (eval form)\n} catch (RuntimeException e) {\n  (if (.contains (.getMessage e) \"outside of current ns\")\n    (intern (symbol (namespace target-sym)) (symbol (name target-sym)) value)\n    (throw e))\n}","preventionTips":["def only unqualified symbols in the current namespace.","Use intern to create vars in other namespaces, alter-var-root to change existing ones.","Keep code generation emitting plain symbol names."],"tags":["compiler","namespaces","clojure","vars"],"backgroundTag":"permission-denied","analyzedSha":"f3b143341d6efc6428b523b2eaa099a6cc99156e","analyzedAt":"2026-09-09T12:04:58.961Z","contentChangedAt":"2026-09-09T12:04:58.961Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}