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
Add sub-tiers for shared modules (#79013)
Fraser91cec8cad505
diff --git a/.github/scripts/affected-modules.ts b/.github/scripts/affected-modules.tsindex f4dbbe7e59f..98b4db8b9dd 100644--- a/.github/scripts/affected-modules.ts+++ b/.github/scripts/affected-modules.ts@@ -1,7 +1,12 @@import micromatch from "micromatch";export type ModuleDef = { type: string; pattern: string };-export type Rule = { from: string[]; allow: string[] };++export type Rule = {+ from: string[];+ allow?: string[];+ disallow?: string[];+};export type ModuleNode = { type: string; regex: RegExp };@@ -203,8 +208,11 @@ export function buildModuleGraph(allTypes.map((type) => [type, new Set()]),);for (const rule of rules) {+ // Grandfathered imports that break the disallow rules still exist in the code,+ // and selection must cover every import that actually exists.+ // So disallow rules are ignored, at the cost of a looser graph.const fromTypes = rule.from.flatMap(expandPattern);- const allowTypes = rule.allow.flatMap(expandPattern);+ const allowTypes = (rule.allow ?? []).flatMap(expandPattern);for (const target of allowTypes) {for (const importer of fromTypes) {if (importer !== target) {diff --git a/frontend/lint/module-boundaries-stats.mjs b/frontend/lint/module-boundaries-stats.mjsindex 323796fa7c0..a30b52374ee 100644--- a/frontend/lint/module-boundaries-stats.mjs+++ b/frontend/lint/module-boundaries-stats.mjs@@ -25,7 +25,9 @@ export function getEnforcedModules(elements) {elements.filter((element) =>- element.enforceOutgoing && !EXCLUDED_TYPES.includes(element.type),+ element.enforceOutgoing &&+ (!element.type.startsWith("shared/") || element.enforceSharedTiers) &&+ !EXCLUDED_TYPES.includes(element.type),).map((element) => element.type),);diff --git a/frontend/lint/module-boundaries.mjs b/frontend/lint/module-boundaries.mjsindex ebbb0bc161b..f8208f87a97 100644--- a/frontend/lint/module-boundaries.mjs+++ b/frontend/lint/module-boundaries.mjs@@ -1,9 +1,12 @@+import { sharedRules } from "./shared-tiers.mjs";+const createElement = ({type,name,pattern,mode,enforceOutgoing = true,+ enforceSharedTiers = true,// Outside code must import the module root alias, and the module's own files must import relatively.// Enforced by the `metabase/enforce-module-public-api` rule via `getPublicApiModules` below.enforcePublicApi = false,@@ -20,6 +23,7 @@ const createElement = ({pattern: pattern ?? `frontend/src/metabase/${name}/**`,...(mode && { mode }),enforceOutgoing,+ enforceSharedTiers,...(enforcePublicApi && { publicApiAlias: `metabase/${name}` }),};};@@ -63,12 +67,32 @@ const elements = [// sharedcreateElement({ type: "feature", name: "account" }),createElement({ type: "shared", name: "actions" }),- createElement({ type: "shared", name: "api" }),+ createElement({ type: "shared", name: "api", enforceSharedTiers: false }),createElement({ type: "shared", name: "archive" }),createElement({ type: "feature", name: "auth" }),createElement({ type: "feature", name: "browse" }),createElement({ type: "feature", name: "collections" }),createElement({ type: "shared", name: "comments" }),+ ...[+ "frontend/src/metabase/common/metrics/**",+ "frontend/src/metabase/common/metrics-viewer/**",+ ].map((pattern) =>+ createElement({ type: "shared", name: "metrics-ui", pattern }),+ ),+ createElement({+ type: "shared",+ name: "upsells",+ pattern: "frontend/src/metabase/common/components/upsells/**",+ enforceSharedTiers: false,+ }),+ ...[+ "frontend/src/metabase/common/search/**",+ "frontend/src/metabase/common/components/SearchResult/**",+ "frontend/src/metabase/common/components/SearchResultLink/**",+ "frontend/src/metabase/common/components/InfoText/**",+ ].map((pattern) =>+ createElement({ type: "shared", name: "search-ui", pattern }),+ ),createElement({ type: "shared", name: "common" }),createElement({type: "shared",@@ -77,7 +101,11 @@ const elements = [}),createElement({ type: "shared", name: "data-grid" }),createElement({ type: "shared", name: "databases" }),- createElement({ type: "shared", name: "detail-view" }),+ createElement({+ type: "shared",+ name: "detail-view",+ enforceSharedTiers: false,+ }),// embedding-iframe-sdk, embedding-iframe-sdk-setup and mcp-app must come before// shared/embedding: their patterns are subfolders of// frontend/src/metabase/embedding/, and the first matching element wins.@@ -144,25 +172,33 @@ const elements = [name: "embedding-sdk-shared",pattern: "frontend/src/embedding-sdk-shared/**",}),- createElement({ type: "shared", name: "forms" }),+ createElement({ type: "shared", name: "forms", enforceSharedTiers: false }),createElement({ type: "shared", name: "hoc" }),createElement({ type: "feature", name: "home" }),- createElement({ type: "shared", name: "hooks" }),+ createElement({ type: "shared", name: "hooks", enforceSharedTiers: false }),createElement({ type: "shared", name: "content-translation" }),- createElement({ type: "shared", name: "metabot" }),- createElement({ type: "shared", name: "metadata" }),+ createElement({ type: "shared", name: "metabot", enforceSharedTiers: false }),+ createElement({+ type: "shared",+ name: "metadata",+ enforceSharedTiers: false,+ }),createElement({ type: "feature", name: "models" }),createElement({ type: "shared", name: "monitor" }),- createElement({ type: "shared", name: "nav" }),+ createElement({ type: "shared", name: "nav", enforceSharedTiers: false }),createElement({ type: "shared", name: "new" }),createElement({ type: "shared", name: "notifications" }),createElement({ type: "shared", name: "palette" }),createElement({ type: "shared", name: "parameters" }),- createElement({ type: "shared", name: "plugins" }),+ createElement({ type: "shared", name: "plugins", enforceSharedTiers: false }),createElement({ type: "shared", name: "pulse" }),- createElement({ type: "shared", name: "querying" }),+ createElement({+ type: "shared",+ name: "querying",+ enforceSharedTiers: false,+ }),createElement({ type: "shared", name: "questions" }),- createElement({ type: "shared", name: "redux" }),+ createElement({ type: "shared", name: "redux", enforceSharedTiers: false }),createElement({ type: "shared", name: "rich_text_editing" }),createElement({ type: "shared", name: "route-guards" }),createElement({@@ -170,20 +206,37 @@ const elements = [name: "schema",pattern: "frontend/src/metabase/schema.js",mode: "full",+ enforceSharedTiers: false,+ }),+ createElement({+ type: "shared",+ name: "selectors",+ enforceSharedTiers: false,}),- createElement({ type: "shared", name: "selectors" }),createElement({ type: "feature", name: "setup" }),createElement({ type: "shared", name: "status" }),- createElement({ type: "shared", name: "styled-components" }),+ createElement({+ type: "shared",+ name: "styled-components",+ enforceSharedTiers: false,+ }),createElement({ type: "shared", name: "timelines" }),- createElement({ type: "shared", name: "transforms" }),+ createElement({+ type: "shared",+ name: "transforms",+ enforceSharedTiers: false,+ }),createElement({type: "shared",name: "types",pattern: "frontend/src/types/**",}),- createElement({ type: "shared", name: "urls" }),- createElement({ type: "shared", name: "visualizations" }),+ createElement({ type: "shared", name: "urls", enforceSharedTiers: false }),+ createElement({+ type: "shared",+ name: "visualizations",+ enforceSharedTiers: false,+ }),createElement({ type: "shared", name: "visualizer" }),// feature@@ -344,7 +397,7 @@ const elements = [}),];-const rules = [+const baseRules = [...elements.map((element) => ({// always allow self-importsfrom: [element.type],@@ -408,6 +461,10 @@ const rules = [},];+// The full rule set drives the standalone `bun run module-boundaries` count.+// PR lint uses enforcedRules.+const rules = [...baseRules, ...sharedRules];+/*** Returns a subset of rules that only enforces boundaries for modules with* enforceOutgoing: true. Non-enforced modules get a blanket allow-all.@@ -451,7 +508,26 @@ function buildEnforcedRules(elements, rules) {];}-const enforcedRules = buildEnforcedRules(elements, rules);+/**+ * Modules with enforceSharedTiers: false are dropped from the level rules,+ * which leaves them on the blanket shared -> shared allow.+ */+function buildEnforcedSharedRules(elements, rules) {+ const enforcedTypes = new Set(+ elements+ .filter((el) => el.type.startsWith("shared/") && el.enforceSharedTiers)+ .map((el) => el.type),+ );+ return rules.flatMap((rule) => {+ const from = rule.from.filter((type) => enforcedTypes.has(type));+ return from.length > 0 ? [{ ...rule, from }] : [];+ });+}++const enforcedRules = buildEnforcedRules(elements, [+ ...baseRules,+ ...buildEnforcedSharedRules(elements, sharedRules),+]);function getFeatureModules(els = elements) {return els.map((e) => e.type).filter((type) => type.startsWith("feature/"));diff --git a/frontend/lint/shared-tiers.mjs b/frontend/lint/shared-tiers.mjsnew file mode 100644index 00000000000..8ad085c17b5--- /dev/null+++ b/frontend/lint/shared-tiers.mjs@@ -0,0 +1,146 @@+// Rules for shared modules+//+// The shared tier is split into three SUB-TIERS, each ordered into LEVELS:+//+// shared-utils — plumbing and app services, no product concepts+// shared-platform — building blocks the domains compose+// shared-domain — domain-specific modules, no cross-domain dependencies+//+// Violations are grandfathered per module with enforceSharedTiers: false+// in module-boundaries.mjs.++const SHARED_UTILS_LEVELS = [+ // U0 — foundation: leaf plumbing.+ [+ "shared/schema",+ "shared/urls",+ "shared/styled-components",+ "shared/cljs-dev-tools",+ ],+ // U1 — the api client.+ ["shared/api"],+ // U2 — routing facade.+ ["shared/router"],+ // U3 — the store slices and hooks.+ ["shared/redux"],+ // U4 — the plugin registry.+ ["shared/plugins"],+ // U5 — app services over the store and registry.+ [+ "shared/selectors",+ "shared/content-translation",+ "shared/error-boundary",+ "shared/forms",+ "shared/archive",+ "shared/hooks",+ ],+ // U6 — composition over the levels below.+ // The store factory composes reducers, plugin middlewares, and the router.+ ["shared/hoc", "shared/upsells", "shared/route-guards", "shared/redux-store"],+];++const SHARED_PLATFORM_LEVELS = [+ // P0 — independent peers: chart rendering and database metadata/forms.+ ["shared/visualizations", "shared/databases"],+ // P1 — query editing composes visualizations.+ ["shared/querying"],+ // P2 — building blocks over querying; mutually independent.+ [+ "shared/metadata",+ "shared/parameters",+ "shared/questions",+ "shared/search-ui",+ ],+];++const SHARED_DOMAIN = [+ "shared/actions",+ "shared/comments",+ "shared/custom-viz",+ "shared/data-grid",+ "shared/detail-view",+ "shared/metabot",+ "shared/metrics-ui",+ "shared/nav",+ "shared/notifications",+ "shared/palette",+ "shared/pulse",+ "shared/rich_text_editing",+ "shared/routes-stable-id-aware",+ "shared/static-viz",+ "shared/status",+ "shared/timelines",+ "shared/transforms",+ "shared/visualizer",+];++const SHARED_UTILS = SHARED_UTILS_LEVELS.flat();+const SHARED_PLATFORM = SHARED_PLATFORM_LEVELS.flat();+const TIERED_SHARED = [...SHARED_UTILS, ...SHARED_PLATFORM, ...SHARED_DOMAIN];++// Each level may import only strictly lower levels of its sub-tier,+// plus `base` (the sub-tiers below).+// Same-level peers are deliberately not allowed.+const levelAllows = (levels, base = []) =>+ levels.flatMap((level, i) => {+ const below = [...base, ...levels.slice(0, i).flat()];+ return level.length === 0 || below.length === 0+ ? []+ : [{ from: level, allow: below }];+ });++const sharedRules = [+ // Later rules win, so these must come after the baseline shared/* allow they narrow.+ // Edges to untiered modules (common, monitor, embedding, types) fall through to that allow.+ {+ from: SHARED_UTILS,+ disallow: TIERED_SHARED,+ message:+ "shared-utils modules may only import shared-utils modules in strictly lower levels (see SHARED_UTILS_LEVELS)",+ },+ ...levelAllows(SHARED_UTILS_LEVELS),+ {+ from: SHARED_PLATFORM,+ disallow: TIERED_SHARED,+ message:+ "shared-platform modules may only import shared-utils, and platform modules in strictly lower levels (see SHARED_PLATFORM_LEVELS)",+ },+ ...levelAllows(SHARED_PLATFORM_LEVELS, SHARED_UTILS),+ {+ from: SHARED_DOMAIN,+ disallow: TIERED_SHARED,+ message:+ "shared-domain modules must not import other shared-domain modules; push the shared piece down or compose at feature tier",+ },+ {+ from: SHARED_DOMAIN,+ allow: [...SHARED_UTILS, ...SHARED_PLATFORM],+ },+ // The level rules exclude a module's own level, so re-allow self-imports explicitly.+ ...TIERED_SHARED.map((type) => ({ from: [type], allow: [type] })),+ // Transitional clusters: these domain modules still import each other, so both ways are allowed.+ // Delete each entry once the imports are one-directional.+ {+ from: ["shared/visualizations"],+ allow: [+ "shared/custom-viz",+ "shared/data-grid",+ "shared/static-viz",+ "shared/visualizer",+ ],+ },+ {+ from: ["shared/metabot", "shared/rich_text_editing", "shared/comments"],+ allow: ["shared/metabot", "shared/rich_text_editing", "shared/comments"],+ },+ {+ from: ["shared/nav", "shared/palette"],+ allow: ["shared/nav", "shared/palette"],+ },+ {+ from: ["shared/notifications", "shared/pulse"],+ allow: ["shared/notifications", "shared/pulse"],+ },+];++export { sharedRules };