Skip to content

Commit ab3e000

Browse files
authored
Add --all flag to hatch build for building all workspace members (#2352)
* Add --all flag to hatch build for building all workspace members * Fix so all flag does not require root project as a member and build when no top level project defined handles workspace members only
1 parent bb90277 commit ab3e000

5 files changed

Lines changed: 266 additions & 17 deletions

File tree

‎docs/history/hatch.md‎

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

99
## Unreleased
1010

11+
***Added:***
12+
13+
- Add the `--all`/`-a` flag to the `build` command to build the workspace root and every workspace member defined by the selected environment, consolidating artifacts in the workspace root's `dist` directory by default. A root that does not define a `project` table is skipped so that a top-level `pyproject.toml` may only contain workspace configuration
1114

1215
***Fixed:***
1316

‎docs/how-to/environment/workspace.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ workspace.members = [
7171
]
7272
```
7373

74+
## Building all members
75+
76+
Build an `sdist` and `wheel` for the workspace root and every workspace member with the [`build`](../../cli/reference.md#hatch-build) command's `--all` flag:
77+
78+
```
79+
hatch build --all
80+
```
81+
82+
The root project is built first and does not need to be listed as a member. If the top-level `pyproject.toml` does not define a `project` table and only serves as a container for workspace configuration, the root is skipped and only the members are built. Members are discovered from the selected environment's `workspace.members` option and artifacts are consolidated in the workspace root's `dist` directory by default, ready for e.g. `twine upload dist/*`. Pass a location argument to write artifacts elsewhere:
83+
84+
```
85+
hatch build --all out/artifacts
86+
```
87+
7488
## Test matrices with workspaces
7589

7690
Combine workspace configuration with test matrices:

‎src/hatch/cli/build/__init__.py‎

Lines changed: 96 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
if TYPE_CHECKING:
88
from hatch.cli.application import Application
9+
from hatch.project.core import Project
910

1011

1112
@click.command(short_help="Build a project")
@@ -19,6 +20,16 @@
1920
"The target to build, overriding project defaults. This may be selected multiple times e.g. `-t sdist -t wheel`"
2021
),
2122
)
23+
@click.option(
24+
"--all",
25+
"-a",
26+
"build_all",
27+
is_flag=True,
28+
help=(
29+
"Whether or not to build the workspace root and every workspace member defined by the selected "
30+
"environment. Artifacts are written to the workspace root's `dist` directory by default"
31+
),
32+
)
2233
@click.option(
2334
"--hooks-only", is_flag=True, help="Whether or not to only execute build hooks [env var: `HATCH_BUILD_HOOKS_ONLY`]"
2435
)
@@ -49,18 +60,16 @@
4960
)
5061
@click.option("--clean-only", is_flag=True, hidden=True)
5162
@click.pass_obj
52-
def build(app: Application, location, targets, hooks_only, no_hooks, ext, clean, clean_hooks_after, clean_only):
63+
def build(
64+
app: Application, location, targets, build_all, hooks_only, no_hooks, ext, clean, clean_hooks_after, clean_only
65+
):
5366
"""Build a project."""
5467
app.ensure_environment_plugin_dependencies()
5568

5669
from hatch.config.constants import AppEnvVars
57-
from hatch.project.config import env_var_enabled
58-
from hatch.project.constants import BUILD_BACKEND, DEFAULT_BUILD_DIRECTORY, BuildEnvVars
70+
from hatch.project.constants import DEFAULT_BUILD_DIRECTORY
5971
from hatch.utils.fs import Path
60-
from hatch.utils.runner import ExecutionContext
61-
from hatch.utils.structures import EnvVars
6272

63-
build_dir = Path(location).resolve() if location else None
6473
if ext:
6574
hooks_only = True
6675
targets = ("wheel",)
@@ -73,31 +82,101 @@ def build(app: Application, location, targets, hooks_only, no_hooks, ext, clean,
7382
elif app.quiet:
7483
env_vars[AppEnvVars.QUIET] = str(abs(app.verbosity))
7584

85+
if not build_all:
86+
_build_project(
87+
app,
88+
app.project,
89+
location,
90+
targets,
91+
hooks_only=hooks_only,
92+
no_hooks=no_hooks,
93+
clean=clean,
94+
clean_hooks_after=clean_hooks_after,
95+
clean_only=clean_only,
96+
env_vars=env_vars,
97+
)
98+
return
99+
100+
environment = app.project.get_environment()
101+
members = environment.workspace.members
102+
if not members:
103+
app.abort(
104+
f"The `--all` flag requires workspace members to be defined in field "
105+
f"`tool.hatch.envs.{environment.name}.workspace.members`"
106+
)
107+
108+
# Artifacts from every project are consolidated in a single directory, defaulting to the
109+
# workspace root. The location must be absolute because each member builds from its own path
110+
build_directory = str(Path(location).resolve() if location else app.project.location / DEFAULT_BUILD_DIRECTORY)
111+
112+
# The workspace root is built without needing to be listed as a member, but only when it
113+
# defines a project itself rather than merely being a container for workspace configuration
114+
projects = [app.project] if app.project.defines_project else []
115+
projects.extend(member.project for member in members if member.project.location != app.project.location)
116+
for project in projects:
117+
if not clean_only:
118+
app.display_header(project.metadata.name)
119+
120+
_build_project(
121+
app,
122+
project,
123+
build_directory,
124+
targets,
125+
hooks_only=hooks_only,
126+
no_hooks=no_hooks,
127+
clean=clean,
128+
clean_hooks_after=clean_hooks_after,
129+
clean_only=clean_only,
130+
env_vars=env_vars,
131+
)
132+
133+
134+
def _build_project(
135+
app: Application,
136+
project: Project,
137+
location,
138+
targets,
139+
*,
140+
hooks_only,
141+
no_hooks,
142+
clean,
143+
clean_hooks_after,
144+
clean_only,
145+
env_vars,
146+
):
147+
from hatch.project.config import env_var_enabled
148+
from hatch.project.constants import BUILD_BACKEND, DEFAULT_BUILD_DIRECTORY, BuildEnvVars
149+
from hatch.utils.fs import Path
150+
from hatch.utils.runner import ExecutionContext
151+
from hatch.utils.structures import EnvVars
152+
153+
build_dir = Path(location).resolve() if location else None
154+
76155
with EnvVars(env_vars):
77-
app.project.prepare_build_environment(targets=[target.split(":")[0] for target in targets])
156+
project.prepare_build_environment(targets=[target.split(":")[0] for target in targets])
78157

79-
build_backend = app.project.metadata.build.build_backend
80-
with app.project.location.as_cwd(), app.project.build_env.get_env_vars():
158+
build_backend = project.metadata.build.build_backend
159+
with project.location.as_cwd(), project.build_env.get_env_vars():
81160
for target in targets:
82161
target_name, _, _ = target.partition(":")
83162
if not clean_only:
84163
app.display_header(target_name)
85164

86165
if build_backend != BUILD_BACKEND:
87166
if target_name == "sdist":
88-
directory = build_dir or app.project.location / DEFAULT_BUILD_DIRECTORY
167+
directory = build_dir or project.location / DEFAULT_BUILD_DIRECTORY
89168
directory.ensure_dir_exists()
90-
artifact_path = app.project.build_frontend.build_sdist(directory)
169+
artifact_path = project.build_frontend.build_sdist(directory)
91170
elif target_name == "wheel":
92-
directory = build_dir or app.project.location / DEFAULT_BUILD_DIRECTORY
171+
directory = build_dir or project.location / DEFAULT_BUILD_DIRECTORY
93172
directory.ensure_dir_exists()
94-
artifact_path = app.project.build_frontend.build_wheel(directory)
173+
artifact_path = project.build_frontend.build_wheel(directory)
95174
else:
96175
app.abort(f"Target `{target_name}` is not supported by `{build_backend}`")
97176

98177
app.display_info(
99-
str(artifact_path.relative_to(app.project.location))
100-
if app.project.location in artifact_path.parents
178+
str(artifact_path.relative_to(project.location))
179+
if project.location in artifact_path.parents
101180
else str(artifact_path)
102181
)
103182
else:
@@ -106,7 +185,7 @@ def build(app: Application, location, targets, hooks_only, no_hooks, ext, clean,
106185
# We deliberately pass the location unchanged so that absolute paths may be non-local
107186
# and reflect wherever builds actually take place
108187
if location:
109-
command.extend(("--directory", location))
188+
command.extend(("--directory", str(location)))
110189

111190
if hooks_only or env_var_enabled(BuildEnvVars.HOOKS_ONLY):
112191
command.append("--hooks-only")
@@ -123,7 +202,7 @@ def build(app: Application, location, targets, hooks_only, no_hooks, ext, clean,
123202
if clean_only:
124203
command.append("--clean-only")
125204

126-
context = ExecutionContext(app.project.build_env)
205+
context = ExecutionContext(project.build_env)
127206
context.add_shell_command(command)
128207
context.env_vars.update(env_vars)
129208
app.execute_context(context)

‎src/hatch/project/core.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,19 @@ def metadata(self):
417417

418418
return self._metadata
419419

420+
@cached_property
421+
def defines_project(self) -> bool:
422+
"""
423+
Whether the project file defines a `project` table itself, as opposed to one being
424+
synthesized for environment management only.
425+
"""
426+
if self.root is None or self._project_file_path is None:
427+
return False
428+
429+
from hatch.utils.toml import load_toml_file
430+
431+
return "project" in load_toml_file(str(self._project_file_path))
432+
420433
@property
421434
def raw_config(self):
422435
if self._raw_config is None:

‎tests/cli/build/test_build.py‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,3 +1420,143 @@ def test_plugin_dependencies_unmet(hatch, temp_dir, helpers, mock_plugin_install
14201420
"""
14211421
)
14221422
helpers.assert_plugin_installation(mock_plugin_installation, [dependency])
1423+
1424+
1425+
class TestBuildAll:
1426+
def _create_workspace(self, hatch, temp_dir):
1427+
with temp_dir.as_cwd():
1428+
result = hatch("new", "workspace-root")
1429+
assert result.exit_code == 0, result.output
1430+
1431+
workspace_root = temp_dir / "workspace-root"
1432+
with (workspace_root / "pyproject.toml").open("a") as f:
1433+
f.write(
1434+
"""
1435+
[tool.hatch.envs.default]
1436+
workspace.members = ["packages/*"]
1437+
"""
1438+
)
1439+
1440+
packages_dir = workspace_root / "packages"
1441+
packages_dir.mkdir()
1442+
with packages_dir.as_cwd():
1443+
for project_name in ("member1", "member2"):
1444+
result = hatch("new", project_name)
1445+
assert result.exit_code == 0, result.output
1446+
1447+
return workspace_root
1448+
1449+
def test_no_workspace_members(self, hatch, temp_dir, helpers):
1450+
project_name = "My.App"
1451+
1452+
with temp_dir.as_cwd():
1453+
result = hatch("new", project_name)
1454+
assert result.exit_code == 0, result.output
1455+
1456+
path = temp_dir / "my-app"
1457+
1458+
with path.as_cwd():
1459+
result = hatch("build", "--all")
1460+
1461+
assert result.exit_code == 1, result.output
1462+
assert result.output == helpers.dedent(
1463+
"""
1464+
The `--all` flag requires workspace members to be defined in field `tool.hatch.envs.default.workspace.members`
1465+
"""
1466+
)
1467+
1468+
def test_default(self, hatch, temp_dir):
1469+
workspace_root = self._create_workspace(hatch, temp_dir)
1470+
1471+
with workspace_root.as_cwd():
1472+
result = hatch("build", "--all")
1473+
assert result.exit_code == 0, result.output
1474+
1475+
build_directory = workspace_root / "dist"
1476+
assert build_directory.is_dir()
1477+
1478+
artifacts = sorted(artifact.name for artifact in build_directory.iterdir())
1479+
assert artifacts == [
1480+
"member1-0.0.1-py3-none-any.whl",
1481+
"member1-0.0.1.tar.gz",
1482+
"member2-0.0.1-py3-none-any.whl",
1483+
"member2-0.0.1.tar.gz",
1484+
"workspace_root-0.0.1-py3-none-any.whl",
1485+
"workspace_root-0.0.1.tar.gz",
1486+
]
1487+
1488+
for project_name in ("workspace-root", "member1", "member2"):
1489+
assert f" {project_name} " in result.output
1490+
1491+
for member_name in ("member1", "member2"):
1492+
assert not (workspace_root / "packages" / member_name / "dist").is_dir()
1493+
1494+
def test_virtual_root(self, hatch, temp_dir):
1495+
workspace_root = temp_dir / "workspace"
1496+
workspace_root.mkdir()
1497+
(workspace_root / "pyproject.toml").write_text(
1498+
"""\
1499+
[tool.hatch.envs.default]
1500+
workspace.members = ["packages/*"]
1501+
"""
1502+
)
1503+
1504+
packages_dir = workspace_root / "packages"
1505+
packages_dir.mkdir()
1506+
with packages_dir.as_cwd():
1507+
for project_name in ("member1", "member2"):
1508+
result = hatch("new", project_name)
1509+
assert result.exit_code == 0, result.output
1510+
1511+
with workspace_root.as_cwd():
1512+
result = hatch("build", "--all")
1513+
assert result.exit_code == 0, result.output
1514+
1515+
build_directory = workspace_root / "dist"
1516+
assert build_directory.is_dir()
1517+
1518+
artifacts = sorted(artifact.name for artifact in build_directory.iterdir())
1519+
assert artifacts == [
1520+
"member1-0.0.1-py3-none-any.whl",
1521+
"member1-0.0.1.tar.gz",
1522+
"member2-0.0.1-py3-none-any.whl",
1523+
"member2-0.0.1.tar.gz",
1524+
]
1525+
1526+
def test_explicit_targets(self, hatch, temp_dir):
1527+
workspace_root = self._create_workspace(hatch, temp_dir)
1528+
1529+
with workspace_root.as_cwd():
1530+
result = hatch("build", "--all", "-t", "wheel")
1531+
assert result.exit_code == 0, result.output
1532+
1533+
build_directory = workspace_root / "dist"
1534+
assert build_directory.is_dir()
1535+
1536+
artifacts = sorted(artifact.name for artifact in build_directory.iterdir())
1537+
assert artifacts == [
1538+
"member1-0.0.1-py3-none-any.whl",
1539+
"member2-0.0.1-py3-none-any.whl",
1540+
"workspace_root-0.0.1-py3-none-any.whl",
1541+
]
1542+
1543+
def test_explicit_directory(self, hatch, temp_dir):
1544+
workspace_root = self._create_workspace(hatch, temp_dir)
1545+
build_directory = temp_dir / "artifacts"
1546+
1547+
with workspace_root.as_cwd():
1548+
result = hatch("build", "--all", str(build_directory))
1549+
assert result.exit_code == 0, result.output
1550+
1551+
assert build_directory.is_dir()
1552+
1553+
artifacts = sorted(artifact.name for artifact in build_directory.iterdir())
1554+
assert artifacts == [
1555+
"member1-0.0.1-py3-none-any.whl",
1556+
"member1-0.0.1.tar.gz",
1557+
"member2-0.0.1-py3-none-any.whl",
1558+
"member2-0.0.1.tar.gz",
1559+
"workspace_root-0.0.1-py3-none-any.whl",
1560+
"workspace_root-0.0.1.tar.gz",
1561+
]
1562+
assert not (workspace_root / "dist").is_dir()

0 commit comments

Comments
 (0)