|
| 1 | +--- |
| 2 | +name: rushstack-best-practices |
| 3 | +description: Provides best practices and guidance for working with Rush monorepos. Use when the user is working in a Rush-based repository, asks about Rush commands (install, update, build, rebuild), needs help with project selection, dependency management, build caching, subspace configuration, or troubleshooting Rush-specific issues. |
| 4 | +license: MIT |
| 5 | +metadata: |
| 6 | + author: rushstack |
| 7 | + version: "1.0.0" |
| 8 | +--- |
| 9 | + |
| 10 | +# Rushstack Best Practices |
| 11 | + |
| 12 | +This skill provides essential best practices for working with Rush monorepos. Following these guidelines ensures efficient dependency management, optimal build performance, and proper command usage. |
| 13 | + |
| 14 | +## Important Guidelines |
| 15 | + |
| 16 | +**When encountering unclear issues or questions:** |
| 17 | + |
| 18 | +1. **Never make assumptions** - If unsure about Rush behavior, configuration, or commands |
| 19 | +2. **Search official resources first** - Check documentation and existing issues before guessing |
| 20 | +3. **Provide accurate information** - Base responses on verified sources, not assumptions |
| 21 | +4. **Ask for clarification** - When the problem description is ambiguous or incomplete |
| 22 | + |
| 23 | +## Core Principles |
| 24 | + |
| 25 | +1. **Always use Rush commands** - Avoid npm/pnpm/yarn directly in a Rush monorepo |
| 26 | +2. **Use rushx for single projects** - Like npm run, but Rush-aware |
| 27 | +3. **rush install vs update** - install for CI, update after changes |
| 28 | +4. **rush build vs rebuild** - build for incremental, rebuild for clean |
| 29 | +5. **Projects at 2 levels** - Standard: apps/, libraries/, tools/ |
| 30 | +6. **Selection flags reduce scope** - Use --to, --from, --impacted-by |
| 31 | +7. **Build cache is automatic** - Configure output folders to enable |
| 32 | +8. **Subspace for large repos** - Isolate dependencies when needed |
| 33 | + |
| 34 | +## Project Selection Best Practices |
| 35 | + |
| 36 | +When running commands like `install`, `update`, `build`, `rebuild`, etc., by default all projects under the entire repository are processed. Use these selection flags to improve efficiency: |
| 37 | + |
| 38 | +### --to <PROJECT> |
| 39 | +Select specified project and all its dependencies. |
| 40 | +- Build specific project and its dependencies |
| 41 | +- Ensure complete dependency chain build |
| 42 | +```bash |
| 43 | +rush build --to @my-company/my-project |
| 44 | +rush build --to my-project # If project name is unique |
| 45 | +rush build --to . # Use current directory's project |
| 46 | +``` |
| 47 | + |
| 48 | +### --to-except <PROJECT> |
| 49 | +Select all dependencies of specified project, but not the project itself. |
| 50 | +- Update project dependencies without processing project itself |
| 51 | +- Pre-build dependencies |
| 52 | +```bash |
| 53 | +rush build --to-except @my-company/my-project |
| 54 | +``` |
| 55 | + |
| 56 | +### --from <PROJECT> |
| 57 | +Select specified project and all its downstream dependencies. |
| 58 | +- Validate changes' impact on downstream projects |
| 59 | +- Build all projects affected by specific project |
| 60 | +```bash |
| 61 | +rush build --from @my-company/my-library |
| 62 | +``` |
| 63 | + |
| 64 | +### --impacted-by <PROJECT> |
| 65 | +Select projects that might be affected by specified project changes, excluding dependencies. |
| 66 | +- Quick test of project change impacts |
| 67 | +- Use when dependency status is already correct |
| 68 | +```bash |
| 69 | +rush build --impacted-by @my-company/my-library |
| 70 | +``` |
| 71 | + |
| 72 | +### --impacted-by-except <PROJECT> |
| 73 | +Similar to `--impacted-by`, but excludes specified project itself. |
| 74 | +- Project itself has been manually built |
| 75 | +- Only need to test downstream impacts |
| 76 | +```bash |
| 77 | +rush build --impacted-by-except @my-company/my-library |
| 78 | +``` |
| 79 | + |
| 80 | +### --only <PROJECT> |
| 81 | +Only select specified project, completely ignore dependency relationships. |
| 82 | +- Dependency status is known to be correct |
| 83 | +- Combine with other selection parameters |
| 84 | +```bash |
| 85 | +rush build --only @my-company/my-project |
| 86 | +rush build --impacted-by projectA --only projectB |
| 87 | +``` |
| 88 | + |
| 89 | +## Command Usage Guidelines |
| 90 | + |
| 91 | +### Command Tool Selection |
| 92 | + |
| 93 | +Choose the correct command tool based on different scenarios: |
| 94 | + |
| 95 | +1. **`rush` command** - Execute operations affecting the entire repository or multiple projects |
| 96 | + - Strict parameter validation and documentation |
| 97 | + - Support for global and batch commands |
| 98 | + - Suitable for standardized workflows |
| 99 | + - Use cases: Dependency installation, building, publishing |
| 100 | + |
| 101 | +2. **`rushx` command** - Execute specific scripts for a single project |
| 102 | + - Similar to `npm run` or `pnpm run` |
| 103 | + - Uses Rush version selector for toolchain consistency |
| 104 | + - Prepares shell environment based on Rush configuration |
| 105 | + - Use cases: Running project-specific build scripts, tests, dev servers |
| 106 | + |
| 107 | +3. **`rush-pnpm` command** - Replace direct use of pnpm in Rush repository |
| 108 | + - Sets correct PNPM workspace context |
| 109 | + - Supports Rush-specific enhancements |
| 110 | + - Provides compatibility checks with Rush |
| 111 | + - Use cases: When direct PNPM commands are needed |
| 112 | + |
| 113 | +### Install vs Update |
| 114 | + |
| 115 | +| Command | Behavior | When to Use | |
| 116 | +|---------|----------|-------------| |
| 117 | +| `rush update` | Updates shrinkwrap, installs new dependencies | After cloning, after git pull, after modifying package.json | |
| 118 | +| `rush install` | Read-only install from existing shrinkwrap | CI/CD pipelines, ensuring version consistency | |
| 119 | + |
| 120 | +### Build vs Rebuild |
| 121 | + |
| 122 | +| Command | Behavior | When to Use | |
| 123 | +|---------|----------|-------------| |
| 124 | +| `rush build` | Incremental build, only changed projects | Daily development, quick validation | |
| 125 | +| `rush rebuild` | Clean build all projects | Complete rebuild needed, investigating issues | |
| 126 | + |
| 127 | +## Dependency Management |
| 128 | + |
| 129 | +### Package Manager Selection |
| 130 | +Choose in `rush.json`: |
| 131 | +```json |
| 132 | +{ |
| 133 | + "pnpmVersion": "8.x.x" // Preferred - efficient, strict |
| 134 | + // "npmVersion": "8.x.x" // Alternative |
| 135 | + // "yarnVersion": "1.x.x" // Alternative |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +### Version Constraints |
| 140 | +Configure in `common/config/subspaces/<subspace>/common-versions.json`: |
| 141 | +```json |
| 142 | +{ |
| 143 | + "preferredVersions": { |
| 144 | + "react": "17.0.2", |
| 145 | + "typescript": "~4.5.0" |
| 146 | + }, |
| 147 | + "implicitlyPreferredVersions": true, |
| 148 | + "allowedAlternativeVersions": { |
| 149 | + "typescript": ["~4.5.0", "~4.6.0"] |
| 150 | + } |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +### Adding/Removing Dependencies |
| 155 | +Always use Rush commands, not npm/pnpm directly: |
| 156 | +```bash |
| 157 | +rush add -p lodash --dev # Add dev dependency |
| 158 | +rush add -p react --exact # Add exact version |
| 159 | +rush remove -p lodash # Remove dependency |
| 160 | +``` |
| 161 | + |
| 162 | +## Build Cache Configuration |
| 163 | + |
| 164 | +Configure in `<project>/config/rush-project.json`: |
| 165 | +```json |
| 166 | +{ |
| 167 | + "operationSettings": [ |
| 168 | + { |
| 169 | + "operationName": "build", |
| 170 | + "outputFolderNames": ["lib", "dist"], |
| 171 | + "disableBuildCacheForOperation": false, |
| 172 | + "dependsOnEnvVars": ["MY_ENV_VAR"] |
| 173 | + } |
| 174 | + ] |
| 175 | +} |
| 176 | +``` |
| 177 | + |
| 178 | +**Cache Behavior:** |
| 179 | +- Cache stored in `common/temp/build-cache` |
| 180 | +- Invalidated by: source changes, dependency changes, env vars, command params |
| 181 | +- Parallel builds supported via `enableParallelism` |
| 182 | + |
| 183 | +## Troubleshooting |
| 184 | + |
| 185 | +### Dependency Issues |
| 186 | +- Avoid `npm`, `pnpm`, `yarn` - use Rush commands |
| 187 | +- Run `rush purge` to clean environment |
| 188 | +- Run `rush update --recheck` to force dependency check |
| 189 | + |
| 190 | +### Build Issues |
| 191 | +- Use `rush rebuild` to skip cache |
| 192 | +- Check `rushx build` output for specific errors |
| 193 | +- Use `--verbose` for detailed logs |
| 194 | + |
| 195 | +### Performance Issues |
| 196 | +- Use selection flags (`--to`, `--from`, etc.) to reduce scope |
| 197 | +- Enable build cache in rush-project.json |
| 198 | +- Consider subspace for very large monorepos |
| 199 | + |
| 200 | +## Subspace for Large Monorepos |
| 201 | + |
| 202 | +**What is Subspace:** |
| 203 | +- Allows multiple PNPM lock files in one Rush monorepo |
| 204 | +- Enables independent dependency management per team/project group |
| 205 | +- Reduces risk from dependency updates |
| 206 | +- Improves install/update performance |
| 207 | + |
| 208 | +**When to Use:** |
| 209 | +- Large monorepos (50+ projects) |
| 210 | +- Multiple teams with different dependency needs |
| 211 | +- Conflicting version requirements |
| 212 | +- Need for faster dependency operations |
| 213 | + |
| 214 | +## Official Resources |
| 215 | + |
| 216 | +### Documentation & References |
| 217 | + |
| 218 | +**Official Websites:** |
| 219 | +- [RushStack.io](https://rushstack.io/) - Main documentation site |
| 220 | +- [Rush.js.io](https://rushjs.io/) - Rush build orchestrator documentation |
| 221 | +- [Heft.rushstack.io](https://heft.rushstack.io/) - Heft build tool documentation |
| 222 | +- [API Extractor](https://api-extractor.com/) - API documentation and rollups |
| 223 | + |
| 224 | +**Search Existing Issues:** |
| 225 | +- Before creating new issues, search [rush-stack-builds issues](https://github.com/microsoft/rushstack/issues) |
| 226 | + |
| 227 | +### When to Search vs. Ask |
| 228 | + |
| 229 | +**Search these resources first when:** |
| 230 | +- Encountering error messages |
| 231 | +- Unsure about configuration options |
| 232 | +- Looking for examples or tutorials |
| 233 | +- Need to understand Rush behavior |
| 234 | + |
| 235 | +**Ask the user for clarification when:** |
| 236 | +- The specific use case is unclear |
| 237 | +- Multiple approaches are possible |
| 238 | +- Context is missing to provide accurate guidance |
| 239 | +- The issue might be environment-specific |
| 240 | + |
| 241 | +## Detailed References |
| 242 | + |
| 243 | +For expanded information on specific domains, see: |
| 244 | +- `references/core-commands.md` - Detailed command reference |
| 245 | +- `references/project-configuration.md` - Configuration file specifications |
| 246 | +- `references/dependency-management.md` - Advanced dependency patterns |
| 247 | +- `references/build-system.md` - Build optimization and caching |
| 248 | +- `references/subspace.md` - Subspace setup and usage |
0 commit comments