cybniv / poetry.el

Python dependency management and packaging in Emacs
GNU General Public License v3.0
149 stars 36 forks source link

Poetry projects not recognized on windows 10 #35

Open altomcat opened 2 years ago

altomcat commented 2 years ago

I did an update recently and my poetry projects were no longer recognized. I found a regex error in the poetry-find-project-root function where the final ] must be escaped.

Sometimes I've used a different text editor to edit pyproject.toml . It can add carriage returns at the end of each lines, so I've changed the regex a bit to handle them. There may be a more elegant way to do this, but that's another story.

Here is the diff with the changes I made.

$ git diff
warning: LF will be replaced by CRLF in poetry.el.
The file will have its original line endings in your working directory
diff --git a/poetry.el b/poetry.el
index 8d6b92b..09f7e0c 100644
--- a/poetry.el
+++ b/poetry.el
@@ -431,7 +431,7 @@ credential to use."
                (poetry-with-current-file file
                 (goto-char (point-min))
                 (when (re-search-forward
-                       "^\\[tool\\.poetry\\.scripts\\]$" nil t)
+                       "^\\[tool\\.poetry\\.scripts\\][^M]?$" nil t)
                   (forward-line 1)
                   (beginning-of-line)
                   (while (re-search-forward
@@ -824,11 +824,11 @@ If OPT is non-nil, set an optional dep."
      (goto-char (point-min))
      (if dev
          (unless
-             (re-search-forward "^\\[tool\\.poetry\\.dev-dependencies\\]$"
+             (re-search-forward "^\\[tool\\.poetry\\.dev-dependencies\\][^M]?$"
                                 nil t)
            (poetry-error "No dependencies to remove"))
        (unless
-              (re-search-forward "^\\[tool\\.poetry\\.dependencies\\]$"
+              (re-search-forward "^\\[tool\\.poetry\\.dependencies\\][^M]?$"
                                  nil t)
          (poetry-error "No dependencies to remove")))
      (let ((beg (point))
@@ -871,8 +871,8 @@ If OPT is non-nil, set an optional dep."
               (when file
                 (poetry-with-current-file file
                    (goto-char (point-min))
-                   (when (re-search-forward "^\\[tool\\.poetry\\]$" nil t)
-                     (when (re-search-forward "^name = \"\\(.*\\)\"$" nil t)
+                   (when (re-search-forward "^\\[tool\\.poetry\\][^M]?$" nil t)
+                     (when (re-search-forward "^name = \"\\(.*\\)\"[^M]?$" nil t)
                        (substring-no-properties (match-string 1))))))))))

 ;;;###autoload
@@ -884,7 +884,7 @@ If OPT is non-nil, set an optional dep."
                    (with-temp-buffer
                      (insert-file-contents-literally (concat (file-name-as-directory root) "pyproject.toml"))
                      (buffer-string)))
-                  (_ (string-match "^\\[tool\\.poetry]$" pyproject-contents)))
+                  (_ (string-match "^\\[tool\\.poetry\\][^M]?$" pyproject-contents)))
         (setq poetry-project-root root))))

 (defun poetry-get-virtualenv ()
andreaskundig commented 1 year ago

I have the same problem on windows 11. If run dos2unix on pyproject.toml, then everything works, so a dumb workaround would be to configure git to keep pyproject.toml in unix format. The patch proposed by @altomcat two years ago seems like a good solution, why hasn't this been fixed?

Eason0210 commented 11 months ago

Have the same issue on Windows 10.

Any progress on this?

cybniv commented 9 months ago

I really can not reproduce this on any system, unfortunately.

I'll try to test this in a Windoze VM asap. And it will be Windows on ARM, for what it's worth.

However, the proposed fix is looking not right for me.

There's no final ] that must be escaped IMHO, as ] is only treated as a special character if there's a preceding unquoted [ and there isn't.

https://www.gnu.org/software/emacs/manual/html_node/elisp/Regexp-Special.html

In addition, it's still unclear to me what [^M] should do any good in this case. If anything related to windows carriage return line breaks, the regex should be (\r?\n) instead of $, but this remains to be proven to be even remotely necessary.

For all the users affected by these issue (100% using MS Windows):

I'd be good to have the following information: