diff --git a/src/poetry/core/masonry/builders/builder.py b/src/poetry/core/masonry/builders/builder.py index f2c22e2e3..152bb85a3 100644 --- a/src/poetry/core/masonry/builders/builder.py +++ b/src/poetry/core/masonry/builders/builder.py @@ -102,10 +102,13 @@ def find_excluded_files(self, fmt: str | None = None) -> set[str]: def add_all_files(path: Path, target: set[str]) -> None: all_files = ( - [f for f in path.glob("**/*") if f.is_file()] + [f for f in path.glob("**/*") if f.is_file() or f.is_symlink()] if path.is_dir() else [path] ) + if path.is_dir() and path.is_symlink(): + # the link itself, so that excluding or including it covers it + all_files.append(path) target.update(f.relative_to(self._path).as_posix() for f in all_files) explicitly_excluded: set[str] = set() @@ -146,6 +149,13 @@ def is_excluded(self, filepath: str | Path) -> bool: return False + def _is_excluded_unresolved(self, path: Path) -> bool: + # BuildIncludeFile resolves symlinks, so an excluded symlink + # that points outside the project must be skipped before that. + return path.is_relative_to(self._path) and self.is_excluded( + path.relative_to(self._path) + ) + def find_files_to_add(self, exclude_build: bool = True) -> set[BuildIncludeFile]: """ Finds all files to add to the tarball @@ -184,6 +194,9 @@ def find_files_to_add(self, exclude_build: bool = True) -> set[BuildIncludeFile] if file.is_dir(): if self.format in formats: for current_file in file.glob("**/*"): + if self._is_excluded_unresolved(current_file): + continue + include_file = BuildIncludeFile( path=current_file, project_root=self._path, @@ -200,6 +213,11 @@ def find_files_to_add(self, exclude_build: bool = True) -> set[BuildIncludeFile] to_add.add(include_file) continue + if isinstance(include, PackageInclude) and self._is_excluded_unresolved( + file + ): + continue + include_file = BuildIncludeFile( path=file, project_root=self._path, diff --git a/tests/masonry/builders/conftest.py b/tests/masonry/builders/conftest.py index 1eced65d4..8063dc025 100644 --- a/tests/masonry/builders/conftest.py +++ b/tests/masonry/builders/conftest.py @@ -25,3 +25,26 @@ def complete_with_pycache_and_pyc_files(tmp_path: Path) -> Path: (pycache_location / "some_other_file").touch() return tmp_root + + +@pytest.fixture +def excluded_symlinks(tmp_path: Path) -> Path: + root = fixtures_dir / "excluded_symlinks" + tmp_root = tmp_path / "excluded_symlinks" + shutil.copytree(root, tmp_root) + + outside = tmp_path / "outside" + outside.mkdir() + (outside / "file.txt").touch() + + package = tmp_root / "excluded_symlinks" + try: + # excluded via the "exclude" setting + (package / "tests" / "broken").symlink_to(tmp_path / "missing") + (package / "tests" / "outside").symlink_to(outside, target_is_directory=True) + # excluded via VCS (get_ignored_files has to be mocked) + (package / "vcs_ignored").symlink_to(outside, target_is_directory=True) + except OSError: + pytest.skip("Symlinks are not supported.") + + return tmp_root diff --git a/tests/masonry/builders/fixtures/excluded_symlinks/excluded_symlinks/__init__.py b/tests/masonry/builders/fixtures/excluded_symlinks/excluded_symlinks/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/masonry/builders/fixtures/excluded_symlinks/excluded_symlinks/tests/__init__.py b/tests/masonry/builders/fixtures/excluded_symlinks/excluded_symlinks/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/masonry/builders/fixtures/excluded_symlinks/pyproject.toml b/tests/masonry/builders/fixtures/excluded_symlinks/pyproject.toml new file mode 100644 index 000000000..1cb991635 --- /dev/null +++ b/tests/masonry/builders/fixtures/excluded_symlinks/pyproject.toml @@ -0,0 +1,11 @@ +[tool.poetry] +name = "excluded-symlinks" +version = "1.2.3" +description = "Some description." +authors = [ + "Poetry Maintainers " +] +exclude = ["excluded_symlinks/tests"] + +[tool.poetry.dependencies] +python = "^3.6" diff --git a/tests/masonry/builders/test_sdist.py b/tests/masonry/builders/test_sdist.py index cf2cbdffe..fb51b8d4b 100644 --- a/tests/masonry/builders/test_sdist.py +++ b/tests/masonry/builders/test_sdist.py @@ -682,6 +682,60 @@ def test_sdist_does_not_include_pycache_and_pyc_files( assert not name.endswith(".pyc") +def test_sdist_excluded_symlinks( + excluded_symlinks: Path, mocker: MockerFixture +) -> None: + mocker.patch( + "poetry.core.vcs.git.Git.get_ignored_files", + return_value=["excluded_symlinks/vcs_ignored"], + ) + builder = SdistBuilder(Factory().create_poetry(excluded_symlinks)) + builder.build() + + sdist = excluded_symlinks / "dist" / "excluded_symlinks-1.2.3.tar.gz" + + assert sdist.exists() + + with tarfile.open(str(sdist), "r") as tar: + prefix = "excluded_symlinks-1.2.3/excluded_symlinks/" + names = [n for n in tar.getnames() if n.startswith(prefix)] + assert names == [f"{prefix}__init__.py"] + + +def test_sdist_explicitly_included_vcs_ignored_dir_symlink( + tmp_path: Path, mocker: MockerFixture +) -> None: + project = tmp_path / "project" + (project / "foo").mkdir(parents=True) + (project / "foo" / "__init__.py").touch() + (project / "shared").mkdir() + (project / "shared" / "x.txt").touch() + (project / "pyproject.toml").write_text( + """\ +[project] +name = "foo" +version = "1.2.3" + +[tool.poetry] +include = [{ path = "foo/assets", format = ["sdist", "wheel"] }] +""", + encoding="utf-8", + ) + try: + (project / "foo" / "assets").symlink_to( + Path("..", "shared"), target_is_directory=True + ) + except OSError: + pytest.skip("Symlinks are not supported.") + mocker.patch( + "poetry.core.vcs.git.Git.get_ignored_files", return_value=["foo/assets"] + ) + + builder = SdistBuilder(Factory().create_poetry(project)) + + assert "x.txt" in {f.path.name for f in builder.find_files_to_add()} + + def test_includes() -> None: poetry = Factory().create_poetry(project("with-include")) diff --git a/tests/masonry/builders/test_wheel.py b/tests/masonry/builders/test_wheel.py index b45cda53c..0316d764d 100644 --- a/tests/masonry/builders/test_wheel.py +++ b/tests/masonry/builders/test_wheel.py @@ -179,6 +179,24 @@ def test_wheel_excluded_nested_data() -> None: assert "my_package/public/item2/itemdata2.txt" not in z.namelist() +def test_wheel_excluded_symlinks( + excluded_symlinks: Path, mocker: MockerFixture +) -> None: + mocker.patch( + "poetry.core.vcs.git.Git.get_ignored_files", + return_value=["excluded_symlinks/vcs_ignored"], + ) + WheelBuilder.make(Factory().create_poetry(excluded_symlinks)) + + whl = excluded_symlinks / "dist" / "excluded_symlinks-1.2.3-py3-none-any.whl" + + assert whl.exists() + + with zipfile.ZipFile(str(whl)) as z: + names = [n for n in z.namelist() if n.startswith("excluded_symlinks/")] + assert names == ["excluded_symlinks/__init__.py"] + + def test_include_excluded_code() -> None: module_path = fixtures_dir / "include_excluded_code" poetry = Factory().create_poetry(module_path)