A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
//! A Domain represents an instance of a multiplexer.
//! For example, the gui frontend has its own domain,
//! and we can connect to a domain hosted by a mux server
//! that may be local, running "remotely" inside a WSL
//! container or actually remote, running on the other end
//! of an ssh session somewhere.
use crate::localpane::LocalPane;
use crate::pane::{alloc_pane_id, Pane, PaneId};
use crate::tab::{SplitRequest, Tab, TabId};
use crate::window::WindowId;
use crate::Mux;
use anyhow::{bail, Context, Error};
use async_trait::async_trait;
use config::keyassignment::{SpawnCommand, SpawnTabDomain};
use config::{configuration, ExecDomain, SerialDomain, ValueOrFunc, WslDomain};
use downcast_rs::{impl_downcast, Downcast};
use parking_lot::Mutex;
use portable_pty::{native_pty_system, CommandBuilder, ExitStatus, MasterPty, PtySize, PtySystem};
use std::collections::HashMap;
use std::ffi::OsString;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use wezterm_term::TerminalSize;
static DOMAIN_ID: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::AtomicUsize::new(0);
pub type DomainId = usize;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DomainState {
Detached,
Attached,
}
pub fn alloc_domain_id() -> DomainId {
DOMAIN_ID.fetch_add(1, ::std::sync::atomic::Ordering::Relaxed)
}
#[derive(Debug, Clone, PartialEq)]
pub enum SplitSource {
Spawn {
command: Option<CommandBuilder>,
command_dir: Option<String>,
},
MovePane(PaneId),
}
#[async_trait(?Send)]
pub trait Domain: Downcast + Send + Sync {
/// Spawn a new command within this domain
async fn spawn(
&self,
size: TerminalSize,
command: Option<CommandBuilder>,
command_dir: Option<String>,
window: WindowId,
) -> anyhow::Result<Arc<Tab>> {
let pane = self
.spawn_pane(size, command, command_dir)
.await
.context("spawn")?;
let tab = Arc::new(Tab::new(&size));
tab.assign_pane(&pane);
let mux = Mux::get();
mux.add_tab_and_active_pane(&tab)?;
mux.add_tab_to_window(&tab, window)?;
Ok(tab)
}
async fn split_pane(
&self,
source: SplitSource,
tab: TabId,
pane_id: PaneId,
split_request: SplitRequest,
) -> anyhow::Result<Arc<dyn Pane>> {
let mux = Mux::get();
let tab = match mux.get_tab(tab) {
Some(t) => t,
None => anyhow::bail!("Invalid tab id {}", tab),
};
if let SplitSource::MovePane(src_pane_id) = &source {
return mux.move_pane_to_split(*src_pane_id, tab.tab_id(), pane_id, split_request);
}
let pane_index = match tab.pane_index_for_pane(pane_id) {
Some(index) => index,
None => anyhow::bail!("invalid pane id {}", pane_id),
};
let split_size = match tab.compute_split_size(pane_index, split_request) {
Some(s) => s,
None => anyhow::bail!("invalid pane index {}", pane_index),
};
let pane = match source {
SplitSource::Spawn {
command,
command_dir,
} => {
self.spawn_pane(split_size.second, command, command_dir)
.await?
}
SplitSource::MovePane(_) => unreachable!("MovePane handled above"),
};
tab.split_and_insert(pane_index, split_request, Arc::clone(&pane))?;
Ok(pane)
}
async fn spawn_pane(
&self,
size: TerminalSize,
command: Option<CommandBuilder>,
command_dir: Option<String>,
) -> anyhow::Result<Arc<dyn Pane>>;
/// Spawn a pane destined for the pane stack containing `base_pane_id`
/// (a level-2 tab). The default just spawns a detached pane and leaves
/// the stack insertion to the caller (Mux::spawn_pane_in_stack); domains
/// that proxy to a remote mux (ClientDomain) override this so the remote
/// side performs the stack insertion too.
async fn spawn_pane_in_stack(
&self,
_base_pane_id: PaneId,
size: TerminalSize,
command: Option<CommandBuilder>,
command_dir: Option<String>,
) -> anyhow::Result<Arc<dyn Pane>> {
self.spawn_pane(size, command, command_dir).await
}
/// Move an existing pane into the stack containing `target_pane_id`.
/// The default implementation mutates the local mux tree. Proxy domains
/// override this so the authoritative remote tree is changed as well.
async fn move_pane_to_stack(
&self,
src_pane_id: PaneId,
target_tab_id: TabId,
target_pane_id: PaneId,
) -> anyhow::Result<Arc<dyn Pane>> {
let mux = Mux::get();
let tab = mux
.get_tab(target_tab_id)
.ok_or_else(|| anyhow::anyhow!("Invalid tab id {target_tab_id}"))?;
let pane = mux
.get_pane(src_pane_id)
.ok_or_else(|| anyhow::anyhow!("pane {src_pane_id} not found"))?;
tab.move_pane_to_stack(src_pane_id, target_pane_id)?;
Ok(pane)
}
/// Swap two panes of a tab, each into the other's place. The default
/// implementation mutates the local mux tree. Proxy domains override
/// this so the authoritative remote tree is changed as well.
async fn swap_panes(
&self,
tab_id: TabId,
pane_id: PaneId,
other_pane_id: PaneId,
) -> anyhow::Result<()> {
let tab = Mux::get()
.get_tab(tab_id)
.ok_or_else(|| anyhow::anyhow!("Invalid tab id {tab_id}"))?;
tab.swap_panes(pane_id, other_pane_id)
}
/// The mux will call this method on the domain of the pane that
/// is being moved to give the domain a chance to handle the movement.
/// If this method returns Ok(None), then the mux will handle the
/// movement itself by mutating its local Tabs and Windows.
async fn move_pane_to_new_tab(
&self,
_pane_id: PaneId,
_window_id: Option<WindowId>,
_workspace_for_new_window: Option<String>,
) -> anyhow::Result<Option<(Arc<Tab>, WindowId)>> {
Ok(None)
}
/// Returns false if the `spawn` method will never succeed.
/// There are some internal placeholder domains that are
/// pre-created with local UI that we do not want to allow
/// to show in the launcher/menu as launchable items.
fn spawnable(&self) -> bool {
true
}
/// Returns true if the `detach` method can be used
/// to detach the domain, preserving the associated
/// panes, or false if the `detach` method will never
/// succeed
fn detachable(&self) -> bool;
/// Returns the domain id, which is useful for obtaining
/// a handle on the domain later.
fn domain_id(&self) -> DomainId;
/// Returns the name of the domain.
/// Should be a short identifier.
fn domain_name(&self) -> &str;
/// Returns a label describing the domain.
async fn domain_label(&self) -> String {
self.domain_name().to_string()
}
/// Re-attach to any tabs that might be pre-existing in this domain
async fn attach(&self, window_id: Option<WindowId>) -> anyhow::Result<()>;
/// Detach all tabs
fn detach(&self) -> anyhow::Result<()>;
/// Indicates the state of the domain
fn state(&self) -> DomainState;
}
impl_downcast!(Domain);
/// Inside a Flatpak sandbox `fixup_command` re-routes the spawn to the
/// host through `flatpak-spawn` and resolves the host's own login shell
/// itself, so a choice made here would not be honored -- and the paths
/// this process can see belong to the sandbox runtime, not to the host
/// that would run them. Cached: the file cannot appear or disappear
/// while the process lives, and this is consulted on every spawn.
fn running_under_flatpak() -> bool {
static UNDER_FLATPAK: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*UNDER_FLATPAK.get_or_init(|| Path::new("/.flatpak-info").exists())
}
/// What a `LocalDomain` actually spawns into.
///
/// Recorded at construction rather than inferred from the config: a
/// `thinkterm serial ...` invocation builds its domain from the command
/// line and never registers it in `serial_ports`, so a config lookup
/// would classify it as an ordinary local domain and apply settings that
/// break it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum LocalDomainKind {
/// Programs run directly on this machine.
Plain,
/// A serial port: the pty ignores the command and accepts only the
/// default program.
Serial,
}
pub struct LocalDomain {
pty_system: Mutex<Box<dyn PtySystem + Send>>,
id: DomainId,
name: String,
kind: LocalDomainKind,
}
impl LocalDomain {
pub fn new(name: &str) -> Result<Self, Error> {
Ok(Self::with_pty_system(name, native_pty_system()))
}
fn with_kind(mut self, kind: LocalDomainKind) -> Self {
self.kind = kind;
self
}
fn resolve_exec_domain(&self) -> Option<ExecDomain> {
config::configuration()
.exec_domains
.iter()
.find(|ed| ed.name == self.name)
.cloned()
}
/// Whether this domain runs programs directly on this machine.
///
/// WSL, exec and serial domains are all `LocalDomain`s that run
/// somewhere else -- inside a distribution, inside a container, or
/// not as a process at all -- so a shell chosen for *this* machine is
/// meaningless to them, and forcing one on them breaks them: a serial
/// pty rejects any command that is not the default program outright.
fn is_plain_local_domain(&self, wsl: Option<&WslDomain>) -> bool {
self.kind == LocalDomainKind::Plain
&& wsl.is_none()
&& self.resolve_exec_domain().is_none()
&& !running_under_flatpak()
}
/// Public form of [`Self::is_plain_local_domain`]. "Is a `LocalDomain`"
/// is NOT this question: WSL, exec and serial domains are all
/// `LocalDomain`s whose cwd is interpreted somewhere else.
pub fn is_plain_local(&self) -> bool {
self.is_plain_local_domain(self.resolve_wsl_domain().as_ref())
}
fn resolve_wsl_domain(&self) -> Option<WslDomain> {
config::configuration()
.wsl_domains()
.iter()
.find(|d| d.name == self.name)
.cloned()
}
pub fn with_pty_system(name: &str, pty_system: Box<dyn PtySystem + Send>) -> Self {
let id = alloc_domain_id();
Self {
pty_system: Mutex::new(pty_system),
id,
name: name.to_string(),
kind: LocalDomainKind::Plain,
}
}
pub fn new_wsl(wsl: WslDomain) -> Result<Self, Error> {
Self::new(&wsl.name)
}
pub fn new_exec_domain(exec_domain: ExecDomain) -> anyhow::Result<Self> {
Self::new(&exec_domain.name)
}
pub fn new_serial_domain(serial_domain: SerialDomain) -> anyhow::Result<Self> {
let port = serial_domain.port.as_ref().unwrap_or(&serial_domain.name);
let mut serial = portable_pty::serial::SerialTty::new(&port);
if let Some(baud) = serial_domain.baud {
serial.set_baud_rate(baud as u32);
}
let pty_system = Box::new(serial);
Ok(Self::with_pty_system(&serial_domain.name, pty_system)
.with_kind(LocalDomainKind::Serial))
}
#[cfg(unix)]
fn is_conpty(&self) -> bool {
false
}
#[cfg(windows)]
fn is_conpty(&self) -> bool {
let pty_system = self.pty_system.lock();
let pty_system: &dyn PtySystem = &**pty_system;
pty_system
.downcast_ref::<portable_pty::win::conpty::ConPtySystem>()
.is_some()
}
async fn fixup_command(&self, cmd: &mut CommandBuilder) -> anyhow::Result<()> {
if let Some(wsl) = self.resolve_wsl_domain() {
let mut args: Vec<OsString> = cmd.get_argv().clone();
if args.is_empty() {
if let Some(def_prog) = &wsl.default_prog {
for arg in def_prog {
args.push(arg.into());
}
}
}
let mut argv: Vec<OsString> = vec![
"wsl.exe".into(),
"--distribution".into(),
wsl.distribution
.as_deref()
.unwrap_or(wsl.name.as_str())
.into(),
];
if let Some(cwd) = cmd.get_cwd() {
argv.push("--cd".into());
argv.push(cwd.into());
}
if let Some(user) = &wsl.username {
argv.push("--user".into());
argv.push(user.into());
}
if !args.is_empty() {
argv.push("--exec".into());
for arg in args {
argv.push(arg);
}
}
// TODO: process env list and update WLSENV so that they
// get passed through
cmd.clear_cwd();
*cmd.get_argv_mut() = argv;
} else if let Some(ed) = self.resolve_exec_domain() {
let mut args = vec![];
let mut set_environment_variables = HashMap::new();
for arg in cmd.get_argv() {
args.push(
arg.to_str()
.ok_or_else(|| anyhow::anyhow!("command argument is not utf8"))?
.to_string(),
);
}
for (k, v) in cmd.iter_full_env_as_str() {
set_environment_variables.insert(k.to_string(), v.to_string());
}
let cwd = match cmd.get_cwd() {
Some(cwd) => Some(PathBuf::from(cwd)),
None => None,
};
let spawn_command = SpawnCommand {
label: None,
domain: SpawnTabDomain::DomainName(ed.name.clone()),
args: if args.is_empty() { None } else { Some(args) },
set_environment_variables,
cwd,
position: None,
};
let spawn_command = config::with_lua_config_on_main_thread(|lua| async {
let lua = lua.ok_or_else(|| anyhow::anyhow!("missing lua context"))?;
let value = config::lua::emit_async_callback(
&*lua,
(ed.fixup_command.clone(), (spawn_command.clone())),
)
.await?;
let cmd: SpawnCommand =
luahelper::from_lua_value_dynamic(value).with_context(|| {
format!(
"interpreting SpawnCommand result from ExecDomain {}",
ed.name
)
})?;
Ok(cmd)
})
.await
.with_context(|| format!("calling ExecDomain {} function", ed.name))?;
// Reinterpret the SpawnCommand into the builder
cmd.get_argv_mut().clear();
if let Some(args) = &spawn_command.args {
for arg in args {
cmd.get_argv_mut().push(arg.into());
}
}
cmd.env_clear();
for (k, v) in &spawn_command.set_environment_variables {
cmd.env(k, v);
}
cmd.clear_cwd();
if let Some(cwd) = &spawn_command.cwd {
cmd.cwd(cwd);
}
} else if Path::new("/.flatpak-info").exists() {
// We're running inside a flatpak sandbox.
// Run the command outside the sandbox via flatpak-spawn
let mut args = vec![
"flatpak-spawn".to_string(),
"--host".to_string(),
"--watch-bus".to_string(),
];
if let Some(cwd) = cmd.get_cwd() {
args.push(format!("--directory={}", Path::new(cwd).display()));
}
let is_default_prog = cmd.is_default_prog();
// Note: WEZTERM_UNIX_SOCKET, WEZTERM_CONFIG_(FILE|DIR) and other env
// vars are not included in this.
// We can't include them: their paths are only meaningful in the sandbox
// and cannot be reasonably accessed from outside it in the shell.
for (k, v) in cmd.iter_extra_env_as_str() {
args.push(format!("--env={k}={v}"));
}
for arg in cmd.get_argv() {
args.push(
arg.to_str()
.ok_or_else(|| anyhow::anyhow!("command argument is not utf8"))?
.to_string(),
);
}
if is_default_prog {
// We can't read $SHELL from inside the sandbox, so ask the host.
let output = std::process::Command::new("flatpak-spawn")
.args(["--host", "sh", "-c", "echo $SHELL"])
.output()?;
let shell = String::from_utf8_lossy(&output.stdout);
args.push(shell.trim().to_string());
// Assume we can pass `-l` for a login shell
args.push("-l".to_string());
}
// Avoid setting up the controlling tty as that is not compatible
// with flatpak:
// <https://github.com/flatpak/flatpak/issues/3697>
// <https://github.com/flatpak/flatpak/issues/3285>
cmd.set_controlling_tty(false);
// Re-apply to the builder
cmd.get_argv_mut().clear();
for arg in args {
cmd.get_argv_mut().push(arg.into());
}
cmd.clear_cwd();
log::trace!("made: {cmd:#?}");
} else if let Some(dir) = cmd.get_cwd() {
// I'm not normally a fan of existence checking, but not checking here
// can be painful; in the case where a tab is local but has connected
// to a remote system and that remote has used OSC 7 to set a path
// that doesn't exist on the local system, process spawning can fail.
// Another situation is `sudo -i` has the pane with set to a cwd
// that is not accessible to the user.
let require_cwd = cmd.get_require_cwd();
if let Err(err) = Path::new(&dir).read_dir() {
// A caller that named this directory on purpose gets the
// error instead of the fallback. Per-spawn rather than global:
// the lenient path's `sudo -i` case IS a permission denial,
// and only the caller knows whether its cwd was inherited or
// requested.
if required_cwd_error_is_fatal(require_cwd) {
return Err(anyhow::Error::new(RequiredCwdUnavailable {
dir: PathBuf::from(&dir),
kind: err.kind(),
detail: err.to_string(),
}));
}
log::warn!(
"Directory {:?} is not readable and will not be \
used for the command we are spawning: {:#}",
dir,
err
);
cmd.clear_cwd();
}
}
Ok(())
}
async fn build_command(
&self,
command: Option<CommandBuilder>,
command_dir: Option<String>,
pane_id: PaneId,
) -> anyhow::Result<CommandBuilder> {
let config = configuration();
let wsl = self.resolve_wsl_domain();
// The user's chosen shell occupies the same slot as the Lua
// `default_prog`, one tier above it. Sitting in that slot is what
// keeps a WSL pane out of it: the arm below deliberately never
// consults the global default for a WSL domain, because a program
// chosen for this machine is meaningless inside the distribution.
// Only a command that is still asking for the default program can
// be affected, so an explicit `wezterm cli spawn -- htop` never
// pays for consulting the preference (which stats the chosen
// shell to confirm it still exists).
let wants_default_prog = command
.as_ref()
.map(|cmd| cmd.is_default_prog())
.unwrap_or(true);
let chosen_shell = (wants_default_prog && self.is_plain_local_domain(wsl.as_ref()))
.then(crate::default_prog::preferred_argv)
.flatten();
let chosen_application = chosen_shell
.as_deref()
.and_then(|argv| crate::default_prog::shell_application(argv, cfg!(windows)));
let chosen_argv = match &chosen_application {
Some(crate::default_prog::ShellApplication::Argv(argv)) => Some(argv),
_ => None,
};
let default_prog = match (&wsl, &chosen_application) {
(Some(wsl), _) => wsl.default_prog.as_ref(),
// The choice wins over the Lua option: a stale `default_prog`
// must not make the settings dropdown look inert.
(None, Some(_)) => chosen_argv,
(None, None) => config.default_prog.as_ref(),
};
let mut cmd = match command {
Some(mut cmd) => {
config.apply_cmd_defaults(&mut cmd, default_prog, config.default_cwd.as_ref());
cmd
}
None => config.build_prog(
None,
default_prog,
wsl.as_ref()
.map(|wsl| wsl.default_cwd.as_ref())
.unwrap_or(config.default_cwd.as_ref()),
)?,
};
// Applied after the command exists, and only while it is still
// asking for the default program, so an explicitly spawned program
// (`wezterm cli spawn -- htop`) keeps its own argv and its own
// inherited SHELL.
if let Some(crate::default_prog::ShellApplication::ShellEnv(shell)) = &chosen_application {
// A caller that set SHELL for this spawn meant it -- the
// documented `SpawnCommand { set_environment_variables = {
// SHELL = ... } }` idiom must outrank a global preference.
if cmd.is_default_prog() && cmd.get_env("SHELL").is_none() {
cmd.env("SHELL", shell);
}
}
if let Some(dir) = command_dir {
// Paths entered in the UI (or relayed by a mux client) may use
// `~` for the home directory; the spawn cwd is used verbatim by
// the OS, so expand it here against this process's home.
let dir = if dir == "~" || dir.starts_with("~/") {
config::HOME_DIR
.join(dir.strip_prefix("~/").unwrap_or(""))
.to_string_lossy()
.into_owned()
} else {
dir
};
cmd.cwd(dir);
}
if let Ok(sock) = std::env::var("WEZTERM_UNIX_SOCKET") {
cmd.env("WEZTERM_UNIX_SOCKET", sock);
}
cmd.env("WEZTERM_PANE", pane_id.to_string());
cmd.env(
"THINKTERM_MUX_SERVER_ID",
Mux::get().runtime_server_id().to_string(),
);
if let Some(agent) = Mux::get().agent.as_ref() {
cmd.env("SSH_AUTH_SOCK", agent.path());
}
self.fixup_command(&mut cmd).await?;
Ok(cmd)
}
}
/// Allows sharing the writer between the Pane and the Terminal.
/// This could potentially be eliminated in the future if we can
/// teach the Pane impl to reference the writer in the Termninal,
/// but the Pane trait returns a RefMut and that makes it a bit
/// awkward at the moment.
#[derive(Clone)]
pub(crate) struct WriterWrapper {
writer: Arc<Mutex<Box<dyn Write + Send>>>,
}
impl WriterWrapper {
pub fn new(writer: Box<dyn Write + Send>) -> Self {
Self {
writer: Arc::new(Mutex::new(writer)),
}
}
}
impl std::io::Write for WriterWrapper {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.writer.lock().write(buf)
}
fn flush(&mut self) -> std::io::Result<()> {
self.writer.lock().flush()
}
}
/// Wraps the underlying pty; we use this as a marker for when
/// the spawn attempt failed in order to hold the pane open
pub(crate) struct FailedSpawnPty {
inner: Mutex<Box<dyn MasterPty>>,
}
impl portable_pty::MasterPty for FailedSpawnPty {
fn resize(&self, new_size: PtySize) -> anyhow::Result<()> {
self.inner.lock().resize(new_size)
}
fn get_size(&self) -> anyhow::Result<PtySize> {
self.inner.lock().get_size()
}
fn try_clone_reader(&self) -> anyhow::Result<Box<dyn std::io::Read + Send + 'static>> {
self.inner.lock().try_clone_reader()
}
fn take_writer(&self) -> anyhow::Result<Box<dyn std::io::Write + Send + 'static>> {
self.inner.lock().take_writer()
}
#[cfg(unix)]
fn process_group_leader(&self) -> Option<i32> {
None
}
#[cfg(unix)]
fn as_raw_fd(&self) -> Option<std::os::fd::RawFd> {
None
}
#[cfg(unix)]
fn tty_name(&self) -> Option<std::path::PathBuf> {
None
}
}
/// A fake child process for the case where the spawn attempt
/// failed. It reports as immediately terminated.
#[derive(Debug)]
pub(crate) struct FailedProcessSpawn {}
impl portable_pty::Child for FailedProcessSpawn {
fn try_wait(&mut self) -> std::io::Result<Option<ExitStatus>> {
Ok(Some(ExitStatus::with_exit_code(1)))
}
fn wait(&mut self) -> std::io::Result<ExitStatus> {
Ok(ExitStatus::with_exit_code(1))
}
fn process_id(&self) -> Option<u32> {
None
}
#[cfg(windows)]
fn as_raw_handle(&self) -> Option<std::os::windows::io::RawHandle> {
None
}
}
impl portable_pty::ChildKiller for FailedProcessSpawn {
fn kill(&mut self) -> std::io::Result<()> {
Ok(())
}
fn clone_killer(&self) -> Box<dyn portable_pty::ChildKiller + Send + Sync> {
Box::new(FailedProcessSpawn {})
}
}
#[async_trait(?Send)]
impl Domain for LocalDomain {
async fn spawn_pane(
&self,
size: TerminalSize,
mut command: Option<CommandBuilder>,
command_dir: Option<String>,
) -> anyhow::Result<Arc<dyn Pane>> {
let pane_id = alloc_pane_id();
// A pane a restored layout spawns starts with what it showed.
let kept = crate::kept_output::take_seed(&mut command);
let cmd = self
.build_command(command, command_dir, pane_id)
.await
.context("build_command")?;
let pair = self
.pty_system
.lock()
.openpty(crate::terminal_size_to_pty_size(size)?)?;
let command_line = cmd
.as_unix_command_line()
.unwrap_or_else(|err| format!("error rendering command line: {:?}", err));
let command_description = format!(
"\"{}\" in domain \"{}\"",
if command_line.is_empty() {
cmd.get_shell()
} else {
command_line
},
self.name
);
let child_result = pair.slave.spawn_command(cmd);
let mut writer = WriterWrapper::new(pair.master.take_writer()?);
let mut terminal = wezterm_term::Terminal::new(
size,
std::sync::Arc::new(config::TermConfig::new()),
"ThinkTerm",
config::wezterm_version(),
Box::new(writer.clone()),
);
if self.is_conpty() {
terminal.enable_conpty_quirks();
}
// Before the reader starts, so the shell's first output lands below.
if let Some(kept) = &kept {
terminal.advance_bytes(kept.bytes(size.rows, self.is_conpty()));
}
let pane_writer = terminal.writer_handle();
let pane: Arc<dyn Pane> = match child_result {
Ok(child) => Arc::new(LocalPane::new(
pane_id,
terminal,
child,
pair.master,
pane_writer,
self.id,
command_description,
)),
Err(err) => {
// Show the error to the user in the new pane
write!(writer, "{err:#}").ok();
// and return a dummy pane that has exited
Arc::new(LocalPane::new(
pane_id,
terminal,
Box::new(FailedProcessSpawn {}),
Box::new(FailedSpawnPty {
inner: Mutex::new(pair.master),
}),
pane_writer,
self.id,
command_description,
))
}
};
let mux = Mux::get();
mux.add_pane(&pane)?;
// Kept until here: a spawn that failed on the way leaves the record
// for the next try.
if let Some(kept) = kept {
kept.spend();
}
Ok(pane)
}
fn domain_id(&self) -> DomainId {
self.id
}
fn domain_name(&self) -> &str {
&self.name
}
async fn domain_label(&self) -> String {
if let Some(ed) = self.resolve_exec_domain() {
match &ed.label {
Some(ValueOrFunc::Value(wezterm_dynamic::Value::String(s))) => s.to_string(),
Some(ValueOrFunc::Func(label_func)) => {
let label = config::with_lua_config_on_main_thread(|lua| async {
let lua = lua.ok_or_else(|| anyhow::anyhow!("missing lua context"))?;
let value = config::lua::emit_async_callback(
&*lua,
(label_func.clone(), (self.name.clone())),
)
.await?;
let label: String =
luahelper::from_lua_value_dynamic(value).with_context(|| {
format!(
"interpreting SpawnCommand result from ExecDomain {}",
ed.name
)
})?;
Ok(label)
})
.await;
match label {
Ok(label) => label,
Err(err) => {
log::error!(
"Error while calling label function for ExecDomain `{}`: {err:#}",
self.name
);
self.name.to_string()
}
}
}
_ => self.name.to_string(),
}
} else if let Some(wsl) = self.resolve_wsl_domain() {
wsl.distribution.unwrap_or_else(|| self.name.to_string())
} else {
self.name.to_string()
}
}
async fn attach(&self, _window_id: Option<WindowId>) -> anyhow::Result<()> {
Ok(())
}
fn detachable(&self) -> bool {
false
}
fn detach(&self) -> anyhow::Result<()> {
bail!("detach not implemented for LocalDomain");
}
fn state(&self) -> DomainState {
DomainState::Attached
}
}
/// A spawn could not use the directory its caller required because it could
/// not be opened. A concrete type: the GUI recovers it with `downcast_ref`
/// to decide between "show the recovery page" and "fail".
#[derive(Debug, Clone)]
pub struct RequiredCwdUnavailable {
pub dir: PathBuf,
pub kind: std::io::ErrorKind,
/// The raw OS error, for the user-visible detail line.
pub detail: String,
}
const REQUIRED_CWD_PHRASE: &str = " cannot be opened, and this command requires it: ";
const REQUIRED_CWD_TAG: &str = " [required-cwd:";
/// The message doubles as the wire form: a mux server reports errors as one
/// string, so the GUI recovers this type with `from_message` on that side.
impl std::fmt::Display for RequiredCwdUnavailable {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
let kind = match self.kind {
std::io::ErrorKind::PermissionDenied => "permission-denied",
std::io::ErrorKind::NotFound => "not-found",
_ => "other",
};
write!(
fmt,
"Directory {}{REQUIRED_CWD_PHRASE}{}{REQUIRED_CWD_TAG}{kind}]",
self.dir.display(),
self.detail
)
}
}
impl std::error::Error for RequiredCwdUnavailable {}
impl RequiredCwdUnavailable {
/// Recover the refusal from a message a server sent back, wherever the
/// server's own prefixes put it. `None` for any other message.
pub fn from_message(message: &str) -> Option<Self> {
let phrase_at = message.find(REQUIRED_CWD_PHRASE)?;
let dir_at = message[..phrase_at].rfind("Directory ")? + "Directory ".len();
let rest = &message[phrase_at + REQUIRED_CWD_PHRASE.len()..];
let tag_at = rest.rfind(REQUIRED_CWD_TAG)?;
let close = rest[tag_at + REQUIRED_CWD_TAG.len()..].find(']')?;
let kind = match &rest[tag_at + REQUIRED_CWD_TAG.len()..][..close] {
"permission-denied" => std::io::ErrorKind::PermissionDenied,
"not-found" => std::io::ErrorKind::NotFound,
_ => std::io::ErrorKind::Other,
};
Some(Self {
dir: PathBuf::from(&message[dir_at..phrase_at]),
kind,
detail: rest[..tag_at].to_string(),
})
}
}
/// Whether a cwd that could not be listed should fail the spawn outright.
/// Pulled out so the `sudo -i` regression is pinned by a test: making every
/// inherited PermissionDenied fatal would turn "split a pane out of a root
/// shell" into "will not open at all". Only a caller that named the
/// directory on purpose opts in.
fn required_cwd_error_is_fatal(require_cwd: bool) -> bool {
require_cwd
}
#[cfg(test)]
mod cwd_refusal_tests {
use super::{required_cwd_error_is_fatal, RequiredCwdUnavailable};
/// The `sudo -i` case, and every other pane that merely inherited its cwd:
/// a refusal still degrades to the home directory.
#[test]
fn an_inherited_cwd_still_falls_back_when_refused() {
assert!(!required_cwd_error_is_fatal(false));
}
/// A requested directory that is unavailable fails, so the caller can say so.
#[test]
fn every_requested_cwd_error_is_fatal() {
assert!(required_cwd_error_is_fatal(true));
}
/// The server sends its error as one string with its own prefixes; the
/// GUI must still get the directory and the kind back out of it.
#[test]
fn the_refusal_survives_a_trip_through_a_server_message() {
let refused = RequiredCwdUnavailable {
dir: std::path::PathBuf::from("/Users/x/Documents/a b: c"),
kind: std::io::ErrorKind::PermissionDenied,
detail: "Operation not permitted (os error 1)".to_string(),
};
let message = format!("Error: spawn: {refused}");
let back = RequiredCwdUnavailable::from_message(&message).expect("recovered");
assert_eq!(back.dir, refused.dir);
assert_eq!(back.kind, refused.kind);
assert_eq!(back.detail, refused.detail);
let missing = RequiredCwdUnavailable {
kind: std::io::ErrorKind::NotFound,
..refused
};
assert_eq!(
RequiredCwdUnavailable::from_message(&missing.to_string())
.map(|back| back.kind),
Some(std::io::ErrorKind::NotFound)
);
assert!(RequiredCwdUnavailable::from_message("Error: something else").is_none());
}
}