Skip to content

Commit 916b554

Browse files
committed
clean-ns: fall back to :relative-path, when given
Allow the client to send both the absolute path and a relative path to a file, we use the first one that we can find. The absolute path is used when the client (Emacs) and the JVM are on the same filesystem/same machine. The second is used when they are not on the same filesystem (e.g. running nREPL on a remote machine or in a VM), but the JVM runs in the (mounted) project directory, so that relative file lookups still work. See clojure-emacs/clj-refactor.el#380 (comment)
1 parent c252bd4 commit 916b554

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

src/refactor_nrepl/ns/clean_ns.clj

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
[ns-parser :as ns-parser]
2121
[prune-dependencies :refer [prune-dependencies]]
2222
[rebuild :refer [rebuild-ns-form]]]
23-
[clojure.string :as str]))
23+
[clojure.string :as str]
24+
[clojure.java.io :as io]))
2425

2526
(defn- assert-no-exclude-clause
2627
[use-form]
@@ -35,20 +36,22 @@
3536
(assert-no-exclude-clause (core/get-ns-component ns-form :use))
3637
ns-form)
3738

38-
(defn clean-ns [{:keys [path]}]
39+
(defn clean-ns [{:keys [path relative-path]}]
3940
{:pre [(seq path) (string? path) (core/source-file? path)]}
40-
;; Prefix notation not supported in cljs.
41-
;; We also turn it off for cljc for reasons of symmetry
42-
(config/with-config {:prefix-rewriting (if (or (core/cljs-file? path)
43-
(core/cljc-file? path))
44-
false
45-
(:prefix-rewriting config/*config*))}
46-
(let [ns-form (validate (core/read-ns-form-with-meta path))
47-
deps-preprocessor (if (get config/*config* :prune-ns-form)
48-
#(prune-dependencies % path)
49-
identity)
50-
new-ns-form (-> (ns-parser/parse-ns path)
51-
deps-preprocessor
52-
(rebuild-ns-form ns-form))]
53-
(when-not (= ns-form new-ns-form)
54-
new-ns-form))))
41+
;; Try first the absolute, then the relative path
42+
(let [path (first (filter #(some-> % io/file .exists) [path relative-path]))]
43+
;; Prefix notation not supported in cljs.
44+
;; We also turn it off for cljc for reasons of symmetry
45+
(config/with-config {:prefix-rewriting (if (or (core/cljs-file? path)
46+
(core/cljc-file? path))
47+
false
48+
(:prefix-rewriting config/*config*))}
49+
(let [ns-form (validate (core/read-ns-form-with-meta path))
50+
deps-preprocessor (if (get config/*config* :prune-ns-form)
51+
#(prune-dependencies % path)
52+
identity)
53+
new-ns-form (-> (ns-parser/parse-ns path)
54+
deps-preprocessor
55+
(rebuild-ns-form ns-form))]
56+
(when-not (= ns-form new-ns-form)
57+
new-ns-form)))))

test/refactor_nrepl/ns/clean_ns_test.clj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
(.getAbsolutePath (File. path)))
1111

1212
(defn- clean-msg [path]
13-
{:path (absolute-path path)})
13+
{:path (absolute-path path)
14+
:relative-path path})
1415

1516
(def ns1 (clean-msg "test/resources/ns1.clj"))
1617
(def ns1-cleaned (core/read-ns-form-with-meta (absolute-path "test/resources/ns1_cleaned.clj")))
@@ -48,6 +49,9 @@
4849

4950
(def ns-using-dollar (clean-msg "test/resources/ns_using_dollar.clj"))
5051

52+
(def ns1-relative-path {:path "I do not exist"
53+
:relative-path "test/resources/ns1.clj"})
54+
5155
(deftest combines-requires
5256
(let [requires (core/get-ns-component (clean-ns ns2) :require)
5357
combined-requires (core/get-ns-component ns2-cleaned :require)]
@@ -224,3 +228,7 @@
224228
(deftest does-not-break-import-for-inner-class
225229
(let [cleaned (pprint-ns (clean-ns ns-with-inner-classes))]
226230
(is (re-find #":import.*Line2D\$Double" cleaned))))
231+
232+
(deftest fallback-to-relative-path
233+
(is (= (pprint-ns (clean-ns ns1))
234+
(pprint-ns (clean-ns ns1-relative-path)))))

0 commit comments

Comments
 (0)