Skip to content

Commit

Permalink
[internal] Make generate-lockfiles goal generic (pantsbuild#14122)
Browse files Browse the repository at this point in the history
The goal was originally hardcoded for Python. This allows us to now use it for other languages like Java.

We do this through some elaborate usage of unions + newtyping so that distinct types look the same.

Remaining TODOs:

* Re-enable ambiguity check, and check for ambiguity between user lockfiles, like Python vs. Java.
* Update `help` for the goal to be generic.
  • Loading branch information
Eric-Arellano authored Jan 11, 2022
1 parent d79439a commit e9946d3
Show file tree
Hide file tree
Showing 30 changed files with 714 additions and 457 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
from typing import cast

from pants.backend.codegen.protobuf.target_types import ProtobufDependenciesField
from pants.backend.python.goals.lockfile import PythonLockfileRequest, PythonToolLockfileSentinel
from pants.backend.python.goals import lockfile
from pants.backend.python.goals.lockfile import PythonLockfileRequest
from pants.backend.python.subsystems.python_tool_base import PythonToolRequirementsBase
from pants.core.goals.generate_lockfiles import ToolLockfileSentinel
from pants.engine.addresses import Addresses, UnparsedAddressInputs
from pants.engine.rules import Get, collect_rules, rule
from pants.engine.target import InjectDependenciesRequest, InjectedDependencies
Expand Down Expand Up @@ -72,7 +74,7 @@ class PythonProtobufMypyPlugin(PythonToolRequirementsBase):
default_lockfile_url = git_url(default_lockfile_path)


class MypyProtobufLockfileSentinel(PythonToolLockfileSentinel):
class MypyProtobufLockfileSentinel(ToolLockfileSentinel):
options_scope = PythonProtobufMypyPlugin.options_scope


Expand All @@ -98,6 +100,7 @@ async def inject_dependencies(
def rules():
return [
*collect_rules(),
*lockfile.rules(),
UnionRule(InjectDependenciesRequest, InjectPythonProtobufDependencies),
UnionRule(PythonToolLockfileSentinel, MypyProtobufLockfileSentinel),
UnionRule(ToolLockfileSentinel, MypyProtobufLockfileSentinel),
]
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

from pants.backend.docker.target_types import DockerImageSourceField
from pants.backend.docker.util_rules.docker_build_args import DockerBuildArgs
from pants.backend.python.goals.lockfile import PythonLockfileRequest, PythonToolLockfileSentinel
from pants.backend.python.goals import lockfile
from pants.backend.python.goals.lockfile import PythonLockfileRequest
from pants.backend.python.subsystems.python_tool_base import PythonToolRequirementsBase
from pants.backend.python.target_types import EntryPoint
from pants.backend.python.util_rules.pex import PexRequest, VenvPex, VenvPexProcess
from pants.core.goals.generate_lockfiles import ToolLockfileSentinel
from pants.engine.addresses import Address
from pants.engine.fs import CreateDigest, Digest, FileContent
from pants.engine.process import Process, ProcessResult
Expand Down Expand Up @@ -44,7 +46,7 @@ class DockerfileParser(PythonToolRequirementsBase):
default_lockfile_url = git_url(default_lockfile_path)


class DockerfileParserLockfileSentinel(PythonToolLockfileSentinel):
class DockerfileParserLockfileSentinel(ToolLockfileSentinel):
options_scope = DockerfileParser.options_scope


Expand Down Expand Up @@ -198,5 +200,6 @@ async def parse_dockerfile(request: DockerfileInfoRequest) -> DockerfileInfo:
def rules():
return (
*collect_rules(),
UnionRule(PythonToolLockfileSentinel, DockerfileParserLockfileSentinel),
*lockfile.rules(),
UnionRule(ToolLockfileSentinel, DockerfileParserLockfileSentinel),
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

import logging

from pants.backend.python.goals.lockfile import PythonLockfile, PythonLockfileRequest
from pants.backend.python.goals.lockfile import PythonLockfileRequest
from pants.backend.python.subsystems.setup import PythonSetup
from pants.backend.python.target_types import PythonRequirementsField
from pants.backend.python.util_rules.interpreter_constraints import InterpreterConstraints
from pants.backend.python.util_rules.pex import PexRequirements
from pants.core.goals.generate_lockfiles import Lockfile
from pants.engine.addresses import Addresses
from pants.engine.fs import Workspace
from pants.engine.goal import Goal, GoalSubsystem
Expand Down Expand Up @@ -65,11 +66,11 @@ async def generate_user_lockfile_goal(
return GenerateUserLockfileGoal(exit_code=0)

result = await Get(
PythonLockfile,
Lockfile,
PythonLockfileRequest(
req_strings,
requirements=req_strings,
# TODO(#12314): Use interpreter constraints from the transitive closure.
InterpreterConstraints(python_setup.interpreter_constraints),
interpreter_constraints=InterpreterConstraints(python_setup.interpreter_constraints),
resolve_name="not yet implemented",
lockfile_dest=python_setup.lockfile,
_description=(
Expand Down
9 changes: 6 additions & 3 deletions src/python/pants/backend/python/goals/coverage_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@

import toml

from pants.backend.python.goals.lockfile import PythonLockfileRequest, PythonToolLockfileSentinel
from pants.backend.python.goals import lockfile
from pants.backend.python.goals.lockfile import PythonLockfileRequest
from pants.backend.python.subsystems.python_tool_base import PythonToolBase
from pants.backend.python.target_types import ConsoleScript
from pants.backend.python.util_rules.pex import PexRequest, VenvPex, VenvPexProcess
from pants.backend.python.util_rules.python_sources import (
PythonSourceFiles,
PythonSourceFilesRequest,
)
from pants.core.goals.generate_lockfiles import ToolLockfileSentinel
from pants.core.goals.test import (
ConsoleCoverageReport,
CoverageData,
Expand Down Expand Up @@ -234,7 +236,7 @@ def fail_under(self) -> int:
return cast(int, self.options.fail_under)


class CoveragePyLockfileSentinel(PythonToolLockfileSentinel):
class CoveragePyLockfileSentinel(ToolLockfileSentinel):
options_scope = CoverageSubsystem.options_scope


Expand Down Expand Up @@ -606,6 +608,7 @@ def _get_coverage_report(
def rules():
return [
*collect_rules(),
*lockfile.rules(),
UnionRule(CoverageDataCollection, PytestCoverageDataCollection),
UnionRule(PythonToolLockfileSentinel, CoveragePyLockfileSentinel),
UnionRule(ToolLockfileSentinel, CoveragePyLockfileSentinel),
]
Loading

0 comments on commit e9946d3

Please sign in to comment.