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

pantsbuild / pants / 23177125175

17 Mar 2026 03:32AM UTC coverage: 52.677% (-40.3%) from 92.932%
23177125175

Pull #23177

github

web-flow
Merge 1824dfbf4 into 0b9fdfb0e
Pull Request #23177: Bump the gha-deps group across 1 directory with 4 updates

31687 of 60153 relevant lines covered (52.68%)

1.05 hits per line

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

0.0
/src/python/pants/backend/go/lint/gofmt/rules.py
1
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3

4
from __future__ import annotations
×
5

6
import os.path
×
7
from dataclasses import dataclass
×
8

9
from pants.backend.go.lint.gofmt.skip_field import SkipGofmtField
×
10
from pants.backend.go.lint.gofmt.subsystem import (
×
11
    SUPPORTED_GOFMT_ARGS,
12
    SUPPORTED_GOFMT_ARGS_AS_HELP,
13
    GofmtSubsystem,
14
)
15
from pants.backend.go.target_types import GoPackageSourcesField
×
16
from pants.backend.go.util_rules import goroot
×
17
from pants.backend.go.util_rules.goroot import GoRoot
×
18
from pants.core.goals.fmt import FmtResult, FmtTargetsRequest
×
19
from pants.core.util_rules.partitions import PartitionerType
×
20
from pants.engine.process import Process, execute_process_or_raise
×
21
from pants.engine.rules import collect_rules, implicitly, rule
×
22
from pants.engine.target import FieldSet, Target
×
23
from pants.util.logging import LogLevel
×
24
from pants.util.strutil import pluralize
×
25

26

27
@dataclass(frozen=True)
×
28
class GofmtFieldSet(FieldSet):
×
29
    required_fields = (GoPackageSourcesField,)
×
30

31
    sources: GoPackageSourcesField
×
32

33
    @classmethod
×
34
    def opt_out(cls, tgt: Target) -> bool:
×
35
        return tgt.get(SkipGofmtField).value
×
36

37

38
class GofmtRequest(FmtTargetsRequest):
×
39
    field_set_type = GofmtFieldSet
×
40
    tool_subsystem = GofmtSubsystem  # type: ignore[assignment]
×
41
    partitioner_type = PartitionerType.DEFAULT_SINGLE_PARTITION
×
42

43

44
class GoFmtUnsupportedArgsPassedError(Exception):
×
45
    pass
×
46

47

48
async def _validate_gofmt_args(args: tuple[str, ...]):
×
49
    """Validate that args passed to the gofmt are supported."""
50
    if not set(args).issubset(SUPPORTED_GOFMT_ARGS):
×
51
        raise GoFmtUnsupportedArgsPassedError(
×
52
            f"Only the following style related options are supported: {SUPPORTED_GOFMT_ARGS_AS_HELP}."
53
        )
54

55

56
@rule(desc="Format with gofmt")
×
57
async def gofmt_fmt(
×
58
    request: GofmtRequest.Batch, gofmt: GofmtSubsystem, goroot: GoRoot
59
) -> FmtResult:
60
    await _validate_gofmt_args(gofmt.args)
×
61
    argv = (
×
62
        os.path.join(goroot.path, "bin/gofmt"),
63
        "-w",
64
        *gofmt.args,
65
        # Filter out non-.go files, e.g. assembly sources, from the file list.
66
        *(f for f in request.files if f.endswith(".go")),
67
    )
68
    result = await execute_process_or_raise(
×
69
        **implicitly(
70
            Process(
71
                argv=argv,
72
                input_digest=request.snapshot.digest,
73
                output_files=request.files,
74
                description=f"Run gofmt on {pluralize(len(request.files), 'file')}.",
75
                level=LogLevel.DEBUG,
76
            )
77
        )
78
    )
79
    return await FmtResult.create(request, result)
×
80

81

82
def rules():
×
83
    return (
×
84
        *collect_rules(),
85
        *goroot.rules(),
86
        *GofmtRequest.rules(),
87
    )
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