Skip to content

dotenv-cli - rewrite() does not resolve symlinks #541

Description

@darmstrong8008

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.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions