Skip to content

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

fix(shell): nushell uninstall cleans all candidate locations, dedup candidates (#1050)

Maximilian Roosf4ce6d18048c
diff --git a/src/commands/configure_shell.rs b/src/commands/configure_shell.rs
index d2f37a855..5c1bf6548 100644
--- a/src/commands/configure_shell.rs
+++ b/src/commands/configure_shell.rs
@@ -372,15 +372,10 @@ pub fn scan_shell_configs(
// Find the first existing config file
let target_path = paths.iter().find(|p| p.exists());
- // For Fish/Nushell, also check if the parent directory exists
+ // For Fish/Nushell, also check if any candidate's parent directory exists
// since we create the file there rather than modifying an existing one
let has_config_location = if matches!(shell, Shell::Fish | Shell::Nushell) {
- paths
- .first()
- .and_then(|p| p.parent())
- .map(|p| p.exists())
- .unwrap_or(false)
- || target_path.is_some()
+ paths.iter().any(|p| p.parent().is_some_and(|d| d.exists())) || target_path.is_some()
} else {
target_path.is_some()
};
@@ -957,6 +952,12 @@ pub fn handle_unconfigure_shell(
scan_for_uninstall(shell_filter, false, cmd)
}
+/// Remove a config file with a context-rich error message.
+fn remove_config_file(path: &std::path::Path) -> Result<(), String> {
+ fs::remove_file(path)
+ .map_err(|e| format!("Failed to remove {}: {e}", format_path_for_display(path)))
+}
+
fn scan_for_uninstall(
shell_filter: Option<Shell>,
dry_run: bool,
@@ -1004,13 +1005,7 @@ fn scan_for_uninstall(
superseded_by: None,
});
} else {
- fs::remove_file(fish_path).map_err(|e| {
- format!(
- "Failed to remove {}: {}",
- format_path_for_display(fish_path),
- e
- )
- })?;
+ remove_config_file(fish_path)?;
results.push(UninstallResult {
shell,
path: fish_path.clone(),
@@ -1041,12 +1036,7 @@ fn scan_for_uninstall(
superseded_by: canonical_path.clone(),
});
} else {
- fs::remove_file(&legacy_path).map_err(|e| {
- format!(
- "Failed to remove {}: {e}",
- format_path_for_display(&legacy_path)
- )
- })?;
+ remove_config_file(&legacy_path)?;
results.push(UninstallResult {
shell,
path: legacy_path,
@@ -1063,36 +1053,36 @@ fn scan_for_uninstall(
continue;
}
- // For Nushell, delete entire config file
+ // For Nushell, delete config files from all candidate locations.
+ // Installation might have written to a different path than what we'd pick now
+ // (e.g., `nu` was in PATH during install but not during uninstall).
if matches!(shell, Shell::Nushell) {
- if let Some(config_path) = paths.first() {
- if config_path.exists() {
- if dry_run {
- results.push(UninstallResult {
- shell,
- path: config_path.clone(),
- action: UninstallAction::WouldRemove,
- superseded_by: None,
- });
- } else {
- fs::remove_file(config_path).map_err(|e| {
- format!(
- "Failed to remove {}: {}",
- format_path_for_display(config_path),
- e
- )
- })?;
- results.push(UninstallResult {
- shell,
- path: config_path.clone(),
- action: UninstallAction::Removed,
- superseded_by: None,
- });
- }
+ let mut found_any = false;
+ for config_path in &paths {
+ if !config_path.exists() {
+ continue;
+ }
+ found_any = true;
+ if dry_run {
+ results.push(UninstallResult {
+ shell,
+ path: config_path.clone(),
+ action: UninstallAction::WouldRemove,
+ superseded_by: None,
+ });
} else {
- not_found.push((shell, config_path.clone()));
+ remove_config_file(config_path)?;
+ results.push(UninstallResult {
+ shell,
+ path: config_path.clone(),
+ action: UninstallAction::Removed,
+ superseded_by: None,
+ });
}
}
+ if !found_any && let Some(config_path) = paths.first() {
+ not_found.push((shell, config_path.clone()));
+ }
continue;
}
@@ -1141,13 +1131,7 @@ fn scan_for_uninstall(
action: UninstallAction::WouldRemove,
});
} else {
- fs::remove_file(&completion_path).map_err(|e| {
- format!(
- "Failed to remove {}: {}",
- format_path_for_display(&completion_path),
- e
- )
- })?;
+ remove_config_file(&completion_path)?;
completion_results.push(CompletionUninstallResult {
shell,
path: completion_path,
diff --git a/src/shell/paths.rs b/src/shell/paths.rs
index 6c70cd9f0..bf88e6ab5 100644
--- a/src/shell/paths.rs
+++ b/src/shell/paths.rs
@@ -82,11 +82,6 @@ fn nushell_config_candidates(home: &std::path::Path) -> Vec<PathBuf> {
// Fallbacks for when nu query fails at runtime but succeeded during install:
- // etcetera's platform config dir (matches nushell_config_dir fallback)
- if let Ok(strategy) = choose_base_strategy() {
- candidates.push(strategy.config_dir().join("nushell"));
- }
-
// XDG_CONFIG_HOME/nushell if set
if let Ok(xdg_config) = std::env::var("XDG_CONFIG_HOME") {
candidates.push(PathBuf::from(xdg_config).join("nushell"));
@@ -105,6 +100,10 @@ fn nushell_config_candidates(home: &std::path::Path) -> Vec<PathBuf> {
);
}
+ // Deduplicate while preserving priority order (queried path first)
+ let mut seen = std::collections::HashSet::new();
+ candidates.retain(|p| seen.insert(p.clone()));
+
candidates
}
@@ -322,15 +321,34 @@ mod tests {
}
#[test]
- fn test_nushell_config_candidates_returns_at_least_two() {
+ fn test_nushell_config_candidates_always_has_fallback() {
let home = PathBuf::from("/home/user");
let candidates = nushell_config_candidates(&home);
- // Even without `nu` in PATH, we should get fallback candidates
- // (etcetera config dir + ~/.config/nushell, plus macOS path on macOS)
+ // Even without `nu` in PATH, we should get at least one fallback
+ assert!(
+ !candidates.is_empty(),
+ "Should return at least 1 candidate path, got: {candidates:?}"
+ );
+
+ // On macOS, should have at least 2 (XDG default + Library/Application Support)
+ #[cfg(target_os = "macos")]
assert!(
candidates.len() >= 2,
- "Should return at least 2 candidate paths, got: {candidates:?}"
+ "macOS should have at least 2 candidates, got: {candidates:?}"
+ );
+ }
+
+ #[test]
+ fn test_nushell_config_candidates_no_duplicates() {
+ let home = PathBuf::from("/home/user");
+ let candidates = nushell_config_candidates(&home);
+
+ let unique: std::collections::HashSet<_> = candidates.iter().collect();
+ assert_eq!(
+ candidates.len(),
+ unique.len(),
+ "Candidates should not contain duplicates: {candidates:?}"
);
}
diff --git a/tests/integration_tests/configure_shell.rs b/tests/integration_tests/configure_shell.rs
index 508d6c8d9..1ffa1a62d 100644
--- a/tests/integration_tests/configure_shell.rs
+++ b/tests/integration_tests/configure_shell.rs
@@ -1756,6 +1756,85 @@ fn test_uninstall_shell_nushell(repo: TestRepo, temp_home: TempDir) {
);
}
+/// Test that nushell uninstall cleans up config files at all candidate locations.
+///
+/// Exercises the fix where uninstall iterates all nushell config candidates,
+/// not just the first. Simulates the scenario where the file was installed at
+/// a location that is no longer the primary candidate (e.g., `nu` reported
+/// a different path during install than what we'd pick now).
+#[rstest]
+fn test_uninstall_nushell_cleans_all_candidate_locations(repo: TestRepo, temp_home: TempDir) {
+ let home = std::fs::canonicalize(temp_home.path()).unwrap();
+
+ // Install nushell integration normally (goes to XDG_CONFIG_HOME/nushell)
+ let mut install_cmd = wt_command();
+ repo.configure_wt_cmd(&mut install_cmd);
+ set_temp_home_env(&mut install_cmd, temp_home.path());
+ install_cmd.env("SHELL", "/bin/nu");
+ install_cmd
+ .args(["config", "shell", "install", "nu", "--yes"])
+ .current_dir(repo.root_path());
+
+ let install_output = install_cmd.output().expect("Failed to execute install");
+ assert!(
+ install_output.status.success(),
+ "Install should succeed:\nstderr: {}",
+ String::from_utf8_lossy(&install_output.stderr)
+ );
+
+ let primary_config = home
+ .join(".config")
+ .join("nushell")
+ .join("vendor")
+ .join("autoload")
+ .join("wt.nu");
+ assert!(primary_config.exists(), "Primary config should exist");
+
+ // Copy the config to a secondary candidate location (~/.config is the XDG default,
+ // but also manually create one at a different path to simulate install at a
+ // non-primary location). Use a custom XDG_CONFIG_HOME to make a second candidate
+ // be the primary during uninstall.
+ let secondary_dir = home
+ .join("custom-config")
+ .join("nushell")
+ .join("vendor")
+ .join("autoload");
+ fs::create_dir_all(&secondary_dir).unwrap();
+ let secondary_config = secondary_dir.join("wt.nu");
+ fs::copy(&primary_config, &secondary_config).unwrap();
+
+ // Uninstall with XDG_CONFIG_HOME pointing to the custom location.
+ // The custom path becomes the first candidate, but the original at ~/.config/nushell
+ // should also be cleaned up since uninstall checks all candidates.
+ let mut cmd = wt_command();
+ repo.configure_wt_cmd(&mut cmd);
+ // Override XDG_CONFIG_HOME to point to custom dir, making it the primary candidate
+ cmd.env("HOME", &home);
+ cmd.env("USERPROFILE", &home);
+ cmd.env("XDG_CONFIG_HOME", home.join("custom-config"));
+ cmd.env("APPDATA", home.join("custom-config"));
+ cmd.env("SHELL", "/bin/nu");
+ cmd.args(["config", "shell", "uninstall", "nu", "--yes"])
+ .current_dir(repo.root_path());
+
+ let output = cmd.output().expect("Failed to execute uninstall");
+ assert!(
+ output.status.success(),
+ "Uninstall should succeed:\nstderr: {}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+
+ // Both locations should be cleaned up
+ assert!(
+ !primary_config.exists(),
+ "Primary config at ~/.config/nushell should be deleted: {primary_config:?}"
+ );
+ assert!(
+ !secondary_config.exists(),
+ "Secondary config at custom XDG path should be deleted: {secondary_config:?}"
+ );
+}
+
/// Test that WORKTRUNK_TEST_POWERSHELL_ENV=1 triggers PowerShell auto-detection.
/// This simulates the Windows behavior where we detect PowerShell when SHELL is not set.
#[rstest]