Skip to content

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.

mux: let a spawn require its cwd instead of falling back to home

RoversX2843b76a0943
diff --git a/mux/src/domain.rs b/mux/src/domain.rs
index e471a9f3..0953f13b 100644
--- a/mux/src/domain.rs
+++ b/mux/src/domain.rs
@@ -272,6 +272,13 @@ impl LocalDomain {
&& !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()
@@ -489,7 +496,20 @@ impl LocalDomain {
// 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: {:#}",
@@ -840,3 +860,54 @@ impl Domain for LocalDomain {
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,
+}
+
+impl std::fmt::Display for RequiredCwdUnavailable {
+ fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
+ write!(
+ fmt,
+ "Directory {} cannot be opened, and this command requires it: {}",
+ self.dir.display(),
+ self.detail
+ )
+ }
+}
+
+impl std::error::Error for RequiredCwdUnavailable {}
+
+/// 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;
+
+ /// 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));
+ }
+}
diff --git a/pty/src/cmdbuilder.rs b/pty/src/cmdbuilder.rs
index 11a95cc7..72dd7ba3 100644
--- a/pty/src/cmdbuilder.rs
+++ b/pty/src/cmdbuilder.rs
@@ -207,6 +207,12 @@ pub struct CommandBuilder {
#[cfg(unix)]
pub(crate) umask: Option<libc::mode_t>,
controlling_tty: bool,
+ /// Whether `cwd` is a requirement or a preference. The default is a
+ /// preference: an inherited cwd (OSC 7, `sudo -i`) degrades to the home
+ /// directory. A spawner opening a *named* directory sets this so the
+ /// refusal becomes an error it can show instead of a silent `$HOME`.
+ #[cfg_attr(feature = "serde_support", serde(default))]
+ require_cwd: bool,
}
impl CommandBuilder {
@@ -220,6 +226,7 @@ impl CommandBuilder {
#[cfg(unix)]
umask: None,
controlling_tty: true,
+ require_cwd: false,
}
}
@@ -232,6 +239,7 @@ impl CommandBuilder {
#[cfg(unix)]
umask: None,
controlling_tty: true,
+ require_cwd: false,
}
}
@@ -249,6 +257,17 @@ impl CommandBuilder {
self.controlling_tty
}
+ /// Treat `cwd` as a requirement rather than a preference: a spawner that
+ /// was asked for a *named* directory would rather fail loudly than hand
+ /// back a shell somewhere else. See the field's documentation.
+ pub fn set_require_cwd(&mut self, require_cwd: bool) {
+ self.require_cwd = require_cwd;
+ }
+
+ pub fn get_require_cwd(&self) -> bool {
+ self.require_cwd
+ }
+
/// Create a new builder instance that will run some idea of a default
/// program. Such a builder will panic if `arg` is called on it.
pub fn new_default_prog() -> Self {
@@ -259,6 +278,7 @@ impl CommandBuilder {
#[cfg(unix)]
umask: None,
controlling_tty: true,
+ require_cwd: false,
}
}
@@ -816,6 +836,9 @@ mod tests {
/// of, and nothing else would tell you. When this breaks, either carry
/// the new field in thinkterm_proto::CommandSpec (and bump
/// CODEC_VERSION) or record here why it does not travel.
+ ///
+ /// `require_cwd` deliberately does not travel: it is local-domain policy,
+ /// and a remote server re-derives its own against its own filesystem.
#[test]
fn every_field_is_accounted_for_on_the_wire() {
#[cfg(unix)]
@@ -825,6 +848,7 @@ mod tests {
cwd: _,
umask: _,
controlling_tty: _,
+ require_cwd: _,
} = CommandBuilder::new("prog");
#[cfg(not(unix))]
let CommandBuilder {
@@ -832,9 +856,22 @@ mod tests {
envs: _,
cwd: _,
controlling_tty: _,
+ require_cwd: _,
} = CommandBuilder::new("prog");
}
+ /// The default stays lenient; this flag opts out one caller at a time.
+ #[test]
+ fn cwd_is_a_preference_unless_a_caller_says_otherwise() {
+ assert!(!CommandBuilder::new("prog").get_require_cwd());
+ assert!(!CommandBuilder::new_default_prog().get_require_cwd());
+ assert!(!CommandBuilder::from_argv(vec!["prog".into()]).get_require_cwd());
+
+ let mut cmd = CommandBuilder::new_default_prog();
+ cmd.set_require_cwd(true);
+ assert!(cmd.get_require_cwd());
+ }
+
#[cfg(unix)]
#[test]
fn test_cwd_relative() {