Skip to content

Commit d4c3321

Browse files
feat: add CIBW_ALLOW_EMPTY environment variable (#1937)
Co-authored-by: Matthieu Darbois <mayeut@users.noreply.github.com>
1 parent 5af5df4 commit d4c3321

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

cibuildwheel/__main__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def build_in_directory(args: CommandLineArguments) -> None:
334334

335335
if not identifiers:
336336
message = f"No build identifiers selected: {options.globals.build_selector}"
337-
if args.allow_empty:
337+
if options.globals.allow_empty:
338338
print(f"cibuildwheel: {message}", file=sys.stderr)
339339
else:
340340
raise errors.NothingToDoError(message)

cibuildwheel/options.py

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class GlobalOptions:
7575
build_selector: BuildSelector
7676
test_selector: TestSelector
7777
architectures: set[Architecture]
78+
allow_empty: bool
7879

7980

8081
@dataclasses.dataclass(frozen=True)
@@ -520,6 +521,8 @@ def globals(self) -> GlobalOptions:
520521
self.reader.get("free-threaded-support", env_plat=False, ignore_empty=True)
521522
)
522523

524+
allow_empty = args.allow_empty or strtobool(self.env.get("CIBW_ALLOW_EMPTY", "0"))
525+
523526
prerelease_pythons = args.prerelease_pythons or strtobool(
524527
self.env.get("CIBW_PRERELEASE_PYTHONS", "0")
525528
)
@@ -557,6 +560,7 @@ def globals(self) -> GlobalOptions:
557560
build_selector=build_selector,
558561
test_selector=test_selector,
559562
architectures=architectures,
563+
allow_empty=allow_empty,
560564
)
561565

562566
def build_options(self, identifier: str | None) -> BuildOptions:

docs/options.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,28 @@ This option can also be set using the [command-line option](#command-line) `--pr
621621
CIBW_PRERELEASE_PYTHONS: True
622622
```
623623

624+
### `CIBW_ALLOW_EMPTY` {: #allow-empty}
625+
> Suppress the error code if no wheels match the specified build identifiers
626+
627+
When none of the specified build identifiers match any available versions,
628+
cibuildwheel will typically return error code 3, indicating that there are
629+
no wheels to build. Enabling this option will suppress this error, allowing
630+
the build process to complete without signaling an error.
631+
632+
Default: Off (0). Error code 3 is returned when no builds are selected.
633+
634+
This option can also be set using the [command-line option](#command-line)
635+
`--allow-empty`. This option is not available in the `pyproject.toml` config.
636+
637+
#### Examples
638+
639+
!!! tab examples "Environment variables"
640+
641+
```yaml
642+
# Prevent an error code if the build does not match any wheels
643+
CIBW_ALLOW_EMPTY: True
644+
```
645+
624646
## Build customization
625647

626648
### `CIBW_BUILD_FRONTEND` {: #build-frontend}
@@ -1667,7 +1689,7 @@ cibuildwheel exits 0 on success, or >0 if an error occurs.
16671689
Specific error codes are defined:
16681690

16691691
- 2 means a configuration error
1670-
- 3 means no builds are selected (and --allow-empty wasn't passed)
1692+
- 3 means no builds are selected (and [`--allow-empty`](#allow-empty) wasn't passed)
16711693
- 4 means you specified an option that has been deprecated.
16721694

16731695

test/test_0_basic.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,24 @@ def test_build_identifiers(tmp_path):
6969
), f"{expected_wheels} vs {build_identifiers}"
7070

7171

72-
def test_allow_empty(tmp_path):
72+
@pytest.mark.parametrize(
73+
("add_args", "env_allow_empty"),
74+
[
75+
(["--allow-empty"], {}),
76+
(["--allow-empty"], {"CIBW_ALLOW_EMPTY": "0"}),
77+
(None, {"CIBW_ALLOW_EMPTY": "1"}),
78+
],
79+
)
80+
def test_allow_empty(tmp_path, add_args, env_allow_empty):
7381
project_dir = tmp_path / "project"
7482
basic_project.generate(project_dir)
7583

7684
# Sanity check - --allow-empty should cause a no-op build to complete
7785
# without error
7886
actual_wheels = utils.cibuildwheel_run(
7987
project_dir,
80-
add_env={"CIBW_BUILD": "BUILD_NOTHING_AT_ALL"},
81-
add_args=["--allow-empty"],
88+
add_env={"CIBW_BUILD": "BUILD_NOTHING_AT_ALL", **env_allow_empty},
89+
add_args=add_args,
8290
)
8391

8492
# check that nothing was built

0 commit comments

Comments
 (0)