{"record":{"id":"fe6c4ff2d56df259","repo":"Unity-Technologies/UnityCsReference","slug":"assembly-name-is-a-codegen-assembly-and-cannot","errorCode":null,"errorMessage":"Assembly '{name}' is a CodeGen assembly and cannot be Auto Referenced","messagePattern":"Assembly '(.+?)' is a CodeGen assembly and cannot be Auto Referenced","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Scripting/ScriptCompilation/CustomScriptAssembly.cs","lineNumber":132,"sourceCode":"\n            if (assemblyData == null)\n                throw new System.Exception(\"Json file does not contain an assembly definition\");\n\n            return assemblyData;\n        }\n\n        public void ValidateFields()\n        {\n            if (string.IsNullOrEmpty(name))\n                throw new System.Exception(\"Required property 'name' not set\");\n\n            if ((excludePlatforms != null && excludePlatforms.Length > 0) &&\n                (includePlatforms != null && includePlatforms.Length > 0))\n                throw new System.Exception(\"Both 'excludePlatforms' and 'includePlatforms' are set.\");\n\n            if (autoReferenced && UnityCodeGenHelpers.IsCodeGen(name))\n            {\n                throw new Exception($\"Assembly '{name}' is a CodeGen assembly and cannot be Auto Referenced\");\n            }\n        }\n\n        public static string ToJson(CustomScriptAssemblyData data)\n        {\n            return UnityEngine.JsonUtility.ToJson(data, true);\n        }\n\n        static void UpdateRenamedReferences(CustomScriptAssemblyData data)\n        {\n            if (data.references == null || data.references.Length == 0)\n                return;\n\n            HashSet<string> additionalReferences = null;\n\n            for (int i = 0; i < data.references.Length; ++i)\n            {\n                var reference = data.references[i];","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Scripting/ScriptCompilation/CustomScriptAssembly.cs#L114-L150","documentation":"Thrown by CustomScriptAssemblyData.ValidateFields() (CustomScriptAssembly.cs:132) when autoReferenced is true (its default) AND UnityCodeGenHelpers.IsCodeGen(name) returns true. IsCodeGen returns true for assembly names that start with \"Unity.\" and end with either \".Compiler\" or \".CodeGen\" (case-insensitive). Those suffixes are reserved for Unity's own source-generator / compiler toolchain assemblies, which must never be auto-referenced into every other assembly because doing so would force all assemblies to depend on codegen infrastructure.","triggerScenarios":"Creating an asmdef whose name matches the reserved pattern, e.g. \"Unity.MyPackage.CodeGen\" or \"Unity.Something.Compiler\", while leaving \"autoReferenced\": true (the JSON default). Importing a third-party package whose asmdef reuses the Unity.*.CodeGen naming.","commonSituations":"A package author building a Roslyn source generator package and copying Unity's internal naming convention. Renaming an existing asmdef into the reserved namespace without clearing autoReferenced.","solutions":["Rename the assembly so it does not start with \"Unity.\" combined with a \".CodeGen\" / \".Compiler\" suffix (the \"Unity.\" prefix alone is not enough; both conditions must hold).","If the reserved name is intentional, set \"autoReferenced\": false in the asmdef and have dependents reference it explicitly."],"exampleFix":"// before (.asmdef)\n{\n    \"name\": \"Unity.MyGen.CodeGen\",\n    \"autoReferenced\": true\n}\n// after\n{\n    \"name\": \"MyCompany.MyGen.CodeGen\",\n    \"autoReferenced\": false\n}","handlingStrategy":"validation","validationCode":"// Replicate UnityCodeGenHelpers.IsCodeGen before saving an asmdef\nstatic bool IsReservedCodeGenName(string assemblyName)\n{\n    var n = System.IO.Path.GetFileNameWithoutExtension(assemblyName);\n    bool startsUnity = n.StartsWith(\"Unity.\", StringComparison.OrdinalIgnoreCase);\n    return startsUnity &&\n           (n.EndsWith(\".Compiler\", StringComparison.OrdinalIgnoreCase)\n            || n.EndsWith(\".CodeGen\", StringComparison.OrdinalIgnoreCase));\n}\n\nif (data.autoReferenced && IsReservedCodeGenName(data.name))\n    throw new InvalidOperationException(\n        $\"'{data.name}' matches a reserved CodeGen name; set autoReferenced=false or rename.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Avoid the 'Unity.' prefix for non-Unity-owned assemblies entirely.","If a name must end in .CodeGen/.Compiler, explicitly set \"autoReferenced\": false in the asmdef.","Run a naming lint over package asmdefs to flag reserved-pattern collisions."],"tags":["unity","asmdef","script-compilation","codegen","naming","reserved"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}