The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Nudge with-redefs for vars re-exported through potemkin (#83460)
Chris Truteracc3bed133fc
diff --git a/.clj-kondo/src/hooks/clojure/core/with_redefs.clj b/.clj-kondo/src/hooks/clojure/core/with_redefs.cljindex 2f711cc6bdc..781c265e5d3 100644--- a/.clj-kondo/src/hooks/clojure/core/with_redefs.clj+++ b/.clj-kondo/src/hooks/clojure/core/with_redefs.clj@@ -2,25 +2,44 @@(:require[clj-kondo.hooks-api :as hooks]))-(defn- defn-arity?- "Look up `var-sym` in `analysis` (the result of `hooks/ns-analysis`, keyed by language)- and return true iff kondo recorded a non-empty arity for it. clj-kondo only emits- arities for `defn`-style fns — `defmulti` and plain `def` have neither `:fixed-arities`- nor `:varargs-min-arity`, so the presence of either is a clean, dynamic signal that- the var is a regular function we can safely nudge.+(defn- ns-analysis+ "Like `hooks/ns-analysis`, but nil when kondo can't read the namespace's cache entry."+ [ns-sym]+ ;; kondo throws on the cache of a cljc namespace with `:deprecated` metadata, e.g. `metabase.legacy-mbql.util`.+ (try+ (hooks/ns-analysis ns-sym)+ (catch Exception _ nil)))- We coerce to boolean and `seq`-check `:fixed-arities` so a hypothetical empty set- doesn't leak through as truthy. The smoke test in `with-redefs-test` empirically- verifies the \"arities iff `defn`\" invariant against a real `clj-kondo` run — if a- future kondo release starts emitting arities for `defmulti` it will fail there- rather than silently producing wrong nudges."- [analysis var-sym]- (boolean- (some (fn [lang-vars]- (when-let [v (get lang-vars var-sym)]- (or (seq (:fixed-arities v))- (:varargs-min-arity v))))- (vals analysis))))+(defn- defn-arity?+ "Whether `var-sym` is a `defn`-style function on the `:clj` side of `analysis`, the result of `hooks/ns-analysis`.+ Follows `potemkin/import-vars` re-exports to the defining namespace, and is false when that is ambiguous."+ ([analysis var-sym]+ (defn-arity? analysis var-sym #{}))+ ([analysis var-sym seen]+ ;; Kondo records arities only for `defn`-style fns: a `defmulti` or a plain `def` has neither `:fixed-arities`+ ;; nor `:varargs-min-arity`, so either one marks a regular function we can safely nudge.+ ;; The smoke tests in `with-redefs-test` check this against a real kondo run, so a kondo release that starts+ ;; recording arities for `defmulti` fails there instead of producing wrong nudges.+ (boolean+ (or (when-let [v (get-in analysis [:clj var-sym])]+ ;; The `seq` keeps an empty `:fixed-arities` set from counting as an arity.+ (or (seq (:fixed-arities v))+ (:varargs-min-arity v)))+ ;; Kondo's `hooks/ns-analysis` strips each var's `:imported-ns`, so search every namespace this one re-exports+ ;; from. Any of them could be the one the var was imported from, so all must be readable, and all that define+ ;; the name must be defns.+ ;; Only the namespaces on the current path are in `seen`, so a source reached by two routes is still followed.+ (let [proxied (into {} (for [ns-sym (get-in analysis [:clj :proxied-namespaces])+ :when (not (seen ns-sym))]+ [ns-sym (:clj (ns-analysis ns-sym))]))+ ;; Skip private vars: a renamed import can share its name with an unrelated private var in a source.+ sources (for [[ns-sym vars] proxied+ :let [v (get vars var-sym)]+ :when (and v (not (:private v)))]+ ns-sym)]+ (and (every? some? (vals proxied))+ (seq sources)+ (every? #(defn-arity? {:clj (proxied %)} var-sym (conj seen %)) sources)))))))(defn- safely-nudgeable-lhs?"Is this LHS a regular function (defn) according to kondo's analysis?@@ -42,7 +61,7 @@(let [resolved (hooks/resolve {:name (hooks/sexpr lhs)})ns-sym (:ns resolved)](and (symbol? ns-sym) ; not :clj-kondo/unknown-namespace- (defn-arity? (hooks/ns-analysis ns-sym) (:name resolved))))))+ (defn-arity? (ns-analysis ns-sym) (:name resolved))))))(defn lint-with-redefs"Suggest `with-dynamic-fn-redefs` when every LHS is known to be a `defn`-style var.@@ -55,9 +74,10 @@The LHS check uses kondo's own analysis cache rather than a hand-maintained list ofmultimethod names — adding a new `defmulti` doesn't require touching this hook."- [{:keys [node]}]+ [{:keys [node lang]}](let [[_with-redefs bindings-vec] (:children node)]- (when (hooks/vector-node? bindings-vec)+ ;; The `with-dynamic-fn-redefs` macro is clj-only, so a var only counts as a defn by its clj definition.+ (when (and (= :clj lang) (hooks/vector-node? bindings-vec))(let [pairs (partition-all 2 (:children bindings-vec))](when (and (seq pairs)(every? (fn [[lhs rhs]]diff --git a/.clj-kondo/test/hooks/clojure/core/with_redefs_test.clj b/.clj-kondo/test/hooks/clojure/core/with_redefs_test.cljindex b6778106ace..33ce5d9f0d0 100644--- a/.clj-kondo/test/hooks/clojure/core/with_redefs_test.clj+++ b/.clj-kondo/test/hooks/clojure/core/with_redefs_test.clj@@ -22,7 +22,21 @@a-multimethod {:ns example.ns :name a-multimethod} ; defmulti — no aritiescan-read? {:ns example.ns :name can-read?} ; defmultican-query? {:ns example.ns :name can-query?} ; defmulti- a-value {:ns example.ns :name a-value}}) ; (def a-value 42) — no arities+ a-value {:ns example.ns :name a-value} ; (def a-value 42) — no arities+ ;; Re-exported with potemkin/import-vars: kondo records no arity here, see `proxied-vars`.+ proxied-fn {:ns example.ns :name proxied-fn}+ proxied-multi {:ns example.ns :name proxied-multi}+ proxied-clash {:ns example.ns :name proxied-clash}})++(def ^:private proxied-vars+ "How `example.real-ns`, where the `proxied-*` vars are really defined, looks in its own analysis."+ '{proxied-fn {:ns example.real-ns :name proxied-fn :fixed-arities #{1}}+ proxied-multi {:ns example.real-ns :name proxied-multi}+ proxied-clash {:ns example.real-ns :name proxied-clash :fixed-arities #{1}}})++(def ^:private other-proxied-vars+ "A second re-export source that also defines `proxied-clash`, as a defmulti."+ '{proxied-clash {:ns example.other-ns :name proxied-clash}})(defn- stub-resolve [{nm :name}](when (symbol? nm)@@ -31,26 +45,45 @@{:ns 'example.ns :name bare}{:ns :clj-kondo/unknown-namespace :name bare}))))-(defn- stub-ns-analysis [_ns-sym]- ;; All stubbed vars live in `example.ns` for simplicity.- {:clj stub-vars})+(def ^:private stub-analyses+ {'example.ns {:clj (assoc stub-vars :proxied-namespaces '(example.real-ns example.other-ns))}+ 'example.real-ns {:clj proxied-vars}+ 'example.other-ns {:clj other-proxied-vars}})++(defn- analyses+ "A stand-in for `hooks/ns-analysis` that serves `stub-analyses` with `overrides` merged in, both keyed by namespace.+ Like kondo, it returns `{}` for a namespace that has no cache entry."+ [overrides]+ (let [by-ns (merge stub-analyses overrides)]+ #(get by-ns % {})))++(def ^:private stub-ns-analysis+ (analyses nil))++(defn- cljs-defn+ "How kondo records a defn, for the `:cljs` side of a namespace's analysis."+ [ns-sym var-sym]+ {:ns ns-sym, :name var-sym, :fixed-arities #{1}})(defn- lint"Run the hook on the given source. Pass a string to preserve reader-macro literals like`#(...)` (whose `:fn` node tag is distinct from a regular list); pass a quoted formfor the common case where exact node shape doesn't matter. The optional`analysis-fn` stub overrides `hooks/ns-analysis` for tests that want to simulate a- cache miss or other non-default cache state."+ cache miss or other non-default cache state, and `lang` the language kondo is linting the form as."([src] (lint src stub-ns-analysis))- ([src analysis-fn]- (binding [clj-kondo.impl.utils/*ctx* {:config {:linters {:metabase/prefer-with-dynamic-fn-redefs {:level :warning}}}- :ignores (atom nil)- :findings (atom [])- :namespaces (atom {})}]+ ([src analysis-fn] (lint src analysis-fn :clj))+ ([src analysis-fn lang]+ (binding [clj-kondo.impl.utils/*ctx*+ {:config {:linters {:metabase/prefer-with-dynamic-fn-redefs {:level :warning}}}+ :ignores (atom nil)+ :findings (atom [])+ :namespaces (atom {})}](with-redefs [hooks/resolve stub-resolvehooks/ns-analysis analysis-fn](hooks.clojure.core.with-redefs/lint-with-redefs- {:node (hooks/parse-string (if (string? src) src (pr-str src)))}))+ {:node (hooks/parse-string (if (string? src) src (pr-str src)))+ :lang lang}))@(:findings clj-kondo.impl.utils/*ctx*))))(deftest ^:synchronized flags-when-every-lhs-is-defn-test@@ -80,7 +113,10 @@(is (=? [{:type :metabase/prefer-with-dynamic-fn-redefs}](lint '(with-redefs [plain-fn (constantly 1)plain-fn-2 (fn [] 2)]- (plain-fn)))))))+ (plain-fn))))))+ (testing "a defn re-exported with potemkin/import-vars"+ (is (=? [{:type :metabase/prefer-with-dynamic-fn-redefs}]+ (lint '(with-redefs [proxied-fn (constantly 1)] :body))))))(deftest ^:synchronized skips-multimethod-and-value-targets-test(testing "defmulti — no arity in analysis, so don't nudge"@@ -99,54 +135,143 @@a-multimethod (fn [& _] nil)]:body))))))-(defn- spit-fixture! [^java.io.File f content]- (.mkdirs (.getParentFile f))- (spit f content))+(deftest ^:synchronized judges-vars-by-their-clj-definition-test+ (testing "a cljc var that is a defn only on the cljs side"+ (is (= [] (lint '(with-redefs [a-multimethod (fn [& _] nil)] :body)+ (analyses {'example.ns {:clj stub-vars+ :cljs {'a-multimethod (cljs-defn 'example.ns 'a-multimethod)}}})))))+ (testing "a cljc re-export source that is a defn only on the cljs side"+ (is (= [] (lint '(with-redefs [proxied-multi (fn [& _] nil)] :body)+ (analyses {'example.real-ns+ {:clj proxied-vars+ :cljs {'proxied-multi (cljs-defn 'example.real-ns 'proxied-multi)}}})))))+ (testing "a form kondo lints as cljs, where `with-dynamic-fn-redefs` doesn't exist"+ (is (= [] (lint '(with-redefs [plain-fn (fn [x] x)] :body) stub-ns-analysis :cljs)))))++(deftest ^:synchronized skips-uncertain-re-exports-test+ (testing "a defmulti re-exported with potemkin/import-vars"+ (is (= [] (lint '(with-redefs [proxied-multi (fn [& _] nil)] :body)))))+ (testing "a name defined by two re-export sources, only one of them a defn"+ (is (= [] (lint '(with-redefs [proxied-clash (fn [& _] nil)] :body)))))+ (testing "a re-export source kondo can't read might be where the var comes from"+ (is (= [] (lint '(with-redefs [proxied-clash (fn [& _] nil)] :body)+ (fn [ns-sym]+ (when (= 'example.other-ns ns-sym)+ (throw (ClassCastException. "unreadable cache entry")))+ (stub-ns-analysis ns-sym))))))+ (testing "a re-export source missing from the cache might be where the var comes from"+ (is (= [] (lint '(with-redefs [proxied-clash (fn [& _] nil)] :body)+ (analyses {'example.other-ns {}})))))+ (testing "a re-export source with no clj cache entry might be where the var comes from"+ (is (= [] (lint '(with-redefs [proxied-clash (fn [& _] nil)] :body)+ (analyses {'example.other-ns {:cljs other-proxied-vars}})))))+ (testing "a private defn in a re-export source can't be what a var of the same name was imported from"+ (is (= [] (lint '(with-redefs [proxied-fn (fn [& _] nil)] :body)+ (analyses {'example.real-ns {:clj (assoc-in proxied-vars ['proxied-fn :private] true)}}))))))++(deftest ^:synchronized follows-nested-re-exports-test+ (testing "a defn reached both directly and through a second re-export"+ (is (=? [{:type :metabase/prefer-with-dynamic-fn-redefs}]+ (lint '(with-redefs [proxied-fn (fn [& _] nil)] :body)+ (analyses+ {'example.ns {:clj (assoc stub-vars :proxied-namespaces '(example.facade example.real-ns))}+ 'example.facade {:clj {'proxied-fn {:ns 'example.facade, :name 'proxied-fn}+ :proxied-namespaces '(example.real-ns)}}}))))))++(deftest ^:synchronized terminates-on-cyclic-re-exports-test+ (testing "two namespaces that re-export from each other"+ (is (= [] (lint '(with-redefs [proxied-fn (fn [& _] nil)] :body)+ (analyses {'example.ns {:clj (assoc stub-vars :proxied-namespaces '(example.cyclic))}+ 'example.cyclic {:clj {'proxied-fn {:ns 'example.cyclic, :name 'proxied-fn}+ :proxied-namespaces '(example.ns)}}}))))))(defn- delete-tree! [^java.io.File f](when (.isDirectory f)(run! delete-tree! (.listFiles f)))(.delete f))-(defn- run-kondo-twice- "Lint `src` first to populate `cache-dir` (mirroring the real `kondo --lint src test`- pipeline), then lint `test-file` against that cache and return its findings. The- two-pass shape is required because the hook reads its decisions from the cache, which- is only written *after* a file is analysed."- [cache-dir src-file test-file]- (kondo/run! {:lint [(.getPath src-file)] :cache-dir cache-dir :config-dir ".clj-kondo"})- (:findings (kondo/run! {:lint [(.getPath test-file)] :cache-dir cache-dir :config-dir ".clj-kondo"})))+(defn- kondo-findings+ "Findings from a real kondo run over `test-files`, each a map of relative path to content.+ Lints `src-files` first to fill the cache, as `kondo --lint src test` does.+ The hook reads its decisions from the cache, which is only written after a file is analysed."+ [src-files test-files]+ (let [tmp-dir (.toFile (java.nio.file.Files/createTempDirectory+ "with-redefs-smoke" (into-array java.nio.file.attribute.FileAttribute [])))+ lint! (fn [dir files]+ (doseq [[path content] files]+ (io/make-parents tmp-dir dir path)+ (spit (io/file tmp-dir dir path) content))+ (kondo/run! {:lint [(str (io/file tmp-dir dir))]+ :cache-dir (str (io/file tmp-dir "cache"))+ :config-dir ".clj-kondo"}))]+ (try+ (lint! "src" src-files)+ (:findings (lint! "test" test-files))+ (finally (delete-tree! tmp-dir)))))(deftest ^:synchronized integration-arities-iff-defn-smoke-test(testing "real kondo run validates the load-bearing invariant: only `defn`-style varsget arities recorded — `defmulti` and plain `def` do not. If a future kondorelease breaks this, the smoke test fails here rather than the hook silentlyproducing wrong nudges."- (let [tmp-dir (.toFile (java.nio.file.Files/createTempDirectory- "with-redefs-smoke" (into-array java.nio.file.attribute.FileAttribute [])))- cache-dir (str tmp-dir "/cache")- src (io/file tmp-dir "smoke_fixture.clj")- tst (io/file tmp-dir "smoke_fixture_test.clj")]- (try- (spit-fixture! src "(ns smoke-fixture)+ (let [findings (kondo-findings+ {"smoke_fixture.clj" "(ns smoke-fixture)(defmulti the-multi {:arglists '([x])} (fn [x] x))(defn the-defn [x] x)(def the-value 42)-")- (spit-fixture! tst "(ns smoke-fixture-test+"}+ {"smoke_fixture_test.clj" "(ns smoke-fixture-test(:require [smoke-fixture :as f]))(with-redefs [f/the-multi (constantly nil)] :a)(with-redefs [f/the-defn (constantly nil)] :b)(with-redefs [f/the-value (constantly nil)] :c)-")- (let [findings (run-kondo-twice cache-dir src tst)- nudges (filter #(= :metabase/prefer-with-dynamic-fn-redefs (:type %)) findings)- nudge-rows (set (map :row nudges))]- ;; Row 3 = the-multi, row 4 = the-defn, row 5 = the-value (matches the- ;; with-redefs lines in the test fixture above).- (testing "the defn binding gets nudged"- (is (contains? nudge-rows 4)))- (testing "the defmulti and def bindings do not get nudged"- (is (not (contains? nudge-rows 3)))- (is (not (contains? nudge-rows 5)))))- (finally (delete-tree! tmp-dir))))))+"})+ nudges (filter #(= :metabase/prefer-with-dynamic-fn-redefs (:type %)) findings)+ nudge-rows (set (map :row nudges))]+ ;; Row 3 = the-multi, row 4 = the-defn, row 5 = the-value (matches the+ ;; with-redefs lines in the test fixture above).+ (testing "the defn binding gets nudged"+ (is (contains? nudge-rows 4)))+ (testing "the defmulti and def bindings do not get nudged"+ (is (not (contains? nudge-rows 3)))+ (is (not (contains? nudge-rows 5)))))))++(deftest ^:synchronized integration-follows-potemkin-import-vars-smoke-test+ (testing "real kondo run: only the re-exported defn (row 4) gets nudged"+ ;; Row 5 is a re-exported defmulti. Row 6 is a defmulti imported under the name of a private defn in its source.+ (is (=? [{:type :metabase/prefer-with-dynamic-fn-redefs, :row 4}]+ (kondo-findings+ {"smoke_impl.clj" "(ns smoke-impl)+(defn the-defn [x] x)+(defmulti the-multi {:arglists '([x])} (fn [x] x))+(defn- renamed [x] x)+"+ "smoke_core.clj" "(ns smoke-core+ (:require [potemkin :as p]+ [smoke-impl]))+(p/import-vars+ [smoke-impl+ the-defn+ the-multi])+(p/import-def smoke-impl/the-multi renamed)+"}+ {"smoke_core_test.clj" "(ns smoke-core-test+ (:require+ [smoke-core :as core]))+(with-redefs [core/the-defn (constantly nil)] :a)+(with-redefs [core/the-multi (constantly nil)] :b)+(with-redefs [core/renamed (constantly nil)] :c)+"})))))++(deftest ^:synchronized integration-deprecated-cljc-namespace-smoke-test+ (testing "real kondo run: a var in a deprecated cljc namespace is skipped without a hook error"+ (is (= [] (kondo-findings+ {"smoke_deprecated.cljc" "(ns smoke-deprecated {:deprecated \"use something else\"})+(defn the-defn [x] x)+"}+ {"smoke_deprecated_test.clj" "(ns smoke-deprecated-test+ {:clj-kondo/config '{:linters {:deprecated-namespace {:level :off}}}}+ (:require+ [smoke-deprecated :as dep]))+(with-redefs [dep/the-defn (constantly nil)] :a)+"})))))