Skip to content

TRAMP improvements #3412

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

Merged
merged 3 commits into from
Aug 18, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
- [#3402](https://github.com/clojure-emacs/cider/issues/3402): fix `cider-format-connection-params` edge case for Emacs 29.
- [#3393](https://github.com/clojure-emacs/cider/issues/3393): Recompute namespace info on each shadow-cljs recompilation or evaluation.
- Recompute namespace info on each fighweel-main recompilation.
- [#3250](https://github.com/clojure-emacs/cider/issues/3250): don't lose the CIDER session over TRAMP files.
- Fix the `xref-find-definitions` CIDER backend to return correct filenames.
- Fix the `cider-xref-fn-deps` buttons to direct to the right file.
- Make TRAMP functionality work when using non-standard ports.

### Changes

Expand Down
8 changes: 5 additions & 3 deletions cider-common.el
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ create a valid path."
(match-string 1 filename)
filename)))

(defun cider-make-tramp-prefix (method user host)
"Constructs a Tramp file prefix from METHOD, USER, HOST.
(defun cider-make-tramp-prefix (method user host &optional port)
"Constructs a Tramp file prefix from METHOD, USER, HOST, PORT.
It originated from Tramp's `tramp-make-tramp-file-name'. The original be
forced to make full file name with `with-parsed-tramp-file-name', not providing
prefix only option."
Expand All @@ -240,6 +240,8 @@ prefix only option."
(if (string-match tramp-ipv6-regexp host)
(concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
host))
(when port
(concat "#" port))
tramp-postfix-host-format))

(defun cider-tramp-prefix (&optional buffer)
Expand All @@ -253,7 +255,7 @@ if BUFFER is local."
(when (tramp-tramp-file-p name)
(with-parsed-tramp-file-name name v
(with-no-warnings
(cider-make-tramp-prefix v-method v-user v-host))))))
(cider-make-tramp-prefix v-method v-user v-host v-port))))))

(defun cider--client-tramp-filename (name &optional buffer)
"Return the tramp filename for path NAME relative to BUFFER.
Expand Down
2 changes: 2 additions & 0 deletions cider-connection.el
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,8 @@ REPL defaults to the current REPL."
(when (string-match-p "#uzip" file)
(let ((avfs-path (directory-file-name (expand-file-name (or (getenv "AVFSBASE") "~/.avfs/")))))
(setq file (replace-regexp-in-string avfs-path "" file t t))))
(when-let ((tp (cider-tramp-prefix (current-buffer))))
(setq file (string-remove-prefix tp file)))
(when (process-live-p proc)
(let* ((classpath (or (process-get proc :cached-classpath)
(let ((cp (with-current-buffer repl
Expand Down
61 changes: 61 additions & 0 deletions dev/sample-project/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Use an official Clojure runtime as a parent image
FROM clojure:temurin-17-lein-bullseye

# Set environment variables to non-interactive (this prevents some prompts)
ENV DEBIAN_FRONTEND=noninteractive

# Define environment variable for nREPL port
ENV NREPL_PORT=7888

RUN apt-get update \
&& apt-get install -y openssh-server locales \
&& mkdir /var/run/sshd

RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && \
locale-gen
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

RUN locale-gen en_US.UTF-8
RUN locale-gen en en_US en_US.UTF-8
RUN dpkg-reconfigure locales

# Set root password
RUN echo 'root:cider' | chpasswd

RUN sed -i 's/^#* *PasswordAuthentication .*/PasswordAuthentication yes/' /etc/ssh/sshd_config
RUN sed -i 's/^#* *PermitRootLogin .*/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -i 's/^#* *ChallengeResponseAuthentication .*/ChallengeResponseAuthentication yes/' /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

# Expose SSH port
EXPOSE 22

CMD ["/usr/sbin/sshd", "-D"]

# Set the working directory in the docker image
WORKDIR /usr/src/app

# Copy the current directory contents into the directory at /usr/src/app in the image
COPY . /usr/src/app

# Install any needed packages specified in project.clj
RUN lein deps

# Forward all relevant env vars for ssh sessions
RUN echo "export JAVA_HOME=${JAVA_HOME}" >> /root/.bashrc
RUN echo "export LEIN_HOME=${LEIN_HOME}" >> /root/.bashrc
RUN echo "export LEIN_JAVA_CMD=${LEIN_JAVA_CMD}" >> /root/.bashrc
RUN echo "export LEIN_JVM_OPTS=${LEIN_JVM_OPTS}" >> /root/.bashrc
RUN echo "export LEIN_ROOT=${LEIN_ROOT}" >> /root/.bashrc
RUN echo "export NREPL_PORT=${NREPL_PORT}" >> /root/.bashrc
RUN echo "export PATH=${PATH}" >> /root/.bashrc

# Make port 7888 available to the world outside this container (nREPL default port)
#EXPOSE 7888

# Run lein repl when the container launches, on port defined by NREPL_PORT
#CMD ["lein", "repl", ":headless", ":host", "0.0.0.0", ":port", "7888"]
10 changes: 10 additions & 0 deletions dev/sample-project/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
build:
DOCKER_BUILDKIT=0 docker build --no-cache -t cider-tramp-dev .

run: build
docker run -p 7888:7888 -p 8022:22 cider-tramp-dev

ssh:
ssh-keygen -R "[localhost]:8022"
echo "Password is: cider"
ssh root@localhost -p 8022
17 changes: 17 additions & 0 deletions dev/sample-project/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
This project spins up a Clojure project within a Docker image.

The Docker image exposes a SSH server.

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

To get started:

* In one terminal tab, run `make run` to run the Docker image
* Once it's ready, from another tab, run `make ssh`
* The password is `cider`
* `cd /usr/src/app; lein repl :headless :host 0.0.0.0 :port 7888`

Now, from emacs you can `cider-connect` to localhost.

* `M-:`, `(dired "/sshx:root@localhost#8022:/usr/src/app")`
* `M-x cider-connect` (choose `localhost`, `7888`)
5 changes: 5 additions & 0 deletions dev/sample-project/project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(defproject cider-tramp-dev "0"
:dependencies [[org.clojure/clojure "1.11.1"]
[clj-http "3.12.3"]]
:source-paths ["src"]
:plugins [[cider/cider-nrepl "0.35.0"]])
3 changes: 3 additions & 0 deletions dev/sample-project/src/foo.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(ns foo
(:require
[clj-http.client :as client]))
8 changes: 7 additions & 1 deletion test/cider-common-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,15 @@

(describe "cider-make-tramp-prefix"
(it "returns tramp-prefix only"
;;; The third parameter is a host. It must contains a port number.
;;; The third parameter is a host.
(expect (cider-make-tramp-prefix "ssh" "cider-devs" "192.168.50.9#22")
:to-equal "/ssh:[email protected]#22:")
(expect (cider-make-tramp-prefix "ssh" "cider-devs" "192.168.50.9")
:to-equal "/ssh:[email protected]:")
(expect (cider-make-tramp-prefix "ssh" "cider-devs" "192.168.50.9" "12345")
:to-equal "/ssh:[email protected]#12345:")
(expect (cider-make-tramp-prefix "ssh" "cider-devs" "192.168.50.9#12345")
:to-equal "/ssh:[email protected]#12345:")
;;; These two cases are for using ssh config alias.
(expect (cider-make-tramp-prefix "ssh" nil "test.cider.com")
:to-equal "/ssh:test.cider.com:")
Expand Down