Skip to content

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

fix: correct list_worktrees() path for submodule main worktree (#1070)

Maximilian Roosa45fa7f03016
diff --git a/src/commands/config/show.rs b/src/commands/config/show.rs
index d71c5d5f2..b58a13927 100644
--- a/src/commands/config/show.rs
+++ b/src/commands/config/show.rs
@@ -490,8 +490,8 @@ fn render_project_config(out: &mut String) -> anyhow::Result<()> {
}
// Check for deprecations with show_brief_warning=false (silent mode)
- // Only show in main worktree (where .git is a directory)
- let is_main_worktree = repo_root.join(".git").is_dir();
+ // Only write migration file in main worktree, not linked worktrees
+ let is_main_worktree = !repo.current_worktree().is_linked().unwrap_or(true);
let has_deprecations = if let Ok(Some(info)) = worktrunk::config::check_and_migrate(
&config_path,
&contents,
diff --git a/src/config/project.rs b/src/config/project.rs
index 391f5b4ce..4d47c8b0e 100644
--- a/src/config/project.rs
+++ b/src/config/project.rs
@@ -134,10 +134,9 @@ impl ProjectConfig {
.map_err(|e| ConfigError::Message(format!("Failed to read config file: {}", e)))?;
// Check for deprecated template variables and create migration file if needed
- // Only write migration file in main worktree (where .git is a directory)
- // Linked worktrees have .git as a file pointing to the main worktree
+ // Only write migration file in main worktree, not linked worktrees
// Use show_brief_warning=true to emit a brief pointer to `wt config show`
- let is_main_worktree = repo_root.join(".git").is_dir();
+ let is_main_worktree = !repo.current_worktree().is_linked().unwrap_or(true);
let repo_for_hints = if write_hints { Some(repo) } else { None };
let _ = super::deprecation::check_and_migrate(
&config_path,
diff --git a/src/git/repository/worktrees.rs b/src/git/repository/worktrees.rs
index b446b83d1..93efbe356 100644
--- a/src/git/repository/worktrees.rs
+++ b/src/git/repository/worktrees.rs
@@ -22,7 +22,34 @@ impl Repository {
pub fn list_worktrees(&self) -> anyhow::Result<Vec<WorktreeInfo>> {
let stdout = self.run_command(&["worktree", "list", "--porcelain"])?;
let raw_worktrees = WorktreeInfo::parse_porcelain_list(&stdout)?;
- Ok(raw_worktrees.into_iter().filter(|wt| !wt.bare).collect())
+ let mut worktrees: Vec<_> = raw_worktrees.into_iter().filter(|wt| !wt.bare).collect();
+
+ // Submodule path correction.
+ //
+ // Git's `get_main_worktree()` computes the main worktree path by stripping
+ // a trailing `/.git` from the common dir. For submodules, the common dir is
+ // `.git/modules/sub` (no trailing `/.git`), so git leaves it unchanged —
+ // reporting the git data directory as the "main worktree" path. Git does not
+ // consult `core.worktree` in this code path.
+ //
+ // We detect this by checking whether the first worktree's path equals
+ // git_common_dir (which never holds for normal repos, where git_common_dir
+ // is `.git` inside the worktree). When matched, we correct it using
+ // repo_path(), which resolves the actual working directory via
+ // `git rev-parse --show-toplevel` (which does read core.worktree).
+ //
+ // We fix this here rather than at each call site because list_worktrees()
+ // is the single point where worktree paths enter the system — all consumers
+ // (worktree_for_branch, current_worktree_info, resolve_worktree, etc.)
+ // depend on paths being working directories. If git fixes this upstream,
+ // the condition stops triggering.
+ if let Some(first) = worktrees.first_mut()
+ && canonicalize(&first.path).ok().as_deref() == Some(self.git_common_dir())
+ {
+ first.path = self.repo_path().to_path_buf();
+ }
+
+ Ok(worktrees)
}
/// Get the WorktreeInfo struct for the current worktree, if we're inside one.
diff --git a/tests/integration_tests/repository.rs b/tests/integration_tests/repository.rs
index e3c06df5c..c538882ff 100644
--- a/tests/integration_tests/repository.rs
+++ b/tests/integration_tests/repository.rs
@@ -662,6 +662,35 @@ fn test_repo_path_in_submodule() {
"git_common_dir should be in parent's .git/modules/ for a submodule, got: {:?}",
git_common_dir
);
+
+ // Verify list_worktrees() returns corrected paths for submodule main worktree.
+ // Git's `worktree list` reports the main worktree as .git/modules/sub for submodules,
+ // which is wrong — it should be the actual working directory.
+ let worktrees = repository.list_worktrees().unwrap();
+ assert!(
+ !worktrees.is_empty(),
+ "list_worktrees() should return at least the main worktree"
+ );
+ let main_wt_path = dunce::canonicalize(&worktrees[0].path).unwrap();
+ assert_eq!(
+ main_wt_path, expected,
+ "list_worktrees()[0].path should be the submodule working directory, not .git/modules/sub"
+ );
+
+ // Verify worktree_for_branch() returns the corrected path (this is what `wt switch` uses)
+ let main_branch = worktrees[0]
+ .branch
+ .as_deref()
+ .expect("submodule main worktree should have a branch");
+ let found_path = repository
+ .worktree_for_branch(main_branch)
+ .unwrap()
+ .unwrap();
+ let found_canonical = dunce::canonicalize(&found_path).unwrap();
+ assert_eq!(
+ found_canonical, expected,
+ "worktree_for_branch() should return submodule working directory for default branch"
+ );
}
// =============================================================================