PSA/scripts/check-circular-deps.mjs
Hermes 284313f908
Some checks are pending
Bidi Control Character Guard / bidi-control-guard (push) Waiting to run
Circular Dependency Check / Check for new circular dependencies (push) Waiting to run
Citus Migration Smoke / Combined migrations on single-node Citus (push) Waiting to run
E2E Fresh Install Tests / fresh-install-e2e (push) Waiting to run
ext-v2 guardrails / Run ext-v2 guard and ESLint (push) Waiting to run
Integration Tests / Check for relevant changes (push) Waiting to run
Integration Tests / ${{ (github.event_name == 'schedule' || github.event.inputs.suite == 'full') && 'Full integration suite' || 'Tier-1 integration subset' }} (push) Blocked by required conditions
Mobile checks / Mobile lint + typecheck (push) Waiting to run
Mobile checks / Mobile unit tests (push) Waiting to run
Mobile checks / Mobile dependency audit (report) (push) Waiting to run
Mobile checks / Mobile reproducibility checks (push) Waiting to run
Secrets guard (env backups) / Ensure no tracked env backup files (push) Waiting to run
Temporal Readiness / fast-readiness (push) Waiting to run
Temporal Readiness / docker-parity (push) Waiting to run
TypeScript Type Check / Nx affected typecheck (push) Waiting to run
Unit Tests / Skipped-test budget (push) Waiting to run
Unit Tests / Nx affected unit tests (push) Waiting to run
Unit Tests / Server unit coverage (informational) (push) Waiting to run
Validate Tenant Management Schema / Check for relevant changes (push) Waiting to run
Validate Tenant Management Schema / Validate Tenant Management Schema (push) Blocked by required conditions
EE Workflows Build Guard / ee-workflows-build-guard (push) Waiting to run
Initial import of AlgaPSA codebase from PSA server
Excluded: .git, node_modules, secrets/, compose.env, assemblyscript tgz

Source: /opt/alga-psa on psa.joliet.tech
2026-06-22 16:12:17 -05:00

137 lines
4.0 KiB
JavaScript

#!/usr/bin/env node
/**
* Detects circular dependencies in the Nx project graph.
*
* Usage:
* npx nx graph --file=graph.json
* node scripts/check-circular-deps.mjs graph.json
*
* With baseline (fails only on NEW cycles):
* node scripts/check-circular-deps.mjs graph.json --baseline .github/known-cycles.json
*
* To update the baseline with current cycles:
* node scripts/check-circular-deps.mjs graph.json --update-baseline .github/known-cycles.json
*/
import { readFileSync, writeFileSync } from 'node:fs';
const args = process.argv.slice(2);
const graphPath = args.find(a => !a.startsWith('--'));
function getFlag(flag) {
const idx = args.indexOf(flag);
return idx !== -1 && idx + 1 < args.length ? args[idx + 1] : null;
}
const baselinePath = getFlag('--baseline');
const updateBaselinePath = getFlag('--update-baseline');
if (!graphPath) {
console.error('Usage: node scripts/check-circular-deps.mjs <graph.json> [--baseline <known-cycles.json>] [--update-baseline <path>]');
process.exit(1);
}
// Parse the Nx project graph
const graph = JSON.parse(readFileSync(graphPath, 'utf8'));
const deps = graph.graph?.dependencies || {};
// Build adjacency list
const adj = {};
for (const [source, edges] of Object.entries(deps)) {
adj[source] = edges.map(e => e.target);
}
// Detect all cycles using DFS with coloring
const GRAY = 1;
const BLACK = 2;
const color = {};
const rawCycles = [];
function dfs(node, path) {
color[node] = GRAY;
path.push(node);
for (const neighbor of adj[node] || []) {
if (color[neighbor] === GRAY) {
const cycleStart = path.indexOf(neighbor);
rawCycles.push(path.slice(cycleStart));
} else if (color[neighbor] !== BLACK) {
dfs(neighbor, path);
}
}
path.pop();
color[node] = BLACK;
}
for (const node of Object.keys(deps)) {
if (!color[node]) dfs(node, []);
}
// Normalize cycles for stable comparison:
// rotate so the lexicographically smallest node is first
function normalizeCycle(cycle) {
if (cycle.length === 0) return cycle;
let minIdx = 0;
for (let i = 1; i < cycle.length; i++) {
if (cycle[i] < cycle[minIdx]) minIdx = i;
}
return [...cycle.slice(minIdx), ...cycle.slice(0, minIdx)];
}
const cycles = rawCycles
.map(normalizeCycle)
.map(c => c.join(' -> '))
.filter((v, i, a) => a.indexOf(v) === i) // deduplicate
.sort();
// Update baseline mode
if (updateBaselinePath) {
writeFileSync(updateBaselinePath, JSON.stringify({ cycles }, null, 2) + '\n');
console.log(`Wrote ${cycles.length} cycles to ${updateBaselinePath}`);
process.exit(0);
}
// Load baseline if provided
let knownCycles = new Set();
if (baselinePath) {
try {
const baseline = JSON.parse(readFileSync(baselinePath, 'utf8'));
knownCycles = new Set(baseline.cycles || []);
} catch {
console.error(`Warning: could not read baseline at ${baselinePath}, treating all cycles as new`);
}
}
// Categorize cycles
const newCycles = cycles.filter(c => !knownCycles.has(c));
const resolvedCycles = [...knownCycles].filter(c => !cycles.includes(c));
// Report
console.log(`Total cycles found: ${cycles.length}`);
if (resolvedCycles.length > 0) {
console.log(`\nResolved cycles (${resolvedCycles.length}) -- remove from baseline:`);
resolvedCycles.forEach(c => console.log(` - ${c}`));
}
if (baselinePath) {
console.log(`Known (baselined) cycles: ${cycles.length - newCycles.length}`);
}
if (newCycles.length > 0) {
console.log(`\n::error::NEW circular dependencies detected (${newCycles.length}):\n`);
newCycles.forEach(c => console.log(` - ${c}`));
console.log('\nTo fix: remove the import causing the cycle.');
console.log('To baseline (temporary): run `node scripts/check-circular-deps.mjs graph.json --update-baseline .github/known-cycles.json`');
process.exit(1);
} else if (cycles.length > 0 && !baselinePath) {
console.log('\nCircular dependencies found:');
cycles.forEach(c => console.log(` - ${c}`));
process.exit(1);
} else {
console.log('\nNo new circular dependencies.');
process.exit(0);
}