/
kondo.clj
238 lines (211 loc) · 10.7 KB
/
kondo.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
(ns clojure-lsp.kondo
(:require
[clj-kondo.core :as kondo]
[clojure-lsp.config :as config]
[clojure-lsp.feature.diagnostics :as f.diagnostic]
[clojure-lsp.settings :as settings]
[clojure-lsp.shared :as shared]
[clojure.core.async :as async]
[clojure.java.io :as io]
[clojure.set :as set]
[clojure.string :as string]
[lsp4clj.protocols.logger :as logger]))
(set! *warn-on-reflection* true)
(defn clj-kondo-version []
(string/trim (slurp (io/resource "CLJ_KONDO_VERSION"))))
(def clj-kondo-analysis-batch-size 50)
(defn ^:private project-config-dir [project-root-uri]
(when project-root-uri
;; TODO: might be better to use clj-kondo.impl.core/config-dir, but it's not
;; yet part of the public kondo api.
#_(kondo/config-dir (shared/uri->filename project-root-uri))
(let [config-dir (io/file (shared/uri->filename project-root-uri) ".clj-kondo")]
(when (and (shared/file-exists? config-dir)
(shared/directory? config-dir))
config-dir))))
(defn ^:private config-path [config-dir]
(let [config-file (io/file config-dir "config.edn")]
(.getCanonicalPath ^java.io.File config-file)))
(defn home-config-path []
(let [xdg-config-home (or (config/get-env "XDG_CONFIG_HOME")
(io/file (config/get-property "user.home") ".config"))]
(config-path (io/file xdg-config-home "clj-kondo"))))
(defn project-config-path [project-root-uri]
(config-path (project-config-dir project-root-uri)))
(defn config-hash
[project-root]
(let [err (java.io.StringWriter.)]
(binding [*err* err]
(let [result (-> project-root
(io/file ".clj-kondo")
kondo/resolve-config
kondo/config-hash)]
(when-not (string/blank? (str err))
(logger/error (str err)))
result))))
(defmacro catch-kondo-errors [err-hint & body]
(let [m (meta &form)
err-sym (gensym "err")
out-sym (gensym "out")
e-sym (gensym "e")]
`(let [~err-sym (java.io.StringWriter.)
~out-sym (java.io.StringWriter.)]
(try
(binding [*err* ~err-sym
*out* ~out-sym]
(let [result# (do ~@body)]
(when-not (string/blank? (str ~err-sym))
~(with-meta `(logger/warn "Non-fatal error from clj-kondo:" (str ~err-sym)) m))
(when-not (string/blank? (str ~out-sym))
~(with-meta `(logger/warn "Output from clj-kondo:" (str ~out-sym)) m))
result#))
(catch Exception ~e-sym
~(with-meta `(logger/error ~e-sym "Error running clj-kondo on" ~err-hint) m))))))
(defn entry->normalized-entries [{:keys [bucket] :as element}]
(cond
;; We create two entries here (and maybe more for refer)
(= :namespace-usages bucket)
(cond-> [(set/rename-keys element {:to :name})]
(:alias element)
(conj (set/rename-keys (assoc element :bucket :namespace-alias) {:alias-row :name-row
:alias-col :name-col
:alias-end-row :name-end-row
:alias-end-col :name-end-col})))
(contains? #{:locals :local-usages :keywords} bucket)
[(-> element
(assoc :name-row (or (:name-row element) (:row element))
:name-col (or (:name-col element) (:col element))
:name-end-row (or (:name-end-row element) (:end-row element))
:name-end-col (or (:name-end-col element) (:end-col element))))]
:else
[element]))
(defn ^:private valid-element? [{:keys [name-row name-col name-end-row name-end-col] :as _element}]
(and name-row
name-col
name-end-row
name-end-col))
(defn normalize-analysis [analysis]
(for [[bucket vs] analysis
v vs
element (entry->normalized-entries (assoc v :bucket bucket))
:when (valid-element? element)]
element))
(defn ^:private with-additional-config
[config settings]
(cond-> config
(get-in settings [:linters :clj-kondo :report-duplicates] true)
(->
(assoc-in [:config :linters :unresolved-symbol :report-duplicates] true)
(assoc-in [:config :linters :unresolved-namespace :report-duplicates] true)
(assoc-in [:config :linters :unresolved-var :report-duplicates] true))))
(defn ^:private project-custom-lint!
[db {:keys [analysis config] :as kondo-ctx}]
(when-not (= :off (get-in config [:linters :clojure-lsp/unused-public-var :level]))
(let [new-analysis (group-by :filename (normalize-analysis analysis))]
(f.diagnostic/lint-project-diagnostics! new-analysis kondo-ctx db))))
(defn ^:private custom-lint-for-reference-files!
[files db {:keys [analysis] :as kondo-ctx}]
(shared/logging-time
"Linting references took %s secs"
(let [new-analysis (group-by :filename (normalize-analysis analysis))
updated-analysis (merge (:analysis @db) new-analysis)]
(doseq [file files]
(f.diagnostic/unused-public-var-lint-for-single-file! file updated-analysis kondo-ctx db)))))
(defn ^:private single-file-custom-lint!
[{:keys [analysis config] :as kondo-ctx} uri db]
(when-not (= :off (get-in config [:linters :clojure-lsp/unused-public-var :level]))
(let [filename (-> analysis :var-definitions first :filename)
updated-analysis (assoc (:analysis @db) filename (normalize-analysis analysis))]
(if (settings/get db [:linters :clj-kondo :async-custom-lint?] true)
(async/go-loop [tries 1]
(if (>= tries 200)
(logger/info "Max tries reached when async custom linting" uri)
(if (contains? (:processing-changes @db) uri)
(do
(Thread/sleep 50)
(recur (inc tries)))
(let [old-findings (get-in @db [:findings filename])
new-findings (f.diagnostic/unused-public-var-lint-for-single-file-merging-findings! filename updated-analysis kondo-ctx db)]
;; This equality check doesn't seem necessary, but it helps
;; avoid an infinite loop. See
;; https://github.com/clojure-lsp/clojure-lsp/issues/796#issuecomment-1065830737
;; and the surrounding discussion. Even if the new-findings are
;; `=` to the old-findings, they never seem to be `identical?`.
;; (TODO: understand why?). If we swap them in, the
;; `compare-and-set!` in `file-management.analyze-changes` is
;; guaranteed to fail (since `compare-and-set!` is based on
;; object identity, not equality). That will trigger a
;; re-analysis and re-linting, bringing us back to this line and
;; starting the loop again.
(when (not= old-findings new-findings)
(swap! db assoc-in [:findings filename] new-findings))
(when (not= :unknown (shared/uri->file-type uri))
(f.diagnostic/sync-lint-file! uri db))))))
(f.diagnostic/unused-public-var-lint-for-single-file! filename updated-analysis kondo-ctx db)))))
(defn kondo-for-paths [paths db external-analysis-only?]
(-> {:cache true
:parallel true
:copy-configs (settings/get db [:copy-kondo-configs?] true)
:lint [(string/join (System/getProperty "path.separator") paths)]
:config {:output {:analysis {:arglists true
:locals false
:keywords true
:protocol-impls true}
:canonical-paths true}}}
(shared/assoc-some :custom-lint-fn (when-not external-analysis-only?
(partial project-custom-lint! db)))
(with-additional-config (settings/all db))))
(defn kondo-copy-configs [paths db]
{:cache true
:parallel true
:skip-lint true
:copy-configs (settings/get db [:copy-kondo-configs?] true)
:lint [(string/join (System/getProperty "path.separator") paths)]
:config {:output {:canonical-paths true}}})
(defn kondo-for-reference-filenames [filenames db]
(-> (kondo-for-paths filenames db false)
(assoc :custom-lint-fn (partial custom-lint-for-reference-files! filenames db))))
(defn kondo-for-single-file [uri db]
(-> {:cache true
:lint ["-"]
:copy-configs (settings/get db [:copy-kondo-configs?] true)
:lang (shared/uri->file-type uri)
:filename (shared/uri->filename uri)
:config-dir (project-config-dir (:project-root-uri @db))
:custom-lint-fn #(single-file-custom-lint! % uri db)
:config {:output {:analysis {:arglists true
:locals true
:keywords true
:protocol-impls true
:context [:clojure.test
:re-frame.core]}
:canonical-paths true}}}
(with-additional-config (settings/all db))))
(defn run-kondo-on-paths! [paths external-analysis-only? {:keys [db]}]
(catch-kondo-errors (str "paths " (string/join ", " paths))
(kondo/run! (kondo-for-paths paths db external-analysis-only?))))
(defn run-kondo-on-paths-batch!
"Run kondo on paths by partitioning the paths, with this we should call
kondo more times but with fewer paths to analyze, improving memory."
[paths public-only? update-callback components]
(let [total (count paths)
batch-count (int (Math/ceil (float (/ total clj-kondo-analysis-batch-size))))]
(logger/info (str "Analyzing " total " paths with clj-kondo with batch size of " batch-count " ..."))
(if (<= total clj-kondo-analysis-batch-size)
(run-kondo-on-paths! paths public-only? components)
(->> paths
(partition-all clj-kondo-analysis-batch-size)
(map-indexed (fn [index batch-paths]
(logger/info "Analyzing" (str (inc index) "/" batch-count) "batch paths with clj-kondo...")
(update-callback (inc index) batch-count)
(run-kondo-on-paths! batch-paths public-only? components)))
(reduce shared/deep-merge)))))
(defn run-kondo-on-reference-filenames! [filenames db]
(catch-kondo-errors (str "files " (string/join ", " filenames))
(kondo/run! (kondo-for-reference-filenames filenames db))))
(defn run-kondo-on-text! [text uri db]
(catch-kondo-errors (shared/uri->filename uri)
(with-in-str text (kondo/run! (kondo-for-single-file uri db)))))
(defn run-kondo-copy-configs! [paths {:keys [db]}]
(catch-kondo-errors (str "paths " (string/join ", " paths))
(kondo/run! (kondo-copy-configs paths db))))