← Back to Pull requests

Skip in-tree directory links so sdist keeps the real package #2388

Open
Kayforkind opened this pull request with 90 lines diff across 2 files
View on GitHub
Kayforkind Kayforkind commented on Aug 13, 2026 06:59 UTC
Author

Summary

  • safe_walk follows directory links and records (st_dev, st_ino) to stop cycles. A junction or symlink whose name sorts before its target (bar → foo) was marking foo's inode as already seen, so the real package was never walked.

  • In-tree directory symlinks and Windows junctions are now skipped so the real path is packed. Out-of-tree links are still followed. Cycle detection is unchanged.

  • exclude of the alias no longer drops the real package's files.
  • Fixes #1197.
    Fixes #2008.

    Test plan

  • test_in_tree_dir_link_does_not_hide_real_directory (issue #1197 repro)

  • test_excluding_dir_link_keeps_real_directory (#2008 exclude footgun)

  • hatch test tests/backend/builders → 469 passed, 10 skipped

  • Existing test_infinite_loop_prevention still valid (unix)
  • AI disclosure

    Assisted by Cursor. I directed the change, reviewed inode first-seen-wins vs cycle detection, and verified the tests above. No Co-authored-by LLM trailer.

    Made with Cursor

    Diff: 90 lines (+88 / -2) across 2 files Reviewer: cjames23 Review duration: 97116m
    All Vouch review confidence checks passed
    Residual risk 0.04 = Change Risk (0.43) × (1 − Review Confidence (0.90)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.43 · 90 lines diff across 2 files
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.96 · 3 substantive comment(s)
    0.96
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    89%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.270
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    5 items
    cjames23 cjames23 commented on code on Aug 18, 2026 21:55 UTC
    Code Review
    backend/src/hatchling/builders/utils.py:1
    @@ -19,11 +20,51 @@ def replace_file(src: str, dst: str) -> None:
             os.remove(src)
     
     
    +def _is_dir_link(path: str) -> bool:
    +    try:
    +        st = os.lstat(path)
    +    except OSError:
    +        return False
    +    if stat.S_ISLNK(st.st_mode):
    +        return True
    +    # Windows junctions are reparse points; ``os.path.islink`` is false for them
    +    # on some Python versions.
    +    file_attributes = getattr(st, "st_file_attributes", 0)
    +    return bool(file_attributes & getattr(stat, "FILE_ATTRIBUTE_REPARSE_POINT", 0x400))
    +
    +
    +def _is_inside_directory(root: str, path: str) -> bool:
    +    try:
    +        common = os.path.commonpath([root, path])
    +    except ValueError:
    +        return False
    +    return os.path.normcase(common) == os.path.normcase(root)
    +
    +
     def safe_walk(path: str) -> Iterable[tuple[str, list[str], list[str]]]:
         seen = set()
    +    walk_root = os.path.realpath(path)
    +    walk_root_abs = os.path.abspath(path)
         for root, dirs, files in os.walk(path, followlinks=True):
    -        stat = os.stat(root)
    -        identifier = stat.st_dev, stat.st_ino
    +        # An in-tree directory symlink/junction that sorts before its target
    +        # would otherwise mark the target inode as seen and skip the real
    +        # directory (issues #1197, #2008). Skip the alias so the real path is

    Do not include issue links or reference numbers in comments. And comments should explain things that are not clearly explainable by reading the code itself.

    cjames23 cjames23 commented on code on Aug 18, 2026 21:56 UTC
    Code Review
    tests/backend/builders/plugin/test_interface.py:1
    @@ -166,6 +166,57 @@ def test_infinite_loop_prevention(self, temp_dir):
                     str(project_dir / "foo" / "bar.txt"),
                 ]
     
    +    def test_in_tree_dir_link_does_not_hide_real_directory(self, temp_dir):
    +        """A directory link that sorts before its target must not drop the real package.
    +
    +        https://github.com/pypa/hatch/issues/1197
    +        https://github.com/pypa/hatch/issues/2008

    Remove issue links from comments

    cjames23 cjames23 requested changes on Aug 18, 2026 21:58 UTC
    Changes Requested Reviewer

    Requested changes.

    Kayforkind Kayforkind commented on Aug 19, 2026 15:27 UTC
    Author

    @cjames23 thanks for the master sync. Comment cleanup in ackend/src/hatchling/builders/utils.py and ests/backend/builders/plugin/test_interface.py is in ef34209 — issue links dropped, comment kept to the inode first-seen-wins trap that isn't obvious from the code. CI is green on the merged HEAD. Ready when you are for another look.

    Reviewers
    Assignees
    Kayforkind Kayforkind
    Labels
    risk: low confidence: 89.9% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 43.4% (MEDIUM) · Higher = larger blast radius
    43.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 89.9% · Lower = rushed or shallow review
    89.9%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 4.4% · Safe to merge
    4.4%
    Repository