Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If current buffer is a TRAMP buffer, use its filename for SSH tunnel #3264

Merged
merged 3 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

- Bump the injected nREPL version to 1.0.
- [#3291](https://github.com/clojure-emacs/cider/pull/3291): **Remove** the `'cljs-pending` `repl-type`. It is replaced by `cider-repl-cljs-upgrade-pending`.
- [#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.

### Bugs fixed

Expand Down
4 changes: 3 additions & 1 deletion doc/modules/ROOT/pages/basics/up_and_running.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,9 @@ config files such as `~/.ssh/config` and `~/.ssh/known_hosts`. This is known to
cause problems with complex or nonstandard ssh configs.

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

== Connecting via unix domain file socket

Expand Down
18 changes: 15 additions & 3 deletions nrepl-client.el
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,18 @@ If NO-ERROR is non-nil, show messages instead of throwing an error."
(defun nrepl--ssh-tunnel-connect (host port)
"Connect to a remote machine identified by HOST and PORT through SSH tunnel."
(message "[nREPL] Establishing SSH tunneled connection to %s:%s ..." host port)
(let* ((remote-dir (if host (format "/ssh:%s:" host) default-directory))
(let* ((current-buf (buffer-file-name))
(tramp-file-regexp "/ssh:\\(.+@\\)?\\(.+?\\)\\(:\\|#\\).+")
(remote-dir (cond
OknoLombarda marked this conversation as resolved.
Show resolved Hide resolved
;; If current buffer is a TRAMP buffer and its host is
;; the same as HOST, reuse its connection parameters for
;; SSH tunnel.
((and (string-match tramp-file-regexp current-buf)
(string= host (match-string 2 current-buf))) current-buf)
;; Otherwise, if HOST was provided, use it for connection.
(host (format "/ssh:%s:" host))
;; Use default directory as fallback.
(t default-directory)))
(ssh (or (executable-find "ssh")
(error "[nREPL] Cannot locate 'ssh' executable")))
(cmd (nrepl--ssh-tunnel-command ssh remote-dir port))
Expand Down Expand Up @@ -598,11 +609,12 @@ If NO-ERROR is non-nil, show messages instead of throwing an error."
;; forwarding is set up, which is used to synchronise on, so that
;; the port forwarding is up when we try to connect.
(format-spec
"%s -v -N -L %p:localhost:%p %u'%h'"
"%s -v -N -L %p:localhost:%p %u'%h' %n"
`((?s . ,ssh)
(?p . ,port)
(?h . ,v-host)
(?u . ,(if v-user (format "-l '%s' " v-user) ""))))))
(?u . ,(if v-user (format "-l '%s' " v-user) ""))
(?n . ,(if v-port (format "-p '%s' " v-port) ""))))))

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

Expand Down