Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: skip broken symlinks at destination in copy-ignored (#1549)
worktrunk-bot5e5dec4d8c08
diff --git a/src/commands/step_commands.rs b/src/commands/step_commands.rsindex 89073d2de..e441d5edd 100644--- a/src/commands/step_commands.rs+++ b/src/commands/step_commands.rs@@ -751,14 +751,19 @@ pub fn step_copy_ignored(copied_count += 1;}} else {- // Skip existing files for idempotent hook usage- match reflink_copy::reflink_or_copy(src_entry, &dest_entry) {- Ok(_) => copied_count += 1,- Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {}- Err(e) => {- return Err(- anyhow::Error::from(e).context(format!("copying {display_path}"))- );+ // Skip existing entries (files or symlinks) for idempotent hook usage.+ // Check symlink_metadata (not exists()) because exists() follows symlinks+ // and returns false for broken ones, which would cause reflink_or_copy to+ // fail with ENOENT on some platforms when copying through the broken symlink.+ if dest_entry.symlink_metadata().is_err() {+ match reflink_copy::reflink_or_copy(src_entry, &dest_entry) {+ Ok(_) => copied_count += 1,+ Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {}+ Err(e) => {+ return Err(+ anyhow::Error::from(e).context(format!("copying {display_path}"))+ );+ }}}}@@ -885,14 +890,18 @@ fn copy_dir_recursive_fallback(src: &Path, dest: &Path, force: bool) -> anyhow::if force {remove_if_exists(&dest_path)?;}- // Skip existing files for idempotent hook usage- match reflink_copy::reflink_or_copy(&src_path, &dest_path) {- Ok(_) => {}- Err(e) if e.kind() == ErrorKind::AlreadyExists => {}- Err(e) => {- return Err(- anyhow::Error::from(e).context(format!("copying {}", src_path.display()))- );+ // Skip existing entries (files or symlinks) for idempotent hook usage.+ // Check symlink_metadata (not exists()) because exists() follows symlinks+ // and returns false for broken ones, which would cause reflink_or_copy to+ // fail with ENOENT on some platforms when copying through the broken symlink.+ if dest_path.symlink_metadata().is_err() {+ match reflink_copy::reflink_or_copy(&src_path, &dest_path) {+ Ok(_) => {}+ Err(e) if e.kind() == ErrorKind::AlreadyExists => {}+ Err(e) => {+ return Err(anyhow::Error::from(e)+ .context(format!("copying {}", src_path.display())));+ }}}}diff --git a/tests/integration_tests/step_copy_ignored.rs b/tests/integration_tests/step_copy_ignored.rsindex 315b120b5..dd27319a7 100644--- a/tests/integration_tests/step_copy_ignored.rs+++ b/tests/integration_tests/step_copy_ignored.rs@@ -1015,6 +1015,73 @@ fn test_copy_ignored_error_includes_path_file(mut repo: TestRepo) {);}+/// Test that broken symlinks at the destination don't prevent copying regular files (GitHub #1547)+///+/// When copy-ignored runs and the destination already has a broken symlink where a+/// regular file would be copied, reflink_or_copy follows the symlink and gets ENOENT+/// instead of EEXIST. This should be treated as "already exists" (skip), not an error.+#[cfg(unix)]+#[rstest]+fn test_copy_ignored_broken_symlink_at_dest_for_regular_file(mut repo: TestRepo) {+ let feature_path = repo.add_worktree("feature");++ // Create a regular file in main that's gitignored+ fs::write(repo.root_path().join(".env"), "SECRET=value").unwrap();+ fs::write(repo.root_path().join(".gitignore"), ".env\n").unwrap();++ // Create a broken symlink at the destination where the file would be copied.+ // The target path has a non-existent parent dir so fs::copy fails with ENOENT.+ std::os::unix::fs::symlink("/nonexistent/path/file", feature_path.join(".env")).unwrap();++ // Verify it's a broken symlink (symlink_metadata succeeds, but exists() returns false)+ assert!(feature_path.join(".env").symlink_metadata().is_ok());+ assert!(!feature_path.join(".env").exists());++ // copy-ignored should succeed, skipping the broken symlink+ let output = repo+ .wt_command()+ .args(["step", "copy-ignored"])+ .current_dir(&feature_path)+ .output()+ .unwrap();+ assert!(+ output.status.success(),+ "copy-ignored should succeed when destination has broken symlink: {}",+ String::from_utf8_lossy(&output.stderr)+ );+}++/// Test that broken symlinks inside directories don't prevent copying (GitHub #1547)+#[cfg(unix)]+#[rstest]+fn test_copy_ignored_broken_symlink_in_dir_at_dest(mut repo: TestRepo) {+ let feature_path = repo.add_worktree("feature");++ // Create target directory with a regular file in main+ let target_dir = repo.root_path().join("target");+ fs::create_dir_all(&target_dir).unwrap();+ fs::write(target_dir.join("data.txt"), "content").unwrap();+ fs::write(repo.root_path().join(".gitignore"), "target/\n").unwrap();++ // Create destination target dir with a broken symlink where data.txt would go+ let dest_target = feature_path.join("target");+ fs::create_dir_all(&dest_target).unwrap();+ std::os::unix::fs::symlink("/nonexistent/path/file", dest_target.join("data.txt")).unwrap();++ // copy-ignored should succeed, skipping the broken symlink+ let output = repo+ .wt_command()+ .args(["step", "copy-ignored"])+ .current_dir(&feature_path)+ .output()+ .unwrap();+ assert!(+ output.status.success(),+ "copy-ignored should succeed when dir entry has broken symlink at dest: {}",+ String::from_utf8_lossy(&output.stderr)+ );+}+/// Test that VCS metadata directories are excluded from copy-ignored (GitHub issue #1249)////// VCS metadata directories like `.jj` (Jujutsu), `.hg` (Mercurial) contain internal