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

pantsbuild / pants / 22740642519

05 Mar 2026 11:00PM UTC coverage: 52.677% (-40.3%) from 92.931%
22740642519

Pull #23157

github

web-flow
Merge 2aa18e6d4 into f0030f5e7
Pull Request #23157: [pants ng] Partition source files by config.

31678 of 60136 relevant lines covered (52.68%)

0.53 hits per line

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

98.15
/src/python/pants/backend/go/subsystems/golang.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
1✔
5

6
import logging
1✔
7
import os
1✔
8

9
from pants.core.util_rules.asdf import AsdfPathString
1✔
10
from pants.option.option_types import BoolOption, StrListOption, StrOption
1✔
11
from pants.option.subsystem import Subsystem
1✔
12
from pants.util.memo import memoized_property
1✔
13
from pants.util.ordered_set import OrderedSet
1✔
14
from pants.util.strutil import softwrap
1✔
15

16
logger = logging.getLogger(__name__)
1✔
17

18

19
_DEFAULT_COMPILER_FLAGS = ("-g", "-O2")
1✔
20

21

22
class GolangSubsystem(Subsystem):
1✔
23
    options_scope = "golang"
1✔
24
    help = "Options for Golang support."
1✔
25

26
    class EnvironmentAware(Subsystem.EnvironmentAware):
1✔
27
        env_vars_used_by_options = ("PATH",)
1✔
28

29
        _go_search_paths = StrListOption(
1✔
30
            default=["<PATH>"],
31
            help=softwrap(
32
                f"""
33
                A list of paths to search for Go and extra tools needed by go.
34

35
                Specify absolute paths to directories with the `go` binary, e.g. `/usr/bin`.
36
                Earlier entries will be searched first.
37

38
                The following special strings are supported:
39

40
                * `<PATH>`, the contents of the PATH environment variable
41
                * `{AsdfPathString.STANDARD}`, {AsdfPathString.STANDARD.description("Go")}
42
                * `{AsdfPathString.LOCAL}`, {AsdfPathString.LOCAL.description("binary")}
43
                """
44
            ),
45
        )
46
        _subprocess_env_vars = StrListOption(
1✔
47
            default=["LANG", "LC_CTYPE", "LC_ALL", "PATH"],
48
            help=softwrap(
49
                """
50
            Environment variables to set when invoking the `go` tool.
51
            Entries are either strings in the form `ENV_VAR=value` to set an explicit value;
52
            or just `ENV_VAR` to copy the value from Pants's own environment.
53
            """
54
            ),
55
            advanced=True,
56
        )
57

58
        _cgo_tool_search_paths = StrListOption(
1✔
59
            default=["<PATH>"],
60
            help=softwrap(
61
                """
62
                A list of paths to search for tools needed by CGo (e.g., gcc, g++).
63

64
                Specify absolute paths to directories with tools needed by CGo , e.g. `/usr/bin`.
65
                Earlier entries will be searched first.
66

67
                The following special strings are supported:
68

69
                * `<PATH>`, the contents of the PATH environment variable
70
                """
71
            ),
72
        )
73

74
        _extra_tools = StrListOption(
1✔
75
            default=[],
76
            help=softwrap(
77
                """
78
                List any additional executable tools required for the `go` tool to work.
79
                The paths to these tools will be included in the PATH used in the execution sandbox.
80
                E.g. `go mod download` may require the `git` tool to download private modules.
81
                """
82
            ),
83
            advanced=True,
84
        )
85

86
        cgo_gcc_binary_name = StrOption(
1✔
87
            default="gcc",
88
            advanced=True,
89
            help=softwrap(
90
                """
91
                Name of the tool to use to compile C code included via CGo in a Go package.
92
                Pants will search for the tool using the paths specified by the
93
                `[golang].cgo_tool_search_paths` option.
94
                """
95
            ),
96
        )
97

98
        cgo_gxx_binary_name = StrOption(
1✔
99
            default="g++",
100
            advanced=True,
101
            help=softwrap(
102
                """
103
                Name of the tool to use to compile C++ code included via CGo in a Go package.
104
                Pants will search for the tool using the paths specified by the
105
                `[golang].cgo_tool_search_paths` option.
106
                """
107
            ),
108
        )
109

110
        cgo_fortran_binary_name = StrOption(
1✔
111
            default="gfortran",
112
            advanced=True,
113
            help=softwrap(
114
                """
115
                Name of the tool to use to compile fortran code included via CGo in a Go package.
116
                Pants will search for the tool using the paths specified by the
117
                `[golang].cgo_tool_search_paths` option.
118
                """
119
            ),
120
        )
121

122
        external_linker_binary_name = StrOption(
1✔
123
            default="gcc",
124
            advanced=True,
125
            help=softwrap(
126
                """
127
                Name of the tool to use as the "external linker" when invoking `go tool link`.
128
                Pants will search for the tool using the paths specified by the
129
                `[golang].cgo_tool_search_paths` option.
130
                """
131
            ),
132
        )
133

134
        cgo_c_flags = StrListOption(
1✔
135
            default=lambda _: list(_DEFAULT_COMPILER_FLAGS),
136
            advanced=True,
137
            help=softwrap(
138
                """
139
                Compiler options used when compiling C code when Cgo is enabled. Equivalent to setting the
140
                CGO_CFLAGS environment variable when invoking `go`.
141
                """
142
            ),
143
        )
144

145
        cgo_cxx_flags = StrListOption(
1✔
146
            default=lambda _: list(_DEFAULT_COMPILER_FLAGS),
147
            advanced=True,
148
            help=softwrap(
149
                """
150
                Compiler options used when compiling C++ code when Cgo is enabled. Equivalent to setting the
151
                CGO_CXXFLAGS environment variable when invoking `go`.
152
                """
153
            ),
154
        )
155

156
        cgo_fortran_flags = StrListOption(
1✔
157
            default=lambda _: list(_DEFAULT_COMPILER_FLAGS),
158
            advanced=True,
159
            help=softwrap(
160
                """
161
                Compiler options used when compiling Fortran code when Cgo is enabled. Equivalent to setting the
162
                CGO_FFLAGS environment variable when invoking `go`.
163
                """
164
            ),
165
        )
166

167
        cgo_linker_flags = StrListOption(
1✔
168
            default=lambda _: list(_DEFAULT_COMPILER_FLAGS),
169
            advanced=True,
170
            help=softwrap(
171
                """
172
                Compiler options used when linking native code when Cgo is enabled. Equivalent to setting the
173
                CGO_LDFLAGS environment variable when invoking `go`.
174
                """
175
            ),
176
        )
177

178
        @property
1✔
179
        def raw_go_search_paths(self) -> tuple[str, ...]:
1✔
180
            return tuple(self._go_search_paths)
1✔
181

182
        @property
1✔
183
        def env_vars_to_pass_to_subprocesses(self) -> tuple[str, ...]:
1✔
184
            return tuple(sorted(set(self._subprocess_env_vars)))
1✔
185

186
        @memoized_property
1✔
187
        def cgo_tool_search_paths(self) -> tuple[str, ...]:
1✔
188
            def iter_path_entries():
1✔
189
                for entry in self._cgo_tool_search_paths:
1✔
190
                    if entry == "<PATH>":
1✔
191
                        path = self._options_env.get("PATH")
1✔
192
                        if path:
1✔
193
                            yield from path.split(os.pathsep)
1✔
194
                    else:
195
                        yield entry
×
196

197
            return tuple(OrderedSet(iter_path_entries()))
1✔
198

199
        @property
1✔
200
        def extra_tools(self) -> tuple[str, ...]:
1✔
201
            return tuple(sorted(set(self._extra_tools)))
1✔
202

203
    minimum_expected_version = StrOption(
1✔
204
        default="1.17",
205
        help=softwrap(
206
            """
207
            The minimum Go version the distribution discovered by Pants must support.
208

209
            For example, if you set `'1.17'`, then Pants will look for a Go binary that is 1.17+,
210
            e.g. 1.17 or 1.18.
211

212
            You should still set the Go version for each module in your `go.mod` with the `go`
213
            directive.
214

215
            Do not include the patch version.
216
            """
217
        ),
218
    )
219
    tailor_go_mod_targets = BoolOption(
1✔
220
        default=True,
221
        help=softwrap(
222
            """
223
            If true, add a `go_mod` target with the `tailor` goal wherever there is a
224
            `go.mod` file.
225
            """
226
        ),
227
        advanced=True,
228
    )
229
    tailor_package_targets = BoolOption(
1✔
230
        default=True,
231
        help=softwrap(
232
            """
233
            If true, add a `go_package` target with the `tailor` goal in every directory with a
234
            `.go` file.
235
            """
236
        ),
237
        advanced=True,
238
    )
239
    tailor_binary_targets = BoolOption(
1✔
240
        default=True,
241
        help=softwrap(
242
            """
243
            If true, add a `go_binary` target with the `tailor` goal in every directory with a
244
            `.go` file with `package main`.
245
            """
246
        ),
247
        advanced=True,
248
    )
249

250
    cgo_enabled = BoolOption(
1✔
251
        default=True,
252
        help=softwrap(
253
            """\
254
            Enable Cgo support, which allows Go and C code to interact. This option must be enabled for any
255
            packages making use of Cgo to actually be compiled with Cgo support.
256

257
            See https://go.dev/blog/cgo and https://pkg.go.dev/cmd/cgo for additional information about Cgo.
258
            """
259
        ),
260
    )
261

262
    asdf_tool_name = StrOption(
1✔
263
        default="go-sdk",
264
        help=softwrap(
265
            """
266
            The ASDF tool name to use when searching for installed Go distributions using the ASDF tool
267
            manager (https://asdf-vm.com/). The default value for this option is for the `go-sdk` ASDF plugin
268
            (https://github.com/yacchi/asdf-go-sdk.git). There are other plugins. If you wish to use one of them,
269
            then set this option to the ASDF tool name under which that other plugin was installed into ASDF.
270
            """
271
        ),
272
        advanced=True,
273
    )
274

275
    asdf_bin_relpath = StrOption(
1✔
276
        default="bin",
277
        help=softwrap(
278
            """
279
            The path relative to an ASDF install directory to use to find the `bin` directory within an installed
280
            Go distribution. The default value for this option works for the `go-sdk` ASDF plugin. Other ASDF
281
            plugins that install Go may have a different relative path to use.
282
            """
283
        ),
284
        advanced=True,
285
    )
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