// scripts/ci-checks.ts // // Which browser checks a run of CI needs. A pull request runs the fast tier: // the checks of the components its diff touches, and a small smoke set. The // full matrix runs where a merge is decided: in the merge queue // (`merge_group`), on a push to main, nightly, and on a pull request that // carries the `full-ci` label. The fast tier is off until the repository // variable CI_FAST_TIER is `1`: before that every pull request runs the full // matrix, as it always did. // // The browser matrix lives here, not in the workflow: the `changes` job runs // `bun scripts/ci-checks.ts select` and hands the result to the `browser` job // as its matrix. test/build/ci-checks.test.ts keeps this file whole: every // check is tagged, and every folder of src/components is either covered by a // tagged check or listed as having none. // // When in doubt the answer is the full matrix: a path this file does not // know, a change to shared code, a component with no check of its own. import { appendFileSync, existsSync, readFileSync } from "node:fs"; import { join, posix } from "node:path"; export interface Group { /** The job's name: `browser ()`. */ group: string; /** The engines the job installs. */ browsers: string; /** The package.json scripts the job runs, in order. */ checks: readonly string[]; /** Arguments passed to every script of the group. */ args?: string; } // A run takes as long as its slowest group, so a check that grows moves to a // group of its own rather than making one group longer. Every check is in // exactly one group. The `:floor` commands run a check again on the lowest // peer version package.json allows, and put the installed version back before // they return (scripts/check-at-peer-floor.ts), so their place in a group // does not matter. export const GROUPS: readonly Group[] = [ { group: "elements-a", browsers: "chromium", checks: ["elements:check:a"] }, { group: "elements-b", browsers: "chromium", checks: ["elements:check:b"] }, { group: "shadow-styles", browsers: "chromium", checks: ["shadow-styles:check"] }, { group: "adapters", browsers: "chromium", checks: ["react:check", "react-ssr:check", "vue:check", "svelte:check", "svelte-ssr:check", "vue-ssr:check", "solid-ssr:check", "solid:check", "solid-ssr:floor", "vue-ssr:floor", "react-types:check", "vue-types:check", "solid-types:check", "svelte-types:check", "react-types:floor", "vue-types:floor", "solid-types:floor", "svelte-types:floor"] }, { group: "components-a", browsers: "chromium webkit", checks: ["core:check", "carousel:check", "a11y-svg:check"] }, { group: "carousel-reference", browsers: "chromium webkit", checks: ["carousel-reference:check"] }, { group: "components-b", browsers: "chromium", checks: ["consumer:check", "tabs:check", "tooltip:check", "tooltip-menu:check", "drawer:check", "navigation-bar:check", "toolbar-card:check", "split-button:check", "select:check", "datepicker-deferred:check", "readme-browser:check", "rtl:check", "progress:check", "dialog-afteropen:check", "dialog-reduced-motion:check", "dialog-small-viewport:check", "switch-transition:check", "bottom-sheet:check"] }, { group: "navigation-rail", browsers: "chromium webkit", checks: ["navigation-rail:check"] }, { group: "tooltip-hide-delay", browsers: "chromium webkit", checks: ["tooltip-hide-delay:check"] }, { group: "drawer-divider", browsers: "chromium webkit", checks: ["drawer-divider:check"] }, { group: "slider", browsers: "chromium webkit", checks: ["slider:check"] }, { group: "menu-deferred", browsers: "chromium", checks: ["menu-deferred:check", "menu-container-space:check"] }, { group: "menu-item-checked", browsers: "chromium", checks: ["menu-item-checked:check"] }, { group: "menu-focus-ring", browsers: "webkit", checks: ["menu-focus-ring:check"] }, { group: "menu-drifts", browsers: "chromium webkit", checks: ["menu-drifts:check", "menu-opening-focus:check"] }, { group: "fab-menu-ripple", browsers: "chromium", checks: ["fab-menu-ripple:check"] }, { group: "fab-menu-position", browsers: "chromium", checks: ["fab-menu-position:check"] }, { group: "fab-menu-items", browsers: "chromium", checks: ["fab-menu-items:check"] }, { group: "app-bar-conformance", browsers: "chromium", checks: ["app-bar-conformance:check", "top-app-bar:check"] }, { group: "closed-surfaces", browsers: "chromium", checks: ["closed-surfaces:check"] }, { group: "divider-sheet-conformance", browsers: "chromium", checks: ["divider-sheet-conformance:check"] }, { group: "list-conformance", browsers: "chromium webkit", checks: ["list-conformance:check"] }, { group: "list-drifts", browsers: "chromium webkit", checks: ["list-drifts:check"] }, { group: "search-trailing-space", browsers: "chromium webkit", checks: ["search-trailing-space:check"] }, { group: "conformance-matrix", browsers: "chromium webkit", checks: ["conformance"], args: "--matrix --check" }, { group: "bottom-app-bar-toolbar", browsers: "chromium", checks: ["bottom-app-bar-toolbar:check"] }, { group: "button-profile", browsers: "chromium", checks: ["button-profile:check"] }, { group: "listbox", browsers: "chromium firefox webkit", checks: ["listbox:check"] }, { group: "card-state-layers", browsers: "chromium webkit", checks: ["card-state-layers:check"] }, { group: "settle-helper", browsers: "chromium webkit", checks: ["browser-settle:check"] }, { group: "styles", browsers: "chromium", checks: ["preupgrade:check", "tokens:check", "tokens:catalogue-browser:check", "contrast-emit:check", "theme-root-scope:check", "theme-runtime:check", "theme-roles:check", "theme-origins:check", "spring-tokens:check", "base-lite:check", "base-elements:check", "typescale-consumers:check", "component-sheets:check", "density:check"] }, { group: "ssr-a", browsers: "chromium", checks: ["ssr:check:a"] }, { group: "ssr-b", browsers: "firefox webkit", checks: ["ssr:check:b"] }, ]; /** * The components a check covers: folders of src/components. A check that is * not listed here tests the library as a whole (every element, the adapters, * the tokens, server rendering) and runs in the full matrix only, except for * the smoke set. */ export const COVERS: Readonly> = { "carousel:check": ["carousel"], "carousel-reference:check": ["carousel"], "tabs:check": ["tabs"], "tooltip:check": ["tooltip"], "tooltip-menu:check": ["tooltip", "menu"], "tooltip-hide-delay:check": ["tooltip"], "drawer:check": ["drawer"], "drawer-divider:check": ["drawer", "divider"], "navigation-bar:check": ["navigation-bar"], "navigation-rail:check": ["navigation-rail"], "toolbar-card:check": ["toolbar", "card"], "card-state-layers:check": ["card"], "split-button:check": ["split-button"], "select:check": ["select"], "datepicker-deferred:check": ["datepicker"], "progress:check": ["progress"], "dialog-afteropen:check": ["dialog"], "dialog-reduced-motion:check": ["dialog"], "dialog-small-viewport:check": ["dialog"], "closed-surfaces:check": ["dialog", "snackbar"], "switch-transition:check": ["switch"], "bottom-sheet:check": ["bottom-sheet"], "divider-sheet-conformance:check": ["divider", "side-sheet", "bottom-sheet"], "slider:check": ["slider"], "menu-deferred:check": ["menu"], "menu-container-space:check": ["menu"], "menu-item-checked:check": ["menu"], "menu-focus-ring:check": ["menu"], "menu-drifts:check": ["menu"], "menu-opening-focus:check": ["menu"], "fab-menu-ripple:check": ["fab-menu", "fab"], "fab-menu-position:check": ["fab-menu", "fab"], "fab-menu-items:check": ["fab-menu", "fab"], "app-bar-conformance:check": ["top-app-bar", "bottom-app-bar"], "top-app-bar:check": ["top-app-bar"], "bottom-app-bar-toolbar:check": ["bottom-app-bar", "bottom-app-bar-toolbar", "toolbar"], "list-conformance:check": ["list"], "list-drifts:check": ["list"], "listbox:check": ["listbox", "list"], "search-trailing-space:check": ["search"], "button-profile:check": ["button"], }; /** * Components with no browser check of their own. A change to one of them runs * the full matrix: only the checks of the whole library exercise it. A new * folder of src/components goes into COVERS (with its check) or here. */ export const NO_CHECK_OF_ITS_OWN: readonly string[] = [ "badge", "button-group", "checkbox", "chips", "extended-fab", "icon-button", "loading-indicator", "radios", "text-field", "timepicker", ]; /** Run in the fast tier whenever library code changed: the core's own check and a consumer's build. */ export const SMOKE: readonly string[] = ["core:check", "consumer:check"]; /** A pull request with this label runs the full matrix. */ export const FULL_LABEL = "full-ci"; /** Files that change with most pull requests and that another job already verifies. */ const NEUTRAL = new Set(["scripts/fixtures/sass-config-baseline.json", "scripts/fixtures/type-shapes.json"]); export interface Selection { /** The whole matrix, the comparators included. */ full: boolean; /** * The pull request asked for the full matrix with the label: every browser * and comparator job runs, whatever the diff touches, a documents-only * change included. */ forced: boolean; /** Why, in one line, for the job's log. */ reason: string; /** The checks to run; every check when `full`. */ checks: string[]; /** The browser job's matrix: the groups that keep at least one check. */ matrix: Group[]; } const everyCheck = (): string[] => GROUPS.flatMap((group) => [...group.checks]); const matrixOf = (checks: ReadonlySet): Group[] => GROUPS.map((group) => ({ ...group, checks: group.checks.filter((check) => checks.has(check)) })).filter((group) => group.checks.length > 0); const whole = (reason: string, forced = false): Selection => ({ full: true, forced, reason, checks: everyCheck(), matrix: GROUPS.map((group) => ({ ...group })) }); /** The branch the merge queue protects: only a pull request into it takes the fast tier. */ export const QUEUED_BRANCH = "main"; /** The checks whose package.json command names a script file. */ export const checksOfScript = (file: string, scripts: Readonly>): string[] => everyCheck().filter((check) => (scripts[check] ?? "").split(/\s+/).includes(file)); /** * What one changed file asks for: nothing (a document, a unit test, a pin * another job verifies), some checks, a component, or the full matrix. */ export const classify = (file: string, components: ReadonlySet, scripts: Readonly>): { kind: "none" } | { kind: "full"; why: string } | { kind: "component"; name: string } | { kind: "checks"; checks: string[] } => { if (NEUTRAL.has(file)) return { kind: "none" }; // README.md and npm-readme.md hold examples a browser check runs. if (file === "README.md" || file === "npm-readme.md") return { kind: "checks", checks: ["readme-browser:check"] }; if (file.endsWith(".md") || file.startsWith("docs/") || file.startsWith(".changes/") || file.startsWith(".github/ISSUE_TEMPLATE/")) return { kind: "none" }; // Unit tests run whole in their own job. if (file.startsWith("test/")) return { kind: "none" }; // A component's own folder and its stylesheet. Its custom element // (src/elements/.ts) is not here: only the checks of the whole // library exercise the elements, so that file runs the full matrix. const component = /^src\/components\/([^/]+)\//.exec(file) ?? /^src\/styles\/components\/_([^/]+)\.scss$/.exec(file); if (component && components.has(component[1]!)) return { kind: "component", name: component[1]! }; if (/^scripts\/check-[\w.-]+\.ts$/.test(file)) { const checks = checksOfScript(file, scripts); if (checks.length) return { kind: "checks", checks }; } // The conformance matrix: its data and the modules that measure each component. if (file.startsWith("conformance/") || file.startsWith("scripts/conformance/")) return { kind: "checks", checks: [CONFORMANCE] }; return { kind: "full", why: file }; }; /** The check that re-measures the conformance matrix. */ const CONFORMANCE = "conformance"; /** For each component folder, the component folders it imports directly; `"*"` stands for the components' barrel. */ export type Imports = ReadonlyMap>; /** * Reads the imports between component folders from the source, so a change to * one component also selects the components built on it. TypeScript: every * `from "…"`, `import "…"` and `import("…")` under src/components//. * Sass: every `@use`, `@forward` and `@import` between the stylesheets of * src/styles/components. */ export const componentImports = async (components: ReadonlySet): Promise>> => { const imports = new Map>(); const add = (from: string, to: string): void => { if (from === to) return; let set = imports.get(from); if (!set) imports.set(from, (set = new Set())); set.add(to); }; for await (const file of new Bun.Glob("**/*.{ts,tsx}").scan({ cwd: "src/components" })) { const owner = file.split("/")[0]!; if (!components.has(owner)) continue; const text = await Bun.file(join("src/components", file)).text(); for (const match of text.matchAll(/(?:\bfrom\s*|\bimport\s*\(\s*|\bimport\s+)["']([^"']+)["']/g)) { const specifier = match[1]!; if (!specifier.startsWith(".")) continue; const target = posix.normalize(posix.join("src/components", posix.dirname(file), specifier)); if (target === "src/components" || target === "src/components/index" || target === "src/components/index.ts") add(owner, "*"); const folder = /^src\/components\/([^/]+)/.exec(target)?.[1]; if (folder && components.has(folder)) add(owner, folder); } } for await (const file of new Bun.Glob("_*.scss").scan({ cwd: "src/styles/components" })) { const owner = file.replace(/^_|\.scss$/g, ""); if (!components.has(owner)) continue; const text = await Bun.file(join("src/styles/components", file)).text(); for (const match of text.matchAll(/@(?:use|forward|import)\s+["']([^"']+)["']/g)) { const name = posix.basename(match[1]!).replace(/^_|\.scss$/g, ""); // Only a sibling stylesheet: `../abstract/…` and `sass:…` are not components. if (/^\.?\/?_?[\w-]+(?:\.scss)?$/.test(match[1]!) && components.has(name)) add(owner, name); } } return imports; }; /** The components that import any of `names`, directly or through other components. */ export const dependentsOf = (names: ReadonlySet, imports: Imports): Set => { const reached = new Set(names); for (let grew = true; grew;) { grew = false; for (const [component, imported] of imports) { if (reached.has(component)) continue; if (imported.has("*") || [...imported].some((name) => reached.has(name))) { reached.add(component); grew = true; } } } for (const name of names) reached.delete(name); return reached; }; export interface SelectInput { /** The changed files, or undefined when the run is not a pull request's. */ files?: readonly string[]; /** The pull request's labels. */ labels?: readonly string[]; /** The workflow already decided to run everything (a release, a base that could not be read). */ runAll?: boolean; /** The fast tier is switched on for the repository. Off, every pull request runs the full matrix. */ fastTier?: boolean; /** The branch the pull request targets. Only `main` has a merge queue to run the full matrix before a merge. */ base?: string; components: ReadonlySet; scripts: Readonly>; /** The imports between component folders, from `componentImports`. */ imports: Imports; } export const select = ({ files, labels = [], runAll = false, fastTier = false, base, components, scripts, imports }: SelectInput): Selection => { if (!files) return whole("not a pull request: the full matrix"); // The label first: it forces every job, whether or not the fast tier is on. if (labels.includes(FULL_LABEL)) return whole(`the pull request carries the ${FULL_LABEL} label`, true); if (!fastTier) return whole(`the fast tier is not switched on (the repository variable ${FAST_TIER_VARIABLE})`); if (base !== QUEUED_BRANCH) return whole(`the pull request targets ${base ?? "an unknown branch"}: only ${QUEUED_BRANCH} has a merge queue to run the full matrix before a merge`); if (!files.length) return whole("an empty diff: nothing says what to leave out"); if (runAll) return whole("the workflow asked for every check"); const checks = new Set(); const changed = new Set(); for (const file of files) { const asked = classify(file, components, scripts); if (asked.kind === "full") return whole(`${asked.why} is shared, or unknown to scripts/ci-checks.ts`); if (asked.kind === "checks") for (const check of asked.checks) checks.add(check); if (asked.kind === "component") changed.add(asked.name); } // A component that imports a changed one can break with it: it is touched too. const dependents = dependentsOf(changed, imports); const touched = new Set([...changed, ...dependents]); for (const name of touched) { if (!NO_CHECK_OF_ITS_OWN.includes(name)) continue; return whole(changed.has(name) ? `the ${name} component has no browser check of its own` : `${name}, which is built on a changed component, has no browser check of its own`); } for (const [check, covered] of Object.entries(COVERS)) { if (covered.some((name) => touched.has(name))) checks.add(check); } if (touched.size) for (const check of SMOKE) checks.add(check); const names = [...changed].sort().join(", "); const built = dependents.size ? `, and ${[...dependents].sort().join(", ")} built on ${changed.size > 1 ? "them" : "it"}` : ""; return { full: false, forced: false, reason: checks.size ? `the fast tier${names ? ` for ${names}${built}` : ""}: ${checks.size} of ${everyCheck().length} checks` : "no browser check: nothing the diff touches is run in a browser", checks: everyCheck().filter((check) => checks.has(check)), matrix: matrixOf(checks), }; }; /** The repository variable that switches the fast tier on, when it is `1`. */ export const FAST_TIER_VARIABLE = "CI_FAST_TIER"; /** The folders of src/components, read from the checkout. */ export const componentFolders = async (): Promise> => { const names = new Set(); for await (const path of new Bun.Glob("*/").scan({ cwd: "src/components", onlyFiles: false })) names.add(path.replace(/\/$/, "")); return names; }; // `bun scripts/ci-checks.ts select`: reads the changed files from the file // named by CI_CHANGED_FILES (one per line; unset or missing when the run is // not a pull request's), the labels from CI_LABELS (space separated), // CI_RUN_ALL, CI_FAST_TIER and CI_BASE_REF, and writes to GITHUB_OUTPUT: // `full`, `forced` (the label asked for every job), `browser` (whether any // browser check runs) and `browser_matrix`. if (import.meta.main && process.argv[2] === "select") { const list = process.env.CI_CHANGED_FILES; const files = list && existsSync(list) ? readFileSync(list, "utf8").split("\n").filter(Boolean) : undefined; const { scripts } = await Bun.file("package.json").json() as { scripts: Record }; const components = await componentFolders(); const result = select({ files, labels: (process.env.CI_LABELS ?? "").split(/\s+/).filter(Boolean), runAll: process.env.CI_RUN_ALL === "true", fastTier: process.env.CI_FAST_TIER === "1", base: process.env.CI_BASE_REF || undefined, components, scripts, imports: await componentImports(components), }); console.log(result.reason); for (const group of result.matrix) console.log(` ${group.group} [${group.browsers}]: ${group.checks.join(" ")}`); // The workflow's matrix wants each group's checks as one string, and `args` always present. const matrix = result.matrix.map((group) => ({ group: group.group, browsers: group.browsers, checks: group.checks.join(" "), args: group.args ?? "" })); if (process.env.GITHUB_OUTPUT) { appendFileSync(process.env.GITHUB_OUTPUT, `full=${result.full}\nforced=${result.forced}\nbrowser=${matrix.length > 0}\nbrowser_matrix=${JSON.stringify(matrix)}\n`); } }