Repository: wezterm/wezterm (language: rust)

Issue:
split-pane --move-pane-id can kill panes
### What Operating System(s) are you seeing this problem on?

macOS

### Which Wayland compositor or X11 Window manager(s) are you using?

AeroSpace

### WezTerm version

20240203-110809-5046fc22 + main

### Did you try the latest nightly build to see if the issue is better (or worse!) than your current version?

Yes, and I updated the version box above to show the version of the nightly that I tried

### Describe the bug

Example:
```
+-----+
|  A  |
+-----+
|  B  |
+-----+
```
Command: `split-pane --pane-id $B_PANE_ID --move-pane-id $A_PANE_ID`

In `Domain::split_pane` (in `mux/src/domain.rs`), with a `SplitSource::MovePane`, the splitting pane (pane B) is first translated into a pane index (1) for its tab. Then, pane A is removed from its tab. However, this changes pane B's index (now 0). Subsequently, when we call `tab.split_and_insert(b_old_index, ...`, the old index 1 is incorrect; in this example it is in fact invalid as there is only a single pane, and pane A never gets reattached to the tab.

### To Reproduce

In a new tab, split the pane vertically. In the bottom pane, run:
`wezterm cli split-pane --move-pane-id $(wezterm cli get-pane-direction up)`

### Configuration

no config

### Expected Behavior

Top pane should be moved into the right side of a horizontal split of the previously bottom pane.

### Logs

_No response_

### Anything else?

_No response_


Discussion / hints:

Fix patch:
diff --git a/mux/src/domain.rs b/mux/src/domain.rs
index 3cb3767d5c3..23177b44633 100644
--- a/mux/src/domain.rs
+++ b/mux/src/domain.rs
@@ -127,7 +127,17 @@ pub trait Domain: Downcast + Send + Sync {
             }
         };
 
-        tab.split_and_insert(pane_index, split_request, Arc::clone(&pane))?;
+        // pane_index may have changed if src_pane was also in the same tab
+        let final_pane_index = match tab
+            .iter_panes_ignoring_zoom()
+            .iter()
+            .find(|p| p.pane.pane_id() == pane_id)
+        {
+            Some(p) => p.index,
+            None => anyhow::bail!("invalid pane id {}", pane_id),
+        };
+
+        tab.split_and_insert(final_pane_index, split_request, Arc::clone(&pane))?;
         Ok(pane)
     }
 


Test patch: