Unity-Technologies/UnityCsReference · error · ArgumentOutOfRangeException

targetCacheSize should be >= 0, got {targetCacheSize}

Error message

targetCacheSize should be >= 0, got {targetCacheSize}

What it means

ArgumentOutOfRangeException from RunCleanBeeCacheInternal: targetCacheSize must be >= 0. The cache-cleanup routine sizes/evicts entries against this target, so a negative target is invalid.

Source

Thrown at Editor/Mono/Scripting/ScriptCompilation/BeeDriver/UnityBeeDriver.cs:231

            // Note that cacheSize is specified as the total number of used bytes
            // for all the files in the cache and not as the actual size of occupied
            // blocks on the disk, which will add some overhead.
            if (cacheSize < 0)
                cacheSize = 256 * 1024 * 1024;

            if (cacheDir == null)
                cacheDir = BeeCacheDir;

            if (runAsync)
                Task.Run(() => RunCleanBeeCacheInternal(cacheDir, cacheSize));
            else
                RunCleanBeeCacheInternal(cacheDir, cacheSize);
        }

        private static void RunCleanBeeCacheInternal(NPath beeCacheDir, long targetCacheSize)
        {
            if (targetCacheSize < 0)
                throw new ArgumentOutOfRangeException($"targetCacheSize should be >= 0, got {targetCacheSize}");

            var sw = new Stopwatch();
            sw.Start();

            NPath trashDir = beeCacheDir.Combine("trash");
            trashDir.EnsureDirectoryExists();

            long cacheSize = 0;

            List<CacheEntry> cacheEntries = new List<CacheEntry>();
            foreach (var subdir in beeCacheDir.Directories())
            {
                if (subdir.Equals(trashDir))
                    continue;

                // Each subdirectory corresponds to a deepcache entry, with the
                // cache lookup key as the directory name. Files inside those
                // subdirectories are the blobs referred to by the cache lookup.

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Clamp the cache size to 0 (or a positive value) before calling the clean routine.
  2. Treat -1/unknown config values as 'no limit' rather than passing them through.

Example fix

// before
RunCleanBeeCacheInternal(cacheDir, cacheSize);
// after
long target = Math.Max(0, cacheSize);
RunCleanBeeCacheInternal(cacheDir, target);
Defensive patterns

Strategy: validation

Validate before calling

long target = Math.Max(0, cacheSize);
RunCleanBeeCacheInternal(cacheDir, target);

Type guard

static bool IsValidCacheSize(long size) => size >= 0;

Prevention

When it happens

Trigger: Calling the Bee cache clean routine with a negative targetCacheSize (cacheSize argument).

Common situations: Computing a cache size from a config setting that defaults to -1/unknown, an arithmetic underflow, or misreading a 'disabled' sentinel as a real size.

Related errors


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