Skip to content

fix: handle broken symlinks in shared lib hard_link step#159

Merged
eugenehp merged 1 commit intoeugenehp:mainfrom
mihailinl:fix/hard-link-broken-symlinks
Apr 3, 2026
Merged

fix: handle broken symlinks in shared lib hard_link step#159
eugenehp merged 1 commit intoeugenehp:mainfrom
mihailinl:fix/hard-link-broken-symlinks

Conversation

@mihailinl
Copy link
Copy Markdown
Contributor

Problem

On Linux, cmake creates versioned symlink chains for shared libraries:

libllama.so -> libllama.so.0 -> libllama.so.0.0.445

When the library version changes between builds, old symlinks in target/debug/ become broken (dangling). The current code checks if !dst.exists() before calling hard_link(), but Path::exists() returns false for broken symlinks — even though the directory entry still exists. hard_link() then fails with EEXIST, causing a panic on rebuild.

This affects any Linux user with the dynamic-link feature enabled.

Fix

Use symlink_metadata() instead of exists() to detect any existing entry (including broken symlinks), and remove it before creating the hard link. Extracted into a force_hard_link closure to avoid duplication across the three copy sites.

Testing

Tested on Arch Linux (CachyOS) with:

  • cargo build (clean) — works
  • cargo build (rebuild, symlinks present) — works (previously panicked)
  • Static build (no dynamic-link feature) — unaffected, code path not reached

On Linux, cmake creates versioned symlink chains for shared libraries
(e.g. libllama.so -> libllama.so.0 -> libllama.so.0.0.445). When the
library version changes between builds, old symlinks become broken.

Path::exists() returns false for broken symlinks, but hard_link() fails
with EEXIST because the directory entry still exists. This causes panics
on rebuild when using dynamic-link feature on Linux.

Fix: use symlink_metadata() to detect any existing entry (including
broken symlinks) and remove it before creating the hard link.
@eugenehp eugenehp merged commit c92f878 into eugenehp:main Apr 3, 2026
4 checks passed
@eugenehp
Copy link
Copy Markdown
Owner

eugenehp commented Apr 3, 2026

Good catch, been testing this over the CI mostly. Going to add to the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants