Skip to content

Commit 053562e

Browse files
authored
Sideloader: handle binary files, support multiple directories (#3041)
Make sure that files are served byte-for-byte as they are on disk, without emacs doing any coding system conversion. This also prevents base64-encode from complaining about multibyte characters. Support multiple directories as sources for the sideloader, e.g. cider-nrepl/src, cider-nrepl/resources, orchard/src.
1 parent b9c0138 commit 053562e

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* [#3020](https://github.com/clojure-emacs/cider/issues/3020): Fix session linking on Windows, e.g. when jumping into a library on the classpath.
1515
* [#3031](https://github.com/clojure-emacs/cider/pull/3031): Fix `cider-eval-defun-up-to-point` failing to match delimiters correctly in some cases, resulting in reader exceptions.
1616
* [#3039](https://github.com/clojure-emacs/cider/pull/3039): Allow starting the sideloader for the tooling session.
17+
* [#3041](https://github.com/clojure-emacs/cider/pull/3041): Sideloader: handle binary files, support multiple directories
1718

1819
## 1.1.1 (2021-05-24)
1920

cider-eval.el

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,29 @@ When invoked with a prefix ARG the command doesn't prompt for confirmation."
189189

190190
;;; Sideloader
191191

192-
(defvar cider-sideloader-dir (file-name-directory load-file-name))
192+
(defvar cider-sideloader-dirs
193+
(list (file-name-directory load-file-name))
194+
"Directories where we look for resources requested by the sideloader.")
195+
196+
;; based on f-read-bytes
197+
(defun cider-read-bytes (path)
198+
"Read binary data from PATH.
199+
Return the binary data as unibyte string."
200+
(with-temp-buffer
201+
(set-buffer-multibyte nil)
202+
(setq buffer-file-coding-system 'binary)
203+
(insert-file-contents-literally path nil)
204+
(buffer-substring-no-properties (point-min) (point-max))))
193205

194206
(defun cider-provide-file (file)
195207
"Provide FILE in a format suitable for sideloading."
196-
(let ((file (expand-file-name file cider-sideloader-dir)))
197-
(if (file-exists-p file)
198-
(with-current-buffer (find-file-noselect file)
199-
(base64-encode-string (substring-no-properties (buffer-string)) 'no-line-breaks))
208+
(let ((file (seq-find
209+
#'file-exists-p
210+
(seq-map (lambda (dir)
211+
(expand-file-name file dir))
212+
cider-sideloader-dirs))))
213+
(if file
214+
(base64-encode-string (cider-read-bytes file) 'no-line-breaks)
200215
;; if we can't find the file we should return an empty string
201216
(base64-encode-string ""))))
202217

@@ -235,7 +250,8 @@ If CONNECTION is nil, use `cider-current-repl'."
235250
If CONNECTION is nil, use `cider-current-repl'."
236251
(interactive)
237252
(message "Starting nREPL's sideloader")
238-
(cider-request:sideloader-start connection))
253+
(cider-request:sideloader-start connection)
254+
(cider-request:sideloader-start connection 'tooling))
239255

240256

241257
;;; Dealing with compilation (evaluation) errors and warnings

test/cider-eval-test.el

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
(filename (make-temp-file "abc.clj")))
3838
(with-temp-file filename
3939
(dotimes (_ 60) (insert "x")))
40-
(expect (cider-provide-file filename) :not :to-match "\n"))))
40+
(expect (cider-provide-file filename) :not :to-match "\n")))
41+
(it "can handle multibyte characters"
42+
(let ((cider-sideloader-dir "/tmp")
43+
(default-directory "/tmp")
44+
(filename (make-temp-file "abc.clj")))
45+
(with-temp-file filename
46+
(insert "🍻"))
47+
(expect (cider-provide-file filename) :to-equal "8J+Nuw=="))))
4148

4249
(provide 'cider-eval-tests)

0 commit comments

Comments
 (0)