From e216642d101ad9d4b26c7c412436b80806a181d3 Mon Sep 17 00:00:00 2001 From: Zihan Dai Date: Wed, 11 Nov 2026 03:02:25 +1100 Subject: [PATCH 1/2] Skip excluded symlinks that point outside the project An excluded or VCS-ignored symlink inside a package was resolved before the exclusion check. If it pointed outside the project root, for example a broken link or a virtualenv's python, relative_to() raised and the build failed. Check the exclusion on the path inside the project before resolving it, and keep symlinks when collecting the files of an excluded directory, since is_file() is false for broken links and links to directories. --- src/poetry/core/masonry/builders/builder.py | 17 +++++++++++++- tests/masonry/builders/conftest.py | 23 +++++++++++++++++++ .../excluded_symlinks/__init__.py | 0 .../excluded_symlinks/tests/__init__.py | 0 .../fixtures/excluded_symlinks/pyproject.toml | 11 +++++++++ tests/masonry/builders/test_sdist.py | 20 ++++++++++++++++ tests/masonry/builders/test_wheel.py | 18 +++++++++++++++ 7 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 tests/masonry/builders/fixtures/excluded_symlinks/excluded_symlinks/__init__.py create mode 100644 tests/masonry/builders/fixtures/excluded_symlinks/excluded_symlinks/tests/__init__.py create mode 100644 tests/masonry/builders/fixtures/excluded_symlinks/pyproject.toml diff --git a/src/poetry/core/masonry/builders/builder.py b/src/poetry/core/masonry/builders/builder.py index f2c22e2e3..86070ad90 100644 --- a/src/poetry/core/masonry/builders/builder.py +++ b/src/poetry/core/masonry/builders/builder.py @@ -102,7 +102,7 @@ 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] ) @@ -146,6 +146,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 +191,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 +210,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..3ba1f0751 100644 --- a/tests/masonry/builders/test_sdist.py +++ b/tests/masonry/builders/test_sdist.py @@ -682,6 +682,26 @@ 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_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) From 1b5b275d8205d7c3c4594b36c274060fc10b49d7 Mon Sep 17 00:00:00 2001 From: Zihan Dai Date: Thu, 12 Nov 2026 22:21:46 +1100 Subject: [PATCH 2/2] Count a matched directory link itself as excluded or included A VCS-ignored link to a directory that is explicitly included was dropped from the build: the explicitly included set held only the files below the link, so the link path stayed in the excluded set, and the new unresolved check skipped every file under it. --- src/poetry/core/masonry/builders/builder.py | 3 ++ tests/masonry/builders/test_sdist.py | 34 +++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/poetry/core/masonry/builders/builder.py b/src/poetry/core/masonry/builders/builder.py index 86070ad90..152bb85a3 100644 --- a/src/poetry/core/masonry/builders/builder.py +++ b/src/poetry/core/masonry/builders/builder.py @@ -106,6 +106,9 @@ def add_all_files(path: Path, target: set[str]) -> None: 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() diff --git a/tests/masonry/builders/test_sdist.py b/tests/masonry/builders/test_sdist.py index 3ba1f0751..fb51b8d4b 100644 --- a/tests/masonry/builders/test_sdist.py +++ b/tests/masonry/builders/test_sdist.py @@ -702,6 +702,40 @@ def test_sdist_excluded_symlinks( 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"))