bevyengine/bevy · error · GridPlacementError

Spans cannot be zero length

Error message

Spans cannot be zero length

What it means

GridPlacementError::InvalidZeroSpan is the thiserror variant produced by try_into_grid_span when a u16 span of 0 fails NonZero::new — 'Spans cannot be zero length'. The public constructors .expect() it (surfacing as the 'Invalid span value of 0.' panics), and the Display text appears when the error is propagated or formatted by fallible user code built on the enum.

Solutions

  1. Guarantee spans are >= 1 at the call site; one track is the minimum occupancy.
  2. Map InvalidZeroSpan in fallible code to a default span of 1 (or reject the record with a precise message).
  3. Skip creating the node when a computed span legitimately equals 0 rather than remapping silently.
  4. Unit-test data sources (configs, save files) for zero spans before they reach GridPlacement.

Example fix

// before
fn item(span: u16) -> Result<GridPlacement, GridPlacementError> {
    Ok(GridPlacement::span(span)) // panics on span == 0
}

// after
fn item(span: u16) -> Result<GridPlacement, GridPlacementError> {
    if span == 0 { return Err(GridPlacementError::InvalidZeroSpan); }
    Ok(GridPlacement::span(span))
}
Defensive patterns

Strategy: validation

Validate before calling

use std::num::NonZero;

fn checked_span(span: u16) -> Result<NonZero<u16>, GridPlacementError> {
    NonZero::new(span).ok_or(GridPlacementError::InvalidZeroSpan)
}

Type guard

fn is_valid_grid_span(span: u16) -> bool { span != 0 }

Try / catch

match checked_span(span) {
    Ok(span) => Ok(GridPlacement::span(span.get())),
    Err(e @ GridPlacementError::InvalidZeroSpan) => {
        warn!("{e}");
        Ok(GridPlacement::auto()) // span defaults to 1
    }
    Err(e) => Err(e),
}

Prevention

When it happens

Trigger: Passing span == 0 to GridPlacement::span/start_span/end_span or set_span (converted to a panic); handling or logging a GridPlacementError::InvalidZeroSpan returned from a fallible placement-validation layer.

Common situations: Defensive placement builders that return Result and need to distinguish bad spans from bad lines; data-validation pipelines reporting why a serialized layout was rejected; shared layout libraries reused across Bevy versions.

Related errors


AI-assisted analysis of bevyengine/bevy@a62cce8c05 (2026-08-20). Data as JSON: /api/errors/048c959ddea2b0c2. Report an issue: GitHub.

Appendix: source

Thrown at crates/bevy_ui/src/ui_node.rs:2252

            tracks: SmallVec::from_buf([GridTrack::min_content()]),
        }
        .into()
    }

    /// Create a repeating set of grid tracks with max-content size
    pub fn max_content<T: From<Self>>(repetition: u16) -> T {
        Self {
            repetition: GridTrackRepetition::Count(repetition),
            tracks: SmallVec::from_buf([GridTrack::max_content()]),
        }
        .into()
    }

    /// Create a repeating set of `fit-content()` grid tracks with fixed pixel limit
    pub fn fit_content_px<T: From<Self>>(repetition: u16, limit: f32) -> T {
        Self {
            repetition: GridTrackRepetition::Count(repetition),
            tracks: SmallVec::from_buf([GridTrack::fit_content_px(limit)]),
        }
        .into()
    }

    /// Create a repeating set of `fit-content()` grid tracks with percentage limit
    pub fn fit_content_percent<T: From<Self>>(repetition: u16, limit: f32) -> T {
        Self {
            repetition: GridTrackRepetition::Count(repetition),
            tracks: SmallVec::from_buf([GridTrack::fit_content_percent(limit)]),
        }
        .into()
    }

    /// Create a repeating set of `minmax()` grid track
    pub fn minmax<T: From<Self>>(
        repetition: impl Into<GridTrackRepetition>,
        min: MinTrackSizingFunction,
        max: MaxTrackSizingFunction,

View on GitHub (pinned to a62cce8c05)