Skip to content

Commit

Permalink
Remove per-language indirection for formatter plugins. (#14166)
Browse files Browse the repository at this point in the history
To prepare to add batching for formatters as part of #13462, this change removes the need to implement per-backend `@rules` that pipeline `FmtRequest`s that apply to a particular language. Instead, targets are grouped by which `FmtRequest`s apply to them, and then those requests are run sequentially.

There will be further changes to the formatting API in support of #13462, so this API is not final.

[ci skip-rust]
[ci skip-build-wheels]
  • Loading branch information
stuhood authored Jan 13, 2022
1 parent 8c4cfc5 commit 18114b9
Show file tree
Hide file tree
Showing 39 changed files with 234 additions and 830 deletions.
2 changes: 0 additions & 2 deletions src/python/pants/backend/experimental/go/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from pants.backend.go import target_type_rules
from pants.backend.go.go_sources import load_go_binary
from pants.backend.go.goals import check, package_binary, run_binary, tailor, test
from pants.backend.go.lint import fmt
from pants.backend.go.lint.gofmt import skip_field as gofmt_skip_field
from pants.backend.go.lint.gofmt.rules import rules as gofmt_rules
from pants.backend.go.subsystems import golang
Expand Down Expand Up @@ -53,7 +52,6 @@ def rules():
*package_binary.rules(),
*load_go_binary.rules(),
# Gofmt
*fmt.rules(),
*gofmt_rules(),
*gofmt_skip_field.rules(),
]
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
See https://github.com/myint/autoflake for details.
"""

from pants.backend.python.lint import python_fmt
from pants.backend.python.lint.autoflake import rules as autoflake_rules
from pants.backend.python.lint.autoflake import skip_field, subsystem


def rules():
return (*autoflake_rules.rules(), *python_fmt.rules(), *skip_field.rules(), *subsystem.rules())
return (*autoflake_rules.rules(), *skip_field.rules(), *subsystem.rules())
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
A tool to automatically upgrade syntax for newer versions of the language.
"""

from pants.backend.python.lint import python_fmt
from pants.backend.python.lint.pyupgrade import rules as pyupgrade_rules
from pants.backend.python.lint.pyupgrade import skip_field, subsystem


def rules():
return (*pyupgrade_rules.rules(), *python_fmt.rules(), *skip_field.rules(), *subsystem.rules())
return (*pyupgrade_rules.rules(), *skip_field.rules(), *subsystem.rules())
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from pants.backend.experimental.scala.register import rules as all_scala_rules
from pants.backend.scala.lint import scala_lang_fmt
from pants.backend.scala.lint.scalafmt import rules as scalafmt_rules
from pants.backend.scala.lint.scalafmt import skip_field


def rules():
return [
*all_scala_rules(),
*scala_lang_fmt.rules(),
*scalafmt_rules.rules(),
*skip_field.rules(),
]
3 changes: 1 addition & 2 deletions src/python/pants/backend/experimental/terraform/register.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from pants.backend.python.util_rules.pex import rules as pex_rules
from pants.backend.terraform import dependency_inference, style, tailor, target_gen, tool
from pants.backend.terraform.goals import check
from pants.backend.terraform.lint import fmt
from pants.backend.terraform.lint.tffmt.tffmt import rules as tffmt_rules
from pants.backend.terraform.target_types import (
TerraformModulesGeneratorTarget,
Expand All @@ -26,7 +26,6 @@ def rules():
*target_gen.rules(),
*target_types_rules(),
*tool.rules(),
*fmt.rules(),
*style.rules(),
*pex_rules(),
*tffmt_rules(),
Expand Down
67 changes: 0 additions & 67 deletions src/python/pants/backend/go/lint/fmt.py

This file was deleted.

7 changes: 3 additions & 4 deletions src/python/pants/backend/go/lint/gofmt/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
import os.path
from dataclasses import dataclass

from pants.backend.go.lint.fmt import GoLangFmtRequest
from pants.backend.go.lint.gofmt.skip_field import SkipGofmtField
from pants.backend.go.lint.gofmt.subsystem import GofmtSubsystem
from pants.backend.go.subsystems import golang
from pants.backend.go.subsystems.golang import GoRoot
from pants.backend.go.target_types import GoPackageSourcesField
from pants.core.goals.fmt import FmtResult
from pants.core.goals.fmt import FmtRequest, FmtResult
from pants.core.goals.lint import LintRequest, LintResult, LintResults
from pants.core.util_rules.source_files import SourceFiles, SourceFilesRequest
from pants.engine.fs import Digest
Expand All @@ -37,7 +36,7 @@ def opt_out(cls, tgt: Target) -> bool:
return tgt.get(SkipGofmtField).value


class GofmtRequest(GoLangFmtRequest):
class GofmtRequest(FmtRequest):
field_set_type = GofmtFieldSet


Expand Down Expand Up @@ -113,6 +112,6 @@ def rules():
return [
*collect_rules(),
*golang.rules(),
UnionRule(GoLangFmtRequest, GofmtRequest),
UnionRule(FmtRequest, GofmtRequest),
UnionRule(LintRequest, GofmtRequest),
]
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import pytest

from pants.backend.go import target_type_rules
from pants.backend.go.lint import fmt
from pants.backend.go.lint.gofmt.rules import GofmtFieldSet, GofmtRequest
from pants.backend.go.lint.gofmt.rules import rules as gofmt_rules
from pants.backend.go.target_types import GoModTarget, GoPackageTarget
Expand Down Expand Up @@ -36,7 +35,6 @@ def rule_runner() -> RuleRunner:
rule_runner = RuleRunner(
target_types=[GoModTarget, GoPackageTarget],
rules=[
*fmt.rules(),
*gofmt_rules(),
*source_files.rules(),
*target_type_rules.rules(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import pytest

from pants.backend.go import target_type_rules
from pants.backend.go.lint import fmt
from pants.backend.go.lint.vet import skip_field
from pants.backend.go.lint.vet.rules import GoVetFieldSet, GoVetRequest
from pants.backend.go.lint.vet.rules import rules as go_vet_rules
Expand Down Expand Up @@ -38,7 +37,6 @@ def rule_runner() -> RuleRunner:
rule_runner = RuleRunner(
target_types=[GoModTarget, GoPackageTarget],
rules=[
*fmt.rules(),
*skip_field.rules(),
*go_vet_rules(),
*source_files.rules(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import dataclasses
from dataclasses import dataclass

from pants.backend.java.lint import java_fmt
from pants.backend.java.lint.google_java_format.skip_field import SkipGoogleJavaFormatField
from pants.backend.java.lint.google_java_format.subsystem import GoogleJavaFormatSubsystem
from pants.backend.java.lint.java_fmt import JavaFmtRequest
from pants.backend.java.target_types import JavaSourceField
from pants.core.goals.fmt import FmtResult
from pants.core.goals.fmt import FmtRequest, FmtResult
from pants.core.goals.lint import LintRequest, LintResult, LintResults
from pants.core.util_rules.source_files import SourceFiles, SourceFilesRequest
from pants.engine.fs import Digest
Expand All @@ -35,7 +33,7 @@ def opt_out(cls, tgt: Target) -> bool:
return tgt.get(SkipGoogleJavaFormatField).value


class GoogleJavaFormatRequest(JavaFmtRequest, LintRequest):
class GoogleJavaFormatRequest(FmtRequest, LintRequest):
field_set_type = GoogleJavaFormatFieldSet


Expand Down Expand Up @@ -167,8 +165,7 @@ async def generate_google_java_format_lockfile_request(
def rules():
return [
*collect_rules(),
*java_fmt.rules(),
UnionRule(JavaFmtRequest, GoogleJavaFormatRequest),
UnionRule(FmtRequest, GoogleJavaFormatRequest),
UnionRule(LintRequest, GoogleJavaFormatRequest),
UnionRule(JvmToolLockfileSentinel, GoogleJavaFormatToolLockfileSentinel),
]
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import pytest

from pants.backend.java.compile.javac import rules as javac_rules
from pants.backend.java.lint import java_fmt
from pants.backend.java.lint.google_java_format import rules as gjf_fmt_rules
from pants.backend.java.lint.google_java_format import skip_field
from pants.backend.java.lint.google_java_format.rules import (
Expand Down Expand Up @@ -43,7 +42,6 @@ def rule_runner() -> RuleRunner:
*util_rules(),
*java_util_rules(),
*target_types_rules(),
*java_fmt.rules(),
*gjf_fmt_rules.rules(),
*skip_field.rules(),
QueryRule(LintResults, (GoogleJavaFormatRequest,)),
Expand Down
61 changes: 0 additions & 61 deletions src/python/pants/backend/java/lint/java_fmt.py

This file was deleted.

1 change: 0 additions & 1 deletion src/python/pants/backend/python/lint/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
# Licensed under the Apache License, Version 2.0 (see LICENSE).

python_sources()
python_tests(name="tests", timeout=180)
7 changes: 3 additions & 4 deletions src/python/pants/backend/python/lint/autoflake/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

from pants.backend.python.lint.autoflake.skip_field import SkipAutoflakeField
from pants.backend.python.lint.autoflake.subsystem import Autoflake
from pants.backend.python.lint.python_fmt import PythonFmtRequest
from pants.backend.python.target_types import InterpreterConstraintsField, PythonSourceField
from pants.backend.python.util_rules import pex
from pants.backend.python.util_rules.pex import PexRequest, VenvPex, VenvPexProcess
from pants.core.goals.fmt import FmtResult
from pants.core.goals.fmt import FmtRequest, FmtResult
from pants.core.goals.lint import LintRequest, LintResult, LintResults
from pants.core.util_rules.source_files import SourceFiles, SourceFilesRequest
from pants.engine.fs import Digest
Expand All @@ -34,7 +33,7 @@ def opt_out(cls, tgt: Target) -> bool:
return tgt.get(SkipAutoflakeField).value


class AutoflakeRequest(PythonFmtRequest, LintRequest):
class AutoflakeRequest(FmtRequest, LintRequest):
field_set_type = AutoflakeFieldSet


Expand Down Expand Up @@ -142,7 +141,7 @@ def strip_check_result(output: str) -> str:
def rules():
return [
*collect_rules(),
UnionRule(PythonFmtRequest, AutoflakeRequest),
UnionRule(FmtRequest, AutoflakeRequest),
UnionRule(LintRequest, AutoflakeRequest),
*pex.rules(),
]
3 changes: 1 addition & 2 deletions src/python/pants/backend/python/lint/black/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
https://black.readthedocs.io/en/stable/.
"""

from pants.backend.python.lint import python_fmt
from pants.backend.python.lint.black import rules as black_rules
from pants.backend.python.lint.black import skip_field, subsystem


def rules():
return (*black_rules.rules(), *python_fmt.rules(), *skip_field.rules(), *subsystem.rules())
return (*black_rules.rules(), *skip_field.rules(), *subsystem.rules())
7 changes: 3 additions & 4 deletions src/python/pants/backend/python/lint/black/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@

from pants.backend.python.lint.black.skip_field import SkipBlackField
from pants.backend.python.lint.black.subsystem import Black
from pants.backend.python.lint.python_fmt import PythonFmtRequest
from pants.backend.python.subsystems.setup import PythonSetup
from pants.backend.python.target_types import InterpreterConstraintsField, PythonSourceField
from pants.backend.python.util_rules import pex
from pants.backend.python.util_rules.interpreter_constraints import InterpreterConstraints
from pants.backend.python.util_rules.pex import PexRequest, VenvPex, VenvPexProcess
from pants.core.goals.fmt import FmtResult
from pants.core.goals.fmt import FmtRequest, FmtResult
from pants.core.goals.lint import LintRequest, LintResult, LintResults
from pants.core.util_rules.config_files import ConfigFiles, ConfigFilesRequest
from pants.core.util_rules.source_files import SourceFiles, SourceFilesRequest
Expand All @@ -37,7 +36,7 @@ def opt_out(cls, tgt: Target) -> bool:
return tgt.get(SkipBlackField).value


class BlackRequest(PythonFmtRequest, LintRequest):
class BlackRequest(FmtRequest, LintRequest):
field_set_type = BlackFieldSet


Expand Down Expand Up @@ -161,7 +160,7 @@ async def black_lint(field_sets: BlackRequest, black: Black) -> LintResults:
def rules():
return [
*collect_rules(),
UnionRule(PythonFmtRequest, BlackRequest),
UnionRule(FmtRequest, BlackRequest),
UnionRule(LintRequest, BlackRequest),
*pex.rules(),
]
Loading

0 comments on commit 18114b9

Please sign in to comment.