LGUG2Z/komorebi · error

Invalid command

Error message

Invalid command

What it means

In komorebi-bar, a mouse-triggered `MouseMessage::Command` is executed via `execute` in config.rs. The command is first converted with `replace_env()` into a `PathBuf`, then unwrapped to a `&str` with `.to_str().expect("Invalid command")`. This expect panics when the command bytes are not valid UTF-8, so the library deliberately aborts rather than execute a garbled command string.

Source

Thrown at komorebi-bar/src/config.rs:499

                    messages.push(SocketMessage::MouseFollowsFocus(false));
                    messages.push(config.message.clone());
                    messages.push(SocketMessage::MouseFollowsFocus(mouse_follows_focus));
                } else {
                    messages.push(config.message.clone());
                }

                tracing::debug!("Sending messages: {messages:?}");

                if komorebi_client::send_batch(messages).is_err() {
                    tracing::error!("could not send commands");
                }
            }
            MouseMessage::Command(cmd) => {
                tracing::debug!("Executing command: {}", cmd);

                let cmd_no_env = cmd.replace_env();

                if exec_powershell(cmd_no_env.to_str().expect("Invalid command")).is_err() {
                    tracing::error!("Failed to execute '{}'", cmd);
                }
            }
        };
    }
}

impl KomobarConfig {
    pub fn read(path: &PathBuf) -> color_eyre::Result<Self> {
        let content = std::fs::read_to_string(path)?;
        let mut value: Self = match path.extension().unwrap().to_string_lossy().as_str() {
            "json" => serde_json::from_str(&content)?,
            _ => panic!("unsupported format"),
        };

        if value.frame.is_none() {
            value.frame = Some(FrameConfig {
                inner_margin: Position {

View on GitHub (pinned to e0709f02bf)

Solutions

  1. Re-save the config file (komorebi.json / bar config) as UTF-8 without BOM, then reload the bar.
  2. Check the command string in the config for stray non-ASCII characters or pasted binary content and retype it plainly.
  3. If an environment variable is expanded into the command, verify that variable's value is valid UTF-8 (`echo $Env:MYVAR | Format-Hex`).
  4. As a library-side hardening, replace `.expect("Invalid command")` with a graceful `.to_str().ok()` plus an error log instead of a panic.

Example fix

// before
if exec_powershell(cmd_no_env.to_str().expect("Invalid command")).is_err() {
    tracing::error!("Failed to execute '{}'", cmd);
}
// after
match cmd_no_env.to_str() {
    Some(cmd_str) => {
        if exec_powershell(cmd_str).is_err() {
            tracing::error!("Failed to execute '{}'", cmd);
        }
    }
    None => tracing::error!("Invalid command (not valid UTF-8): {}", cmd),
}
Defensive patterns

Strategy: validation

Validate before calling

// caller-side check before sending a MouseMessage::Command
fn is_valid_command(cmd: &str) -> bool {
    !cmd.trim().is_empty() && std::str::from_utf8(cmd.as_bytes()).is_ok()
}

Type guard

fn command_to_str(cmd: &PathBuf) -> Option<&str> {
    cmd.to_str() // returns None when the command bytes are not valid UTF-8
}

Try / catch

// Rust: avoid the expect by handling the None case
match cmd_no_env.to_str() {
    Some(s) => { let _ = exec_powershell(s); }
    None => tracing::error!("Invalid command (non-UTF-8): {}", cmd),
}

Prevention

When it happens

Trigger: Calling the bar's command-execution path (e.g. via komo's `komorebic` mouse bindings that send `MouseMessage::Command(cmd)`) where `cmd.replace_env()` yields a `PathBuf` whose contents are not valid UTF-8, so `to_str()` returns None and the expect fires.

Common situations: Config files saved with a non-UTF-8 encoding (e.g. UTF-16, common when edited in PowerShell's default editor), commands containing binary bytes pasted from the clipboard, or environment-variable substitution (`%VAR%`/`$Env:` expansion) that injects non-UTF-8 bytes into the command string.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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