Unity-Technologies/UnityCsReference · error · InvalidDataException

Dependency graph data seems to be corrupted. Expected {lengt

Error message

Dependency graph data seems to be corrupted. Expected {lengthOfDataExcludingPayload} bytes but got {stream.Length} bytes.

What it means

Thrown by ValidateMinimumLength while deserializing the cached assembly dependency graph. The reader expects a fixed header (signature + 1 version byte + 8-byte hash length + 32-byte SHA256 hash) plus at least one payload byte; if the stream is shorter than that minimum, the cached binary is considered truncated or corrupted and reading is aborted before deserialization.

Source

Thrown at Editor/Mono/Scripting/APIUpdater/AssemblyDependencyGraph.cs:346

            stream.Position = startOfSerializedData;

            var serializer = new DataContractSerializer(
                typeof(AssemblyDependencyGraph),
                new DataContractSerializerSettings { PreserveObjectReferences = true });
            return (AssemblyDependencyGraph)serializer.ReadObject(stream);
        }

        static void ValidateMinimumLength(Stream stream)
        {
            var lengthOfDataExcludingPayload =
                                Signature.Length +
                                1 + // Version
                                sizeof(long) + // hash length
                                32; // actual SHA256 hash
            if (stream.Length < lengthOfDataExcludingPayload + 1)
            {
                throw new InvalidDataException($"Dependency graph data seems to be corrupted. Expected {lengthOfDataExcludingPayload} bytes but got {stream.Length} bytes.");
            }
        }

        internal DependencyEntry FindAssembly(string dependent)
        {
            return m_Graph.Find(e => e.m_Name == dependent);
        }

        [Serializable]
        [DataContract]
        internal class DependencyEntry : IComparable<DependencyEntry>
        {
            [DataMember]
            public string m_Name;
            [DataMember]
            public List<DependencyEntry> m_Dependencies;

            public DependencyEntry()

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Delete the Library/ folder (or specifically the dependency graph cache file) and relaunch Unity to rebuild it.
  2. Free disk space and ensure no process terminates the editor during a build/import.
  3. If it persists on a clean Library/, verify the project was not migrated across incompatible Unity versions and re-import assets.
Defensive patterns

Strategy: validation

Validate before calling

// Minimum header size the reader requires: signature + version + hash length + 32-byte hash + 1 payload byte.
// Replace Signature.Length with the actual signature constant used by the reader.
long min = Signature.Length + 1 + sizeof(long) + 32 + 1;
if (stream == null || stream.Length < min)
    throw new InvalidDataException($"Dependency graph stream too short ({stream?.Length}); rebuild the cache.");

Try / catch

try
{
    var graph = AssemblyDependencyGraph.Load(stream);
}
catch (InvalidDataException)
{
    // Cache is corrupt: delete and rebuild.
    File.Delete(cacheFilePath);
    var graph = AssemblyDependencyGraph.Rebuild();
}

Prevention

When it happens

Trigger: Calling the dependency graph load path (AssemblyDependencyGraph deserialization) on a stream whose Length is below Signature.Length + 1 + sizeof(long) + 32 + 1 bytes, i.e. ValidateMinimumLength runs against a short/garbage stream.

Common situations: Corrupted or partially-written Library/ cache (editor killed mid-write, disk full, interrupted import), a Library/ folder copied from source control with partial files, or a dependency-graph cache produced by a different/incompatible Unity version.

Related errors


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