Skip to content

fix: preserve directory permissions during copy-ignored step - #1590

Merged
max-sixty merged 4 commits into
mainfrom
fix/issue-1589
Mar 18, 2026
Merged

max-sixty merged 4 commits into
mainfrom
fix/issue-1589

Conversation

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Problem

copy_dir_recursive_fallback() uses fs::create_dir_all() to create destination directories, which always uses default permissions (0755). This means source directory permissions are lost during the copy-ignored step.

For example, a directory with mode 0700 (used by tools like Postgres for data directories) becomes 0755 in the new worktree, breaking applications that require specific directory permissions.

Solution

After creating the destination directory, copy the source directory's permissions using fs::set_permissions() on Unix. This preserves the original permission mode for all directories copied recursively.

Testing

  • Added test_copy_ignored_preserves_directory_permissions — creates a directory with mode 0700, copies it via copy-ignored, and verifies the destination has the same mode
  • Updated test_copy_ignored_error_includes_path_directory — the source directory now also has restricted permissions so that the permission-preservation code maintains the error scenario
  • All 37 copy-ignored tests pass

Closes #1589 — automated triage

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Permissions must be set after copying contents, not before. If a source directory lacks write permission (e.g., 0o555 or 0o500 — common for read-only caches, vendor dirs), the current code sets the restrictive permissions on dest and then tries to copy files into it, which fails with EACCES.

Move the set_permissions block below the for entry in fs::read_dir(src)? loop.

The test only covers 0o700 (which includes write), so it doesn't catch this.

Comment thread src/commands/step_commands.rs Resolved
@max-sixty

Copy link
Copy Markdown
Owner

@worktrunk-bot fix tests please

worktrunk-bot and others added 4 commits March 17, 2026 23:11
`copy_dir_recursive_fallback()` used `fs::create_dir_all()` which creates
directories with default permissions (0755), ignoring the source directory's
permissions. This caused directories with restrictive permissions (e.g., 0700
for a Postgres data directory) to become world-readable in new worktrees.

Now copies source directory permissions to the destination after creation.

Closes #1589

Co-Authored-By: Claude <[email protected]>
If the source directory lacks write permission (e.g., 0o555), setting
permissions before copying makes the destination read-only and causes
file copies to fail with EACCES. Move set_permissions after the loop.

Also adds a test case for 0o555 directories and fixes the existing
error-path test to not depend on source permissions.

Co-Authored-By: Claude <[email protected]>
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Rebased onto main — the branch was 4 commits behind, which caused snapshot mismatches in 61 tests (mock commands, CI status, switch PR/MR tests). All 1204 integration tests pass locally after rebase. Pushed and waiting for CI.

@max-sixty

Copy link
Copy Markdown
Owner

OK — and is there a way to copy the paths while retaining permissions from the initial copy? I worry this is a patch on an underlying issue...

@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Good question. For files, reflink_or_copy already preserves permissions — no fixup needed there. The issue is only with directories, where fs::create_dir_all() always uses default permissions (0755) and there's no cross-platform way to specify permissions at creation time.

On Unix we could use DirBuilderExt::mode() to create directories with the source permissions upfront, but that actually breaks for restrictive directories — e.g., a 0o555 (read-only) directory would fail when we try to copy files into it. So the create-with-defaults → copy contents → set-final-permissions ordering is intentional (commit a6ed0d8 fixed exactly this after the first attempt set permissions too early).

The two-step approach is the standard pattern for recursive copies that preserve permissions — cp -a does the same thing internally (creates writable, copies, then fixes up). I don't think there's a cleaner alternative here.

@max-sixty
max-sixty merged commit eec328c into main Mar 18, 2026
22 checks passed
@max-sixty
max-sixty deleted the fix/issue-1589 branch March 18, 2026 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automated-fix Automated CI fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Directory permissions not preserved on copy ignored step

2 participants