|
| 1 | +# How to configure workspace environments |
| 2 | + |
| 3 | +----- |
| 4 | + |
| 5 | +Workspace environments allow you to manage multiple related packages within a single environment. This is useful for monorepos or projects with multiple interdependent packages. |
| 6 | + |
| 7 | +## Basic workspace configuration |
| 8 | + |
| 9 | +Define workspace members in your environment configuration using the `workspace.members` option: |
| 10 | + |
| 11 | +```toml config-example |
| 12 | +[tool.hatch.envs.default] |
| 13 | +workspace.members = [ |
| 14 | + "packages/core", |
| 15 | + "packages/utils", |
| 16 | + "packages/cli" |
| 17 | +] |
| 18 | +``` |
| 19 | + |
| 20 | +Workspace members are automatically installed as editable packages in the environment. |
| 21 | + |
| 22 | +## Pattern matching |
| 23 | + |
| 24 | +Use glob patterns to automatically discover workspace members: |
| 25 | + |
| 26 | +```toml config-example |
| 27 | +[tool.hatch.envs.default] |
| 28 | +workspace.members = ["packages/*"] |
| 29 | +``` |
| 30 | + |
| 31 | +## Excluding members |
| 32 | + |
| 33 | +Exclude specific packages from workspace discovery: |
| 34 | + |
| 35 | +```toml config-example |
| 36 | +[tool.hatch.envs.default] |
| 37 | +workspace.members = ["packages/*"] |
| 38 | +workspace.exclude = ["packages/experimental*"] |
| 39 | +``` |
| 40 | + |
| 41 | +## Member-specific features |
| 42 | + |
| 43 | +Install specific optional dependencies for workspace members: |
| 44 | + |
| 45 | +```toml config-example |
| 46 | +[tool.hatch.envs.default] |
| 47 | +workspace.members = [ |
| 48 | + {path = "packages/core", features = ["dev"]}, |
| 49 | + {path = "packages/utils", features = ["test", "docs"]}, |
| 50 | + "packages/cli" |
| 51 | +] |
| 52 | +``` |
| 53 | + |
| 54 | +## Environment-specific workspaces |
| 55 | + |
| 56 | +Different environments can include different workspace members: |
| 57 | + |
| 58 | +```toml config-example |
| 59 | +[tool.hatch.envs.unit-tests] |
| 60 | +workspace.members = ["packages/core", "packages/utils"] |
| 61 | +scripts.test = "pytest tests/unit" |
| 62 | + |
| 63 | +[tool.hatch.envs.integration-tests] |
| 64 | +workspace.members = ["packages/*"] |
| 65 | +scripts.test = "pytest tests/integration" |
| 66 | + |
| 67 | +[tool.hatch.envs.docs] |
| 68 | +workspace.members = [ |
| 69 | + {path = "packages/core", features = ["docs"]}, |
| 70 | + {path = "packages/utils", features = ["docs"]} |
| 71 | +] |
| 72 | +``` |
| 73 | + |
| 74 | +## Test matrices with workspaces |
| 75 | + |
| 76 | +Combine workspace configuration with test matrices: |
| 77 | + |
| 78 | +```toml config-example |
| 79 | +[[tool.hatch.envs.test.matrix]] |
| 80 | +python = ["3.9", "3.10", "3.11", "3.12"] |
| 81 | + |
| 82 | +[tool.hatch.envs.test] |
| 83 | +workspace.members = ["packages/*"] |
| 84 | +dependencies = ["pytest", "coverage"] |
| 85 | +scripts.test = "pytest {args}" |
| 86 | + |
| 87 | +[[tool.hatch.envs.test-core.matrix]] |
| 88 | +python = ["3.9", "3.10", "3.11", "3.12"] |
| 89 | + |
| 90 | +[tool.hatch.envs.test-core] |
| 91 | +workspace.members = ["packages/core"] |
| 92 | +dependencies = ["pytest", "coverage"] |
| 93 | +scripts.test = "pytest packages/core/tests {args}" |
| 94 | +``` |
| 95 | + |
| 96 | +## Performance optimization |
| 97 | + |
| 98 | +Enable parallel dependency resolution for faster environment setup: |
| 99 | + |
| 100 | +```toml config-example |
| 101 | +[tool.hatch.envs.default] |
| 102 | +workspace.members = ["packages/*"] |
| 103 | +workspace.parallel = true |
| 104 | +``` |
| 105 | + |
| 106 | +## Cross-member dependencies |
| 107 | + |
| 108 | +Workspace members can depend on each other. When installed, pip or uv will resolve the dependency relationships: |
| 109 | + |
| 110 | +```toml tab="packages/app/pyproject.toml" |
| 111 | +[project] |
| 112 | +name = "app" |
| 113 | +dependencies = ["core", "utils"] |
| 114 | +``` |
| 115 | + |
| 116 | +```toml tab="pyproject.toml" |
| 117 | +[tool.hatch.envs.default] |
| 118 | +workspace.members = [ |
| 119 | + "packages/core", |
| 120 | + "packages/utils", |
| 121 | + "packages/app" |
| 122 | +] |
| 123 | +``` |
| 124 | + |
| 125 | +All workspace members are installed as editable packages. Pip or uv handles resolving the dependencies between app, core, and utils during installation. |
| 126 | + |
| 127 | +## Monorepo example |
| 128 | + |
| 129 | +Complete configuration for a typical monorepo structure: |
| 130 | + |
| 131 | +```toml config-example |
| 132 | +# Root pyproject.toml |
| 133 | +[project] |
| 134 | +name = "my-monorepo" |
| 135 | +version = "1.0.0" |
| 136 | + |
| 137 | +[tool.hatch.envs.default] |
| 138 | +workspace.members = ["packages/*"] |
| 139 | +workspace.exclude = ["packages/experimental*"] |
| 140 | +workspace.parallel = true |
| 141 | +dependencies = ["pytest", "black", "ruff"] |
| 142 | + |
| 143 | +[tool.hatch.envs.test] |
| 144 | +workspace.members = [ |
| 145 | + {path = "packages/core", features = ["test"]}, |
| 146 | + {path = "packages/utils", features = ["test"]}, |
| 147 | + "packages/cli" |
| 148 | +] |
| 149 | +dependencies = ["pytest", "coverage", "pytest-cov"] |
| 150 | +scripts.test = "pytest --cov {args}" |
| 151 | + |
| 152 | +[tool.hatch.envs.lint] |
| 153 | +detached = true |
| 154 | +workspace.members = ["packages/*"] |
| 155 | +dependencies = ["ruff", "black", "mypy"] |
| 156 | +scripts.check = ["ruff check .", "black --check .", "mypy ."] |
| 157 | +scripts.fmt = ["ruff check --fix .", "black ."] |
| 158 | + |
| 159 | +[[tool.hatch.envs.ci.matrix]] |
| 160 | +python = ["3.9", "3.10", "3.11", "3.12"] |
| 161 | + |
| 162 | +[tool.hatch.envs.ci] |
| 163 | +template = "test" |
| 164 | +workspace.parallel = false # Disable for CI stability |
| 165 | +``` |
| 166 | + |
| 167 | +## Library with plugins example |
| 168 | + |
| 169 | +Configuration for a library with optional plugins: |
| 170 | + |
| 171 | +```toml config-example |
| 172 | +[tool.hatch.envs.default] |
| 173 | +workspace.members = ["core"] |
| 174 | +dependencies = ["pytest"] |
| 175 | + |
| 176 | +[tool.hatch.envs.full] |
| 177 | +workspace.members = [ |
| 178 | + "core", |
| 179 | + "plugins/database", |
| 180 | + "plugins/cache", |
| 181 | + "plugins/auth" |
| 182 | +] |
| 183 | +dependencies = ["pytest", "pytest-asyncio"] |
| 184 | + |
| 185 | +[tool.hatch.envs.database-only] |
| 186 | +workspace.members = [ |
| 187 | + "core", |
| 188 | + {path = "plugins/database", features = ["postgresql", "mysql"]} |
| 189 | +] |
| 190 | + |
| 191 | +[[tool.hatch.envs.plugin-test.matrix]] |
| 192 | +plugin = ["database", "cache", "auth"] |
| 193 | + |
| 194 | +[tool.hatch.envs.plugin-test] |
| 195 | +workspace.members = [ |
| 196 | + "core", |
| 197 | + "plugins/{matrix:plugin}" |
| 198 | +] |
| 199 | +scripts.test = "pytest plugins/{matrix:plugin}/tests {args}" |
| 200 | +``` |
| 201 | + |
| 202 | +## Multi-service application example |
| 203 | + |
| 204 | +Configuration for microservices development: |
| 205 | + |
| 206 | +```toml config-example |
| 207 | +[tool.hatch.envs.default] |
| 208 | +workspace.members = ["shared"] |
| 209 | +dependencies = ["pytest", "requests"] |
| 210 | + |
| 211 | +[tool.hatch.envs.api] |
| 212 | +workspace.members = [ |
| 213 | + "shared", |
| 214 | + {path = "services/api", features = ["dev"]} |
| 215 | +] |
| 216 | +dependencies = ["fastapi", "uvicorn"] |
| 217 | +scripts.dev = "uvicorn services.api.main:app --reload" |
| 218 | + |
| 219 | +[tool.hatch.envs.worker] |
| 220 | +workspace.members = [ |
| 221 | + "shared", |
| 222 | + {path = "services/worker", features = ["dev"]} |
| 223 | +] |
| 224 | +dependencies = ["celery", "redis"] |
| 225 | +scripts.dev = "celery -A services.worker.tasks worker --loglevel=info" |
| 226 | + |
| 227 | +[tool.hatch.envs.integration] |
| 228 | +workspace.members = [ |
| 229 | + "shared", |
| 230 | + "services/api", |
| 231 | + "services/worker", |
| 232 | + "services/frontend" |
| 233 | +] |
| 234 | +dependencies = ["pytest", "httpx", "docker"] |
| 235 | +scripts.test = "pytest tests/integration {args}" |
| 236 | +``` |
| 237 | + |
| 238 | +## Documentation generation example |
| 239 | + |
| 240 | +Configuration for generating documentation across packages: |
| 241 | + |
| 242 | +```toml config-example |
| 243 | +[tool.hatch.envs.docs] |
| 244 | +workspace.members = [ |
| 245 | + {path = "packages/core", features = ["docs"]}, |
| 246 | + {path = "packages/cli", features = ["docs"]}, |
| 247 | + {path = "packages/plugins", features = ["docs"]} |
| 248 | +] |
| 249 | +dependencies = [ |
| 250 | + "mkdocs", |
| 251 | + "mkdocs-material", |
| 252 | + "mkdocstrings[python]" |
| 253 | +] |
| 254 | +scripts.serve = "mkdocs serve" |
| 255 | +scripts.build = "mkdocs build" |
| 256 | + |
| 257 | +[tool.hatch.envs.docs-api-only] |
| 258 | +workspace.members = [ |
| 259 | + {path = "packages/core", features = ["docs"]} |
| 260 | +] |
| 261 | +template = "docs" |
| 262 | +scripts.serve = "mkdocs serve --config-file mkdocs-api.yml" |
| 263 | +``` |
| 264 | + |
| 265 | +## Development workflow example |
| 266 | + |
| 267 | +Configuration supporting different development workflows: |
| 268 | + |
| 269 | +```toml config-example |
| 270 | +[tool.hatch.envs.dev] |
| 271 | +workspace.members = ["packages/*"] |
| 272 | +workspace.parallel = true |
| 273 | +dependencies = [ |
| 274 | + "pytest", |
| 275 | + "black", |
| 276 | + "ruff", |
| 277 | + "mypy", |
| 278 | + "pre-commit" |
| 279 | +] |
| 280 | +scripts.setup = "pre-commit install" |
| 281 | +scripts.test = "pytest {args}" |
| 282 | +scripts.lint = ["ruff check .", "black --check .", "mypy ."] |
| 283 | +scripts.fmt = ["ruff check --fix .", "black ."] |
| 284 | + |
| 285 | +[tool.hatch.envs.feature] |
| 286 | +template = "dev" |
| 287 | +workspace.members = [ |
| 288 | + "packages/core", |
| 289 | + "packages/{env:FEATURE_PACKAGE}" |
| 290 | +] |
| 291 | +scripts.test = "pytest packages/{env:FEATURE_PACKAGE}/tests {args}" |
| 292 | + |
| 293 | +[[tool.hatch.envs.release.matrix]] |
| 294 | +package = ["core", "utils", "cli"] |
| 295 | + |
| 296 | +[tool.hatch.envs.release] |
| 297 | +detached = true |
| 298 | +workspace.members = ["packages/{matrix:package}"] |
| 299 | +dependencies = ["build", "twine"] |
| 300 | +scripts.build = "python -m build packages/{matrix:package}" |
| 301 | +scripts.publish = "twine upload packages/{matrix:package}/dist/*" |
| 302 | +``` |
0 commit comments