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

pantsbuild / pants / 18517631058

15 Oct 2025 04:18AM UTC coverage: 69.207% (-11.1%) from 80.267%
18517631058

Pull #22745

github

web-flow
Merge 642a76ca1 into 99919310e
Pull Request #22745: [windows] Add windows support in the stdio crate.

53815 of 77759 relevant lines covered (69.21%)

2.42 hits per line

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

79.17
/src/python/pants/init/plugin_resolver.py
1
# Copyright 2015 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3

4
from __future__ import annotations
7✔
5

6
import importlib.metadata
7✔
7
import logging
7✔
8
import site
7✔
9
import sys
7✔
10
from collections.abc import Iterable
7✔
11
from dataclasses import dataclass
7✔
12
from typing import cast
7✔
13

14
from packaging.requirements import Requirement
7✔
15

16
from pants.backend.python.util_rules.interpreter_constraints import InterpreterConstraints
7✔
17
from pants.backend.python.util_rules.pex import PexRequest, VenvPexProcess, create_venv_pex
7✔
18
from pants.backend.python.util_rules.pex_environment import PythonExecutable
7✔
19
from pants.backend.python.util_rules.pex_requirements import PexRequirements
7✔
20
from pants.core.environments.rules import determine_bootstrap_environment
7✔
21
from pants.engine.collection import DeduplicatedCollection
7✔
22
from pants.engine.env_vars import CompleteEnvironmentVars
7✔
23
from pants.engine.environment import EnvironmentName
7✔
24
from pants.engine.internals.selectors import Params
7✔
25
from pants.engine.internals.session import SessionValues
7✔
26
from pants.engine.process import ProcessCacheScope, execute_process_or_raise
7✔
27
from pants.engine.rules import QueryRule, collect_rules, implicitly, rule
7✔
28
from pants.init.bootstrap_scheduler import BootstrapScheduler
7✔
29
from pants.init.import_util import find_matching_distributions
7✔
30
from pants.option.global_options import GlobalOptions
7✔
31
from pants.option.options_bootstrapper import OptionsBootstrapper
7✔
32
from pants.util.logging import LogLevel
7✔
33

34
logger = logging.getLogger(__name__)
7✔
35

36

37
@dataclass(frozen=True)
7✔
38
class PluginsRequest:
7✔
39
    # Interpreter constraints to resolve for, or None to resolve for the interpreter that Pants is
40
    # running under.
41
    interpreter_constraints: InterpreterConstraints | None
7✔
42
    # Requirement constraints to resolve with. If plugins will be loaded into the global working_set
43
    # (i.e., onto the `sys.path`), then these should be the current contents of the working_set.
44
    constraints: tuple[Requirement, ...]
7✔
45
    # Backend requirements to resolve
46
    requirements: tuple[str, ...]
7✔
47

48

49
class ResolvedPluginDistributions(DeduplicatedCollection[str]):
7✔
50
    sort_input = True
7✔
51

52

53
@rule
7✔
54
async def resolve_plugins(
7✔
55
    request: PluginsRequest,
56
    global_options: GlobalOptions,
57
) -> ResolvedPluginDistributions:
58
    """This rule resolves plugins using a VenvPex, and exposes the absolute paths of their dists.
59

60
    NB: This relies on the fact that PEX constructs venvs in a stable location (within the
61
    `named_caches` directory), but consequently needs to disable the process cache: see the
62
    ProcessCacheScope reference in the body.
63
    """
64
    req_strings = sorted(global_options.plugins + request.requirements)
×
65

66
    requirements = PexRequirements(
×
67
        req_strings_or_addrs=req_strings,
68
        constraints_strings=(str(constraint) for constraint in request.constraints),
69
        description_of_origin="configured Pants plugins",
70
    )
71
    if not requirements:
×
72
        return ResolvedPluginDistributions()
×
73

74
    python: PythonExecutable | None = None
×
75
    if not request.interpreter_constraints:
×
76
        python = PythonExecutable.fingerprinted(
×
77
            sys.executable, ".".join(map(str, sys.version_info[:3])).encode("utf8")
78
        )
79

80
    plugins_pex = await create_venv_pex(
×
81
        **implicitly(
82
            PexRequest(
83
                output_filename="pants_plugins.pex",
84
                internal_only=True,
85
                python=python,
86
                requirements=requirements,
87
                interpreter_constraints=request.interpreter_constraints or InterpreterConstraints(),
88
                description=f"Resolving plugins: {', '.join(req_strings)}",
89
            )
90
        )
91
    )
92

93
    # NB: We run this Process per-restart because it (intentionally) leaks named cache
94
    # paths in a way that invalidates the Process-cache. See the method doc.
95
    cache_scope = (
×
96
        ProcessCacheScope.PER_SESSION
97
        if global_options.plugins_force_resolve
98
        else ProcessCacheScope.PER_RESTART_SUCCESSFUL
99
    )
100

101
    plugins_process_result = await execute_process_or_raise(
×
102
        **implicitly(
103
            VenvPexProcess(
104
                plugins_pex,
105
                argv=("-c", "import os, site; print(os.linesep.join(site.getsitepackages()))"),
106
                description="Extracting plugin locations",
107
                level=LogLevel.DEBUG,
108
                cache_scope=cache_scope,
109
            )
110
        )
111
    )
112
    return ResolvedPluginDistributions(plugins_process_result.stdout.decode().strip().split("\n"))
×
113

114

115
class PluginResolver:
7✔
116
    """Encapsulates the state of plugin loading.
117

118
    Plugin loading is inherently stateful, and so the system enviroment on `sys.path` will be
119
    mutated by each call to `PluginResolver.resolve`.
120
    """
121

122
    def __init__(
7✔
123
        self,
124
        scheduler: BootstrapScheduler,
125
        interpreter_constraints: InterpreterConstraints | None = None,
126
        inherit_existing_constraints: bool = True,
127
    ) -> None:
128
        self._scheduler = scheduler
2✔
129
        self._interpreter_constraints = interpreter_constraints
2✔
130
        self._inherit_existing_constraints = inherit_existing_constraints
2✔
131

132
    def resolve(
7✔
133
        self,
134
        options_bootstrapper: OptionsBootstrapper,
135
        env: CompleteEnvironmentVars,
136
        requirements: Iterable[str] = (),
137
    ) -> list[str]:
138
        """Resolves any configured plugins and adds them to the sys.path as a side effect."""
139

140
        def to_requirement(d):
2✔
141
            return f"{d.name}=={d.version}"
2✔
142

143
        distributions: list[importlib.metadata.Distribution] = []
2✔
144
        if self._inherit_existing_constraints:
2✔
145
            distributions = list(find_matching_distributions(None))
2✔
146

147
        request = PluginsRequest(
2✔
148
            self._interpreter_constraints,
149
            tuple(to_requirement(dist) for dist in distributions),
150
            tuple(requirements),
151
        )
152

153
        result = []
2✔
154
        for resolved_plugin_location in self._resolve_plugins(options_bootstrapper, env, request):
2✔
155
            # Activate any .pth files plugin wheels may have.
156
            orig_sys_path_len = len(sys.path)
×
157
            site.addsitedir(resolved_plugin_location)
×
158
            if len(sys.path) > orig_sys_path_len:
×
159
                result.append(resolved_plugin_location)
×
160

161
        return result
2✔
162

163
    def _resolve_plugins(
7✔
164
        self,
165
        options_bootstrapper: OptionsBootstrapper,
166
        env: CompleteEnvironmentVars,
167
        request: PluginsRequest,
168
    ) -> ResolvedPluginDistributions:
169
        session = self._scheduler.scheduler.new_session(
2✔
170
            "plugin_resolver",
171
            session_values=SessionValues(
172
                {
173
                    OptionsBootstrapper: options_bootstrapper,
174
                    CompleteEnvironmentVars: env,
175
                }
176
            ),
177
        )
178
        params = Params(request, determine_bootstrap_environment(session))
2✔
179
        return cast(
2✔
180
            ResolvedPluginDistributions,
181
            session.product_request(ResolvedPluginDistributions, [params])[0],
182
        )
183

184

185
def rules():
7✔
186
    return [
2✔
187
        QueryRule(ResolvedPluginDistributions, [PluginsRequest, EnvironmentName]),
188
        *collect_rules(),
189
    ]
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

© 2025 Coveralls, Inc