/
deps_edn.clj
280 lines (261 loc) · 11.2 KB
/
deps_edn.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
(ns clj-kondo.impl.linters.deps-edn
"Linter for deps.edn and bb.edn file contents."
(:require [clj-kondo.impl.findings :as findings]
[clj-kondo.impl.linters.edn-utils :as edn-utils]
[clj-kondo.impl.utils :refer [sexpr node->line]]
[clojure.string :as str]))
(set! *warn-on-reflection* true)
(defn lint-paths-container [ctx node ]
(let [form (sexpr node)]
(when-not (vector? form)
(findings/reg-finding!
ctx
(node->line (:filename ctx)
node
:deps.edn
(str "Expected vector, found: " (edn-utils/name-for-type form))))
true)))
(defn lint-bb-edn-paths-elems [ctx node]
(doseq [node (:children node)]
(let [form (sexpr node)]
(when-not (string? form)
(findings/reg-finding!
ctx
(node->line (:filename ctx)
node
:deps.edn
(str "Expected string, found: " (edn-utils/name-for-type form))))))))
(defn lint-deps-edn-paths-elems [ctx node]
(doseq [node (:children node)]
(let [form (sexpr node)]
(when-not (or (keyword? form) (string? form))
(findings/reg-finding!
ctx
(node->line (:filename ctx)
node
:deps.edn
(str "Expected string or keyword, found: " (edn-utils/name-for-type form))))))))
(defn lint-bb-edn-paths [ctx node]
(when (and node (not (lint-paths-container ctx node)))
(lint-bb-edn-paths-elems ctx node)))
(defn lint-deps-edn-paths [ctx node]
(when (and node (not (lint-paths-container ctx node)))
(lint-deps-edn-paths-elems ctx node)))
(defn lint-qualified-lib [ctx node]
(let [form (sexpr node)]
(when-not (or (qualified-symbol? form)
;; fix for namespaced maps
(:namespace node))
(findings/reg-finding!
ctx
(node->line (:filename ctx)
node
:deps.edn
#_{:clj-kondo/ignore[:format]} ;; fixed on master
(format "Libs must be qualified, change %s => %<s/%<s" form)))
true)))
(defn lint-qualified-deps [ctx nodes]
(run! #(lint-qualified-lib ctx %) nodes))
(defn derive-git-url-from-lib [lib]
(when lib
(let [lib (str lib)]
(when (or
(str/starts-with? lib "io.")
(str/starts-with? lib "com.")
(str/starts-with? lib "ht.sr."))
lib))))
(defn lint-dep-coord [ctx lib node]
(let [form (sexpr node)
git-url (or (:git/url form)
(derive-git-url-from-lib lib))]
(if-not (map? form)
(findings/reg-finding!
ctx
(node->line (:filename ctx)
node
:deps.edn
(str "Expected map, found: " (edn-utils/name-for-type form))))
(or (when-let [version (:mvn/version form)]
(when (or (= "RELEASE" version)
(= "LATEST" version))
(findings/reg-finding!
ctx
(node->line (:filename ctx)
node
:deps.edn
(str "Non-determistic version."))))
true)
(when (:git/url form)
(when (and (:git/sha form) (:sha form))
(findings/reg-finding!
ctx
(node->line (:filename ctx)
node
:deps.edn
(str "Conflicting keys :git/sha and :sha."))))
(when (and (:git/tag form) (:tag form))
(findings/reg-finding!
ctx
(node->line (:filename ctx)
node
:deps.edn
(str "Conflicting keys :git/tag and :tag."))))
(when-not (or (:git/sha form) (:sha form))
(findings/reg-finding!
ctx
(node->line (:filename ctx)
node
:deps.edn
(str "Missing required key :git/sha."))))
true)
;; no linting yet
(:local/root form)
;; in this case git/url is inferred from lib
;; see https://clojure.org/reference/deps_and_cli#_coord_attributes
(and git-url (:git/sha form))
nil
;; no condition met, generic warning
(findings/reg-finding!
ctx
(node->line (:filename ctx)
node
:deps.edn
(str "Missing required key: :mvn/version, :git/url or :local/root.")))))))
(defn lint-deps [ctx kvs]
(doseq [[lib coord] kvs]
(let [error? (lint-qualified-lib ctx lib)]
(lint-dep-coord ctx (when-not error? (sexpr lib)) coord))))
(defn lint-alias-keys [ctx nodes]
(run! (fn [node]
(let [form (sexpr node)]
(if (not (keyword? form))
(findings/reg-finding!
ctx
(node->line (:filename ctx)
node
:deps.edn
(str "Prefer keyword for alias.")))
(when (contains? #{:deps :extra-deps :jvm-opts} form)
(findings/reg-finding!
ctx
(node->line (:filename ctx)
node
:deps.edn
(str "Suspicious alias name: " (name form))))))))
nodes))
(defn lint-aliases [ctx alias-nodes]
(run! (fn [alias-node]
(when-let [jvm-opts-node (:jvm-opts alias-node)]
(let [jvm-opts-form (sexpr jvm-opts-node)]
(when (not (and (sequential? jvm-opts-form)
(every? string? jvm-opts-form)))
(findings/reg-finding!
ctx
(node->line (:filename ctx)
jvm-opts-node
:deps.edn
(str "JVM opts should be seqable of strings.")))))))
alias-nodes))
(defn lint-mvn-repos [ctx mvn-repos]
(let [repo-map-nodes (edn-utils/val-nodes mvn-repos)]
(run! (fn [repo-map-node]
(let [form (sexpr repo-map-node)]
(when-not (and (map? form)
(:url form))
(findings/reg-finding!
ctx
(node->line (:filename ctx)
repo-map-node
:deps.edn
(str "Expected: map with :url."))))))
repo-map-nodes)))
(defn- find-task-cycle
([adj current path seen?]
(if (seen? current)
(vec (drop-while (partial not= current) path))
(when-let [dests (get adj current)]
(some #(find-task-cycle adj %1 (conj path current) (conj seen? current)) dests))))
([task-map]
(let [adj (->> (filter (fn [[t t-def]] (and (symbol? t) (map? t-def))) task-map)
(map (fn [[t t-def]] [t (:depends t-def)]))
(into {}))]
(some #(find-task-cycle adj % [] #{}) (keys adj)))))
(defn lint-tasks [ctx expr]
(let [tasks (edn-utils/sexpr-keys expr)
known-task? (set (keys tasks))
path (find-task-cycle (sexpr expr))
rotate #(vec (take (count path) (drop % (cycle path))))
seg->index (->> (partition 2 1 (conj path (first path)))
(map-indexed #(do [%2 %1]))
(into {}))]
(doseq [[t-key t-def] tasks
:let [t-map (edn-utils/sexpr-keys t-def)
private? (or (:private t-map)
(= \- (first (name t-key))))]
:when (identical? :map (:tag t-def))]
(when-not (or (:doc t-map)
private?)
(findings/reg-finding! ctx
(node->line (:filename ctx)
t-def
:bb.edn-task-missing-docstring
(format "Docstring missing for task: %s" t-key))))
(doseq [dep-task (:children (:depends t-map))
:let [cycle-idx (get seg->index [t-key (:value dep-task)])
unknown-dep? (not (known-task? (:value dep-task)))]
:when (or unknown-dep? cycle-idx)]
(if cycle-idx
(findings/reg-finding! ctx
(node->line (:filename ctx)
dep-task
:bb.edn-cyclic-task-dependency
(str "Cyclic task dependency: "
(apply str (interpose " -> " (conj (rotate cycle-idx) t-key))))))
(findings/reg-finding! ctx
(node->line (:filename ctx)
dep-task
:bb.edn-undefined-task
(str "Depending on undefined task: " (:value dep-task)))))))))
(defn lint-bb-edn [ctx expr]
(try
(let [bb-edn (edn-utils/sexpr-keys expr)]
(lint-bb-edn-paths ctx (:paths bb-edn))
(lint-deps ctx (-> bb-edn :deps edn-utils/node-map))
(when-let [key-node (and (:requires bb-edn)
(some #(when (= :requires (:k %)) %) (edn-utils/key-nodes expr)))]
(findings/reg-finding! ctx
(node->line (:filename ctx)
key-node
:bb.edn-unexpected-key
"Global :requires belong in the :tasks map.")))
(when-let [tasks (:tasks bb-edn)]
(lint-tasks ctx tasks)))
;; Due to ubiquitous use of sexpr, we're catching coercion errors here and let them slide.
(catch Exception e
(binding [*out* *err*]
(println "ERROR: " (.getMessage e))))))
(defn lint-deps-edn [ctx expr]
(try
(let [deps-edn (edn-utils/sexpr-keys expr)
_ (lint-deps-edn-paths ctx (:paths deps-edn))
deps (:deps deps-edn)
_ (lint-deps ctx (edn-utils/node-map deps))
aliases (:aliases deps-edn)
alias-keys (edn-utils/key-nodes aliases)
_ (lint-alias-keys ctx alias-keys)
alias-maps (edn-utils/val-nodes aliases)
alias-maps (map edn-utils/sexpr-keys alias-maps)
_ (lint-aliases ctx alias-maps)
extra-dep-maps (map :extra-deps alias-maps)
_ (run! #(lint-deps ctx (edn-utils/node-map %)) extra-dep-maps)
extra-dep-map-vals (mapcat edn-utils/val-nodes extra-dep-maps)
;; _ (lint-dep-coordinates ctx extra-dep-map-vals)
extra-dep-map-vals (map edn-utils/sexpr-keys extra-dep-map-vals)
exclusions (map (comp :children :exclusions) extra-dep-map-vals)
_ (run! #(lint-qualified-deps ctx %) exclusions)]
(when-let [mvn-repos (:mvn/repos deps-edn)]
(lint-mvn-repos ctx mvn-repos)))
;; Due to ubiquitous use of sexpr, we're catching coercion errors here and let them slide.
(catch Exception e
(binding [*out* *err*]
(println "ERROR: " (.getMessage e))))))