Skip to content

Commit f6bab4d

Browse files
committed
rules_go improvement to externalize the nogo fix
1 parent e9ee69a commit f6bab4d

21 files changed

+2252
-264
lines changed

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use_repo(
3636
"com_github_gogo_protobuf",
3737
"com_github_golang_mock",
3838
"com_github_golang_protobuf",
39+
"com_github_pmezard_go_difflib",
3940
"org_golang_google_genproto",
4041
"org_golang_google_grpc",
4142
"org_golang_google_grpc_cmd_protoc_gen_go_grpc",

go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
4949
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
5050
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
5151
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
52-
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5352
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
5453
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
5554
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=

go/.DS_Store

-8 KB
Binary file not shown.

go/private/actions/archive.bzl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,18 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
6464
out_facts = go.declare_file(go, name = source.library.name, ext = pre_ext + ".facts")
6565
out_nogo_log = go.declare_file(go, name = source.library.name, ext = pre_ext + ".nogo.log")
6666
out_nogo_validation = go.declare_file(go, name = source.library.name, ext = pre_ext + ".nogo")
67+
68+
# out_nogo_fix_tmp holds the fixes produced by the RunNogo action, out_nogo_fix holds the fixes produced by the ValidateNogo action.
69+
# They have the same content, but ValidateNogo propagates the fixes and eventually outputs the fixes.
70+
# --run_validations (default=True) ensures nogo fixes (if any) are produced for not only the input targets but also their dependent targets.
71+
# Otherwise, if we put the fixes into the OutputGroupInfo section of the input targets, it will not include the fixes for the dependent targets.
72+
out_nogo_fix_tmp = go.declare_file(go, name = source.library.name, ext = pre_ext + ".nogo.fix.tmp")
6773
out_nogo_fix = go.declare_file(go, name = source.library.name, ext = pre_ext + ".nogo.fix")
6874
else:
6975
out_facts = None
7076
out_nogo_log = None
7177
out_nogo_validation = None
78+
out_nogo_fix_tmp = None
7279
out_nogo_fix = None
7380

7481
direct = source.deps
@@ -115,6 +122,7 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
115122
out_facts = out_facts,
116123
out_nogo_log = out_nogo_log,
117124
out_nogo_validation = out_nogo_validation,
125+
out_nogo_fix_tmp = out_nogo_fix_tmp,
118126
out_nogo_fix = out_nogo_fix,
119127
nogo = nogo,
120128
out_cgo_export_h = out_cgo_export_h,
@@ -145,6 +153,7 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
145153
out_facts = out_facts,
146154
out_nogo_log = out_nogo_log,
147155
out_nogo_validation = out_nogo_validation,
156+
out_nogo_fix_tmp = out_nogo_fix_tmp,
148157
out_nogo_fix = out_nogo_fix,
149158
nogo = nogo,
150159
gc_goopts = source.gc_goopts,
@@ -191,7 +200,7 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
191200
facts_file = out_facts,
192201
runfiles = source.runfiles,
193202
_validation_output = out_nogo_validation,
194-
_out_nogo_fix = out_nogo_fix,
203+
_nogo_fix_output = out_nogo_fix,
195204
_cgo_deps = cgo_deps,
196205
)
197206
x_defs = dict(source.x_defs)

go/private/actions/compilepkg.bzl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def emit_compilepkg(
7070
out_facts = None,
7171
out_nogo_log = None,
7272
out_nogo_validation = None,
73+
out_nogo_fix_tmp = None,
7374
out_nogo_fix = None,
7475
nogo = None,
7576
out_cgo_export_h = None,
@@ -88,8 +89,11 @@ def emit_compilepkg(
8889
fail("nogo must be specified if and only if out_nogo_log is specified")
8990
if bool(nogo) != bool(out_nogo_validation):
9091
fail("nogo must be specified if and only if out_nogo_validation is specified")
92+
if bool(nogo) != bool(out_nogo_fix_tmp):
93+
fail("nogo must be specified if and only if out_nogo_fix_tmp is specified")
9194
if bool(nogo) != bool(out_nogo_fix):
9295
fail("nogo must be specified if and only if out_nogo_fix is specified")
96+
9397
if cover and go.coverdata:
9498
archives = archives + [go.coverdata]
9599

@@ -223,6 +227,7 @@ def emit_compilepkg(
223227
out_facts = out_facts,
224228
out_log = out_nogo_log,
225229
out_validation = out_nogo_validation,
230+
out_nogo_fix_tmp = out_nogo_fix_tmp,
226231
out_nogo_fix = out_nogo_fix,
227232
nogo = nogo,
228233
)
@@ -241,6 +246,7 @@ def _run_nogo(
241246
out_facts,
242247
out_log,
243248
out_validation,
249+
out_nogo_fix_tmp,
244250
out_nogo_fix,
245251
nogo):
246252
"""Runs nogo on Go source files, including those generated by cgo."""
@@ -250,7 +256,7 @@ def _run_nogo(
250256
[archive.data.facts_file for archive in archives if archive.data.facts_file] +
251257
[archive.data.export_file for archive in archives])
252258
inputs_transitive = [sdk.tools, sdk.headers, go.stdlib.libs]
253-
outputs = [out_facts, out_log, out_nogo_fix]
259+
outputs = [out_facts, out_log, out_nogo_fix_tmp]
254260

255261
args = go.builder_args(go, "nogo", use_path_mapping = True)
256262
args.add_all(sources, before_each = "-src")
@@ -275,7 +281,7 @@ def _run_nogo(
275281
args.add_all(archives, before_each = "-facts", map_each = _facts)
276282
args.add("-out_facts", out_facts)
277283
args.add("-out_log", out_log)
278-
args.add("-fixpath", out_nogo_fix)
284+
args.add("-out_fix", out_nogo_fix_tmp)
279285
args.add("-nogo", nogo)
280286

281287
# This action runs nogo and produces the facts files for downstream nogo actions.
@@ -304,9 +310,12 @@ def _run_nogo(
304310
validation_args.add("nogovalidation")
305311
validation_args.add(out_validation)
306312
validation_args.add(out_log)
313+
validation_args.add(out_nogo_fix_tmp)
314+
validation_args.add(out_nogo_fix)
315+
307316
go.actions.run(
308-
inputs = [out_log],
309-
outputs = [out_validation],
317+
inputs = [out_log, out_nogo_fix_tmp],
318+
outputs = [out_validation, out_nogo_fix],
310319
mnemonic = "ValidateNogo",
311320
executable = go.toolchain._builder,
312321
arguments = [validation_args],

go/private/rules/binary.bzl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,20 @@ def _go_binary_impl(ctx):
148148
executable = executable,
149149
)
150150
validation_output = archive.data._validation_output
151+
nogo_fix_output = archive.data._nogo_fix_output
152+
153+
nogo_validation_outputs = []
154+
if validation_output:
155+
nogo_validation_outputs.append(validation_output)
156+
if nogo_fix_output:
157+
nogo_validation_outputs.append(nogo_fix_output)
151158

152159
providers = [
153160
archive,
154161
OutputGroupInfo(
155162
cgo_exports = archive.cgo_exports,
156163
compilation_outputs = [archive.data.file],
157-
_validation = [validation_output] if validation_output else [],
164+
_validation = nogo_validation_outputs,
158165
),
159166
]
160167

go/private/rules/library.bzl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ def _go_library_impl(ctx):
4848
source = go.library_to_source(go, ctx.attr, library, ctx.coverage_instrumented())
4949
archive = go.archive(go, source)
5050
validation_output = archive.data._validation_output
51-
nogo_fix_output = archive.data._out_nogo_fix
51+
nogo_fix_output = archive.data._nogo_fix_output
52+
53+
nogo_validation_outputs = []
54+
if validation_output:
55+
nogo_validation_outputs.append(validation_output)
56+
if nogo_fix_output:
57+
nogo_validation_outputs.append(nogo_fix_output)
5258

5359
return [
5460
library,
@@ -66,8 +72,7 @@ def _go_library_impl(ctx):
6672
OutputGroupInfo(
6773
cgo_exports = archive.cgo_exports,
6874
compilation_outputs = [archive.data.file],
69-
out_nogo_fix = [nogo_fix_output] if nogo_fix_output else [],
70-
_validation = [validation_output] if validation_output else [],
75+
_validation = nogo_validation_outputs,
7176
),
7277
]
7378

go/private/rules/test.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ def _go_test_impl(ctx):
7575
internal_archive = go.archive(go, internal_source)
7676
if internal_archive.data._validation_output:
7777
validation_outputs.append(internal_archive.data._validation_output)
78+
if internal_archive.data._nogo_fix_output:
79+
validation_outputs.append(internal_archive.data._nogo_fix_output)
80+
7881
go_srcs = [src for src in internal_source.srcs if src.extension == "go"]
7982

8083
# Compile the library with the external black box tests
@@ -95,6 +98,8 @@ def _go_test_impl(ctx):
9598
external_archive = go.archive(go, external_source, is_external_pkg = True)
9699
if external_archive.data._validation_output:
97100
validation_outputs.append(external_archive.data._validation_output)
101+
if external_archive.data._nogo_fix_output:
102+
validation_outputs.append(external_archive.data._nogo_fix_output)
98103

99104
# now generate the main function
100105
repo_relative_rundir = ctx.attr.rundir or ctx.label.package or "."

go/private/sdk.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def _go_download_sdk_impl(ctx):
9191
)
9292

9393
data = ctx.read("versions.json")
94+
ctx.delete("versions.json")
9495
sdks_by_version = _parse_versions_json(data)
9596

9697
if not version:

go/tools/builders/BUILD.bazel

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ go_test(
3131
],
3232
)
3333

34+
go_test(
35+
name = "nogo_change_test",
36+
size = "small",
37+
srcs = [
38+
"difflib.go",
39+
"nogo_change.go",
40+
"nogo_change_serialization.go",
41+
"nogo_change_serialization_test.go",
42+
"nogo_change_test.go",
43+
],
44+
deps = [
45+
"@org_golang_x_tools//go/analysis",
46+
],
47+
)
48+
3449
go_test(
3550
name = "stdliblist_test",
3651
size = "small",
@@ -95,11 +110,11 @@ go_source(
95110
name = "nogo_srcs",
96111
srcs = [
97112
"constants.go",
113+
"difflib.go",
98114
"env.go",
99115
"flags.go",
100116
"nogo_change.go",
101117
"nogo_change_serialization.go",
102-
"nogo_edit.go",
103118
"nogo_main.go",
104119
"nogo_typeparams_go117.go",
105120
"nogo_typeparams_go118.go",

go/tools/builders/builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,6 @@ func main() {
6262
log.SetPrefix(verb + ": ")
6363

6464
if err := action(rest); err != nil {
65-
log.Fatalf("\n$$$$$$$$$$$$$$$$$$$$$$$$ fatal: %+v", err)
65+
log.Fatal(err)
6666
}
6767
}

0 commit comments

Comments
 (0)