Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO NOT REVIEW] Debug macos issue from #4201 by peng3141 #4212

Closed
wants to merge 12 commits into from
Closed
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use_repo(
"com_github_gogo_protobuf",
"com_github_golang_mock",
"com_github_golang_protobuf",
"com_github_pmezard_go_difflib",
"org_golang_google_genproto",
"org_golang_google_grpc",
"org_golang_google_grpc_cmd_protoc_gen_go_grpc",
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/gogo/protobuf v1.3.2
github.com/golang/mock v1.7.0-rc.1
github.com/golang/protobuf v1.5.3
github.com/pmezard/go-difflib v1.0.0
golang.org/x/net v0.26.0
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
Expand Down
5 changes: 5 additions & 0 deletions go/private/actions/archive.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
out_facts = go.declare_file(go, name = source.name, ext = pre_ext + ".facts")
out_nogo_log = go.declare_file(go, name = source.name, ext = pre_ext + ".nogo.log")
out_nogo_validation = go.declare_file(go, name = source.name, ext = pre_ext + ".nogo")
out_nogo_fix = go.declare_file(go, name = source.name, ext = pre_ext + ".nogo.patch")
else:
out_facts = None
out_nogo_log = None
out_nogo_validation = None
out_nogo_fix = None

direct = source.deps

Expand Down Expand Up @@ -113,6 +115,7 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
out_facts = out_facts,
out_nogo_log = out_nogo_log,
out_nogo_validation = out_nogo_validation,
out_nogo_fix = out_nogo_fix,
nogo = nogo,
out_cgo_export_h = out_cgo_export_h,
gc_goopts = source.gc_goopts,
Expand Down Expand Up @@ -142,6 +145,7 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
out_facts = out_facts,
out_nogo_log = out_nogo_log,
out_nogo_validation = out_nogo_validation,
out_nogo_fix = out_nogo_fix,
nogo = nogo,
gc_goopts = source.gc_goopts,
cgo = False,
Expand Down Expand Up @@ -185,6 +189,7 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
facts_file = out_facts,
runfiles = source.runfiles,
_validation_output = out_nogo_validation,
_nogo_fix_output = out_nogo_fix,
_cgo_deps = cgo_deps,
)
x_defs = dict(source.x_defs)
Expand Down
12 changes: 10 additions & 2 deletions go/private/actions/compilepkg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def emit_compilepkg(
out_facts = None,
out_nogo_log = None,
out_nogo_validation = None,
out_nogo_fix = None,
nogo = None,
out_cgo_export_h = None,
gc_goopts = [],
Expand All @@ -89,6 +90,8 @@ def emit_compilepkg(
fail("nogo must be specified if and only if out_nogo_log is specified")
if have_nogo != (out_nogo_validation != None):
fail("nogo must be specified if and only if out_nogo_validation is specified")
if bool(nogo) != bool(out_nogo_fix):
fail("nogo must be specified if and only if out_nogo_fix is specified")

if cover and go.coverdata:
archives = archives + [go.coverdata]
Expand Down Expand Up @@ -220,6 +223,7 @@ def emit_compilepkg(
out_facts = out_facts,
out_log = out_nogo_log,
out_validation = out_nogo_validation,
out_fix = out_nogo_fix,
nogo = nogo,
)

Expand All @@ -233,6 +237,7 @@ def _run_nogo(
out_facts,
out_log,
out_validation,
out_fix,
nogo):
"""Runs nogo on Go source files, including those generated by cgo."""
sdk = go.sdk
Expand All @@ -241,7 +246,7 @@ def _run_nogo(
[archive.data.facts_file for archive in archives if archive.data.facts_file] +
[archive.data.export_file for archive in archives])
inputs_transitive = [sdk.tools, sdk.headers, go.stdlib.libs]
outputs = [out_facts, out_log]
outputs = [out_facts, out_log, out_fix]

nogo_args = go.tool_args(go)
if cgo_go_srcs:
Expand All @@ -251,6 +256,7 @@ def _run_nogo(
nogo_args.add_all(archives, before_each = "-facts", map_each = _facts)
nogo_args.add("-out_facts", out_facts)
nogo_args.add("-out_log", out_log)
nogo_args.add("-out_fix", out_fix)
nogo_args.add("-nogo", nogo)

# This action runs nogo and produces the facts files for downstream nogo actions.
Expand Down Expand Up @@ -279,8 +285,10 @@ def _run_nogo(
validation_args.add("nogovalidation")
validation_args.add(out_validation)
validation_args.add(out_log)
validation_args.add(out_fix)

go.actions.run(
inputs = [out_log],
inputs = [out_log, out_fix],
outputs = [out_validation],
mnemonic = "ValidateNogo",
executable = go.toolchain._builder,
Expand Down
19 changes: 19 additions & 0 deletions go/private/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,25 @@ def go_rules_dependencies(force = False):
patch_args = ["-p1"],
)

# Needed for nogo to generate unified diff
# releaser:upgrade-dep pmezard go-difflib
wrapper(
http_archive,
name = "com_github_pmezard_go_difflib",
# v1.0.0, latest as of 2024-12-19
urls = [
"https://mirror.bazel.build/github.com/pmezard/go-difflib/archive/refs/tags/v1.0.0.tar.gz",
"https://github.com/pmezard/go-difflib/archive/refs/tags/v1.0.0.tar.gz",
],
sha256 = "28f3dc1b5c0efd61203ab07233f774740d3bf08da4d8153fb5310db6cea0ebda",
strip_prefix = "go-difflib-1.0.0",
patches = [
# releaser:patch-cmd gazelle -repo_root . -go_prefix github.com/pmezard/go-difflib -go_naming_convention import_alias
Label("//third_party:com_github_pmezard_go_difflib-gazelle.patch"),
],
patch_args = ["-p1"],
)

# releaser:upgrade-dep golang sys
wrapper(
http_archive,
Expand Down
2 changes: 2 additions & 0 deletions go/private/rules/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,14 @@ def _go_binary_impl(ctx):
executable = executable,
)
validation_output = archive.data._validation_output
nogo_fix_output = archive.data._nogo_fix_output

providers = [
archive,
OutputGroupInfo(
cgo_exports = archive.cgo_exports,
compilation_outputs = [archive.data.file],
nogo_fix = [nogo_fix_output] if nogo_fix_output else [],
_validation = [validation_output] if validation_output else [],
),
]
Expand Down
2 changes: 2 additions & 0 deletions go/private/rules/library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def _go_library_impl(ctx):
go_info = new_go_info(go, ctx.attr)
archive = go.archive(go, go_info)
validation_output = archive.data._validation_output
nogo_fix_output = archive.data._nogo_fix_output

return [
go_info,
Expand All @@ -65,6 +66,7 @@ def _go_library_impl(ctx):
OutputGroupInfo(
cgo_exports = archive.cgo_exports,
compilation_outputs = [archive.data.file],
nogo_fix = [nogo_fix_output] if nogo_fix_output else [],
_validation = [validation_output] if validation_output else [],
),
]
Expand Down
3 changes: 2 additions & 1 deletion go/private/rules/nogo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def _nogo_impl(ctx):
go,
struct(
embed = [ctx.attr._nogo_srcs],
deps = analyzer_archives,
deps = analyzer_archives + [ctx.attr._go_difflib[GoArchive]],
),
generated_srcs = [nogo_main],
name = go.label.name + "~nogo",
Expand Down Expand Up @@ -102,6 +102,7 @@ _nogo = rule(
),
"_cgo_context_data": attr.label(default = "//:cgo_context_data_proxy"),
"_go_config": attr.label(default = "//:go_config"),
"_go_difflib": attr.label(default = "@com_github_pmezard_go_difflib//difflib:go_default_library"),
"_stdlib": attr.label(default = "//:stdlib"),
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
Expand Down
6 changes: 6 additions & 0 deletions go/private/rules/test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def _go_test_impl(ctx):
)

validation_outputs = []
nogo_fix_outputs = []

# Compile the library to test with internal white box tests
internal_go_info = new_go_info(
Expand All @@ -79,6 +80,8 @@ def _go_test_impl(ctx):
internal_archive = go.archive(go, internal_go_info)
if internal_archive.data._validation_output:
validation_outputs.append(internal_archive.data._validation_output)
if internal_archive.data._nogo_fix_output:
nogo_fix_outputs.append(internal_archive.data._nogo_fix_output)
go_srcs = [src for src in internal_go_info.srcs if src.extension == "go"]

# Compile the library with the external black box tests
Expand All @@ -99,6 +102,8 @@ def _go_test_impl(ctx):
external_archive = go.archive(go, external_go_info, is_external_pkg = True)
if external_archive.data._validation_output:
validation_outputs.append(external_archive.data._validation_output)
if external_archive.data._nogo_fix_output:
nogo_fix_outputs.append(external_archive.data._nogo_fix_output)

# now generate the main function
repo_relative_rundir = ctx.attr.rundir or ctx.label.package or "."
Expand Down Expand Up @@ -207,6 +212,7 @@ def _go_test_impl(ctx):
),
OutputGroupInfo(
compilation_outputs = [internal_archive.data.file],
nogo_fix = nogo_fix_outputs,
_validation = validation_outputs,
),
coverage_common.instrumented_files_info(
Expand Down
14 changes: 14 additions & 0 deletions go/tools/builders/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ go_test(
],
)

go_test(
name = "nogo_fix_test",
size = "small",
srcs = [
"nogo_fix.go",
"nogo_fix_test.go",
],
deps = [
"@com_github_pmezard_go_difflib//difflib:go_default_library",
"@org_golang_x_tools//go/analysis",
],
)

go_test(
name = "stdliblist_test",
size = "small",
Expand Down Expand Up @@ -107,6 +120,7 @@ go_source(
"constants.go",
"env.go",
"flags.go",
"nogo_fix.go",
"nogo_main.go",
"nogo_typeparams_go117.go",
"nogo_typeparams_go118.go",
Expand Down
14 changes: 10 additions & 4 deletions go/tools/builders/nogo.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func nogo(args []string) error {
var deps, facts archiveMultiFlag
var importPath, packagePath, nogoPath, packageListPath string
var testFilter string
var outFactsPath, outLogPath string
var outFactsPath, outLogPath, outFixPath string
var coverMode string
fs.Var(&unfilteredSrcs, "src", ".go, .c, .cc, .m, .mm, .s, or .S file to be filtered and checked")
fs.Var(&ignoreSrcs, "ignore_src", ".go, .c, .cc, .m, .mm, .s, or .S file to be filtered and checked, but with its diagnostics ignored")
Expand All @@ -39,6 +39,8 @@ func nogo(args []string) error {
fs.StringVar(&nogoPath, "nogo", "", "The nogo binary")
fs.StringVar(&outFactsPath, "out_facts", "", "The file to emit serialized nogo facts to")
fs.StringVar(&outLogPath, "out_log", "", "The file to emit nogo logs into")
fs.StringVar(&outFixPath, "out_fix", "", "The path of the file that stores the nogo fixes")

if err := fs.Parse(args); err != nil {
return err
}
Expand Down Expand Up @@ -82,10 +84,10 @@ func nogo(args []string) error {
return err
}

return runNogo(workDir, nogoPath, goSrcs, ignoreSrcs, facts, importPath, importcfgPath, outFactsPath, outLogPath)
return runNogo(workDir, nogoPath, goSrcs, ignoreSrcs, facts, importPath, importcfgPath, outFactsPath, outLogPath, outFixPath)
}

func runNogo(workDir string, nogoPath string, srcs, ignores []string, facts []archive, packagePath, importcfgPath, outFactsPath string, outLogPath string) error {
func runNogo(workDir string, nogoPath string, srcs, ignores []string, facts []archive, packagePath, importcfgPath, outFactsPath, outLogPath, outFixPath string) error {
if len(srcs) == 0 {
// emit_compilepkg expects a nogo facts file, even if it's empty.
// We also need to write the validation output log.
Expand All @@ -97,10 +99,15 @@ func runNogo(workDir string, nogoPath string, srcs, ignores []string, facts []ar
if err != nil {
return fmt.Errorf("error writing empty nogo log file: %v", err)
}
err = os.WriteFile(outFixPath, nil, 0o666)
if err != nil {
return fmt.Errorf("error writing empty nogo fix file: %v", err)
}
return nil
}
args := []string{nogoPath}
args = append(args, "-p", packagePath)
args = append(args, "-fix", outFixPath)
args = append(args, "-importcfg", importcfgPath)
for _, fact := range facts {
args = append(args, "-fact", fmt.Sprintf("%s=%s", fact.importPath, fact.file))
Expand Down Expand Up @@ -148,4 +155,3 @@ func runNogo(workDir string, nogoPath string, srcs, ignores []string, facts []ar
}
return nil
}

Loading