Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
From 617e99a2ec13b1e725a3b2604511ecb530ee689a Mon Sep 17 00:00:00 2001
From: sean <sean@sean-xps2>
Date: Tue, 3 Sep 2019 09:50:46 +0200
Subject: [PATCH] Canonicalize local roots
When reading a deps file, turn relative paths names in :local/root
coordinates into absolute pathnames. The parent directory of the deps
file, not the current directory, is used in the conversion.
In a monorepo, the clojure CLI's config-dir deps.edn file might
contain relative paths to other parts of the repo.
For example, let's consider the following monorepo.
config/
clojure/
deps.edn
tools/
lint/
deps.edn
src/...
B/
deps.edn
src/..
A/
deps.edn
src/...
Let's image we want to add a repo-wide linting alias in
config/clojure/deps.edn.
{:aliases
{:lint {:extra-deps {acme/lint {:local/root ../../tools/lint}}
:main-opts ["-m" "acme.lint.core"]}}}
Developers set CLJ_CONFIG="config/clojure/deps.edn" in their shell
configuration. Clojure linting code lives at "tools/lint/deps.edn".
In all projects in the repo (e.g. at "A/deps.den", and
"tools/B/deps.den"), we would like `clojure -A:lint` to run the
linters.
Running `clojure -A:lint` in directory A, however, adds
"A/../../tools/lint/src" to the classpath, rather than
"A/../tools/lint/src" or "/path/to/repo/tools/lint/src".
This patch fixes the issue by turning the relative paths into absolute
paths upon reading the deps.edn file.
---
src/main/clojure/clojure/tools/deps/alpha/reader.clj | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/src/main/clojure/clojure/tools/deps/alpha/reader.clj b/src/main/clojure/clojure/tools/deps/alpha/reader.clj
index ec59b2b..898006b 100644
--- a/src/main/clojure/clojure/tools/deps/alpha/reader.clj
+++ b/src/main/clojure/clojure/tools/deps/alpha/reader.clj
@@ -66,11 +66,19 @@
(vector? %) ((fn [v] (mapv canonicalize-sym v))))
deps-map))
+(defn- canonicalize-local-roots
+ [deps-map]
+ (walk/postwalk
+ #(cond-> %
+ (and (map? %) (find % :local/root)) (update :local/root (comp dir/canonicalize jio/file)))
+ deps-map))
+
(defn slurp-deps
"Read a single deps.edn file from disk and canonicalize symbols,
return a deps map."
[dep-file]
- (-> dep-file slurp-edn-map canonicalize-all-syms))
+ (dir/with-dir (-> dep-file jio/file .getAbsoluteFile .getParentFile)
+ (-> dep-file slurp-edn-map canonicalize-all-syms canonicalize-local-roots)))
(defn- merge-or-replace
"If maps, merge, otherwise replace"
--
2.17.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.