Skip to content

fix(install): add os.path.samefile() guard to prevent copytree same-file crash#4297

Merged
rysweet merged 2 commits intomainfrom
fix/samefile-guard-4296
Apr 17, 2026
Merged

fix(install): add os.path.samefile() guard to prevent copytree same-file crash#4297
rysweet merged 2 commits intomainfrom
fix/samefile-guard-4296

Conversation

@rysweet
Copy link
Copy Markdown
Owner

@rysweet rysweet commented Apr 10, 2026

Summary

When AMPLIHACK_HOME points at the source tree itself, copytree_manifest() calls shutil.copytree() with source_dir == target_dir, causing a SameFileError crash.

Changes

  • src/amplihack/install.py: Added os.path.samefile() guard inside the copytree_manifest() for-loop, before the shutil.copytree() call. When source and target resolve to the same directory, the copy is skipped with a warning message.
  • tests/unit/test_copytree_samefile.py: 3 regression tests covering same-dir, symlink-same-dir, and normal-copy scenarios.

Testing

pytest tests/unit/test_copytree_samefile.py -v  # 3 passed

Fixes #4296

…ile crash

When AMPLIHACK_HOME points at the source tree itself, copytree_manifest
would call shutil.copytree with source_dir == target_dir, causing a
SameFileError crash. Add a guard using os.path.samefile() that detects
this condition and skips the copy with a warning message.

Fixes #4296

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown
Contributor

Repo Guardian - Passed

All changed files in this PR are durable, production-quality additions:

File Assessment
src/amplihack/install.py ✅ Bug fix — adds os.path.samefile() guard to prevent shutil.SameFileError. Permanent tooling code.
tests/unit/test_copytree_samefile.py ✅ Unit tests for the same-file guard. Well-structured, parameterized, no ephemeral content.

No ephemeral content, temporary scripts, or point-in-time documents detected.

Generated by Repo Guardian for issue #4297 ·

Documents the os.path.samefile() guard, symptoms, root cause, and
verification steps. Links to both Python and Rust PRs.

Refs: #4296

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown
Contributor

Repo Guardian - Passed

All changed files in this PR are durable, production-quality additions:

File Assessment
docs/troubleshooting/README.md ✅ Documentation index update — adds links to new troubleshooting guides. Durable.
docs/troubleshooting/copytree-same-file-crash.md ✅ Formal troubleshooting guide with Symptoms, Root Cause, Fix, and Verification sections. Structured for future users encountering the same issue. Not ephemeral.
src/amplihack/install.py ✅ Bug fix — adds os.path.samefile() guard to prevent shutil.SameFileError. Permanent tooling code.
tests/unit/test_copytree_samefile.py ✅ Unit tests for the same-file guard. Well-structured regression tests. Permanent.

No ephemeral content, temporary scripts, or point-in-time documents detected.

Generated by Repo Guardian for issue #4297 ·

@rysweet
Copy link
Copy Markdown
Owner Author

rysweet commented Apr 17, 2026

Review complete — fix is correct and merge-ready.

  • os.path.samefile() guard in copytree_manifest() properly prevents SameFileError when source/staging overlap
  • Tests cover: same-dir, symlink-same-dir, and normal-copy (3/3 passing locally)
  • CI: 22 passing, 1 failing (Code Atlas — unrelated), 7 skipped
  • cli.py:883 already has its own os.path.realpath() guard for the direct shutil.copytree call
  • Docs and troubleshooting guide are thorough

@rysweet rysweet merged commit 4960494 into main Apr 17, 2026
29 of 30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: copytree_manifest crashes with 'are the same file' when source and staging overlap

1 participant