Skip to content

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

Count requires from a nested module into its ancestors (#84120)

Chris Truter51025ffc8e70
diff --git a/.claude/agents/modules-backend-expert.md b/.claude/agents/modules-backend-expert.md
index bb2cb9c2ac4..3fe61254056 100644
--- a/.claude/agents/modules-backend-expert.md
+++ b/.claude/agents/modules-backend-expert.md
@@ -20,7 +20,7 @@ The preloaded `backend-module-conventions` skill covers the standard namespaces,
| `.clj-kondo/src/hooks/metabase/toucan/db_ns.clj` | `:metabase/t2-query-namespace` lint (app-DB calls only in `<module>.db`). |
| `.clj-kondo/config/modules/ratchets.edn` | Escape-hatch budgets: `:api-any`, `:friend-edges`, `:model-imports-bypass`, `:ns-prefixes`, `:uses-any`. |
| `.clj-kondo/ratchets.edn` | Inline-ignore budgets, incl. `:metabase/modules` and `:metabase/t2-query-namespace`. |
-| `dev.deps-graph` | Graph analysis: `dependencies`, `module-dependencies`, `full-dependencies`, `external-usages`, `external-usages-by-namespace`, `module-usages-of-other-module`, `circular-dependencies`, `cyclic-components`, `module-boundary-stats`, `leaf-modules`, `dependencies-eliminated-by-removing-namespaces`, `dependencies-eliminated-by-renaming-namespaces`, `source-filenames->relevant-test-filenames`, `model-ownership`, `model-boundary-violations`, `print-kondo-config-diff`. |
+| `dev.deps-graph` | Graph analysis: `dependencies`, `module-dependencies`, `full-dependencies`, `external-usages`, `external-usages-by-namespace`, `module-usages-of-other-module`, `circular-dependencies`, `cyclic-components`, `module-boundary-stats`, `upward-references`, `leaf-modules`, `dependencies-eliminated-by-removing-namespaces`, `dependencies-eliminated-by-renaming-namespaces`, `source-filenames->relevant-test-filenames`, `model-ownership`, `model-boundary-violations`, `print-kondo-config-diff`. |
| `dev.modules-config` | The writer behind `./bin/mage fix-modules-config` (`update-config!` warm, `fix-config!` cold). |
| `dev.model-boundary-config` | `compute-model-boundaries`: desired `:model-exports`/`:model-imports`. |
| `dev.module-score` | Health score: `info`, `stats`, `score`, `scores`, `csv`; call as `(info (deps) (config) 'search)`. |
diff --git a/dev/src/dev/deps_graph.clj b/dev/src/dev/deps_graph.clj
index 556a9c8100d..d6bfcc8b269 100644
--- a/dev/src/dev/deps_graph.clj
+++ b/dev/src/dev/deps_graph.clj
@@ -953,15 +953,73 @@
;;;; Module boundary analysis
+(defn module-cycles
+ "Each module cycle with its members and [[module-graph/cycle-metrics]], largest first.
+ The strong bridges are the single dependency edges whose removal splits a cycle."
+ ([]
+ (module-cycles (dependencies)))
+ ([deps]
+ (module-graph/cycles (module-dependencies deps))))
+
+(defn- enterprise-companion?
+ "Whether `module` is the `enterprise/X` companion of OSS module `ancestor`."
+ [module ancestor]
+ (and (= "enterprise" (namespace module))
+ (= ancestor (symbol (name module)))))
+
+(defn- upward-references*
+ "The [[upward-references]] of `deps`, given the [[module-cycles]] of the same `deps` as `cycles`."
+ [deps config cycles]
+ ;; A nested module may use its ancestors' internals, and a parent usually requires its children, so one
+ ;; require back up is enough to make a cycle.
+ ;; The linter allows these requires, so nothing else reports them.
+ (let [module->cycle-members (u/for-map [{:keys [modules]} cycles
+ :let [members (set modules)]
+ module modules]
+ [module members])]
+ (vec (for [{:keys [module], :as file} deps
+ dep (:deps file)
+ :let [ancestor (:module dep)]
+ :when (and (not= module ancestor)
+ (modules/descendant-of? config module ancestor)
+ (not (enterprise-companion? module ancestor)))
+ :let [api (modules/module-api-namespaces config ancestor)]]
+ {:namespace (:namespace file)
+ :module module
+ :required-namespace (:namespace dep)
+ :ancestor ancestor
+ ;; A nil API means `:api :any`, where every namespace is public.
+ :api? (or (nil? api) (contains? api (:namespace dep)))
+ :cycle? (contains? (module->cycle-members module) ancestor)}))))
+
+(defn upward-references
+ "Requires from a module into one of its ancestors.
+ An `enterprise/X` module requiring OSS `X` is expected and left out.
+
+ Returns one map per require:
+
+ - `:namespace`, `:module` the requiring side
+ - `:required-namespace`, `:ancestor` the required side
+ - `:api?` whether the required namespace is in the ancestor's `:api`
+ - `:cycle?` whether the ancestor also reaches the module, closing a cycle"
+ ([]
+ (upward-references (dependencies) (kondo-config)))
+ ([deps config]
+ (upward-references* deps config (module-cycles deps))))
+
(defn module-boundary-stats
"REPL diagnostics for the module graph:
- - `:api-any-namespaces` namespaces exposed by `:api :any` modules
- - `:module-count` configured modules
- - `:scc-module-sizes` modules per cycle, largest first
- - `:scc-namespace-sizes` namespaces per cycle, in the same order
- - `:scc-densities` edges per cycle over the most it could have, in the same order
- - `:scc-cycle-ranks` independent cycles per cycle, in the same order
+ - `:api-any-namespaces` namespaces exposed by `:api :any` modules
+ - `:module-count` configured modules
+ - `:scc-module-sizes` modules per cycle, largest first
+ - `:scc-namespace-sizes` namespaces per cycle, in the same order
+ - `:scc-densities` edges per cycle over the most it could have, in the same order
+ - `:scc-cycle-ranks` independent cycles per cycle, in the same order
+ - `:upward-requires` requires from a module into an ancestor, see [[upward-references]]
+ - `:upward-module-edges` distinct module and ancestor pairs behind them
+ - `:upward-internal-requires` upward requires that reach past the ancestor's `:api`
+ - `:upward-cycle-requires` upward requires that close a cycle
These values are not ratcheted because any source change can move them. Use namespace sizes to track
cycle reduction: splitting a module can grow a cycle's module count without removing namespaces.
@@ -972,24 +1030,22 @@
([deps config]
(let [any-modules (into #{} (keep (fn [[module cfg]] (when (= :any (:api cfg)) module))) config)
ns-counts (frequencies (keep :module deps))
- cycles (module-graph/cycles (module-dependencies deps))]
- {:api-any-namespaces (count (filter #(contains? any-modules (:module %)) deps))
- :module-count (count config)
- :scc-module-sizes (mapv :size cycles)
- :scc-namespace-sizes (mapv (fn [{:keys [modules]}]
- (transduce (map #(get ns-counts % 0)) + 0 modules))
- cycles)
- :scc-densities (mapv #(u/round-to-decimals 2 (:density %)) cycles)
- :scc-cycle-ranks (mapv :cycle-rank cycles)})))
-
-(defn module-cycles
- "Each module cycle with its members and [[module-graph/cycle-metrics]], largest first.
- The strong bridges are the single dependency edges whose removal splits a cycle."
- ([]
- (module-cycles (dependencies)))
- ([deps]
- (module-graph/cycles (module-dependencies deps))))
+ cycles (module-cycles deps)
+ upward (upward-references* deps config cycles)]
+ {:api-any-namespaces (count (filter #(contains? any-modules (:module %)) deps))
+ :module-count (count config)
+ :scc-module-sizes (mapv :size cycles)
+ :scc-namespace-sizes (mapv (fn [{:keys [modules]}]
+ (transduce (map #(get ns-counts % 0)) + 0 modules))
+ cycles)
+ :scc-densities (mapv #(u/round-to-decimals 2 (:density %)) cycles)
+ :scc-cycle-ranks (mapv :cycle-rank cycles)
+ :upward-requires (count upward)
+ :upward-module-edges (count (into #{} (map (juxt :module :ancestor)) upward))
+ :upward-internal-requires (count (remove :api? upward))
+ :upward-cycle-requires (count (filter :cycle? upward))})))
(comment
(module-boundary-stats)
+ (frequencies (map (juxt :module :ancestor) (upward-references)))
(module-cycles))
diff --git a/dev/test/metabase/core/modules_test.clj b/dev/test/metabase/core/modules_test.clj
index 299dfb8ec88..a34a4690a8a 100644
--- a/dev/test/metabase/core/modules_test.clj
+++ b/dev/test/metabase/core/modules_test.clj
@@ -395,14 +395,94 @@
c {}
d {}}]
(testing "stats list each cycle's sizes, rounded density, and cycle rank"
- (is (= {:api-any-namespaces 2
- :module-count 4
- :scc-module-sizes [3]
- :scc-namespace-sizes [4]
- :scc-densities [0.67]
- :scc-cycle-ranks [2]}
+ (is (= {:api-any-namespaces 2
+ :module-count 4
+ :scc-module-sizes [3]
+ :scc-namespace-sizes [4]
+ :scc-densities [0.67]
+ :scc-cycle-ranks [2]
+ :upward-requires 0
+ :upward-module-edges 0
+ :upward-internal-requires 0
+ :upward-cycle-requires 0}
(dev.deps-graph/module-boundary-stats deps config))))))
+(deftest ^:parallel upward-references-test
+ (let [deps '[{:namespace metabase.x.core
+ :module x
+ :deps [{:namespace metabase.x.y.core, :module x.y}]}
+ {:namespace metabase.x.y.core
+ :module x.y
+ :deps [{:namespace metabase.x.core, :module x}
+ {:namespace metabase.x.impl, :module x}
+ {:namespace metabase.x.y.impl, :module x.y}]}
+ {:namespace metabase.w.v.core
+ :module w.v
+ :deps [{:namespace metabase.w.impl, :module w}]}
+ {:namespace metabase.x.y.z.core
+ :module x.y.z
+ ;; In a cycle with `z`, which `x` is not part of.
+ :deps [{:namespace metabase.x.core, :module x}
+ {:namespace metabase.z.core, :module z}]}
+ {:namespace metabase-enterprise.x.core
+ :module enterprise/x
+ :deps [{:namespace metabase.x.impl, :module x}]}
+ {:namespace metabase-enterprise.x.y.core
+ :module enterprise/x.y
+ ;; Module `x.y` is not an ancestor: the ancestors of `enterprise/x.y` are `enterprise/x` and `x`.
+ :deps [{:namespace metabase.x.y.impl, :module x.y}
+ {:namespace metabase.x.impl, :module x}]}
+ {:namespace metabase.z.core
+ :module z
+ :deps [{:namespace metabase.x.core, :module x}
+ {:namespace metabase.x.y.z.core, :module x.y.z}]}]
+ config '{enterprise/x {}
+ enterprise/x.y {}
+ w {:api :any}
+ w.v {}
+ x {}
+ x.y {}
+ x.y.z {}
+ z {}}]
+ (testing "a require into any ancestor is listed, except an enterprise companion requiring its own OSS module"
+ (is (= '[{:namespace metabase.x.y.core
+ :module x.y
+ :required-namespace metabase.x.core
+ :ancestor x
+ :api? true
+ :cycle? true}
+ {:namespace metabase.x.y.core
+ :module x.y
+ :required-namespace metabase.x.impl
+ :ancestor x
+ :api? false
+ :cycle? true}
+ {:namespace metabase.w.v.core
+ :module w.v
+ :required-namespace metabase.w.impl
+ :ancestor w
+ :api? true
+ :cycle? false}
+ {:namespace metabase.x.y.z.core
+ :module x.y.z
+ :required-namespace metabase.x.core
+ :ancestor x
+ :api? true
+ :cycle? false}
+ {:namespace metabase-enterprise.x.y.core
+ :module enterprise/x.y
+ :required-namespace metabase.x.impl
+ :ancestor x
+ :api? false
+ :cycle? false}]
+ (dev.deps-graph/upward-references deps config))))
+ (testing "stats count upward requires, their module pairs, and the internal and cycle-closing subsets"
+ (is (=? {:upward-requires 5
+ :upward-module-edges 4
+ :upward-internal-requires 2
+ :upward-cycle-requires 2}
+ (dev.deps-graph/module-boundary-stats deps config))))))
+
(deftest ^:parallel module-boundary-config-values-have-valid-types-test
(testing "Module boundary keys have the shapes the ratchet counts expect"
(doseq [[module config] (dev.deps-graph/kondo-config)]