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
Report dynamic requires which linter cannot verify (#78082)
Chris Trutere24150083f0b
diff --git a/.clj-kondo/ratchets.edn b/.clj-kondo/ratchets.ednindex 81c2b836c15..dc11a4b82f3 100644--- a/.clj-kondo/ratchets.edn+++ b/.clj-kondo/ratchets.edn@@ -24,7 +24,7 @@:metabase/disallow-hardcoded-driver-names-in-tests 34:metabase/discourage-millis-duration 1:metabase/i-like-making-cams-eyes-bleed-with-horrifically-long-tests 13- :metabase/modules 10+ :metabase/modules 23:metabase/namespace-name 4:metabase/prefer-with-dynamic-fn-redefs 28:metabase/test-helpers-use-non-thread-safe-functions 18diff --git a/.clj-kondo/src/hooks/clojure/core/require.clj b/.clj-kondo/src/hooks/clojure/core/require.cljindex 3928ee41df7..669091d8032 100644--- a/.clj-kondo/src/hooks/clojure/core/require.clj+++ b/.clj-kondo/src/hooks/clojure/core/require.clj@@ -20,6 +20,23 @@:message error:type :metabase/modules))))))))+(defn- lint-dynamic-require* [node current-ns config]+ (when (and (not (modules/ignored-namespace? config current-ns))+ (modules/module current-ns))+ (hooks/reg-finding! (assoc (meta node)+ :message "Module dependency cannot be statically determined from a dynamic require"+ :type :metabase/modules))))++(defn- quoted-require? [node]+ (or (= (hooks/tag node) :quote)+ (and (= (hooks/tag node) :list)+ (let [[x] (:children node)]+ (and (hooks/token-node? x)+ (= (hooks/sexpr x) 'quote))))))++(defn- keyword-node? [node]+ (keyword? (hooks/sexpr node)))+(defn- unwrap-require [node](case (hooks/tag node);; some.namespace of some.namespace/some-var@@ -42,7 +59,11 @@;; something like [some.namespace :as whatever]:vector- (recur (first (:children node)))))+ (recur (first (:children node)))++ ;; anything else -- e.g. a runtime expression like `(requiring-resolve (:failing-test-var opts))` --+ ;; has no statically-known namespace+ nil))(comment(unwrap-require (-> "(requiring-resolve 'metabase.notification.payload.execute/execute-dashboard)"@@ -51,20 +72,24 @@second)))(defn- lint-require* [node current-ns config]- (let [[_require & args] (:children node)- required-namespaces (->> (keep unwrap-require args)- ;; `require` runs through `cc/load-libs` which allows- ;; #{:as :reload :reload-all :require :use :verbose :refer :as-alias}- ;; as args. If you use a REPL tool that applies clj-kondo linting (e.g., Clojure MCP)- ;; you may wish to `:reload` or `:reload-all` a namespace. So we shouldn't assume all- ;; args are keywords:- (remove keyword?))]- (lint* node current-ns config required-namespaces)))+ (let [[_require & args] (:children node)+ ;; Keywords such as `:reload` and `:reload-all` are options, not namespace dependencies.+ namespace-nodes (remove keyword-node? args)+ static-nodes (filter quoted-require? namespace-nodes)+ required-namespaces (->> (keep unwrap-require static-nodes)+ (filter simple-symbol?))]+ (lint* node current-ns config required-namespaces)+ (when (or (not= (count static-nodes) (count namespace-nodes))+ (not= (count required-namespaces) (count static-nodes)))+ (lint-dynamic-require* node current-ns config))))(defn- lint-requiring-resolve* [node current-ns config](let [[_requiring-resolve symb-node] (:children node)required-namespace (unwrap-require symb-node)]- (lint* node current-ns config [required-namespace])))+ (if (and (quoted-require? symb-node)+ (simple-symbol? required-namespace))+ (lint* node current-ns config [required-namespace])+ (lint-dynamic-require* node current-ns config))))(defn lint-require [x](lint-require* (:node x) (:ns x) (modules/config x))diff --git a/.clj-kondo/src/hooks/clojure/test.clj b/.clj-kondo/src/hooks/clojure/test.cljindex e577f1a696b..3c93457ba27 100644--- a/.clj-kondo/src/hooks/clojure/test.clj+++ b/.clj-kondo/src/hooks/clojure/test.clj@@ -171,6 +171,7 @@(str/starts-with? (name ns-symb) prefix))["build-drivers.""build."+ "hooks." ; clj-kondo hook tests, outside the module system"i18n." ; bin/i18n"lint-migrations-file-test""load-namespaces." ; bin/load-namespacesdiff --git a/.clj-kondo/test/hooks/clojure/core/require_test.clj b/.clj-kondo/test/hooks/clojure/core/require_test.cljnew file mode 100644index 00000000000..06fd11e7b55--- /dev/null+++ b/.clj-kondo/test/hooks/clojure/core/require_test.clj@@ -0,0 +1,65 @@+(ns hooks.clojure.core.require-test+ (:require+ [clj-kondo.hooks-api :as hooks]+ [clj-kondo.impl.utils]+ [clojure.test :refer :all]+ [hooks.clojure.core.require]))++(def ^:private config+ '{:linters {:metabase/modules {:level :warning}}+ :metabase/modules {search {:uses #{}}+ task {:api #{metabase.task.core}}}})++(defn- lint [hook form current-ns]+ (binding [clj-kondo.impl.utils/*ctx* {:config config+ :ignores (atom nil)+ :findings (atom [])+ :namespaces (atom {})}]+ (hook {:node (hooks/parse-string (pr-str form))+ :ns current-ns+ :config config})+ @(:findings clj-kondo.impl.utils/*ctx*)))++(defn- lint-require [form current-ns]+ (lint hooks.clojure.core.require/lint-require form current-ns))++(defn- lint-requiring-resolve [form current-ns]+ (lint hooks.clojure.core.require/lint-requiring-resolve form current-ns))++(def ^:private dynamic-require-finding+ {:message "Module dependency cannot be statically determined from a dynamic require"+ :type :metabase/modules})++(def ^:private cross-module-finding+ {:message "Module task should not be used in the search module. [:metabase/modules search :uses]"+ :type :metabase/modules})++(deftest ^:parallel dynamic-requiring-resolve-test+ (testing "outside the module system"+ (is (= [] (lint-requiring-resolve '(requiring-resolve (:failing-test-var opts)) 'dev))))+ (testing "inside a module"+ (are [form] (=? [dynamic-require-finding]+ (lint-requiring-resolve form 'metabase.search.core))+ '(requiring-resolve (:failing-test-var opts))+ '(requiring-resolve @some-atom)+ '(requiring-resolve some-local)+ '(requiring-resolve "not-a-symbol"))))++(deftest ^:parallel dynamic-require-test+ (testing "outside the module system"+ (is (= [] (lint-require '(require some-local) 'dev))))+ (testing "inside a module"+ (is (=? [dynamic-require-finding]+ (lint-require '(require some-local) 'metabase.search.core)))))++(deftest ^:parallel static-requiring-resolve-still-linted-test+ (is (=? [cross-module-finding]+ (lint-requiring-resolve '(requiring-resolve 'metabase.task.impl/go)+ 'metabase.search.core))+ "a statically-known cross-module violation is still reported"))++(deftest ^:parallel static-require-still-linted-test+ (is (=? [cross-module-finding]+ (lint-require '(require 'metabase.task.impl :reload)+ 'metabase.search.core))+ "a statically-known cross-module violation is still reported while keyword flags are ignored"))diff --git a/enterprise/backend/src/metabase_enterprise/audit_app/interface.clj b/enterprise/backend/src/metabase_enterprise/audit_app/interface.cljindex e80885b80a5..4a1c21ecabd 100644--- a/enterprise/backend/src/metabase_enterprise/audit_app/interface.clj+++ b/enterprise/backend/src/metabase_enterprise/audit_app/interface.clj@@ -39,5 +39,7 @@(let [query-type (keyword query-type)ns-str (namespace query-type)](when (contains? loadable-namespaces ns-str)+ ;; The namespace comes from the fixed allowlist above.+ #_{:clj-kondo/ignore [:metabase/modules]}(classloader/require (symbol ns-str)))(apply internal-query query-type args)))diff --git a/src/metabase/api/common/internal.clj b/src/metabase/api/common/internal.cljindex 24ac8da0e9f..d222a2e1db4 100644--- a/src/metabase/api/common/internal.clj+++ b/src/metabase/api/common/internal.clj@@ -22,6 +22,8 @@(walk/postwalk(fn [x](if (symbol? x)+ ;; macro schemas can contain symbols from any module+ #_{:clj-kondo/ignore [:metabase/modules]}(try @(requiring-resolve x)(catch Exception _ x)) x))form))diff --git a/src/metabase/cmd/copy.clj b/src/metabase/cmd/copy.cljindex 8ac16046e42..3e2f8bf52c4 100644--- a/src/metabase/cmd/copy.clj+++ b/src/metabase/cmd/copy.clj@@ -456,6 +456,8 @@(doseq [ns-symb (cond->> (vals models.resolution/model->namespace)(not config/ee-available?)(remove #(str/starts-with? (str %) "metabase-enterprise")))]+ ;; Copying the application database requires every registered model namespace.+ #_{:clj-kondo/ignore [:metabase/modules]}(classloader/require ns-symb));; make sure the source database is up-do-date. Skip the encryption check: the source may legitimately be unencrypted;; while MB_ENCRYPTION_SECRET_KEY is set for the target (enabling encryption while migrating off H2); rows are copieddiff --git a/src/metabase/cmd/core.clj b/src/metabase/cmd/core.cljindex a3d4fa3ffa4..9e479e428c5 100644--- a/src/metabase/cmd/core.clj+++ b/src/metabase/cmd/core.clj@@ -44,6 +44,8 @@"Resolves enterprise command by symbol and calls with args, or else throws error if not EE"[symb & args](let [f (try+ ;; Enterprise command symbols are fixed at the call sites below.+ #_{:clj-kondo/ignore [:metabase/modules]}(classloader/require (symbol (namespace symb)))(or (resolve symb)(throw (ex-info (trs "{0} does not exist" symb) {})))diff --git a/src/metabase/cmd/driver_methods.clj b/src/metabase/cmd/driver_methods.cljindex 04ed8041a24..6fdf51fa967 100644--- a/src/metabase/cmd/driver_methods.clj+++ b/src/metabase/cmd/driver_methods.clj@@ -13,7 +13,10 @@:when (and (or (starts-with? "metabase.driver")(starts-with? "metabase.test.data"))(do- (u/ignore-exceptions (classloader/require ns-symb))+ ;; Driver namespaces are discovered from the runtime classpath.+ (u/ignore-exceptions+ #_{:clj-kondo/ignore [:metabase/modules]}+ (classloader/require ns-symb))(find-ns ns-symb)))]ns-symb)))diff --git a/src/metabase/cmd/env_var_dox.clj b/src/metabase/cmd/env_var_dox.cljindex 13515c51f7e..be538d723d4 100644--- a/src/metabase/cmd/env_var_dox.clj+++ b/src/metabase/cmd/env_var_dox.clj@@ -28,6 +28,8 @@:when (and(str/includes? (name ns-symb) "metabase")(not (str/includes? (name ns-symb) "test")))]+ ;; documentation generation deliberately loads every discovered Metabase namespace+ #_{:clj-kondo/ignore [:metabase/modules]}(require ns-symb))(prep-settings @setting/registered-settings));; Or supply a set of namespaces to load@@ -35,6 +37,8 @@([ns-set](doseq [ns-symb (ns.find/find-namespaces (classpath/system-classpath)):when (ns-set (name ns-symb))]+ ;; callers explicitly select the namespaces to document+ #_{:clj-kondo/ignore [:metabase/modules]}(require ns-symb))(prep-settings @setting/registered-settings)))diff --git a/src/metabase/core/bootstrap.clj b/src/metabase/core/bootstrap.cljindex 94511e0eb0d..ab7beeb1acd 100644--- a/src/metabase/core/bootstrap.clj+++ b/src/metabase/core/bootstrap.clj@@ -51,6 +51,8 @@"complexity-score" 'metabase-enterprise.data-complexity-score.cli/entrypointnil)](if startup+ ;; the target is selected from the fixed standalone-mode dispatch above+ #_{:clj-kondo/ignore [:metabase/modules]}((requiring-resolve startup) args)(do (binding [*out* *err*](output! (str "Unknown mode: " mode))diff --git a/src/metabase/core/core.clj b/src/metabase/core/core.cljindex 8c961bcf90f..644f9d4261a 100644--- a/src/metabase/core/core.clj+++ b/src/metabase/core/core.clj@@ -302,6 +302,8 @@(when (not-empty mb-trace-str)(log/warn "WARNING: You have enabled namespace tracing, which could log sensitive information like db passwords.")(doseq [namespace (map symbol (str/split mb-trace-str #",\s*"))]+ ;; tracing namespaces are supplied by the user at runtime+ #_{:clj-kondo/ignore [:metabase/modules]}(try (require namespace)(catch Throwable _(throw (ex-info "A namespace you specified with MB_NS_TRACE could not be required" {:namespace namespace}))))diff --git a/src/metabase/models/interface.clj b/src/metabase/models/interface.cljindex 088e7d4a2e9..e3a4fe94a4e 100644--- a/src/metabase/models/interface.clj+++ b/src/metabase/models/interface.clj@@ -783,7 +783,8 @@([fn-symb :- qualified-symbol?perms-set :- [:set :string]]- (let [f (requiring-resolve fn-symb)]+ ;; resolves the permissions implementation lazily to avoid a models -> permissions load cycle+ (let [f #_{:clj-kondo/ignore [:metabase/modules]} (requiring-resolve fn-symb)](assert f)(u/prog1 (f (current-user-permissions-set) perms-set)(log/tracef "Perms check: %s -> %s" (pr-str (list fn-symb (current-user-permissions-set) perms-set)) <>)))))diff --git a/src/metabase/models/resolution.clj b/src/metabase/models/resolution.cljindex dafda6edc50..d3785288fdd 100644--- a/src/metabase/models/resolution.clj+++ b/src/metabase/models/resolution.clj@@ -174,7 +174,8 @@;; Always require the model's namespace when we know it. It is fast, and there can be race conditions between;; before side effects like `deftransforms` and `define-before-insert` have run(when-let [nspace (get model->namespace x)]- ;; [[classloader/require]] for thread safety+ ;; The registry selects the namespace dynamically; use [[classloader/require]] for thread safety.+ #_{:clj-kondo/ignore [:metabase/modules]}(classloader/require nspace)))x)diff --git a/src/metabase/plugins/init_steps.clj b/src/metabase/plugins/init_steps.cljindex 8bb910b9a35..cc66ad5d9cb 100644--- a/src/metabase/plugins/init_steps.clj+++ b/src/metabase/plugins/init_steps.clj@@ -18,6 +18,8 @@(defmethod do-init-step! :load-namespace [{nmspace :namespace}](log/debug (u/format-color 'blue "Loading plugin namespace %s..." nmspace))+ ;; Plugin namespaces are supplied by plugin manifests at runtime.+ #_{:clj-kondo/ignore [:metabase/modules]}(classloader/require (symbol nmspace)))(defmethod do-init-step! :register-jdbc-driver [{class-name :class}]diff --git a/src/metabase/premium_features/defenterprise.clj b/src/metabase/premium_features/defenterprise.cljindex 4b8d8976eaf..ccef851c487 100644--- a/src/metabase/premium_features/defenterprise.clj+++ b/src/metabase/premium_features/defenterprise.clj@@ -45,7 +45,10 @@availability of EE code and the necessary premium feature. Returns a fn which, when invoked, applies its args to oneof the EE implementation, the OSS implementation, or the fallback function."[ee-ns ee-fn-name]- (let [try-require-ee-ns-once (delay (u/ignore-exceptions (classloader/require ee-ns)))]+ (let [try-require-ee-ns-once (delay (u/ignore-exceptions+ ;; The namespace is supplied by `defenterprise` at macro-expansion time.+ #_{:clj-kondo/ignore [:metabase/modules]}+ (classloader/require ee-ns)))](fn [& args]@try-require-ee-ns-once(let [{:keys [ee oss feature fallback]} (get @registry ee-fn-name)]