Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message for packages in src folder #800

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions src/poetry/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from packaging.utils import canonicalize_name

from poetry.core.utils.helpers import combine_unicode
from poetry.core.utils.helpers import module_name
from poetry.core.utils.helpers import readme_content_type


Expand Down Expand Up @@ -50,6 +51,10 @@ def create_poetry(

# Checking validity
check_result = self.validate(pyproject.data)
src_check_results = self.validate_project_in_src(pyproject.data, cwd)
check_result["errors"].extend(src_check_results["errors"])
check_result["warnings"].extend(src_check_results["warnings"])

if check_result["errors"]:
message = ""
for error in check_result["errors"]:
Expand Down Expand Up @@ -582,6 +587,47 @@ def validate(

return result

@classmethod
def validate_project_in_src(
cls, toml_data: dict[str, Any], base_path: Path | None
) -> dict[str, list[str]]:
"""
Checks the validity of projects located under src/ subdirectory.
"""
result: dict[str, list[str]] = {"errors": [], "warnings": []}

project_name = toml_data.get("project", {}).get("name") or toml_data.get(
"tool", {}
).get("poetry", {}).get("name")

if project_name is None or base_path is None:
return result

project_name = module_name(project_name)

project_dir = base_path / project_name
src_project_dir = base_path / "src" / project_name

project_dir_empty = (
project_dir.exists()
and project_dir.is_dir()
and not any(project_dir.iterdir())
)
src_dir_not_empty = (
src_project_dir.exists()
and src_project_dir.is_dir()
and any(src_project_dir.iterdir())
)

if project_dir_empty and src_dir_not_empty:
result["warnings"].append(
f"Found empty directory '{project_name}' in project root while the actual package is in "
f"'src/{project_name}'. This may cause issues with package installation. "
"Consider removing the empty directory."
)

return result

@classmethod
def _validate_legacy_vs_project(
cls, toml_data: dict[str, Any], result: dict[str, list[str]]
Expand Down
Empty file added tests/fixtures/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions tests/fixtures/project_with_src_folder/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[tool.poetry]
name = "project-with-src-folder"
version = "1.2.3"
description = "This is a description"
authors = ["Your Name <[email protected]>"]
license = "MIT"

[tool.poetry.dependencies]
python = "*"
19 changes: 19 additions & 0 deletions tests/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,25 @@ def test_create_poetry_empty_readme(tmp_path: Path) -> None:
assert not poetry.package.readmes


def test_create_poetry_src_folder(tmp_path: Path) -> None:
cwd = fixtures_dir / "project_with_src_folder"

(cwd / "project_with_src_folder").mkdir(exist_ok=True)

pyproject = cwd / "pyproject.toml"
with pyproject.open("rb") as f:
content = tomllib.load(f)

assert Factory.validate_project_in_src(content, cwd) == {
"errors": [],
"warnings": [
"Found empty directory 'project_with_src_folder' in project root while the actual package is in "
"'src/project_with_src_folder'. This may cause issues with package installation. "
"Consider removing the empty directory."
],
}


def test_validate() -> None:
complete = fixtures_dir / "complete.toml"
with complete.open("rb") as f:
Expand Down
Loading