Skip to content

Commit

Permalink
TDEPS-256 create-basis - when different project dir is specified, sho…
Browse files Browse the repository at this point in the history
…uld be used to resolve relative local deps
  • Loading branch information
puredanger committed Nov 21, 2023
1 parent be30a1a commit e0390f1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 23 deletions.
58 changes: 35 additions & 23 deletions src/main/clojure/clojure/tools/deps.clj
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,17 @@
project-edn (assoc :project project-edn)
extra-edn (assoc :extra extra-edn))))

(defmacro ^:private in-project-dir
"If project deps.edn is not in the current dir, push project directory
into current directory context while creating basis. Local deps use paths
relative to project dir."
[project-deps & body]
`(if (and (instance? String ~project-deps)
(not (.equals dir/*the-dir* (.getParentFile (jio/file ~project-deps)))))
(dir/with-dir (.getParentFile (jio/file ~project-deps))
~@body)
(do ~@body)))

(defn create-basis
"Create a basis from a set of deps sources and a set of aliases. By default, use
root, user, and project deps and no argmaps (essentially the same classpath you get by
Expand Down Expand Up @@ -821,30 +832,31 @@
:classpath - classpath map per make-classpath-map
:classpath-roots - vector of paths in classpath order"
[{:keys [root user project extra aliases] :as params}]
(let [basis-config (cond-> nil
(contains? params :root) (assoc :root root)
(contains? params :project) (assoc :project project)
(contains? params :user) (assoc :user user)
(contains? params :extra) (assoc :extra extra)
(seq aliases) (assoc :aliases (vec aliases)))

{root-edn :root user-edn :user project-edn :project extra-edn :extra} (create-edn-maps params)
edn-maps [root-edn user-edn project-edn extra-edn]

alias-data (->> edn-maps
(map :aliases)
(in-project-dir project
(let [basis-config (cond-> nil
(contains? params :root) (assoc :root root)
(contains? params :project) (assoc :project project)
(contains? params :user) (assoc :user user)
(contains? params :extra) (assoc :extra extra)
(seq aliases) (assoc :aliases (vec aliases)))

{root-edn :root user-edn :user project-edn :project extra-edn :extra} (create-edn-maps params)
edn-maps [root-edn user-edn project-edn extra-edn]

alias-data (->> edn-maps
(map :aliases)
(remove nil?)
(apply merge-with merge))
argmap-data (->> aliases
(remove nil?)
(apply merge-with merge))
argmap-data (->> aliases
(remove nil?)
(map #(get alias-data %)))
argmap (apply merge-alias-maps argmap-data)

project-tooled-edn (tool project-edn argmap)
merged-edn (merge-edns [root-edn user-edn project-tooled-edn extra-edn])
basis (calc-basis merged-edn {:resolve-args argmap, :classpath-args argmap})]
(cond-> (assoc basis :basis-config basis-config)
(pos? (count argmap)) (assoc :argmap argmap))))
(map #(get alias-data %)))
argmap (apply merge-alias-maps argmap-data)

project-tooled-edn (tool project-edn argmap)
merged-edn (merge-edns [root-edn user-edn project-tooled-edn extra-edn])
basis (calc-basis merged-edn {:resolve-args argmap, :classpath-args argmap})]
(cond-> (assoc basis :basis-config basis-config)
(pos? (count argmap)) (assoc :argmap argmap)))))

(defn resolve-added-libs
"Given an existing map of current libs and a map of libs to add,
Expand Down
17 changes: 17 additions & 0 deletions src/test/clojure/clojure/tools/deps/test_deps.clj
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,23 @@
(is (= ['ex/b {:local/root abs-b}]
(ext/canonicalize 'ex/b {:local/root abs-b} {})))))))))

(deftest test-local-root-relative-to-project-deps
(with-test-dir
(let [base (.getCanonicalFile *test-dir*)
adeps (.getPath (jio/file base "a/deps.edn"))
bdeps (.getPath (jio/file base "b/deps.edn"))
bdir (.getAbsolutePath (jio/file base "b"))]
(jio/make-parents *test-dir* "a/deps.edn")
(jio/make-parents *test-dir* "b/deps.edn")
(spit adeps "{:deps {b/b {:local/root \"../b\"}}}")
(spit bdeps "{:paths [\"src\"]}")
(let [b (deps/create-basis {:user nil :project adeps})]
(is (contains? (:classpath b) (.getAbsolutePath (jio/file bdir "src"))))))))

(comment
(test-local-root-relative-to-project-deps)
)

;; simple check that pom resolution is working - load tda itself as pom dep
(deftest test-local-pom
(is (seq (deps/resolve-deps
Expand Down

0 comments on commit e0390f1

Please sign in to comment.