Skip to content

Commit c37fb45

Browse files
authored
Fix cider-jack-in failing in remote ssh buffers (#3544)
* tramp-sample-project: fix Lein not finding `java` after cider-jack-in This is a workaround that can be removed when `PATH` is picked up correctly when jacking in on the running remote. Circumvents: ``` Tramp: Opening connection nrepl-server for root@localhost using sshx...done [nREPL] Starting server via lein update-in :dependencies conj \[nrepl/nrepl\ \"1.0.0\"\] -- update-in :plugins conj \[cider/cider-nrepl\ \"0.40.0\"\] -- repl :headless :host localhost error in process sentinel: let: Could not start nREPL server: /usr/local/bin/lein: line 224: type: java: not found Leiningen couldn't find 'java' executable, which is required. Please either set JAVA_CMD or put java (>=1.6) in your $PATH (/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin). ("exited abnormally with code 1") ``` * tramp-sample-project: add `cider-jack-in` method to README * Fix `cider-jack-in` failing with SSH remotes Calling cider-jack-in from a tramp buffer on a ssh-remote would throw: "error in process filter: Wrong type argument: stringp, nil" This happens because nrepl--ssh-tunnel-connect is trying to infer the hostname from the current filename: It string-matches on (buffer-file-name) which returns nil in the *cider-uninitialized-repl* buffer. For the jack-in use-case, the local var nrepl-project-dir will always be bound when connecting the client, so we can use it as a fallback. Fixes #3541
1 parent c374b18 commit c37fb45

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

CHANGELOG.md

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

1515
### Bugs fixed
1616

17+
- [#3541](https://github.com/clojure-emacs/cider/issues/3541): Fix `cider-jack-in` failing with SSH remotes.
1718
- [#3559](https://github.com/clojure-emacs/cider/issues/3559): Don't apply [dynamic syntax highlighting](https://docs.cider.mx/cider/config/syntax_highlighting.html) over buffers belonging to unrelated Sesman sessions.
1819

1920
## 1.9.0 (2023-10-24)

dev/tramp-sample-project/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ COPY . /usr/src/app
3636
RUN lein deps
3737

3838
RUN echo "export JAVA_HOME=${JAVA_HOME}" >> /root/.bashrc
39+
RUN echo "export JAVA_CMD=${JAVA_HOME}/bin/java" >> /root/.bashrc
3940
RUN echo "export LEIN_HOME=${LEIN_HOME}" >> /root/.bashrc
4041
RUN echo "export LEIN_JAVA_CMD=${LEIN_JAVA_CMD}" >> /root/.bashrc
4142
RUN echo "export LEIN_JVM_OPTS=${LEIN_JVM_OPTS}" >> /root/.bashrc

dev/tramp-sample-project/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ The Docker image exposes a SSH server.
44

55
This way, for development purposes, we can SSH into it with TRAMP and exercise CIDER's TRAMP-related capabilities.
66

7-
To get started:
7+
## Some ways to get started:
88

9+
### `cider-jack-in` from a tramp buffer
10+
* `M-:` `(async-shell-command "make run")` to run the Docker image
11+
* `M-:` `(find-file "/sshx:root@localhost#8022:/usr/src/app/src/foo.clj")`
12+
* `M-x` `cider-jack-in`
13+
* Enter password: `cider`
14+
15+
### Manually create a remote repl and connect to it
916
* In one terminal tab, run `make run` to run the Docker image
1017
* Once it's ready, from another tab, run `make ssh` and start a repl manually from there
1118
* The password is `cider`

nrepl-client.el

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,12 @@ Discards it if it can be determined that the port is not active."
271271
(shell-command-to-string (format "lsof -i:%s" port-number))))
272272
port-string)))))
273273

274+
(defun nrepl--ssh-file-name-matches-host-p (file-name host)
275+
"Return t, if FILE-NAME is a tramp-file-name on HOST via ssh."
276+
(when (tramp-tramp-file-p file-name)
277+
(with-parsed-tramp-file-name file-name v
278+
(and (member v-method '("ssh" "sshx"))
279+
(member host (list v-host (concat v-host "#" v-port)))))))
274280

275281
;;; Bencode
276282

@@ -587,14 +593,12 @@ If NO-ERROR is non-nil, show messages instead of throwing an error."
587593
(defun nrepl--ssh-tunnel-connect (host port)
588594
"Connect to a remote machine identified by HOST and PORT through SSH tunnel."
589595
(message "[nREPL] Establishing SSH tunneled connection to %s:%s ..." host port)
590-
(let* ((current-buf (buffer-file-name))
591-
(tramp-file-regexp "/ssh:\\(.+@\\)?\\(.+?\\)\\(:\\|#\\).+")
596+
(let* ((file-name (or (buffer-file-name) nrepl-project-dir))
592597
(remote-dir (cond
593598
;; If current buffer is a TRAMP buffer and its host is
594599
;; the same as HOST, reuse its connection parameters for
595600
;; SSH tunnel.
596-
((and (string-match tramp-file-regexp current-buf)
597-
(string= host (match-string 2 current-buf))) current-buf)
601+
((nrepl--ssh-file-name-matches-host-p file-name host) file-name)
598602
;; Otherwise, if HOST was provided, use it for connection.
599603
(host (format "/ssh:%s:" host))
600604
;; Use default directory as fallback.

test/nrepl-client-tests.el

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,27 @@
158158
(expect (match-string 1 msg)
159159
:to-equal "nrepl.sock"))))
160160

161+
(describe "nrepl--ssh-file-name-matches-host-p"
162+
(it "works in the most basic case"
163+
(expect (nrepl--ssh-file-name-matches-host-p "/ssh:host:~/test/" "host")
164+
:to-be-truthy)
165+
(expect (nrepl--ssh-file-name-matches-host-p "/ssh:host:~/test/" "other-host")
166+
:to-be nil))
167+
(it "understands non-standart ssh ports and distinguishes between them"
168+
(expect (nrepl--ssh-file-name-matches-host-p
169+
"/ssh:tester@host#8022:~/test/" "host#8022")
170+
:to-be-truthy)
171+
(expect (nrepl--ssh-file-name-matches-host-p
172+
"/ssh:tester@host#8022:~/test/" "host#7777")
173+
:to-be nil))
174+
(it "works with tramps other ssh methods"
175+
(expect (nrepl--ssh-file-name-matches-host-p
176+
"/sshx:tester@host:~/test/" "host")
177+
:to-be-truthy))
178+
(it "can handle nil"
179+
(expect (nrepl--ssh-file-name-matches-host-p nil nil)
180+
:to-be nil)))
181+
161182
(describe "nrepl-client-lifecycle"
162183
(it "start and stop nrepl client process"
163184

0 commit comments

Comments
 (0)