Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
darmstrong8008 opened this issue Nov 16, 2024 · 0 comments
Open

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

darmstrong8008 opened this issue Nov 16, 2024 · 0 comments

Comments

@darmstrong8008
Copy link

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.

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

No branches or pull requests

1 participant