Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/changelog/1100.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Raise a friendly :class:`build.BuildException` when ``--env-dir`` points at an existing regular file instead of letting
:func:`os.makedirs` surface a raw :exc:`FileExistsError`; also replace the mutable default ``[]`` in
:meth:`DefaultIsolatedEnv.install`'s ``constraints`` parameter with an immutable tuple ``()``.
5 changes: 4 additions & 1 deletion src/build/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ def __enter__(self) -> Self:
path = tempfile.mkdtemp(prefix='build-env-')
else:
path = self._requested_path
if os.path.exists(path) and not os.path.isdir(path):
msg = f'Build environment location is not a directory: {path}'
raise BuildException(msg)
if os.path.isdir(path):
with os.scandir(path) as entries:
if next(entries, None) is not None:
Expand Down Expand Up @@ -203,7 +206,7 @@ def installed_versions(self, requirements: Collection[str]) -> dict[str, str]:
def install(
self,
requirements: Collection[str],
constraints: Collection[str] = [],
constraints: Collection[str] = (),
*,
_fresh: bool = False, # Used internally by CLI to support preset PYTHONPATH
) -> None:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,19 @@ def test_env_dir_rejects_non_empty_location(tmp_path: pathlib.Path) -> None:
assert tmp_path.joinpath('sentinel').exists()


def test_env_dir_rejects_file_at_location(tmp_path: pathlib.Path) -> None:
file_path = tmp_path / 'env-file'
file_path.touch()

with (
pytest.raises(build.BuildException, match='Build environment location is not a directory'),
build.env.DefaultIsolatedEnv(path=str(file_path)),
):
raise AssertionError

assert file_path.is_file()


@pytest.mark.usefixtures('mock_env_create')
def test_env_dir_accepts_existing_empty_location(tmp_path: pathlib.Path) -> None:
with build.env.DefaultIsolatedEnv(path=str(tmp_path)) as env:
Expand Down
Loading