Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix(shell): auto-detect nushell for install even without vendor/autoload (#1032)

Arnaud Limbourgdba80e362dd3
diff --git a/src/commands/configure_shell.rs b/src/commands/configure_shell.rs
index 27e6bd9c8..64b31940a 100644
--- a/src/commands/configure_shell.rs
+++ b/src/commands/configure_shell.rs
@@ -324,6 +324,21 @@ fn should_auto_configure_powershell() -> bool {
}
}
+/// Check if nushell is available on the system.
+///
+/// Nushell's `vendor/autoload` directory may not exist even when nushell is installed,
+/// since it was introduced in nushell v0.96.0 and isn't always created by default.
+/// When `nu` is in PATH, we should auto-configure nushell (creating vendor/autoload/
+/// if needed) rather than silently skipping it.
+fn is_nushell_available() -> bool {
+ // Allow tests to override detection (set via Command::env() in integration tests)
+ if let Ok(val) = std::env::var("WORKTRUNK_TEST_NUSHELL_ENV") {
+ return val == "1";
+ }
+
+ which::which("nu").is_ok()
+}
+
pub fn scan_shell_configs(
shell_filter: Option<Shell>,
dry_run: bool,
@@ -340,6 +355,10 @@ pub fn scan_shell_configs(
default_shells.push(Shell::PowerShell);
}
+ // Check if nushell is available on the system (nu binary in PATH).
+ // vendor/autoload/ may not exist yet, but we should still install if nu is available.
+ let nushell_available = is_nushell_available();
+
let shells = shell_filter.map_or(default_shells, |shell| vec![shell]);
let mut results = Vec::new();
@@ -366,13 +385,12 @@ pub fn scan_shell_configs(
target_path.is_some()
};
- // Auto-configure PowerShell when user is in a PowerShell-compatible environment,
- // even if the profile doesn't exist yet (issue #885). PowerShell doesn't create
- // a profile by default, so most users won't have one until they create it.
- // Detection:
- // - Non-Windows: PSModulePath indicates PowerShell Core
- // - Windows: SHELL not set indicates Windows-native shell (not Git Bash/MSYS2)
- let in_detected_shell = matches!(shell, Shell::PowerShell) && in_powershell_env;
+ // Auto-configure shells when we detect them on the system, even if their
+ // config directory doesn't exist yet:
+ // - PowerShell: profile may not exist (issue #885)
+ // - Nushell: vendor/autoload/ may not exist (introduced in nushell v0.96.0)
+ let in_detected_shell = (matches!(shell, Shell::PowerShell) && in_powershell_env)
+ || (matches!(shell, Shell::Nushell) && nushell_available);
// Only configure if explicitly targeting this shell OR if config file/location exists
// OR if we detected we're running in this shell's environment
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index e0dd21edf..f4c2b4677 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -695,6 +695,8 @@ pub fn configure_cli_command(cmd: &mut Command) {
cmd.env_remove("PSModulePath");
// Disable auto PowerShell detection (tests that need it should set to "1")
cmd.env("WORKTRUNK_TEST_POWERSHELL_ENV", "0");
+ // Disable auto nushell detection (tests that need it should set to "1")
+ cmd.env("WORKTRUNK_TEST_NUSHELL_ENV", "0");
cmd.env("WORKTRUNK_TEST_EPOCH", TEST_EPOCH.to_string());
// Enable warn-level logging so diagnostics show up in test failures
cmd.env("RUST_LOG", "warn");
diff --git a/tests/common/pty.rs b/tests/common/pty.rs
index f4dc7eff9..ba36b5b21 100644
--- a/tests/common/pty.rs
+++ b/tests/common/pty.rs
@@ -261,6 +261,8 @@ fn exec_in_pty_impl(
// Windows: the `home` crate uses USERPROFILE for home_dir()
#[cfg(windows)]
cmd.env("USERPROFILE", home.to_string_lossy().to_string());
+ // Suppress nushell auto-detection for deterministic PTY tests
+ cmd.env("WORKTRUNK_TEST_NUSHELL_ENV", "0");
}
let mut child = pair.slave.spawn_command(cmd).unwrap();
diff --git a/tests/integration_tests/configure_shell.rs b/tests/integration_tests/configure_shell.rs
index 67c99871c..1275856d5 100644
--- a/tests/integration_tests/configure_shell.rs
+++ b/tests/integration_tests/configure_shell.rs
@@ -1564,6 +1564,7 @@ mod pty_tests {
configure_pty_command(&mut cmd);
cmd.env("HOME", temp_home.path());
cmd.env("XDG_CONFIG_HOME", temp_home.path().join(".config"));
+ cmd.env("WORKTRUNK_TEST_NUSHELL_ENV", "0");
cmd.env("SHELL", "/bin/zsh");
// Skip the compinit probe and force the advisory to appear. The probe spawns
// `zsh -ic` which triggers global zshrc configs that can produce "insecure
@@ -1811,3 +1812,67 @@ fn test_powershell_env_detection(repo: TestRepo, temp_home: TempDir) {
content
);
}
+
+/// Test that nushell gets auto-configured when detected, even without vendor/autoload dir.
+///
+/// Parallels test_powershell_env_detection: when nushell is detected on the system,
+/// `wt config shell install` should create vendor/autoload/ and install the wrapper,
+/// rather than skipping with "vendor/autoload not found".
+#[rstest]
+fn test_nushell_auto_detection_creates_vendor_autoload(repo: TestRepo, temp_home: TempDir) {
+ // Don't create vendor/autoload - the whole point is that it doesn't exist yet
+ // but nushell IS detected on the system
+
+ let mut cmd = wt_command();
+ repo.configure_wt_cmd(&mut cmd);
+ set_temp_home_env(&mut cmd, temp_home.path());
+ // Force nushell detection via test env var (parallels WORKTRUNK_TEST_POWERSHELL_ENV)
+ cmd.env("WORKTRUNK_TEST_NUSHELL_ENV", "1");
+ cmd.env("SHELL", "/bin/zsh");
+ cmd.arg("config")
+ .arg("shell")
+ .arg("install")
+ .arg("--yes")
+ .current_dir(repo.root_path());
+
+ let output = cmd.output().expect("Failed to execute command");
+ assert!(
+ output.status.success(),
+ "Command should succeed:\nstderr: {}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ // Nushell should be configured, not skipped
+ assert!(
+ stderr.contains("Created shell extension for") && stderr.contains("nu"),
+ "Nushell should be auto-configured when detected:\n{}",
+ stderr
+ );
+ assert!(
+ !stderr.contains("Skipped nu"),
+ "Nushell should not be skipped when detected:\n{}",
+ stderr
+ );
+
+ // Verify the nushell wrapper was created with vendor/autoload/ directory
+ let home = std::fs::canonicalize(temp_home.path()).unwrap();
+ let nu_config = home
+ .join(".config")
+ .join("nushell")
+ .join("vendor")
+ .join("autoload")
+ .join("wt.nu");
+ assert!(
+ nu_config.exists(),
+ "wt.nu should be created at {:?}",
+ nu_config
+ );
+
+ let content = fs::read_to_string(&nu_config).unwrap();
+ assert!(
+ content.contains("def --env --wrapped wt"),
+ "Should contain nushell function definition: {}",
+ content
+ );
+}