|
| 1 | +--- |
| 2 | +title: Prevent Circular Dependencies Between Packages |
| 3 | +impact: CRITICAL |
| 4 | +impactDescription: Prevents build failures, type errors, ensures honest implementation, and maintains clean dependency graph |
| 5 | +tags: architecture, dependencies, packages, imports, circular |
| 6 | +--- |
| 7 | + |
| 8 | +## Prevent Circular Dependencies Between Packages |
| 9 | + |
| 10 | +**Impact: CRITICAL** |
| 11 | + |
| 12 | +Circular dependencies between packages cause build failures, type errors, and make the codebase unmaintainable. The dependency graph must be acyclic, with clear layers from low-level utilities up to high-level features. |
| 13 | + |
| 14 | +The dependency hierarchy (from lowest to highest): |
| 15 | +``` |
| 16 | +packages/lib (lowest - no feature dependencies) |
| 17 | + ↓ |
| 18 | +packages/app-store |
| 19 | + ↓ |
| 20 | +packages/features |
| 21 | + ↓ |
| 22 | +packages/trpc |
| 23 | + ↓ |
| 24 | +apps/web (highest - can depend on all packages) |
| 25 | +``` |
| 26 | + |
| 27 | +**Benefits:** |
| 28 | +- **Honest implementation**: The dependency graph accurately reflects what code depends on what, with no hidden or circular relationships that create false expectations |
| 29 | +- **Predictable behavior**: Code behaves as the dependency structure suggests - no surprises from circular imports |
| 30 | +- **Clear mental model**: Developers can reason about the system without tracking complex circular relationships |
| 31 | +- **Build reliability**: No circular dependency errors during compilation or bundling |
| 32 | +- **Easier testing**: Lower-level packages can be tested independently without pulling in the entire codebase |
| 33 | + |
| 34 | +### `packages/lib` |
| 35 | + |
| 36 | +**Rules:** |
| 37 | +1. No files in `packages/lib` import from `@calcom/app-store` or `../app-store/**` |
| 38 | +2. No files in `packages/lib` import from `@calcom/features` or `../features/**` |
| 39 | +3. No files in `packages/lib` import from `@calcom/trpc` or `../trpc/**` |
| 40 | +4. No files in `packages/lib` import from `@trpc/server` |
| 41 | +5. **BONUS:** No repository files allowed in `packages/lib` (repositories belong in features or app-store) |
| 42 | + |
| 43 | +**Incorrect:** |
| 44 | + |
| 45 | +```typescript |
| 46 | +// Bad - lib importing from features |
| 47 | +import { getBooking } from "@calcom/features/bookings"; |
| 48 | +import { EventRepository } from "@calcom/features/events/repositories"; |
| 49 | + |
| 50 | +// Bad - lib importing from app-store |
| 51 | +import { googleCalendarHandler } from "@calcom/app-store/googlecalendar"; |
| 52 | + |
| 53 | +// Bad - lib importing from trpc |
| 54 | +import { router } from "@calcom/trpc/server/trpc"; |
| 55 | +``` |
| 56 | + |
| 57 | +**Correct:** |
| 58 | + |
| 59 | +```typescript |
| 60 | +// Good - lib only imports from other lib files or external packages |
| 61 | +import { logger } from "@calcom/lib/logger"; |
| 62 | +import { prisma } from "@calcom/prisma"; |
| 63 | +import type { User } from "@prisma/client"; |
| 64 | +``` |
| 65 | + |
| 66 | +### `packages/app-store` |
| 67 | + |
| 68 | +**Rules:** |
| 69 | +6. No files in `packages/app-store` import from `@calcom/features` or `../features/**` |
| 70 | +7. No files in `packages/app-store` import from `@calcom/trpc` or `../trpc/**` |
| 71 | + |
| 72 | +**Incorrect:** |
| 73 | + |
| 74 | +```typescript |
| 75 | +// Bad - app-store importing from features |
| 76 | +import { BookingService } from "@calcom/features/bookings/services"; |
| 77 | + |
| 78 | +// Bad - app-store importing from trpc |
| 79 | +import { publicProcedure } from "@calcom/trpc/server/trpc"; |
| 80 | +``` |
| 81 | + |
| 82 | +**Correct:** |
| 83 | + |
| 84 | +```typescript |
| 85 | +// Good - app-store imports from lib and prisma |
| 86 | +import { logger } from "@calcom/lib/logger"; |
| 87 | +import { prisma } from "@calcom/prisma"; |
| 88 | +import type { Credential } from "@prisma/client"; |
| 89 | +``` |
| 90 | + |
| 91 | +### `packages/features` |
| 92 | + |
| 93 | +**Rules:** |
| 94 | +8. No files in `packages/features` import from `@calcom/trpc` or `../trpc/**` |
| 95 | +9. No files in `packages/features` import from `@calcom/web` or `@calcom/web/**` |
| 96 | +10. No files in `packages/features` import from `../../apps/web/**` |
| 97 | + |
| 98 | +**Incorrect:** |
| 99 | + |
| 100 | +```typescript |
| 101 | +// Bad - features importing from trpc |
| 102 | +import { router } from "@calcom/trpc/server/trpc"; |
| 103 | + |
| 104 | +// Bad - features importing from web app |
| 105 | +import { getServerSession } from "@calcom/web/lib/auth"; |
| 106 | +import { WebComponent } from "../../apps/web/components/WebComponent"; |
| 107 | +``` |
| 108 | + |
| 109 | +**Correct:** |
| 110 | + |
| 111 | +```typescript |
| 112 | +// Good - features import from lib, app-store, and prisma |
| 113 | +import { logger } from "@calcom/lib/logger"; |
| 114 | +import { prisma } from "@calcom/prisma"; |
| 115 | +import { googleCalendarHandler } from "@calcom/app-store/googlecalendar"; |
| 116 | +``` |
| 117 | + |
| 118 | +### `packages/trpc` |
| 119 | + |
| 120 | +**Rules:** |
| 121 | +11. No files in `packages/trpc` import from `../apps/web/**` |
| 122 | + |
| 123 | +**Incorrect:** |
| 124 | + |
| 125 | +```typescript |
| 126 | +// Bad - trpc importing from web app |
| 127 | +import { getServerSession } from "../../apps/web/lib/auth"; |
| 128 | +``` |
| 129 | + |
| 130 | +**Correct:** |
| 131 | + |
| 132 | +```typescript |
| 133 | +// Good - trpc imports from features, lib, and prisma |
| 134 | +import { BookingService } from "@calcom/features/bookings/services"; |
| 135 | +import { logger } from "@calcom/lib/logger"; |
| 136 | +import { prisma } from "@calcom/prisma"; |
| 137 | +``` |
| 138 | + |
| 139 | +### `packages/testing` |
| 140 | + |
| 141 | +**Rules:** |
| 142 | +12. No files in `packages/testing` import from `@calcom/web` or `@calcom/web/**` |
| 143 | +13. No files in `packages/testing` import from `@calcom/features` or `@calcom/features/**` |
| 144 | +14. No files in `packages/testing` import from `../../apps/web/**` |
| 145 | +15. No files in `packages/testing` import from `../features/**` |
| 146 | + |
| 147 | +**Incorrect:** |
| 148 | + |
| 149 | +```typescript |
| 150 | +// Bad - testing importing from web or features |
| 151 | +import { WebComponent } from "@calcom/web/components"; |
| 152 | +import { BookingService } from "@calcom/features/bookings"; |
| 153 | +``` |
| 154 | + |
| 155 | +**Correct:** |
| 156 | + |
| 157 | +```typescript |
| 158 | +// Good - testing imports from lib and prisma |
| 159 | +import { prisma } from "@calcom/prisma"; |
| 160 | +import { logger } from "@calcom/lib/logger"; |
| 161 | +``` |
| 162 | + |
| 163 | +### `packages/platform/atoms` |
| 164 | + |
| 165 | +**Rules:** |
| 166 | +16. No files in `packages/platform/atoms` import from `@calcom/trpc` or `@calcom/trpc/**` |
| 167 | +17. No files in `packages/platform/atoms` import from `../../trpc` or `../../trpc/**` |
| 168 | +18. No files in `packages/platform/atoms` import from `@calcom/web` |
| 169 | + |
| 170 | +**Incorrect:** |
| 171 | + |
| 172 | +```typescript |
| 173 | +// Bad - platform/atoms importing from trpc or web |
| 174 | +import { router } from "@calcom/trpc/server/trpc"; |
| 175 | +import { trpc } from "../../trpc"; |
| 176 | +import { WebComponent } from "@calcom/web"; |
| 177 | +``` |
| 178 | + |
| 179 | +**Correct:** |
| 180 | + |
| 181 | +```typescript |
| 182 | +// Good - platform/atoms imports from lib, features, and ui |
| 183 | +import { logger } from "@calcom/lib/logger"; |
| 184 | +import { Button } from "@calcom/ui/components/button"; |
| 185 | +import type { Booking } from "@calcom/features/bookings/types"; |
| 186 | +``` |
| 187 | + |
| 188 | +## Enforcement |
| 189 | + |
| 190 | +These rules should be enforced through: |
| 191 | +- ESLint rules that detect forbidden import paths |
| 192 | +- CI checks that fail on circular dependencies |
| 193 | +- Code review guidelines that flag violations |
0 commit comments