Skip to content

Commit f9fbcac

Browse files
cjames23ofek
andauthored
Workspace Support (#2073)
Co-authored-by: Ofek Lev <[email protected]>
1 parent 9e1fc34 commit f9fbcac

File tree

31 files changed

+2492
-212
lines changed

31 files changed

+2492
-212
lines changed

.github/workflows/build-hatch.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
run: |-
5252
uv pip install --system build
5353
uv pip install --system .
54-
uv pip install --system ./backend
54+
hatch env create
5555
5656
# Windows installers don't accept non-integer versions so we ubiquitously
5757
# perform the following transformation: X.Y.Z.devN -> X.Y.Z.N
@@ -166,7 +166,7 @@ jobs:
166166
- name: Install Hatch
167167
run: |-
168168
uv pip install --system -e .
169-
uv pip install --system -e ./backend
169+
hatch env create
170170
171171
- name: Install Rust toolchain
172172
uses: dtolnay/rust-toolchain@stable

.github/workflows/cli.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ jobs:
4343
- name: Install ourself
4444
run: |
4545
uv pip install --system .
46-
uv pip install --system ./backend
4746
4847
- name: Benchmark
4948
run: |

.github/workflows/docs-dev.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
- name: Install ourself
3939
run: |
4040
uv pip install --system -e .
41-
uv pip install --system -e ./backend
41+
hatch env create
4242
4343
- name: Configure Git for GitHub Actions bot
4444
run: |

.github/workflows/docs-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
- name: Install ourself
3737
run: |
3838
uv pip install --system -e .
39-
uv pip install --system -e ./backend
39+
hatch env create
4040
4141
- name: Display full version
4242
run: hatch version

.github/workflows/test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
fail-fast: false
2525
matrix:
2626
os: [ubuntu-latest, windows-latest, macos-latest]
27-
python-version: ['3.9', '3.10', '3.11', '3.12', '3.13']
27+
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
2828

2929
steps:
3030
- uses: actions/checkout@v4
@@ -40,7 +40,7 @@ jobs:
4040
- name: Install ourself
4141
run: |
4242
uv pip install --system -e .
43-
uv pip install --system -e ./backend
43+
hatch env create
4444
4545
- name: Run static analysis
4646
run: hatch fmt --check
@@ -115,7 +115,7 @@ jobs:
115115
strategy:
116116
fail-fast: false
117117
matrix:
118-
python-version: ['3.9', '3.10', '3.11', '3.12', '3.13']
118+
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
119119

120120
steps:
121121
- uses: actions/checkout@v4

docs/history/hatchling.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1212

1313
- Drop support for Python 3.8
1414

15+
***Changed:***
16+
17+
- Drop support for Python 3.8
18+
1519
## [1.27.0](https://github.com/pypa/hatch/releases/tag/hatchling-v1.27.0) - 2024-11-26 ## {: #hatchling-v1.27.0 }
1620

1721
***Added:***
Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
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+
```

docs/meta/authors.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
- Olga Matoula [:material-github:](https://github.com/olgarithms) [:material-twitter:](https://twitter.com/olgarithms_)
1818
- Philip Blair [:material-email:](mailto:[email protected])
1919
- Robert Rosca [:material-github:](https://github.com/robertrosca)
20+
- Cary Hawkins [:material-github](https://github.com/cjames23)

docs/plugins/environment/reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ All environment types should [offer support](#hatch.env.plugin.interface.Environ
5151
- dependencies_in_sync
5252
- sync_dependencies
5353
- dependency_hash
54+
- project_dependencies
5455
- project_root
5556
- sep
5657
- pathsep

0 commit comments

Comments
 (0)