theskumar / theskumar/python-dotenv

dotenv-cli - rewrite() does not resolve symlinks

Open
#541 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
8.9k
Forks
581
PR merge metrics
No merged PRs in 30d

Description

While having a play around w/ dotenv today, I noticed that if my .env file was symlinked elsewhere, using the CLI utility via dotenv set or dotenv unset would not work as I expected it to, so I made this small change to the rewrite function:

diff --git a/src/dotenv/main.py b/src/dotenv/main.py
index 052de05..3606abe 100644
--- a/src/dotenv/main.py
+++ b/src/dotenv/main.py
@@ -132,8 +132,12 @@ def rewrite(
     path: StrPath,
     encoding: Optional[str],
 ) -> Iterator[Tuple[IO[str], IO[str]]]:
-    pathlib.Path(path).touch()
+    path = pathlib.Path(path)

+    if path.is_symlink():
+        path = path.resolve()
+
+    path.touch()
     with tempfile.NamedTemporaryFile(mode="w", encoding=encoding, delete=False) as dest:
         error = None
         try:

This does what I want, but I'm not sure if there are disadvantages to this. I know symlinks are more widely used on linux, but if I'm not mistaken I think this would work on Windows too, since dotenv works with files and not directories.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/dotenv/main.py at the rewrite() function and review how dotenv set and dotenv unset handle a symlinked .env path. Verify the behavior with a symlinked file on the supported platforms; done means those CLI operations update the target file as expected without breaking regular paths.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.