Unity-Technologies/UnityCsReference · error · Exception
Assembly '{name}' is a CodeGen assembly and cannot be Auto R
Error message
Assembly '{name}' is a CodeGen assembly and cannot be Auto Referenced What it means
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.
Source
Thrown at Editor/Mono/Scripting/ScriptCompilation/CustomScriptAssembly.cs:132
if (assemblyData == null)
throw new System.Exception("Json file does not contain an assembly definition");
return assemblyData;
}
public void ValidateFields()
{
if (string.IsNullOrEmpty(name))
throw new System.Exception("Required property 'name' not set");
if ((excludePlatforms != null && excludePlatforms.Length > 0) &&
(includePlatforms != null && includePlatforms.Length > 0))
throw new System.Exception("Both 'excludePlatforms' and 'includePlatforms' are set.");
if (autoReferenced && UnityCodeGenHelpers.IsCodeGen(name))
{
throw new Exception($"Assembly '{name}' is a CodeGen assembly and cannot be Auto Referenced");
}
}
public static string ToJson(CustomScriptAssemblyData data)
{
return UnityEngine.JsonUtility.ToJson(data, true);
}
static void UpdateRenamedReferences(CustomScriptAssemblyData data)
{
if (data.references == null || data.references.Length == 0)
return;
HashSet<string> additionalReferences = null;
for (int i = 0; i < data.references.Length; ++i)
{
var reference = data.references[i];View on GitHub (pinned to 225b0fbdb5)
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.
Example fix
// before (.asmdef)
{
"name": "Unity.MyGen.CodeGen",
"autoReferenced": true
}
// after
{
"name": "MyCompany.MyGen.CodeGen",
"autoReferenced": false
} Defensive patterns
Strategy: validation
Validate before calling
// Replicate UnityCodeGenHelpers.IsCodeGen before saving an asmdef
static bool IsReservedCodeGenName(string assemblyName)
{
var n = System.IO.Path.GetFileNameWithoutExtension(assemblyName);
bool startsUnity = n.StartsWith("Unity.", StringComparison.OrdinalIgnoreCase);
return startsUnity &&
(n.EndsWith(".Compiler", StringComparison.OrdinalIgnoreCase)
|| n.EndsWith(".CodeGen", StringComparison.OrdinalIgnoreCase));
}
if (data.autoReferenced && IsReservedCodeGenName(data.name))
throw new InvalidOperationException(
$"'{data.name}' matches a reserved CodeGen name; set autoReferenced=false or rename."); Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Assembly cannot be have reserved name '{loadedCustomScriptAs
- Assembly with name '{loadedCustomScriptAssembly.Name}' alrea
- Both 'excludePlatforms' and 'includePlatforms' are set.
- Invalid Define Constraint: "{DefineConstraints[i]}" at line
- Platform name '{0}' not supported. Supported platform names:
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/fe6c4ff2d56df259.
Report an issue: GitHub.