Unity-Technologies/UnityCsReference · error · AssemblyDefinitionException

Invalid Define Constraint: "{DefineConstraints[i]}" at line

Error message

Invalid Define Constraint: "{DefineConstraints[i]}" at line {(i+1).ToString()}

What it means

Thrown while building an assembly's symbol-definition context (CustomScriptAssembly.cs:556) when an entry in the asmdef's defineConstraints array fails DefineConstraintsHelper.IsDefineConstraintValid. That helper splits each constraint on '|', trims whitespace, strips a single leading '!' (negation), and validates the remainder with SymbolNameRestrictions.IsValid. An entry is invalid if it is null, empty after trimming, or contains characters illegal in a define symbol. The reported 1-based line number is the index within the defineConstraints array, not a line in the file.

Source

Thrown at Editor/Mono/Scripting/ScriptCompilation/CustomScriptAssembly.cs:556

            var isTestFrameworkAssembly = DefineConstraints != null && Array.Exists(DefineConstraints, x => x == "UNITY_TESTS_FRAMEWORK");
            if (!buildingForEditor && (isTestAssembly || isTestFrameworkAssembly) && !isBuildingWithTestAssemblies)
            {
                return false;
            }

            if (symbolDefinitionContext.IsEmpty())
            {
                throw new ArgumentException("Defines cannot be empty", nameof(symbolDefinitionContext));
            }

            // Log invalid define constraints
            if (DefineConstraints != null)
            {
                for (var i = 0; i < DefineConstraints.Length; ++i)
                {
                    if (!DefineConstraintsHelper.IsDefineConstraintValid(DefineConstraints[i]))
                    {
                        throw new AssemblyDefinitionException($"Invalid Define Constraint: \"{DefineConstraints[i]}\" at line {(i+1).ToString()}", FilePath);
                    }
                }
            }
            symbolDefinitionContext.SetResponseFileDefines(ResponseFileDefines);
            if (!DefineConstraintsHelper.IsDefineConstraintsCompatibleContext(symbolDefinitionContext, DefineConstraints))
            {
                return false;
            }

            if (isTestAssembly && AssetPathMetaData != null && !AssetPathMetaData.IsTestable)
            {
                return false;
            }

            // Compatible with editor and all platforms.
            if (IncludePlatforms == null && ExcludePlatforms == null)
            {
                return true;

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Inspect the defineConstraints entry at the 1-based index named in the message and correct it to a valid preprocessor-symbol name (uppercase letters, digits, underscores; must start with a letter/underscore).
  2. Use the documented OR syntax with '||' (e.g. "UNITY_IOS || UNITY_TVOS") and negation with '!' (e.g. "!UNITY_ANDROID"); ensure no stray spaces inside a single symbol.

Example fix

// before (.asmdef)
"defineConstraints": ["MY PLATFORM", ""]
// after
"defineConstraints": ["MY_PLATFORM"]
Defensive patterns

Strategy: validation

Validate before calling

// Validate a define constraint the same way IsDefineConstraintValid does
static bool IsValidDefineConstraint(string c)
{
    if (c == null) return false;
    foreach (var part in c.Split('|'))
    {
        var s = part.Trim().TrimStart('!');
        // valid symbol: starts letter/_, letters/digits/_ only, non-empty
        if (s.Length == 0) return false;
        if (!(char.IsLetter(s[0]) || s[0] == '_')) return false;
        foreach (var ch in s)
            if (!(char.IsLetterOrDigit(ch) || ch == '_')) return false;
    }
    return true;
}

foreach (var c in data.defineConstraints ?? Array.Empty<string>())
    if (!IsValidDefineConstraint(c))
        throw new InvalidOperationException($"Invalid define constraint: '{c}'");

Prevention

When it happens

Trigger: An asmdef "defineConstraints" array containing an entry with illegal characters (spaces inside the symbol, hyphens, leading digits, or empty strings). Examples that fail: "", " ", "MY SYMBOL", "1FOO", or a null entry.

Common situations: Typos when hand-editing constraints. Pasting constraints from documentation that uses human-readable labels instead of actual define symbols. Leaving an empty string from a template. Using lowercase with spaces like "unity ios".

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/bb2e6ccd5687c695. Report an issue: GitHub.