Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Fix duplicates of the same namespace name (#81974)

Ben Grabow503349c6aeb6
diff --git a/.github/file-paths.yaml b/.github/file-paths.yaml
index 16e144c0a65..55827f9d1c6 100644
--- a/.github/file-paths.yaml
+++ b/.github/file-paths.yaml
@@ -159,6 +159,8 @@ project_backend_checks:
- "modules/**/*.cljc"
- "modules/**/*.cljs"
- "**deps.edn"
+ - ".clj-kondo/{src,test}/**/*.{clj,cljc,cljs}"
+ - "{resources,test_resources,test_config}/**/*.{clj,cljc,cljs}"
- ".clj-kondo/config.edn"
- ".clj-kondo/config/modules/config.edn"
- ".github/team.json"
diff --git a/dev/test/metabase/core/namespace_uniqueness_test.clj b/dev/test/metabase/core/namespace_uniqueness_test.clj
new file mode 100644
index 00000000000..20dcd3bd5a2
--- /dev/null
+++ b/dev/test/metabase/core/namespace_uniqueness_test.clj
@@ -0,0 +1,63 @@
+(ns metabase.core.namespace-uniqueness-test
+ "Tests that no namespace is declared by more than one file on a single platform's classpath.
+
+ When two files declare the same namespace, `require` loads whichever the classpath reaches first and
+ the other file's vars never exist. Nothing fails: the shadowed file's tests simply never run.
+
+ An EE test that would otherwise take the same `metabase.*` name as its OSS counterpart is named with an
+ `-ee-test` suffix -- see [[metabase.notification.payload.core-ee-test]] and
+ [[metabase.notification.payload.execute-ee-test]]."
+ (:require
+ [clojure.java.classpath :as classpath]
+ [clojure.java.io :as io]
+ [clojure.string :as str]
+ [clojure.test :refer :all]
+ [clojure.tools.namespace.file :as ns.file]
+ [clojure.tools.namespace.find :as ns.find]
+ [clojure.tools.namespace.parse :as ns.parse]
+ [metabase.util :as u]))
+
+(set! *warn-on-reflection* true)
+
+(def ^:private project-root
+ (str (.getCanonicalPath (io/file ".")) "/"))
+
+(defn- relative-path [^java.io.File file]
+ (let [path (.getCanonicalPath file)]
+ (if (str/starts-with? path project-root)
+ (subs path (count project-root))
+ path)))
+
+(defn- declarations
+ "Every `[namespace relative-path]` pair `platform` can load from the classpath directories.
+
+ `platform` is [[ns.find/clj]] or [[ns.find/cljs]]; each includes `.cljc`, so a `.cljc` file is
+ reported under both. `local/src` is skipped: the `:dev` profile puts it on the classpath precisely so a
+ developer can shadow a project namespace locally."
+ [platform]
+ (for [dir (remove #(str/starts-with? (relative-path %) "local/")
+ (classpath/classpath-directories))
+ file (ns.find/find-sources-in-dir dir platform)
+ ;; The classpath includes source directories we do not control.
+ :let [decl (try
+ (ns.file/read-file-ns-decl file (:read-opts platform))
+ (catch Exception _ nil))]
+ ;; Some classpath .clj files have no ns form, e.g. resources/data_readers.clj.
+ :when decl]
+ [(ns.parse/name-from-ns-decl decl) (relative-path file)]))
+
+(defn- collisions
+ "Namespaces `platform` can load from more than one file, as `{namespace [path ...]}`."
+ [platform]
+ (into (sorted-map)
+ (keep (fn [[ns-symb paths]]
+ (when (next paths)
+ [ns-symb (vec (sort paths))])))
+ (u/group-by first second (declarations platform))))
+
+(deftest ^:parallel no-duplicate-namespaces-test
+ (doseq [[platform-name platform] {"clj" ns.find/clj, "cljs" ns.find/cljs}]
+ (testing (str "no namespace is declared by two " platform-name " files\n"
+ "Rename one of them -- see this namespace's docstring for the convention used when an"
+ " EE test shadows its OSS counterpart.")
+ (is (= {} (collisions platform))))))
diff --git a/enterprise/backend/test/metabase/notification/payload/core_test.clj b/enterprise/backend/test/metabase/notification/payload/core_ee_test.clj
similarity index 86%
rename from enterprise/backend/test/metabase/notification/payload/core_test.clj
rename to enterprise/backend/test/metabase/notification/payload/core_ee_test.clj
index b6988a41cda..8d7075d8de4 100644
--- a/enterprise/backend/test/metabase/notification/payload/core_test.clj
+++ b/enterprise/backend/test/metabase/notification/payload/core_ee_test.clj
@@ -1,4 +1,7 @@
-(ns metabase.notification.payload.core-test
+(ns metabase.notification.payload.core-ee-test
+ "EE-only tests for `metabase.notification.payload.core`. Kept in its own ns to avoid
+ colliding with the OSS `metabase.notification.payload.core-test` ns, which lives under
+ `test/` and would otherwise shadow this file on the classpath."
(:require
[clojure.test :refer :all]
[metabase.notification.payload.core :as notification.payload]
diff --git a/mage/src/mage/project_tests.clj b/mage/src/mage/project_tests.clj
index e2c98ba8661..aabd05d866d 100644
--- a/mage/src/mage/project_tests.clj
+++ b/mage/src/mage/project_tests.clj
@@ -16,8 +16,11 @@
'[metabase.core.kondo-ratchet-test
metabase.core.kondo-ratchet-check-test])
+(def ^:private source-layout-check-namespaces
+ '[metabase.core.namespace-uniqueness-test])
+
(def ^:private backend-check-namespaces
- (vec (concat module-check-namespaces ratchet-test-namespaces)))
+ (vec (concat module-check-namespaces ratchet-test-namespaces source-layout-check-namespaces)))
(def ^:private default-suites
"Suites the bare `project-tests` command runs, in order."
diff --git a/mage/test/mage/project_tests_test.clj b/mage/test/mage/project_tests_test.clj
index 9b04a41f6d7..473bf871511 100644
--- a/mage/test/mage/project_tests_test.clj
+++ b/mage/test/mage/project_tests_test.clj
@@ -43,7 +43,11 @@
(testing "both suites run, in order"
(is (= [["clojure" "-M:test"]
["clojure" "-X:dev:dev/test:ee:ee-dev:drivers:drivers-dev:test:ci" ":only"
- "[dev.modules-config-test metabase.core.modules-test metabase.core.kondo-ratchet-test metabase.core.kondo-ratchet-check-test]"]]
+ (str '[dev.modules-config-test
+ metabase.core.modules-test
+ metabase.core.kondo-ratchet-test
+ metabase.core.kondo-ratchet-check-test
+ metabase.core.namespace-uniqueness-test])]]
calls)))
(testing "the failed suite is reported"
(is (= ["migrations"] failed))