spectreconsole/spectre.console · error · ArgumentException
Array does not contain enough space for items
Error message
Array does not contain enough space for items
What it means
Thrown by CircularBuffer<T>.CopyTo(T[] array, int arrayIndex) when the destination array does not have enough space starting at arrayIndex to hold all elements currently in the buffer. CircularBuffer is Spectre.Console's internal ring-buffer collection used for progress samples and other bounded history tracking. The check is array.Length - arrayIndex < Count.
Source
Thrown at src/Spectre.Console/Internal/CircularBuffer.cs:260
_start = _end;
}
}
}
public void Clear()
{
_start = 0;
_end = 0;
_buffer.Clear();
}
public bool Contains(T item) => IndexOf(item) != -1;
public void CopyTo(T[] array, int arrayIndex)
{
if (array.Length - arrayIndex < Count)
{
throw new ArgumentException("Array does not contain enough space for items");
}
for (var index = 0; index < Count; ++index)
{
array[index + arrayIndex] = this[index];
}
}
public T[] ToArray()
{
if (IsEmpty)
{
return Array.Empty<T>();
}
var array = new T[Count];
for (var index = 0; index < Count; ++index)
{View on GitHub (pinned to 0acc92fada)
Solutions
- Allocate the destination array with size >= buffer.Count: var array = new T[buffer.Count]; buffer.CopyTo(array, 0);
- Correct the arrayIndex so that array.Length - arrayIndex >= buffer.Count.
- Use buffer.ToArray() instead of CopyTo when you just need a snapshot — it allocates the correct size internally.
- If implementing CopyTo yourself, validate arrayIndex >= 0 and the remaining capacity before delegating.
Example fix
// before var dest = new T[10]; buffer.CopyTo(dest, 8); // fails if buffer.Count > 2 // after var dest = new T[buffer.Count + startIndex]; buffer.CopyTo(dest, startIndex);
Defensive patterns
Strategy: validation
Validate before calling
// Validate array capacity before CopyTo
if (array.Length - arrayIndex < buffer.Count)
{
throw new ArgumentException("Destination array too small.");
}
buffer.CopyTo(array, arrayIndex);
// Or simply use ToArray() which handles sizing:
var snapshot = buffer.ToArray(); Try / catch
try
{
buffer.CopyTo(array, arrayIndex);
}
catch (ArgumentException ex) when (ex.Message.Contains("enough space"))
{
// Re-allocate with correct size and retry
array = new T[buffer.Count];
buffer.CopyTo(array, 0);
} Prevention
- Always size the destination array to at least buffer.Count.
- Prefer buffer.ToArray() over manual CopyTo when you need a fresh snapshot.
- Double-check arrayIndex arithmetic: ensure array.Length - arrayIndex >= buffer.Count.
When it happens
Trigger: Calling CopyTo with a destination array smaller than the buffer's current element count, or with an arrayIndex that leaves insufficient remaining slots. For example: buffer.CopyTo(new T[5], 3) when buffer.Count is 4 (only 2 slots available from index 3).
Common situations: Pre-allocating a destination array with the wrong size (e.g., using a default capacity instead of buffer.Count); passing an arrayIndex that was computed incorrectly; resizing logic that shrinks the array after the buffer grew. This is typically hit in LINQ or collection-copy scenarios, or when code manually implements ICollection<T>.CopyTo.
Related errors
AI-assisted analysis of spectreconsole/spectre.console@0acc92fada (2026-08-13).
Data as JSON: /api/errors/dbf8459819e0a6b6.
Report an issue: GitHub.