Skip to content

Remove special handling of Boot #285

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 2 commits into from
Aug 15, 2024
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 @@ -2,6 +2,8 @@

## master (unreleased)

* [#285](https://github.com/clojure-emacs/orchard/issues/285): **BREAKING:** Remove special handling of Boot classpath.

## 0.26.3 (2024-08-14)

* [#282](https://github.com/clojure-emacs/orchard/issues/282): Inspector: don't crash when inspecting internal classes.
Expand Down
4 changes: 1 addition & 3 deletions src/orchard/info.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
[orchard.cljs.analysis :as cljs-ana]
[orchard.cljs.meta :as cljs-meta]
[orchard.java :as java]
[orchard.java.classpath :as cp]
[orchard.java.resource :as resource]
[orchard.meta :as m]
[orchard.misc :as misc]))
Expand Down Expand Up @@ -158,8 +157,7 @@
meta

(merge (when-let [file-path (:file meta)]
{:file (cond-> file-path
(misc/boot-project?) cp/classpath-file-relative-path)})))))
{:file file-path})))))

(defn info
"Provide the info map for the input ns and sym.
Expand Down
53 changes: 2 additions & 51 deletions src/orchard/java/classpath.clj
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
(ns orchard.java.classpath
"Classpath access and modification utilities.

Provides an alternative to the java.classpath contrib library.
The library is Boot-aware, meaning it takes into account the
classpath manipulation magic, performed by the Boot build tool."
Provides an alternative to the java.classpath contrib library."
(:require
[clojure.java.io :as io]
[clojure.string :as string]
[orchard.misc :as misc])
(:import
(java.io File)
(java.net URI URL URLClassLoader)
(java.nio.file Paths)
(java.net URL URLClassLoader)
(java.util.jar JarEntry JarFile)))

;;; Classloaders
Expand Down Expand Up @@ -97,48 +93,3 @@
(file-seq f))
(filter #(not (.isDirectory ^File %)))
(map #(.getPath (.relativize (.toURI url) (.toURI ^File %)))))))))

;;; Boot support - previously part of cider-nrepl
;;
;; The Boot build tool stores files in a temporary directory, so
;; we have to do a bit of work to figure out where the real resources are.

(defn boot-classloader
"Creates a class-loader that knows original source files paths in Boot project."
[]
(let [class-path (System/getProperty "fake.class.path")
dir-separator (System/getProperty "file.separator")
paths (string/split class-path (re-pattern (System/getProperty "path.separator")))
urls (map
(fn [path]
(let [url (if (re-find #".jar$" path)
(str "file:" path)
(str "file:" path dir-separator))]
(.toURL (URI. url))))
paths)]
;; TODO: Figure out how to add the JDK sources here
(new java.net.URLClassLoader (into-array java.net.URL urls))))

(defn boot-aware-classloader
[]
(if (misc/boot-project?)
(boot-classloader)
(context-classloader)))

(defn classpath-file-relative-path
"Boot stores files in a temporary directory & ClojureScript stores
the :file metadata location absolutely instead of relatively to the
classpath. This means when doing jump to source in Boot &
ClojureScript, you end up at the temp file. This code attempts to
find the classpath-relative location of the file, so that it can be
opened correctly."
[s]
(let [path (Paths/get s (into-array String []))
path-count (.getNameCount path)]
(or (first
(sequence
(comp (map #(.subpath path % path-count))
(map str)
(filter io/resource))
(range path-count)))
s)))
4 changes: 2 additions & 2 deletions src/orchard/java/resource.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
:url (io/as-url file)})))
(remove #(.startsWith ^String (:relpath %) "META-INF/"))
(remove #(re-matches #".*\.(clj[cs]?|java|class)" (:relpath %)))))
(filter (memfn ^File isDirectory) (map io/as-file (cp/classpath (cp/boot-aware-classloader))))))
(filter (memfn ^File isDirectory) (map io/as-file (cp/classpath (cp/context-classloader))))))

(defn resource-full-path ^URL [relative-path]
(io/resource relative-path (cp/boot-aware-classloader)))
(io/resource relative-path (cp/context-classloader)))

(defn resource-path-tuple
"If it's a resource, return a tuple of the relative path and the full
Expand Down
14 changes: 0 additions & 14 deletions src/orchard/misc.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,6 @@
(defn os-windows? []
(.startsWith (System/getProperty "os.name") "Windows"))

(defn boot-fake-classpath
"Retrieve Boot's fake classpath.
When using Boot, fake.class.path contains the original directories with source
files, which makes it way more useful than the real classpath.
See https://github.com/boot-clj/boot/issues/249 for details."
[]
(System/getProperty "fake.class.path"))

(defn boot-project?
"Check whether we're dealing with a Boot project.
We figure this by checking for the presence of Boot's fake classpath."
[]
(not (nil? (boot-fake-classpath))))

(defn url?
"Check whether the argument is an url"
[u]
Expand Down
29 changes: 4 additions & 25 deletions test/orchard/info_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -454,17 +454,10 @@
:doc "catch-clause => (catch classname name expr*)\n finally-clause => (finally expr*)\n\n Catches and handles Java exceptions.",
:name finally,
:special-form true,
:url "https://clojure.org/special_forms#finally"}]
(testing "- boot project"
(with-redefs [orchard.misc/boot-project? (constantly true)]
(let [i (info/info* params)]
(is (= expected (select-keys i [:ns :name :doc :forms :special-form :url])))
(is (nil? (:file i))))))

(testing "- no boot project"
(let [i (info/info* params)]
(is (= expected (select-keys i [:ns :name :doc :forms :special-form :url])))
(is (nil? (:file i))))))))
:url "https://clojure.org/special_forms#finally"}
i (info/info* params)]
(is (= expected (select-keys i [:ns :name :doc :forms :special-form :url])))
(is (nil? (:file i))))))

(deftest file-resolution-no-defs-issue-75-test
(testing "File resolves, issue #75"
Expand Down Expand Up @@ -721,20 +714,6 @@
(info/normalize-params)
(select-keys [:ns :unqualified-sym]))))))

(deftest boot-file-resolution-test
;; this checks the files on the classpath soo you need the test-resources
;; and specifically test-resources/orchard/test_ns.cljc
;;
;; Note that :file in :meta is left untouched
(when cljs-available?
(with-redefs [orchard.misc/boot-project? (constantly true)]
(is (= '{:ns orchard.test-ns
:name x
:file "orchard/test_ns.cljc"}
(-> (merge @*cljs-params* '{:ns orchard.test-ns :sym x})
(info/info*)
(select-keys [:ns :name :file])))))))

(deftest indirect-vars-cljs
(when cljs-available?
(testing "Uses logic from `merge-meta-for-indirect-var-cljs`"
Expand Down