Skip to content

Commit b02920a

Browse files
authored
[Fix #2822] If current buffer is a TRAMP buffer, use its filename for SSH tunnel (#3264)
If current buffer is a TRAMP buffer, use its filename for SSH tunnel. This way, username and SSH port will be parsed from existing SSH connection, instead of using current username and default SSH port. Also fixes #3261.
1 parent 7571f4d commit b02920a

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

CHANGELOG.md

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

1414
- Bump the injected nREPL version to 1.0.
1515
- [#3291](https://github.com/clojure-emacs/cider/pull/3291): **Remove** the `'cljs-pending` `repl-type`. It is replaced by `cider-repl-cljs-upgrade-pending`.
16+
- [#3261](https://github.com/clojure-emacs/cider/issues/3261): If user is connecting to nREPL from a TRAMP buffer, use its connection parameters (port, username) for establishing SSH tunnel.
1617

1718
### Bugs fixed
1819

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@ config files such as `~/.ssh/config` and `~/.ssh/known_hosts`. This is known to
262262
cause problems with complex or nonstandard ssh configs.
263263

264264
You can safely run `cider-jack-in-*` while working with remote files over TRAMP. CIDER
265-
will handle this use-case transparently for you.
265+
will reuse existing SSH connection's parameters (like port and username) for establishing SSH tunnel.
266+
The same will happen if you try to `cider-connect-*` to a host that matches the one you're currently
267+
connected to.
266268

267269
== Connecting via unix domain file socket
268270

nrepl-client.el

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,18 @@ If NO-ERROR is non-nil, show messages instead of throwing an error."
570570
(defun nrepl--ssh-tunnel-connect (host port)
571571
"Connect to a remote machine identified by HOST and PORT through SSH tunnel."
572572
(message "[nREPL] Establishing SSH tunneled connection to %s:%s ..." host port)
573-
(let* ((remote-dir (if host (format "/ssh:%s:" host) default-directory))
573+
(let* ((current-buf (buffer-file-name))
574+
(tramp-file-regexp "/ssh:\\(.+@\\)?\\(.+?\\)\\(:\\|#\\).+")
575+
(remote-dir (cond
576+
;; If current buffer is a TRAMP buffer and its host is
577+
;; the same as HOST, reuse its connection parameters for
578+
;; SSH tunnel.
579+
((and (string-match tramp-file-regexp current-buf)
580+
(string= host (match-string 2 current-buf))) current-buf)
581+
;; Otherwise, if HOST was provided, use it for connection.
582+
(host (format "/ssh:%s:" host))
583+
;; Use default directory as fallback.
584+
(t default-directory)))
574585
(ssh (or (executable-find "ssh")
575586
(error "[nREPL] Cannot locate 'ssh' executable")))
576587
(cmd (nrepl--ssh-tunnel-command ssh remote-dir port))
@@ -598,11 +609,12 @@ If NO-ERROR is non-nil, show messages instead of throwing an error."
598609
;; forwarding is set up, which is used to synchronise on, so that
599610
;; the port forwarding is up when we try to connect.
600611
(format-spec
601-
"%s -v -N -L %p:localhost:%p %u'%h'"
612+
"%s -v -N -L %p:localhost:%p %u'%h' %n"
602613
`((?s . ,ssh)
603614
(?p . ,port)
604615
(?h . ,v-host)
605-
(?u . ,(if v-user (format "-l '%s' " v-user) ""))))))
616+
(?u . ,(if v-user (format "-l '%s' " v-user) ""))
617+
(?n . ,(if v-port (format "-p '%s' " v-port) ""))))))
606618

607619
(autoload 'comint-watch-for-password-prompt "comint" "(autoload).")
608620

0 commit comments

Comments
 (0)