fix: prevent file deletion during self-linking in link_or_copy
Metadata
Current evaluation
Merged fix for link_or_copy preventing accidental source deletion during self-linking. Added a samefile check to safely skip identical paths, resolving #6168. Approved and merged after CI validation.
Suggested action: —
No scores available.
Issue body
## Description
This PR fixes an issue where the `organize` step fails if a staged symlink points back to its own source directory structure.
Previously, in `link_or_copy`, if `source` and `destination` resolved to the same physical file (e.g., via a symlink), catching the `EEXIST` error and unlinking the destination resulted in the unintended deletion of the source file itself, leading to a `FileNotFoundError`.
### The Fix
Introduced a `samefile()` check in `craft_parts/utils/file_utils.py` to return early and safely ignore these self-linking collisions without crashing or deleting the source:
Fixes https://github.com/canonical/snapcraft/issues/6168
```python
if source.exists() and destination.exists() and source.samefile(destination):
return
```
- [x] Have you followed the guidelines for contributing?
- [x] Have you signed the [CLA](http://www.ubuntu.com/legal/contributors/)?
- [x] Have you successfully run `make lint && make test`?
- [x] Have you added an entry to the changelog (`docs/reference/changelog.rst`)?
Evaluation history
| Date | Model | Scores | Action | Summary |
|---|---|---|---|---|
| qwen/qwen3.6-35b-a3b | — | — | Merged fix for link_or_copy preventing accidental source deletion during self-linking. Added a samefile check to safely skip identical paths, resolving #6168. Approved and merged after CI validation. | |
| qwen/qwen3.6-35b-a3b |
Staleness:
5
Complexity:
15
Confidence:
90
|
needs review | Fixes a bug in link_or_copy that deleted source files during self-linking collisions by adding a samefile() check. PR has 2 approvals, 0 unresolved comments, and passing CI excluding flaky infra failures. Awaiting CI re-run. | |
| qwen/qwen3.6-35b-a3b |
Staleness:
40
Complexity:
15
Confidence:
90
|
needs review | Fixes file deletion bug during self-linking collisions by adding a samefile() check. Approved by two reviewers, currently awaiting CI re-run for unrelated flaky failures. | |
| qwen/qwen3.6-35b-a3b |
Staleness:
5
Complexity:
15
Confidence:
90
|
needs review | Fixes file deletion during self-linking collisions by adding a samefile() check in link_or_copy. Approved by one reviewer, awaiting CI re-run for unrelated flaky failures. | |
| qwen/qwen3.6-35b-a3b |
Staleness:
15
Complexity:
15
Confidence:
90
|
needs review | Fixes a bug where link_or_copy deletes source files during self-linking collisions by adding a samefile() check. PR has 1 approval, 0 unresolved comments, and awaits CI workflow approval and maintainer review. | |
| qwen/qwen3.6-35b-a3b |
Staleness:
10
Complexity:
15
Confidence:
85
|
needs review | Fixes a bug where self-linking collisions in link_or_copy caused unintended source file deletion. Adds a samefile() check to safely ignore collisions. Approved by one reviewer, awaiting CI approval and final merge. | |
| qwen/qwen3.6-35b-a3b |
Staleness:
60
Complexity:
15
Confidence:
85
|
needs review | Adds a samefile() check to link_or_copy to prevent accidental source file deletion during self-linking collisions. Currently awaiting maintainer review and CI workflow approval. | |
| qwen/qwen3.6-35b-a3b |
Staleness:
20
Complexity:
15
Confidence:
90
|
needs review | Fixes a file deletion bug in link_or_copy when handling self-linking symlinks by adding a samefile check. Awaiting maintainer review and CI approval. | |
| qwen3.6-35b-a3b-mtp-q6 |
Staleness:
5
Complexity:
15
Confidence:
90
|
needs review | Fixes file deletion bug in link_or_copy when source and destination resolve to the same file via symlink. Adds samefile() check to prevent accidental unlinking. Currently awaiting maintainer review and CI approval. | |
| qwen3.6-35b-a3b-mtp-q6 |
Staleness:
5
Complexity:
15
Confidence:
90
|
needs review | Adds a samefile check to link_or_copy to prevent accidental source deletion when staging symlinks pointing to their own source. Fixes #6168. Awaits maintainer review. |
Update history
| Date | Change |
|---|---|
| updated | |
| updated | |
| updated | |
| updated | |
| updated | |
| updated | |
| updated | |
| updated | |
| updated | |
| updated | |
| updated |
Related issues
| Issue | Project | State | Summary | Similarity |
|---|---|---|---|---|
| #3497 repo: fix regression in fix_symlink() | snapcraft | merged | Merged a fix for a regression in fix_symlink() where copied files retained stale symlinks. The update ensures symlinks correctly point to new files and adds spread tests for relative and host copy scenarios. Approved by two reviewers and passed CI. |