Skip to content

Commit 71a319e

Browse files
rlbdvbbatsov
authored andcommitted
Detect nrepl+unix:socket.file configurations
"leiningen ... :socket nrepl.sock" reports the unix domain socket on stdout like this: nREPL server listening on nrepl+unix:nrepl.sock Detect that in nrepl-server-filter and when found, set the nrepl-endpoint to a suitable "local-unix-domain-socket" host-based entry.
1 parent 944c07b commit 71a319e

File tree

4 files changed

+51
-20
lines changed

4 files changed

+51
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### New features
66

7+
- [#3314](https://github.com/clojure-emacs/cider/issues/3314): Detect `nrepl+unix` sockets (say via `lein nrepl :headless :socket nrepl.sock`).
78
- [#3262](https://github.com/clojure-emacs/cider/issues/3262): Add navigation functionality to `npfb` keys inside the data inspector's buffer.
89
- [#3310](https://github.com/clojure-emacs/cider/issues/3310): Add ability to use custom coordinates in jack-in-dependecies
910

doc/modules/ROOT/pages/basics/up_and_running.adoc

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,13 @@ CIDER's support for Unix sockets is considered experimental and its
280280
interface might change in future CIDER releases.
281281

282282
When locally running nREPL servers, there is the option to listen on a
283-
socket file instead of opening a network port. This can have
284-
advantages in some use cases, e.g. when working with virtual networks
285-
(containers) where sharing a file socket can be vastly simpler than
286-
managing bridge networks and firewall setups.
283+
socket file instead of opening a network port. As long as access to
284+
the parent directory of the socket is sufficiently protected, this is
285+
much more secure than the network port, since any local user can
286+
access the port-provided REPL. It can also be be helpful in other
287+
cases, e.g. when working with virtual networks (containers) where
288+
sharing a file socket can be vastly simpler than managing bridge
289+
networks and firewall setups.
287290

288291
After having started an nREPL server on a file socket, e.g. with the
289292
`clj` command (see https://nrepl.org/nrepl/usage/server.html for other
@@ -297,6 +300,12 @@ $ clj -R:nREPL -m nrepl.cmdline --socket nrepl.sock
297300
you can then connect CIDER by using the `local-unix-domain-socket`
298301
special hostname with `cider-connect`: kbd:[M-x] `cider-connect` kbd:[RET] `local-unix-domain-socket` kbd:[RET] `nrepl.sock` kbd:[RET].
299302

303+
At the moment only with `leiningen`, commands like `cider-jack-in`
304+
will detect and use the unix domain socket if one is requested via the
305+
`:socket` argument. This can be arranged by specifying a prefix
306+
argument to `cider-jack-in`, e.g. kbd:[C-u] kbd:[M-x] `cider-jack-in`,
307+
or by adjusting `cider-lein-parameters`.
308+
300309
== What's Next?
301310

302311
So, what to do next now that CIDER's ready for action? Here are a few ideas:

nrepl-client.el

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,12 @@ been determined."
11091109
(propertize cmd 'face 'font-lock-keyword-face))
11101110
serv-proc)))
11111111

1112-
(defconst nrepl-listening-address-regexp
1112+
(defconst nrepl-listening-unix-address-regexp
1113+
(rx
1114+
(and "nREPL server listening on" (+ " ")
1115+
"nrepl+unix:" (group-n 1 (+ not-newline)))))
1116+
1117+
(defconst nrepl-listening-inet-address-regexp
11131118
(rx (or
11141119
;; standard
11151120
(and "nREPL server started on port " (group-n 1 (+ (any "0-9"))))
@@ -1155,18 +1160,26 @@ up."
11551160
(when-let* ((win (get-buffer-window)))
11561161
(set-window-point win (point)))))
11571162
;; detect the port the server is listening on from its output
1158-
(when (and (null nrepl-endpoint)
1159-
(string-match nrepl-listening-address-regexp output))
1160-
(let ((host (or (match-string 2 output)
1161-
(file-remote-p default-directory 'host)
1162-
"localhost"))
1163-
(port (string-to-number (match-string 1 output))))
1164-
(setq nrepl-endpoint (list :host host
1165-
:port port))
1166-
(message "[nREPL] server started on %s" port)
1167-
(cider--process-plist-put process :cider--nrepl-server-ready t)
1168-
(when nrepl-on-port-callback
1169-
(funcall nrepl-on-port-callback (process-buffer process)))))))))
1163+
(when (null nrepl-endpoint)
1164+
(let ((end (cond
1165+
((string-match nrepl-listening-unix-address-regexp output)
1166+
(let ((path (match-string 1 output)))
1167+
(message "[nREPL] server started on nrepl+unix:%s" path)
1168+
(list :host "local-unix-domain-socket"
1169+
:port path
1170+
:socket-file path)))
1171+
((string-match nrepl-listening-inet-address-regexp output)
1172+
(let ((host (or (match-string 2 output)
1173+
(file-remote-p default-directory 'host)
1174+
"localhost"))
1175+
(port (string-to-number (match-string 1 output))))
1176+
(message "[nREPL] server started on %s" port)
1177+
(list :host host :port port))))))
1178+
(when end
1179+
(setq nrepl-endpoint end)
1180+
(cider--process-plist-put process :cider--nrepl-server-ready t)
1181+
(when nrepl-on-port-callback
1182+
(funcall nrepl-on-port-callback (process-buffer process))))))))))
11701183

11711184
(defmacro emacs-bug-46284/when-27.1-windows-nt (&rest body)
11721185
"Only evaluate BODY when Emacs bug #46284 has been detected."

test/nrepl-client-tests.el

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,29 +114,37 @@
114114
(describe "nrepl-parse-port"
115115
(it "standard"
116116
(let ((msg "nREPL server started on port 58882 on host kubernetes.docker.internal - nrepl://kubernetes.docker.internal:58882"))
117-
(expect (string-match nrepl-listening-address-regexp msg)
117+
(expect (string-match nrepl-listening-inet-address-regexp msg)
118118
:not :to-be nil)
119119
(expect (match-string 1 msg)
120120
:to-equal "58882")
121121
(expect (match-string 2 msg)
122122
:to-be nil)))
123123
(it "babashka"
124124
(let ((msg "Started nREPL server at 127.0.0.1:1667"))
125-
(expect (string-match nrepl-listening-address-regexp msg)
125+
(expect (string-match nrepl-listening-inet-address-regexp msg)
126126
:not :to-be nil)
127127
(expect (match-string 1 msg)
128128
:to-equal "1667")
129129
(expect (match-string 2 msg)
130130
:to-equal "127.0.0.1")))
131131
(it "shadow"
132132
(let ((msg "shadow-cljs - nREPL server started on port 50999"))
133-
(expect (string-match nrepl-listening-address-regexp msg)
133+
(expect (string-match nrepl-listening-inet-address-regexp msg)
134134
:not :to-be nil)
135135
(expect (match-string 1 msg)
136136
:to-equal "50999")
137137
(expect (match-string 2 msg)
138138
:to-be nil))))
139139

140+
(describe "nrepl-parse-sock"
141+
(it "standard"
142+
(let ((msg "nREPL server listening on nrepl+unix:nrepl.sock"))
143+
(expect (string-match nrepl-listening-unix-address-regexp msg)
144+
:not :to-be nil)
145+
(expect (match-string 1 msg)
146+
:to-equal "nrepl.sock"))))
147+
140148
(describe "nrepl-client-lifecycle"
141149
(it "start and stop nrepl client process"
142150

0 commit comments

Comments
 (0)