Skip to content

Commit 029685c

Browse files
authored
fix: suggest poetry self lock for self commands (#10715)
1 parent 9eac0a9 commit 029685c

6 files changed

Lines changed: 63 additions & 4 deletions

File tree

src/poetry/console/commands/self/self_command.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from poetry.console.commands.installer_command import InstallerCommand
1515
from poetry.factory import Factory
1616
from poetry.pyproject.toml import PyProjectTOML
17+
from poetry.utils.constants import POETRY_SYSTEM_PROJECT_NAME
1718
from poetry.utils.env import EnvManager
1819
from poetry.utils.env import SystemEnv
1920
from poetry.utils.helpers import directory
@@ -80,7 +81,7 @@ def generate_system_pyproject(self) -> None:
8081
"dict[str, Any]", content["dependency-groups"]
8182
)
8283

83-
package = ProjectPackage(name="poetry-instance", version=__version__)
84+
package = ProjectPackage(name=POETRY_SYSTEM_PROJECT_NAME, version=__version__)
8485
package.add_dependency(Dependency(name="poetry", constraint=f"{__version__}"))
8586

8687
package.python_versions = ".".join(str(v) for v in self.env.version_info[:3])

src/poetry/console/commands/show.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from poetry.console.commands.env_command import EnvCommand
1515
from poetry.console.commands.group_command import GroupCommand
16+
from poetry.utils.constants import POETRY_SYSTEM_PROJECT_NAME
1617

1718

1819
if TYPE_CHECKING:
@@ -147,8 +148,8 @@ def handle(self) -> int:
147148

148149
if not self.poetry.locker.is_locked():
149150
self.line_error(
150-
"<error>Error: poetry.lock not found. Run `poetry lock` to create"
151-
" it.</error>"
151+
f"<error>Error: poetry.lock not found. Run `{self._lock_create_command()}`"
152+
" to create it.</error>"
152153
)
153154
return 1
154155

@@ -165,6 +166,12 @@ def handle(self) -> int:
165166

166167
return self._display_packages_information(locked_repo, root)
167168

169+
def _lock_create_command(self) -> str:
170+
if self.poetry.package.name == POETRY_SYSTEM_PROJECT_NAME:
171+
return "poetry self lock"
172+
173+
return "poetry lock"
174+
168175
def _display_single_package_information(
169176
self, package: str, locked_repository: Repository
170177
) -> int:

src/poetry/installation/installer.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from poetry.repositories import RepositoryPool
1313
from poetry.repositories.installed_repository import InstalledRepository
1414
from poetry.repositories.lockfile_repository import LockfileRepository
15+
from poetry.utils.constants import POETRY_SYSTEM_PROJECT_NAME
1516

1617

1718
if TYPE_CHECKING:
@@ -266,7 +267,7 @@ def _do_install(self) -> int:
266267
if not self._locker.is_fresh():
267268
raise ValueError(
268269
"pyproject.toml changed significantly since poetry.lock was last"
269-
" generated. Run `poetry lock` to fix the lock file."
270+
f" generated. Run `{self._lock_fix_command()}` to fix the lock file."
270271
)
271272
if not (reresolve or self._locker.is_locked_groups_and_markers()):
272273
if self._io.is_verbose():
@@ -380,6 +381,14 @@ def _do_install(self) -> int:
380381

381382
return status
382383

384+
def _lock_fix_command(self) -> str:
385+
# `poetry self` commands operate on Poetry's own system project. When the lock
386+
# file is outdated, users should run `poetry self lock` rather than `poetry lock`.
387+
if self._package.name == POETRY_SYSTEM_PROJECT_NAME:
388+
return "poetry self lock"
389+
390+
return "poetry lock"
391+
383392
def _write_lock_file(
384393
self,
385394
packages: dict[Package, TransitivePackageInfo],

src/poetry/utils/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import os
44

55

6+
# Name of Poetry's own system project used by `poetry self` commands.
7+
POETRY_SYSTEM_PROJECT_NAME = "poetry-instance"
8+
69
# Timeout for HTTP requests using the requests library.
710
REQUESTS_TIMEOUT = int(os.getenv("POETRY_REQUESTS_TIMEOUT", 15))
811

tests/console/commands/self/test_show.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,28 @@ def test_show_format(tester: CommandTester, options: str) -> None:
7070
)
7171
assert tester.execute(options) == 0
7272
assert tester.io.fetch_output().strip() == expected
73+
74+
75+
def test_self_show_errors_without_lock_file(tester: CommandTester) -> None:
76+
system_pyproject_file = SelfCommand.get_default_system_pyproject_file()
77+
system_pyproject_file.write_text(
78+
tomlkit.dumps(
79+
{
80+
"tool": {
81+
"poetry": {
82+
"name": "poetry-instance",
83+
"version": __version__,
84+
"dependencies": {"python": "^3.9", "poetry": __version__},
85+
}
86+
}
87+
}
88+
),
89+
encoding="utf-8",
90+
)
91+
system_pyproject_file.parent.joinpath("poetry.lock").unlink(missing_ok=True)
92+
93+
assert tester.execute() == 1
94+
assert (
95+
tester.io.fetch_error()
96+
== "Error: poetry.lock not found. Run `poetry self lock` to create it.\n"
97+
)

tests/installation/test_installer.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from poetry.repositories import RepositoryPool
3030
from poetry.repositories.installed_repository import InstalledRepository
3131
from poetry.toml.file import TOMLFile
32+
from poetry.utils.constants import POETRY_SYSTEM_PROJECT_NAME
3233
from poetry.utils.env import MockEnv
3334
from poetry.utils.env import NullEnv
3435
from tests.helpers import MOCK_DEFAULT_GIT_REVISION
@@ -228,6 +229,19 @@ def test_not_fresh_lock(installer: Installer, locker: Locker) -> None:
228229
installer.run()
229230

230231

232+
def test_not_fresh_lock_self_project(installer: Installer, locker: Locker) -> None:
233+
installer.set_package(ProjectPackage(POETRY_SYSTEM_PROJECT_NAME, "1.0"))
234+
locker.locked().fresh(False)
235+
with pytest.raises(
236+
ValueError,
237+
match=re.escape(
238+
"pyproject.toml changed significantly since poetry.lock was last generated. "
239+
"Run `poetry self lock` to fix the lock file."
240+
),
241+
):
242+
installer.run()
243+
244+
231245
def test_run_with_dependencies(
232246
installer: Installer, locker: Locker, repo: Repository, package: ProjectPackage
233247
) -> None:

0 commit comments

Comments
 (0)