Skip to content

Commit 564f820

Browse files
authored
Merge GoLibrary and GoSource providers (#4030)
**What type of PR is this?** Code simplification **What does this PR do? Why is it needed?** These providers are 1:1, and we always create a library followed by `source_from_library`. Merging them allows to remove the `library` field from `GoSource` as well as the the `resolve` field, since we don't need to smuggle the closure between the providers anymore. If we do this, we will also need to fix up Gazelle. We can either have a patch in this repo so CI passes and then followup with the Gazelle fix, or somehow shim the API until we release new versions in lockstep. Open to ideas how to best do this. **Which issues(s) does this PR fix?** One step toward #2578 Fixes # **Other notes for review**
1 parent 2dc6308 commit 564f820

27 files changed

+395
-368
lines changed

docs/go/core/rules.bzl

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
[Bourne shell tokenization]: https://docs.bazel.build/versions/master/be/common-definitions.html#sh-tokenization
44
[Gazelle]: https://github.com/bazelbuild/bazel-gazelle
55
[GoArchive]: /go/providers.rst#GoArchive
6-
[GoLibrary]: /go/providers.rst#GoLibrary
76
[GoPath]: /go/providers.rst#GoPath
8-
[GoSource]: /go/providers.rst#GoSource
7+
[GoInfo]: /go/providers.rst#GoInfo
98
[build constraints]: https://golang.org/pkg/go/build/#hdr-Build_Constraints
109
[cc_library deps]: https://docs.bazel.build/versions/master/be/c-cpp.html#cc_library.deps
1110
[cgo]: http://golang.org/cmd/cgo/
@@ -51,9 +50,8 @@ sufficient to match the capabilities of the normal go tools.
5150
- [Bourne shell tokenization]
5251
- [Gazelle]
5352
- [GoArchive]
54-
- [GoLibrary]
5553
- [GoPath]
56-
- [GoSource]
54+
- [GoInfo]
5755
- [build constraints]:
5856
- [cc_library deps]
5957
- [cgo]

docs/go/core/rules.md

+12-20
Large diffs are not rendered by default.

extras/gomock.bzl

+8-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
load("@bazel_skylib//lib:paths.bzl", "paths")
2626
load("//go/private:common.bzl", "GO_TOOLCHAIN", "GO_TOOLCHAIN_LABEL")
2727
load("//go/private:context.bzl", "go_context")
28-
load("//go/private:providers.bzl", "GoLibrary")
28+
load("//go/private:providers.bzl", "GoInfo")
2929
load("//go/private/rules:wrappers.bzl", go_binary = "go_binary_macro")
3030

3131
_MOCKGEN_TOOL = Label("//extras/gomock:mockgen")
@@ -36,10 +36,10 @@ def _gomock_source_impl(ctx):
3636

3737
# In Source mode, it's not necessary to pass through a library, as the only thing we use it for is setting up
3838
# the relative file locations. Forcing users to pass a library makes it difficult in the case where a mock should
39-
# be included as part of that same library, as it results in a dependency loop (GoMock -> GoLibrary -> GoMock).
39+
# be included as part of that same library, as it results in a dependency loop (GoMock -> GoInfo -> GoMock).
4040
# Allowing users to pass an importpath directly bypasses this issue.
4141
# See the test case in //tests/extras/gomock/source_with_importpath for an example.
42-
importpath = ctx.attr.source_importpath if ctx.attr.source_importpath else ctx.attr.library[GoLibrary].importmap
42+
importpath = ctx.attr.source_importpath if ctx.attr.source_importpath else ctx.attr.library[GoInfo].importmap
4343

4444
# create GOPATH and copy source into GOPATH
4545
go_path_prefix = "gopath"
@@ -113,7 +113,7 @@ _gomock_source = rule(
113113
attrs = {
114114
"library": attr.label(
115115
doc = "The target the Go library where this source file belongs",
116-
providers = [GoLibrary],
116+
providers = [GoInfo],
117117
mandatory = False,
118118
),
119119
"source_importpath": attr.string(
@@ -257,7 +257,7 @@ def _gomock_reflect(name, library, out, mockgen_tool, **kwargs):
257257

258258
def _gomock_prog_gen_impl(ctx):
259259
args = ["-prog_only"]
260-
args.append(ctx.attr.library[GoLibrary].importpath)
260+
args.append(ctx.attr.library[GoInfo].importpath)
261261
args.append(",".join(ctx.attr.interfaces))
262262

263263
cmd = ctx.file.mockgen_tool
@@ -280,7 +280,7 @@ _gomock_prog_gen = rule(
280280
attrs = {
281281
"library": attr.label(
282282
doc = "The target the Go library is at to look for the interfaces in. When this is set and source is not set, mockgen will use its reflect code to generate the mocks. If source is set, its dependencies will be included in the GOPATH that mockgen will be run in.",
283-
providers = [GoLibrary],
283+
providers = [GoInfo],
284284
mandatory = True,
285285
),
286286
"out": attr.output(
@@ -313,7 +313,7 @@ def _gomock_prog_exec_impl(ctx):
313313

314314
# annoyingly, the interfaces join has to go after the importpath so we can't
315315
# share those.
316-
args.append(ctx.attr.library[GoLibrary].importpath)
316+
args.append(ctx.attr.library[GoInfo].importpath)
317317
args.append(",".join(ctx.attr.interfaces))
318318

319319
ctx.actions.run_shell(
@@ -337,7 +337,7 @@ _gomock_prog_exec = rule(
337337
attrs = {
338338
"library": attr.label(
339339
doc = "The target the Go library is at to look for the interfaces in. When this is set and source is not set, mockgen will use its reflect code to generate the mocks. If source is set, its dependencies will be included in the GOPATH that mockgen will be run in.",
340-
providers = [GoLibrary],
340+
providers = [GoInfo],
341341
mandatory = True,
342342
),
343343
"out": attr.output(

go/core.rst

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ Core Go rules
55
.. _Bourne shell tokenization: https://docs.bazel.build/versions/master/be/common-definitions.html#sh-tokenization
66
.. _Gazelle: https://github.com/bazelbuild/bazel-gazelle
77
.. _GoArchive: providers.rst#GoArchive
8-
.. _GoLibrary: providers.rst#GoLibrary
98
.. _GoPath: providers.rst#GoPath
10-
.. _GoSource: providers.rst#GoSource
9+
.. _GoInfo: providers.rst#GoInfo
1110
.. _build constraints: https://golang.org/pkg/go/build/#hdr-Build_Constraints
1211
.. _cc_library deps: https://docs.bazel.build/versions/master/be/c-cpp.html#cc_library.deps
1312
.. _cgo: http://golang.org/cmd/cgo/

go/def.bzl

+14-6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ load(
2929
load(
3030
"//go/private:context.bzl",
3131
_go_context = "go_context",
32+
_new_go_info = "new_go_info",
3233
)
3334
load(
3435
"//go/private:go_toolchain.bzl",
@@ -38,10 +39,9 @@ load(
3839
"//go/private:providers.bzl",
3940
_GoArchive = "GoArchive",
4041
_GoArchiveData = "GoArchiveData",
41-
_GoLibrary = "GoLibrary",
42+
_GoInfo = "GoInfo",
4243
_GoPath = "GoPath",
4344
_GoSDK = "GoSDK",
44-
_GoSource = "GoSource",
4545
)
4646
load(
4747
"//go/private/rules:cross.bzl",
@@ -134,11 +134,19 @@ go_tool_library = _go_tool_library
134134
go_toolchain = _go_toolchain
135135
nogo = _nogo
136136

137-
# See go/providers.rst#GoLibrary for full documentation.
138-
GoLibrary = _GoLibrary
137+
# This provider is deprecated and will be removed in a future release.
138+
# Use GoInfo instead.
139+
GoLibrary = _GoInfo
139140

140-
# See go/providers.rst#GoSource for full documentation.
141-
GoSource = _GoSource
141+
# This provider is deprecated and will be removed in a future release.
142+
# Use GoInfo instead.
143+
GoSource = _GoInfo
144+
145+
# See go/providers.rst#GoInfo for full documentation.
146+
GoInfo = _GoInfo
147+
148+
# See go/toolchains.rst#new_go_info for full documentation.
149+
new_go_info = _new_go_info
142150

143151
# See go/providers.rst#GoPath for full documentation.
144152
GoPath = _GoPath

go/nogo.rst

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
.. _go_library: /docs/go/core/rules.md#go_library
99
.. _analysis: https://godoc.org/golang.org/x/tools/go/analysis
1010
.. _Analyzer: https://godoc.org/golang.org/x/tools/go/analysis#Analyzer
11-
.. _GoLibrary: providers.rst#GoLibrary
12-
.. _GoSource: providers.rst#GoSource
11+
.. _GoInfo: providers.rst#GoInfo
1312
.. _GoArchive: providers.rst#GoArchive
1413
.. _vet: https://golang.org/cmd/vet/
1514
.. _golangci-lint: https://github.com/golangci/golangci-lint

go/private/actions/archive.bzl

+15-17
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
4343
if source == None:
4444
fail("source is a required parameter")
4545

46-
testfilter = getattr(source.library, "testfilter", None)
46+
testfilter = getattr(source, "testfilter", None)
4747
pre_ext = ""
4848
if go.mode.linkmode == LINKMODE_C_ARCHIVE:
4949
pre_ext = "_" # avoid collision with go_binary output file with .a extension
@@ -53,17 +53,17 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
5353
pre_ext = ".external"
5454
if _recompile_suffix:
5555
pre_ext += _recompile_suffix
56-
out_lib = go.declare_file(go, name = source.library.name, ext = pre_ext + ".a")
56+
out_lib = go.declare_file(go, name = source.name, ext = pre_ext + ".a")
5757

5858
# store export information for compiling dependent packages separately
59-
out_export = go.declare_file(go, name = source.library.name, ext = pre_ext + ".x")
59+
out_export = go.declare_file(go, name = source.name, ext = pre_ext + ".x")
6060
out_cgo_export_h = None # set if cgo used in c-shared or c-archive mode
6161

6262
nogo = get_nogo(go)
6363
if nogo:
64-
out_facts = go.declare_file(go, name = source.library.name, ext = pre_ext + ".facts")
65-
out_nogo_log = go.declare_file(go, name = source.library.name, ext = pre_ext + ".nogo.log")
66-
out_nogo_validation = go.declare_file(go, name = source.library.name, ext = pre_ext + ".nogo")
64+
out_facts = go.declare_file(go, name = source.name, ext = pre_ext + ".facts")
65+
out_nogo_log = go.declare_file(go, name = source.name, ext = pre_ext + ".nogo.log")
66+
out_nogo_validation = go.declare_file(go, name = source.name, ext = pre_ext + ".nogo")
6767
else:
6868
out_facts = None
6969
out_nogo_log = None
@@ -78,8 +78,8 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
7878
fail("Archive mode does not match {} is {} expected {}".format(a.data.label, mode_string(a.source.mode), mode_string(go.mode)))
7979
runfiles = source.runfiles.merge_all(files)
8080

81-
importmap = "main" if source.library.is_main else source.library.importmap
82-
importpath, _ = effective_importpath_pkgpath(source.library)
81+
importmap = "main" if source.is_main else source.importmap
82+
importpath, _ = effective_importpath_pkgpath(source)
8383

8484
if source.cgo and not go.mode.pure:
8585
# TODO(jayconrod): do we need to do full Bourne tokenization here?
@@ -157,15 +157,13 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
157157
# may be mutable. For now, new copied fields are private (named with
158158
# a leading underscore) since they may change in the future.
159159

160-
# GoLibrary fields
161-
name = source.library.name,
162-
label = source.library.label,
163-
importpath = source.library.importpath,
164-
importmap = source.library.importmap,
165-
importpath_aliases = source.library.importpath_aliases,
166-
pathtype = source.library.pathtype,
167-
168-
# GoSource fields
160+
# GoInfo fields
161+
name = source.name,
162+
label = source.label,
163+
importpath = source.importpath,
164+
importmap = source.importmap,
165+
importpath_aliases = source.importpath_aliases,
166+
pathtype = source.pathtype,
169167
srcs = tuple(source.srcs),
170168
_cover = source.cover,
171169
_embedsrcs = tuple(source.embedsrcs),

go/private/actions/stdlib.bzl

+7-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ load(
1818
"GO_TOOLCHAIN_LABEL",
1919
"SUPPORTS_PATH_MAPPING_REQUIREMENT",
2020
)
21+
load(
22+
"//go/private:context.bzl",
23+
"new_go_info",
24+
)
2125
load(
2226
"//go/private:mode.bzl",
2327
"LINKMODE_NORMAL",
@@ -38,12 +42,11 @@ def emit_stdlib(go):
3842
Otherwise, the standard library will be compiled for the target.
3943
4044
Returns:
41-
A list of providers containing GoLibrary, GoSource and GoStdLib.
45+
A list of providers containing GoInfo and GoStdLib.
4246
"""
43-
library = go.new_library(go)
44-
source = go.library_to_source(go, {}, library, False)
47+
go_info = new_go_info(go, {}, coverage_instrumented = False)
4548
stdlib = _sdk_stdlib(go) if _should_use_sdk_stdlib(go) else _build_stdlib(go)
46-
return [source, library, stdlib]
49+
return [go_info, stdlib]
4750

4851
def _should_use_sdk_stdlib(go):
4952
version = parse_version(go.sdk.version)

0 commit comments

Comments
 (0)