LGUG2Z/komorebi · error

this is not a valid monitor index

Error message

this is not a valid monitor index

What it means

focus_monitor validates the requested monitor index against the number of entries in self.monitors. If monitors.get(idx) is None — the index exceeds the connected/virtual monitor count — the command fails with this message instead of panicking on an out-of-bounds access.

Source

Thrown at komorebi/src/window_manager.rs:3702

            .ok_or_eyre("there is no monitor")?
            .size)
    }

    pub fn focused_monitor_work_area(&self) -> eyre::Result<Rect> {
        Ok(self
            .focused_monitor()
            .ok_or_eyre("there is no monitor")?
            .work_area_size)
    }

    #[tracing::instrument(skip(self))]
    pub fn focus_monitor(&mut self, idx: usize) -> eyre::Result<()> {
        tracing::info!("focusing monitor");

        if self.monitors().get(idx).is_some() {
            self.monitors.focus(idx);
        } else {
            bail!("this is not a valid monitor index");
        }

        Ok(())
    }

    pub fn monitor_idx_from_window(&mut self, window: Window) -> Option<usize> {
        let hmonitor = WindowsApi::monitor_from_window(window.hwnd);

        for (i, monitor) in self.monitors().iter().enumerate() {
            if monitor.id == hmonitor {
                return Option::from(i);
            }
        }

        // our hmonitor might be stale, so if we didn't return above, try querying via the latest
        // info taken from win32_display_data and update our hmonitor while we're at it
        if let Ok(latest) = WindowsApi::monitor(hmonitor) {
            for (i, monitor) in self.monitors_mut().iter_mut().enumerate() {

View on GitHub (pinned to e0709f02bf)

Solutions

  1. Query the current monitor count (komorebic query state / OS APIs) and clamp the index before calling focus_monitor
  2. Use relative cycling (komorebic cycle-monitor) instead of absolute indexes
  3. Regenerate or parameterize configs per machine rather than hardcoding monitor indexes
  4. Catch the error and fall back to the primary monitor

Example fix

// before
komorebic focus-monitor 2 // fails on 2-monitor setup (indexes 0..1)
// after
komorebic cycle-monitor  # relative cycling avoids out-of-range indexes
Defensive patterns

Strategy: validation

Validate before calling

let monitor_count = query_korebi_monitor_count(); // e.g. via komorebic query
if idx < monitor_count { komorebic focus-monitor idx }
else { komorebic focus-monitor 0 }

Try / catch

// fallback to primary monitor
match wm.focus_monitor(idx) {
    Err(_) => wm.focus_monitor(0),
    ok => ok,
}

Prevention

When it happens

Trigger: Calling focus_monitor(idx) (komorebic focus-monitor N, monitor cycling with saved configs) where idx >= monitors.len(), e.g. index 2 on a single-monitor setup, or after a monitor was disconnected.

Common situations: Sharing komorebi.json keybinds across machines with different monitor counts; hotplug changes (undocking a laptop) invalidating previously valid indexes; scripts with hardcoded indexes.

Related errors


AI-assisted analysis of LGUG2Z/komorebi@e0709f02bf (2026-09-06). Data as JSON: /api/errors/421332fb213ecec7. Report an issue: GitHub.