branch: elpa/cider
commit 093f79cf0916c4c5ea7fbb9c113285cd026b7423
Author: vemv <v...@users.noreply.github.com>
Commit: vemv <v...@users.noreply.github.com>

    Check `.nrepl-port`-like files for liveness
    
    Fixes https://github.com/clojure-emacs/cider/issues/3408
---
 CHANGELOG.md    |  1 +
 nrepl-client.el | 16 +++++++++++-----
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2bcd9f6282..530f7ca036 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -31,6 +31,7 @@
 ### Changes
 
 - [#3390](https://github.com/clojure-emacs/cider/issues/3390): Enhance 
`cider-connect` to show all nREPLs available ports, instead of only Leiningen 
ones.
+- [#3408](https://github.com/clojure-emacs/cider/issues/3408): 
`cider-connect`: check `.nrepl-port`-like files for liveness, hiding them if 
they don't reflect an active port. 
 - Preserve the `:cljs-repl-type` more reliably.
 - Improve the presentation of `xref` data.
 - `cider-test`: only show diffs for collections.
diff --git a/nrepl-client.el b/nrepl-client.el
index 97be8e0f8f..23dbbf6a2c 100644
--- a/nrepl-client.el
+++ b/nrepl-client.el
@@ -254,12 +254,18 @@ PARAMS is as in `nrepl-make-buffer-name'."
 (make-obsolete 'nrepl-extract-port 'nrepl-extract-ports "1.5.0")
 
 (defun nrepl--port-from-file (file)
-  "Attempts to read port from a file named by FILE."
+  "Attempts to read port from a file named by FILE.
+
+Discards it if it can be determined that the port is not active."
   (when (file-exists-p file)
-    (with-temp-buffer
-      (insert-file-contents file)
-      (replace-regexp-in-string "\n\\'" "" ;; remove any trailing newline, so 
that our UIs look better.
-                                (buffer-string)))))
+    (let ((port-string (with-temp-buffer
+                         (insert-file-contents file)
+                         (string-trim-right (buffer-string)))))
+      (if (eq system-type 'windows-nt)
+          port-string
+        (when (not (equal ""
+                          (shell-command-to-string (concat "lsof -i:" 
port-string))))
+          port-string)))))
 
 
 ;;; Bencode

Reply via email to