This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Create symlinks relative to cwd
Type: behavior Stage: resolved
Components: Documentation, Library (Lib) Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: SilentGhost, docs@python, serhiy.storchaka, skeo
Priority: normal Keywords:

Created on 2019-05-23 07:09 by skeo, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg343277 - (view) Author: Shannon (skeo) Date: 2019-05-23 07:09
when using pathlib objects to define src and dst for os.symlink (also relevant for Path(dst).symlink_to(src)), if the src path is not absolute, or relative to the directory the link is being created in, a broken link will be created.

example/

src = pathlib.Path('dir1/file')
dst = pathlib.Path('dir2/file')
os.symlink(src, dst) # or dst.symlink_to(src)

this will create a broken link in dir2, attempting to link to dir1/file, relative to dir2.

It seems to me, if src given is a pathlib object (relative to cwd), the linking process should be smart enough to point the link the created symlink to the right place.

os.symlink(src.absolute(), dst) works, but creates an absolute symlink which may not be desired behaviour.

My current workaround is:
os.symlink(os.path.relpath(src, dst.parent), dst)
which creates a working relative symlink as desired. I would suggest this should be the default behaviour of both os.symlink and pathlib.Path().symlink_to when a non-absolute path object is given as src.

Interestingly, src.relative_to(dst.parent) will raise a ValueError while os.path.relpath(src, dst.parent) correctly returns '../dir1/file'.
I also think Path().relative_to should be changed to match the behaviour of os.path.relpath here, but perhaps that is a separate issue.
msg343278 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2019-05-23 07:35
This would be backward incompatible change. I just wanted to point out that the symlink was only broken because the file did not exist, and one might want to legitimately create a link relative to the target's location. I'd imagine creating a link in the same directory is one such example. I think the issue you're raising can be addressed in documentation, by making clear that the relative link will be relative to target. This is already done in the given example for symlink_to, but could be improved using a target in subdirectory and a clarifying note.

The relative_to issue does indeed deserve a separate issue (if there isn't one already).
msg343337 - (view) Author: Shannon (skeo) Date: 2019-05-24 02:32
I can see how this change would be backward incompatible, however the current behaviour seems inconsistent with the way pathlib functions otherwise. Within two lines of code, the same path object can be pointing to two completely different locations simply because it's being used as the src argument to create a symlimk. That's counter to my understanding of the whole point of pathlib.

With regard to relative_to, I've found this issue, which doesn't seem to have been touched since 2016. https://bugs.python.org/issue20012
I'm new here, what's the best way to bump an issue?
msg343353 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2019-05-24 08:17
Shannon, what you're suggesting is prone to the same issue (creating a broken symlink) if a given destination does not exist relative to cwd. I think your current solution is better as it explicitly provides the paths, but it's not generalised enough to warrant a change in behaviour.

If you have anything new to add or would like to add a PR, just post on that issue. As you note, the core developer stated that the current behaviour is by design.
msg343355 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-05-24 08:41
Yes, the current behavior is by design. It is consistent with the os.symlink(), the "ln -s" command, and with any other way of creating symbolic links in other programming languages.
History
Date User Action Args
2022-04-11 14:59:15adminsetgithub: 81200
2019-05-24 08:41:42serhiy.storchakasetstatus: open -> closed
resolution: not a bug
messages: + msg343355

stage: resolved
2019-05-24 08:17:25SilentGhostsetmessages: + msg343353
2019-05-24 02:32:15skeosetmessages: + msg343337
2019-05-23 07:35:01SilentGhostsetassignee: docs@python

components: + Documentation
title: using pathlib objects to create symlinks produces broken links -> Create symlinks relative to cwd
nosy: + serhiy.storchaka, docs@python, SilentGhost
versions: + Python 3.8
messages: + msg343278
2019-05-23 07:09:02skeocreate