Unity-Technologies/UnityCsReference · critical · InvalidDataException

Read only {read} out of {storedHash.Length} bytes for hash.

Error message

Read only {read} out of {storedHash.Length} bytes for hash.

What it means

Thrown by AssemblyDependencyGraph.LoadFrom as InvalidDataException when stream.Read returns fewer bytes than hashLengthSize for the stored hash field. The hash length was read successfully, but the actual hash bytes following it are fewer than declared. This is a truncation in the hash portion of the file, indicating corruption or an incomplete write.

Source

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

            if (version != Version)
            {
                throw new InvalidDataException($"""
                                                Unsupported DependencyGraph version ({version}).{Environment.NewLine}
                                                DependencyGraph file is either corrupted or from a newer version of Unity.
                                                """);
            }

            Span<byte> hashLengthSpan = stackalloc byte[sizeof(long)];
            read = stream.Read(hashLengthSpan);
            if (read != hashLengthSpan.Length)
                throw new InvalidDataException($"Read only {read} out of {hashLengthSpan.Length} bytes for hash length.");

            long hashLengthSize = BitConverter.ToInt64(hashLengthSpan);
            Debug.Assert(hashLengthSize < 256);
            Span<byte> storedHash = stackalloc byte[(int)hashLengthSize];
            read = stream.Read(storedHash);
            if (read != hashLengthSize)
                throw new InvalidDataException($"Read only {read} out of {storedHash.Length} bytes for hash.");

            var startOfSerializedData = stream.Position;
            var hasher = SHA256.Create();
            var computedHash = hasher.ComputeHash(stream);
            if (storedHash.Length != computedHash.Length)
            {
                return null;
            }

            if (!computedHash.AsSpan().SequenceEqual(storedHash))
                return null;

            stream.Position = startOfSerializedData;

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

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Delete the Library/ folder and reopen the project to regenerate all cached data.
  2. Run a disk check if file corruption is recurrent.
  3. Exclude Library/ from sync/sharing tools that may cause partial writes.

Example fix

// No code fix; environment issue.
// Terminal fix:
//   rm -rf Library/
// Then reopen the project in Unity.
Defensive patterns

Strategy: fallback

Validate before calling

// No preventive validation; corruption is detected at read time.
// Ensure no external process interferes with Library/ files.

Try / catch

try
{
    AssemblyDependencyGraph.LoadFrom(stream);
}
catch (InvalidDataException)
{
    // Hash region corrupted; delete Library/ and regenerate.
}

Prevention

When it happens

Trigger: File truncated after the hash length field but before the full hash payload. hashLengthSize value itself is corrupted (e.g., a very large number from garbage data) causing the read to come up short. Interrupted write during serialization.

Common situations: Unity crash during project save. Corrupted download or copy of a project's Library/ folder. Disk errors in the Library/ directory.

Related errors


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