• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pantsbuild / pants / 22285099215

22 Feb 2026 08:52PM UTC coverage: 75.854% (-17.1%) from 92.936%
22285099215

Pull #23121

github

web-flow
Merge c7299df9c into ba8359840
Pull Request #23121: fix issue with optional fields in dependency validator

28 of 29 new or added lines in 2 files covered. (96.55%)

11174 existing lines in 400 files now uncovered.

53694 of 70786 relevant lines covered (75.85%)

1.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.67
/src/python/pants/core/util_rules/stripped_source_files.py
1
# Copyright 2019 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3

4
from collections import defaultdict
4✔
5
from dataclasses import dataclass
4✔
6

7
from pants.core.util_rules.source_files import SourceFiles
4✔
8
from pants.core.util_rules.source_files import rules as source_files_rules
4✔
9
from pants.engine.collection import Collection
4✔
10
from pants.engine.engine_aware import EngineAwareParameter
4✔
11
from pants.engine.fs import DigestSubset, MergeDigests, PathGlobs, RemovePrefix, Snapshot
4✔
12
from pants.engine.intrinsics import digest_subset_to_digest, digest_to_snapshot, remove_prefix
4✔
13
from pants.engine.rules import collect_rules, concurrently, implicitly, rule
4✔
14
from pants.engine.target import SourcesPaths
4✔
15
from pants.source.source_root import (
4✔
16
    SourceRootRequest,
17
    SourceRootsRequest,
18
    get_source_root,
19
    get_source_roots,
20
)
21
from pants.source.source_root import rules as source_root_rules
4✔
22
from pants.util.dirutil import fast_relpath
4✔
23

24

25
@dataclass(frozen=True)
4✔
26
class StrippedSourceFiles:
4✔
27
    """Wrapper for a snapshot of files whose source roots have been stripped."""
28

29
    snapshot: Snapshot
30

31

32
@rule
4✔
33
async def strip_source_roots(source_files: SourceFiles) -> StrippedSourceFiles:
4✔
34
    """Removes source roots from a snapshot.
35

36
    E.g. `src/python/pants/util/strutil.py` -> `pants/util/strutil.py`.
37
    """
38
    if not source_files.snapshot.files:
4✔
39
        return StrippedSourceFiles(source_files.snapshot)
3✔
40

41
    if source_files.unrooted_files:
4✔
UNCOV
42
        rooted_files = set(source_files.snapshot.files) - set(source_files.unrooted_files)
×
UNCOV
43
        rooted_files_snapshot = await digest_to_snapshot(
×
44
            **implicitly(DigestSubset(source_files.snapshot.digest, PathGlobs(rooted_files)))
45
        )
46
    else:
47
        rooted_files_snapshot = source_files.snapshot
4✔
48

49
    source_roots_result = await get_source_roots(
4✔
50
        SourceRootsRequest.for_files(rooted_files_snapshot.files)
51
    )
52

53
    source_roots_to_files = defaultdict(set)
4✔
54
    for f, root in source_roots_result.path_to_root.items():
4✔
55
        source_roots_to_files[root.path].add(str(f))
4✔
56

57
    if len(source_roots_to_files) == 1:
4✔
58
        source_root = next(iter(source_roots_to_files.keys()))
4✔
59
        if source_root == ".":
4✔
60
            resulting_snapshot = rooted_files_snapshot
4✔
61
        else:
62
            resulting_snapshot = await digest_to_snapshot(
4✔
63
                **implicitly(RemovePrefix(rooted_files_snapshot.digest, source_root))
64
            )
65
    else:
66
        digest_subsets = await concurrently(
1✔
67
            digest_subset_to_digest(DigestSubset(rooted_files_snapshot.digest, PathGlobs(files)))
68
            for files in source_roots_to_files.values()
69
        )
70
        resulting_digests = await concurrently(
1✔
71
            remove_prefix(RemovePrefix(digest, source_root))
72
            for digest, source_root in zip(digest_subsets, source_roots_to_files.keys())
73
        )
74
        resulting_snapshot = await digest_to_snapshot(**implicitly(MergeDigests(resulting_digests)))
1✔
75

76
    # Add the unrooted files back in.
77
    if source_files.unrooted_files:
4✔
UNCOV
78
        unrooted_files_digest = await digest_subset_to_digest(
×
79
            DigestSubset(source_files.snapshot.digest, PathGlobs(source_files.unrooted_files))
80
        )
UNCOV
81
        resulting_snapshot = await digest_to_snapshot(
×
82
            **implicitly(MergeDigests((resulting_snapshot.digest, unrooted_files_digest)))
83
        )
84

85
    return StrippedSourceFiles(resulting_snapshot)
4✔
86

87

88
@dataclass(frozen=True)
4✔
89
class StrippedFileName:
4✔
90
    value: str
91

92

93
@dataclass(frozen=True)
4✔
94
class StrippedFileNameRequest(EngineAwareParameter):
4✔
95
    file_path: str
96

97
    def debug_hint(self) -> str:
4✔
98
        return self.file_path
×
99

100

101
@rule
4✔
102
async def strip_file_name(request: StrippedFileNameRequest) -> StrippedFileName:
4✔
103
    source_root = await get_source_root(SourceRootRequest.for_file(request.file_path))
4✔
104
    return StrippedFileName(
4✔
105
        request.file_path
106
        if source_root.path == "."
107
        else fast_relpath(request.file_path, source_root.path)
108
    )
109

110

111
class StrippedSourceFileNames(Collection[str]):
4✔
112
    """The file names from a target's `sources` field, with source roots stripped."""
113

114

115
@rule
4✔
116
async def strip_sources_paths(sources_paths: SourcesPaths) -> StrippedSourceFileNames:
4✔
UNCOV
117
    if not sources_paths.files:
×
UNCOV
118
        return StrippedSourceFileNames()
×
UNCOV
119
    source_root = await get_source_root(SourceRootRequest.for_file(sources_paths.files[0]))
×
UNCOV
120
    if source_root.path == ".":
×
UNCOV
121
        return StrippedSourceFileNames(sources_paths.files)
×
UNCOV
122
    return StrippedSourceFileNames(fast_relpath(f, source_root.path) for f in sources_paths.files)
×
123

124

125
def rules():
4✔
126
    return (*collect_rules(), *source_root_rules(), *source_files_rules())
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc