Unity-Technologies/UnityCsReference · critical · InvalidDataException

Read only {read} out of {hashLengthSpan.Length} bytes for ha

Error message

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

What it means

Thrown by AssemblyDependencyGraph.LoadFrom as InvalidDataException when stream.Read returns fewer bytes than expected (sizeof(long) = 8) for the hash length field. This indicates the file is truncated or corrupted past the signature and version header. Unlike the signature/version errors, this is a structural read failure: the stream ended prematurely.

Source

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

                throw new InvalidDataException($"""
                                                Invalid signature ({BitConverter.ToString(signatureHashSpan.ToArray())}) found (Expected: {BitConverter.ToString(Signature)}).{Environment.NewLine}
                                                DependencyGraph file is either corrupted or from a previous version of Unity. If you observe inexplicable runtime/compilation errors, please delete `Library/` folder and reopen the project.
                                                """);
            }

            int version = stream.ReadByte();
            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;

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Delete the Library/ folder and reopen the project to regenerate the DependencyGraph.
  2. Investigate disk health if truncation recurs frequently.
  3. Ensure no external process locks Library/ files during Unity operation.

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; truncation is an I/O failure.
// Ensure the write process completes (no crashes during save).

Try / catch

try
{
    AssemblyDependencyGraph.LoadFrom(stream);
}
catch (InvalidDataException)
{
    // File is truncated; delete Library/ and regenerate.
}

Prevention

When it happens

Trigger: The DependencyGraph file is truncated due to an interrupted write (Unity crashed during save). Disk full condition during file write. Manual truncation or partial copy of the file.

Common situations: Unity editor crash during domain reload leaving a half-written file. Antivirus or backup software locking/interfering with the file during write. Network drive sync conflicts.

Related errors


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