Monorepo Workflows with AI
You renamed a shared type in your monorepo’s core package. Seven downstream packages now have broken builds, two of them have integration tests that pass locally but fail in CI because they depend on a package that has not been rebuilt yet. The AI tool you used only looked at the file you were editing — it had no idea about the ripple effect across your workspace.
What You’ll Walk Away With
Section titled “What You’ll Walk Away With”- Monorepo-aware prompting strategies that account for cross-package dependencies
- Workflows for coordinated multi-package changes with AI assistance
- Techniques for AI-driven dependency graph analysis and impact assessment
- Automated release coordination patterns using AI tools
- Strategies for shared configuration and convention enforcement
The Monorepo Context Challenge
Section titled “The Monorepo Context Challenge”Monorepos amplify the context problem. A change to a shared utility might affect 15 packages. The AI needs to understand not just the file you are editing, but the entire dependency chain.
Teaching AI Tools Your Monorepo Structure
Section titled “Teaching AI Tools Your Monorepo Structure”Structure your .cursor/rules to encode the monorepo graph:
# .cursor/rules (root)This is a Turborepo monorepo managed with pnpm workspaces.
Package structure:- /packages/core - Shared types, utilities, base classes (NO external deps)- /packages/ui - React component library (depends on: core)- /packages/api-client - API SDK (depends on: core)- /apps/web - Next.js frontend (depends on: core, ui, api-client)- /apps/api - Express backend (depends on: core)- /apps/admin - Admin dashboard (depends on: core, ui, api-client)
Build order: core → ui, api-client → web, api, admin
CRITICAL RULES:- Changes to /packages/core affect ALL other packages. Always check downstream.- Never import from /apps/* into /packages/*- Shared types go in /packages/core/src/types/- Each package has its own tsconfig.json that extends /tsconfig.base.jsonWhen working on cross-package changes, explicitly reference the dependency chain:
@packages/core/src/types @packages/ui/src/components @apps/web/src/pages
Use the CLAUDE.md hierarchy to create monorepo-aware context:
# /CLAUDE.md (root)pnpm monorepo with Turborepo. Run `pnpm turbo build` to build all packages.Run `pnpm turbo test` to test all packages.Package dependency graph: core → ui, api-client → web, api, admin.Always run `pnpm turbo build --filter=...affected` after cross-package changes.
# /packages/core/CLAUDE.mdFoundation package. Changes here cascade everywhere.After ANY modification: run `pnpm turbo build --filter=...dependents`to verify downstream compatibility.Exports must be backward-compatible. Use deprecation notices, not breaking changes.
# /packages/ui/CLAUDE.mdReact component library. Storybook for development: `pnpm storybook`.Components must be exported from /src/index.ts barrel.Every component needs a .stories.tsx and .test.tsx file.Claude Code’s sub-agents can parallelize analysis across packages.
Codex cloud tasks handle monorepo scope naturally:
Monorepo with Turborepo + pnpm workspaces.Before making cross-package changes:1. Run: pnpm turbo build --dry-run --filter=...affected to preview impact2. Identify all packages in the dependency chain3. Make changes starting from the lowest dependency (core) upward4. Run tests after each package modification
After all changes: pnpm turbo build && pnpm turbo testUse Codex worktrees to work on multiple packages simultaneously without Git conflicts.
Cross-Package Refactoring Workflows
Section titled “Cross-Package Refactoring Workflows”The Cascade Pattern
Section titled “The Cascade Pattern”When modifying shared code, work from the bottom of the dependency tree upward.
-
Analyze the impact
Before touching any code, understand what will break.
-
Modify the source package
Change the shared type, utility, or component in the foundational package.
-
Build the source package
Verify the source package compiles correctly in isolation.
-
Update direct dependents
Move one level up the dependency tree. Update each direct dependent.
-
Build and test each dependent
Verify each dependent compiles and its tests pass before moving further up.
-
Continue up the tree
Repeat until you reach the leaf applications.
-
Run the full pipeline
Execute the complete build and test suite to catch any issues.
Shared Configuration Management
Section titled “Shared Configuration Management”AI tools can help maintain consistency across monorepo packages.
Dependency Graph Analysis
Section titled “Dependency Graph Analysis”Understanding your dependency graph is critical for safe changes. AI tools can generate and analyze these graphs.
Analyze the import statements across our entire monorepo and build a dependency graph.Flag any:- Circular dependencies between packages- Apps importing from other apps (forbidden)- Packages importing from apps (forbidden)- Unused packages (no dependents)- Packages with suspiciously deep dependency chains (> 3 levels)
Visualize the graph as a text-based tree structure.claude "Analyze our monorepo's package dependency graph.Read every package.json in /packages/ and /apps/.Build the complete dependency graph and check for:1. Circular dependencies2. Violation of the layering rule (apps must not depend on other apps)3. Version mismatches for shared deps4. Packages that could be merged (similar purpose, small surface)Output the graph and any issues found."Perform a full dependency analysis of this monorepo.For each package, document:- Direct dependencies (internal)- Transitive dependencies (internal)- External dependency versions- Build time and test coverage
Create a dependency graph visualization in /docs/dependency-graph.md.Flag any architectural violations or optimization opportunities.Release Coordination
Section titled “Release Coordination”Coordinated Version Bumping
Section titled “Coordinated Version Bumping”When a change spans multiple packages, version bumping needs to follow the dependency order.
When This Breaks
Section titled “When This Breaks”“The AI modified a shared package without updating dependents.” Your rules file does not encode the dependency graph. Add explicit dependency chains and build-order requirements to your .cursor/rules or CLAUDE.md.
“Build passes locally but fails in CI for downstream packages.” You likely tested with stale builds of dependencies. Always run turbo build --filter=...affected (or equivalent) to rebuild the entire affected chain.
“The AI created circular dependencies between packages.” Add explicit layering rules to your AI configuration: “packages/core MUST NOT import from packages/ui or any app.” AI tools follow constraints well when they are stated clearly.
“Version bumps are inconsistent across packages.” Use changesets or a similar tool for version management. Teach the AI your versioning workflow through rules files rather than relying on ad-hoc prompting.