Skip to content

Commit 7c26ebe

Browse files
authored
Fix hatch.cli.application.Application.ensure_plugin_dependencies PyApp Compatibility (#2390)
* Fix `hatch.cli.application.Application.ensure_plugin_dependencies` * * * cleanup formatting * cleanup formatting
1 parent c4abc45 commit 7c26ebe

5 files changed

Lines changed: 91 additions & 24 deletions

File tree

‎docs/history/hatch.md‎

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

99
## Unreleased
1010

11+
***Fixed:***
12+
13+
- Fix syncing environment plugin requirements on standalone binaries built to forward to `pip`
14+
1115
## [1.18.1](https://github.com/pypa/hatch/releases/tag/hatch-v1.18.1) - 2026-09-16 ## {: #hatch-v1.18.1 }
1216

1317
***Added:***

‎src/hatch/cli/application.py‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,31 +163,28 @@ def ensure_plugin_dependencies(self, dependencies: list[Dependency], *, wait_mes
163163
if not dependencies:
164164
return
165165

166-
from uv import find_uv_bin
167-
168166
from hatch.dep.sync import InstalledDistributions
169167
from hatch.env.utils import add_verbosity_flag
170168

171169
if app_path := os.environ.get("PYAPP"):
172170
from hatch.utils.env import PythonInfo
173171

174-
management_command = os.environ["PYAPP_COMMAND_NAME"]
172+
management_command = os.environ.get("PYAPP_COMMAND_NAME", "self")
175173
executable = self.platform.check_command_output([app_path, management_command, "python-path"]).strip()
176174
python_info = PythonInfo(self.platform, executable=executable)
177175
distributions = InstalledDistributions(sys_path=python_info.sys_path)
178176
if distributions.dependencies_in_sync(dependencies):
179177
return
180-
181-
pip_command = [app_path, management_command, "pip"]
178+
pip_command = [app_path, management_command, "pip", "install", "--disable-pip-version-check"]
182179
else:
183180
distributions = InstalledDistributions()
184181
if distributions.dependencies_in_sync(dependencies):
185182
return
186183

187-
uv_bin = find_uv_bin()
188-
pip_command = [uv_bin, "pip"]
184+
from uv import find_uv_bin
189185

190-
pip_command.extend(["install", "--disable-pip-version-check", "--python", sys.executable])
186+
uv_bin = find_uv_bin()
187+
pip_command = [uv_bin, "pip", "install", "--python", sys.executable]
191188

192189
# Default to -1 verbosity
193190
add_verbosity_flag(pip_command, self.verbosity, adjustment=-1)

‎tests/cli/env/test_create.py‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,6 +1758,61 @@ def test_plugin_dependencies_unmet(hatch, config_file, helpers, temp_dir, mock_p
17581758
assert env_path.name == project_path.name
17591759

17601760

1761+
def test_plugin_dependencies_unmet_pyapp(
1762+
hatch, config_file, helpers, temp_dir, mock_plugin_installation, mock_python_path
1763+
):
1764+
config_file.model.template.plugins["default"]["tests"] = False
1765+
config_file.save()
1766+
1767+
project_name = "My.PyApp"
1768+
1769+
with temp_dir.as_cwd():
1770+
result = hatch("new", project_name)
1771+
1772+
assert result.exit_code == 0, result.output
1773+
1774+
project_path = temp_dir / "my-pyapp"
1775+
data_path = temp_dir / "data"
1776+
data_path.mkdir()
1777+
1778+
dependency = os.urandom(16).hex()
1779+
(project_path / DEFAULT_CONFIG_FILE).write_text(
1780+
helpers.dedent(
1781+
f"""
1782+
[env]
1783+
requires = ["{dependency}"]
1784+
"""
1785+
)
1786+
)
1787+
1788+
project = Project(project_path)
1789+
helpers.update_project_environment(project, "default", {"skip-install": True, **project.config.envs["default"]})
1790+
1791+
management_command = os.environ["PYAPP_COMMAND_NAME"]
1792+
with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path), "PYAPP": sys.executable}):
1793+
result = hatch("env", "create")
1794+
1795+
assert result.exit_code == 0, result.output
1796+
assert result.output == helpers.dedent(
1797+
"""
1798+
Syncing environment plugin requirements
1799+
Creating environment: default
1800+
Checking dependencies
1801+
"""
1802+
)
1803+
# No `--python` may appear here, and the flags must reach the binary in
1804+
# an order both `pip` and `uv` accept
1805+
helpers.assert_plugin_installation(
1806+
mock_plugin_installation,
1807+
[dependency],
1808+
app_command=[sys.executable, management_command],
1809+
)
1810+
# Dependencies must be resolved against the interpreter the binary
1811+
# manages
1812+
assert mock_python_path.call_count == 1
1813+
assert mock_python_path.call_args.args[0] == [sys.executable, management_command, "python-path"]
1814+
1815+
17611816
@pytest.mark.usefixtures("mock_plugin_installation")
17621817
def test_plugin_dependencies_met(hatch, config_file, helpers, temp_dir):
17631818
config_file.model.template.plugins["default"]["tests"] = False

‎tests/conftest.py‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,12 @@ def mock_process(command, **kwargs):
480480

481481

482482
@pytest.fixture
483-
def mock_plugin_installation(mocker):
483+
def mock_python_path(mocker):
484+
return mocker.MagicMock(returncode=0, stdout=sys.executable.encode())
485+
486+
487+
@pytest.fixture
488+
def mock_plugin_installation(mocker, mock_python_path):
484489
from uv import find_uv_bin
485490

486491
subprocess_run = subprocess.run
@@ -500,10 +505,20 @@ def _mock(command, **kwargs):
500505
mocked_subprocess_run(command, **kwargs)
501506
return mocked_subprocess_run
502507

503-
if command[:3] == [sys.executable, "self", "python-path"]:
504-
return mocker.MagicMock(returncode=0, stdout=sys.executable.encode())
508+
# Mirror how the self-management command is derived, rather
509+
# than assuming the exposed command is named `self`
510+
if app_path := os.environ.get("PYAPP"):
511+
app_command = [app_path, os.environ.get("PYAPP_COMMAND_NAME", "self")]
512+
513+
if command[:3] == [*app_command, "python-path"]:
514+
mock_python_path(command, **kwargs)
515+
return mock_python_path
516+
517+
if command[:4] == [*app_command, "pip", "install"]:
518+
mocked_subprocess_run(command, **kwargs)
519+
return mocked_subprocess_run
505520

506-
return subprocess_run(command, **kwargs) # no cov
521+
return subprocess_run(command, **kwargs)
507522

508523
mocker.patch("subprocess.run", side_effect=_mock)
509524

‎tests/helpers/helpers.py‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,14 @@ def get_current_timestamp():
4747
return datetime.now(timezone.utc).timestamp()
4848

4949

50-
def assert_plugin_installation(subprocess_run, dependencies: list[str], *, verbosity=0, count=1):
51-
from uv import find_uv_bin
52-
53-
uv_bin = find_uv_bin()
54-
command = [
55-
uv_bin,
56-
"pip",
57-
"install",
58-
"--disable-pip-version-check",
59-
"--python",
60-
sys.executable,
61-
]
50+
def assert_plugin_installation(subprocess_run, dependencies: list[str], *, verbosity=0, count=1, app_command=None):
51+
if app_command is None:
52+
from uv import find_uv_bin
53+
54+
command = [find_uv_bin(), "pip", "install", "--python", sys.executable]
55+
else:
56+
command = [*app_command, "pip", "install", "--disable-pip-version-check"]
57+
6258
add_verbosity_flag(command, verbosity, adjustment=-1)
6359
command.extend(dependencies)
6460

0 commit comments

Comments
 (0)