Unity-Technologies/UnityCsReference · error · AssemblyDefinitionException

Folder '{loadedCustomScriptAssemblyReference.PathPrefix}' co

Error message

Folder '{loadedCustomScriptAssemblyReference.PathPrefix}' contains multiple assembly definition files

What it means

Thrown during assembly-reference graph loading (LoadingAssemblyDefinition.cs:252) when a loaded asmref's PathPrefix collides with one already registered in prefixToFilePathLookup. PathPrefix is the folder path that 'owns' an assembly definition. Unity requires that a given folder prefix hold at most one assembly-definition file across both .asmdef and .asmref types, so it can deterministically map a script's folder to a single assembly.

Source

Thrown at Editor/Mono/Scripting/ScriptCompilation/LoadingAssemblyDefinition.cs:252

                    if (contents != null)
                    {
                        var jsonContents = contents[i];
                        loadedCustomScriptAssemblyReference = LoadCustomScriptAssemblyReferenceFromJson(fullPath, jsonContents);
                    }
                    else
                    {
                        loadedCustomScriptAssemblyReference = LoadCustomScriptAssemblyReferenceFromJsonPath(fullPath);
                    }

                    if (!m_SkipCustomScriptAssemblyGraphValidation)
                    {
                        // Check both asmdef and asmref files.
                        if (prefixToFilePathLookup.TryGetValue(loadedCustomScriptAssemblyReference.PathPrefix, out var duplicateFilePaths))
                        {
                            var filePaths = new List<string> {loadedCustomScriptAssemblyReference.FilePath};
                            filePaths.AddRange(duplicateFilePaths);

                            throw new AssemblyDefinitionException(
                                $"Folder '{loadedCustomScriptAssemblyReference.PathPrefix}' contains multiple assembly definition files", filePaths.ToArray());
                        }
                    }

                    // Convert GUID references to assembly names
                    if (GUIDReference.IsGUIDReference(loadedCustomScriptAssemblyReference.Reference))
                    {
                        // Generate the guid to assembly lookup?
#pragma warning disable UA2001 // The Banned API Analyzer produces compile errors for any new Linq code. This pre-existing usage has been suppressed, but should be rewritten if possible.
                        guidsToAssemblies = guidsToAssemblies ?? CustomScriptAssemblies.ToDictionary(x => x.GUID);
#pragma warning restore UA2001

                        var guid = Utility.FastToLower(GUIDReference.GUIDReferenceToGUID(loadedCustomScriptAssemblyReference.Reference));
                        if (guidsToAssemblies.TryGetValue(guid, out var foundAssembly))
                        {
                            loadedCustomScriptAssemblyReference.Reference = foundAssembly.Name;
                        }
                    }

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Move the new .asmref (or the duplicate .asmdef) into its own subfolder so its PathPrefix no longer collides.
  2. Delete the redundant assembly-definition file if it was created by mistake.
  3. Confirm the asmref's 'reference' field points at the intended target asmdef elsewhere rather than implying co-location.

Example fix

// before: both Assets/MyLib/A.asmdef and Assets/MyLib/B.asmref exist
// after: move the asmref into a subfolder
//   Assets/MyLib/A.asmdef
//   Assets/MyLib/Editor/B.asmref
Defensive patterns

Strategy: validation

Validate before calling

// Enforce one assembly-definition file per folder prefix before reimport
static string PrefixOf(string asmdefPath) =>
    System.IO.Path.GetDirectoryName(asmdefPath)?.Replace('\\', '/');

var byPrefix = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
foreach (var f in allAsmdefAndAsmrefPaths)
    (byPrefix[PrefixOf(f)] ??= new List<string>()).Add(f);

var collisions = byPrefix.Where(kv => kv.Value.Count > 1).ToList();
if (collisions.Any())
    throw new InvalidOperationException(
        "Multiple assembly-definition files in folder(s): " +
        string.Join(", ", collisions.Select(c => c.Key)));

Prevention

When it happens

Trigger: Adding an .asmref file into a folder whose prefix already maps to an .asmdef (or another .asmref). Two assembly-definition files resolving to the same folder prefix.

Common situations: Placing an asmref next to an existing asmdef to 'extend' it instead of putting it in a subfolder. Copying an asmdef + asmref pair into a folder that already has one.

Related errors


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