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

Bazel fails to patch external repos on macOS 13 #16550

Closed
linzhp opened this issue Oct 25, 2022 · 6 comments
Closed

Bazel fails to patch external repos on macOS 13 #16550

linzhp opened this issue Oct 25, 2022 · 6 comments
Assignees
Labels
P2 We'll consider working on this in future. (Assignee optional) team-OSS Issues for the Bazel OSS team: installation, release processBazel packaging, website type: bug

Comments

@linzhp
Copy link
Contributor

linzhp commented Oct 25, 2022

Description of the bug:

Bazel uses local patch command by default to apply patches to external repositories. macOS 13 downgraded the system patch command from 2.5.8 to 2.0, causing many patches in Bazel repositories failed to apply:

On macOS 12:

$ patch --version
patch 2.5.8
Copyright (C) 1988 Larry Wall
Copyright (C) 2002 Free Software Foundation, Inc.

This program comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of this program
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.

written by Larry Wall and Paul Eggert
$ which -a patch
/usr/bin/patch

On macOS 13:

$ patch --version
patch 2.0-12u11-Apple
$ which -a patch
/usr/bin/patch

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

  1. Create a workspace with the following files
-- BUILD.bazel --
-- WORKSPACE --

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "io_bazel_rules_go",
    sha256 = "099a9fb96a376ccbbb7d291ed4ecbdfd42f6bc822ab77ae6f1b5cb9e914e94fa",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip",
        "https://github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip",
    ],
)

http_archive(
    name = "bazel_gazelle",
    sha256 = "efbbba6ac1a4fd342d5122cbdfdb82aeb2cf2862e35022c752eaddffada7c3f3",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.27.0/bazel-gazelle-v0.27.0.tar.gz",
        "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.27.0/bazel-gazelle-v0.27.0.tar.gz",
    ],
)


load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")

go_rules_dependencies()

go_register_toolchains(version = "1.18.3")

gazelle_dependencies()

go_repository(
    name = "com_github_gogo_protobuf",
    build_directives = [
        "gazelle:go_generate_proto false",
    ],
    importpath = "github.com/gogo/protobuf",
    patch_args = ["-p1"],
    patches = [
        "//patches:gogo-protobuf-exports-files.patch",
    ],
    sum = "h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=",
    version = "v1.3.2",
)
-- patches/BUILD.bazel --
-- patches/gogo-protobuf-exports-files.patch --
diff -urN a/gogoproto/BUILD.bazel b/gogoproto/BUILD.bazel
--- a/gogoproto/BUILD.bazel	2020-05-26 15:48:27.000000000 -0700
+++ b/gogoproto/BUILD.bazel	2020-05-26 16:06:33.000000000 -0700
@@ -22,3 +22,5 @@
         "//protoc-gen-gogo/descriptor:go_default_library",
     ],
 )
+
+exports_files(["gogo.proto"])
  1. Run the following command:
bazel build @com_github_gogo_protobuf//gogoproto:go_default_library
INFO: Repository com_github_gogo_protobuf instantiated at:
  /private/tmp/patch_example/WORKSPACE:38:14: in <toplevel>
Repository rule go_repository defined at:
  /private/var/tmp/_bazel_zplin/16dd8b85f2d87562bbf9b6bf72816565/external/bazel_gazelle/internal/go_repository.bzl:313:32: in <toplevel>
ERROR: An error occurred during the fetch of repository 'com_github_gogo_protobuf':
   Traceback (most recent call last):
	File "/private/var/tmp/_bazel_zplin/16dd8b85f2d87562bbf9b6bf72816565/external/bazel_gazelle/internal/go_repository.bzl", line 311, column 10, in _go_repository_impl
		patch(ctx)
	File "/private/var/tmp/_bazel_zplin/16dd8b85f2d87562bbf9b6bf72816565/external/bazel_gazelle/internal/go_repository.bzl", line 563, column 17, in patch
		fail("Error applying patch %s:\n%s%s" %
Error in fail: Error applying patch //patches:gogo-protobuf-exports-files.patch:
patching file 'gogoproto/BUILD.bazel'
1 out of 1 hunks failed--saving rejects to 'gogoproto/BUILD.bazel.rej'
ERROR: /private/tmp/patch_example/WORKSPACE:38:14: fetching go_repository rule //external:com_github_gogo_protobuf: Traceback (most recent call last):
	File "/private/var/tmp/_bazel_zplin/16dd8b85f2d87562bbf9b6bf72816565/external/bazel_gazelle/internal/go_repository.bzl", line 311, column 10, in _go_repository_impl
		patch(ctx)
	File "/private/var/tmp/_bazel_zplin/16dd8b85f2d87562bbf9b6bf72816565/external/bazel_gazelle/internal/go_repository.bzl", line 563, column 17, in patch
		fail("Error applying patch %s:\n%s%s" %
Error in fail: Error applying patch //patches:gogo-protobuf-exports-files.patch:
patching file 'gogoproto/BUILD.bazel'
1 out of 1 hunks failed--saving rejects to 'gogoproto/BUILD.bazel.rej'
ERROR: Error applying patch //patches:gogo-protobuf-exports-files.patch:
patching file 'gogoproto/BUILD.bazel'
1 out of 1 hunks failed--saving rejects to 'gogoproto/BUILD.bazel.rej'

Which operating system are you running Bazel on?

macOS 13

What is the output of bazel info release?

release 5.3.1

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

No response

Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

Manually install newer version of gpatch with brew install gpatch works. However, Bazel should not rely on the locally installed patch command. Instead, it should download either the source code or binary for GNU patch and use that when users don't provide patch_tool:

patch_tool = "patch"

@ShreeM01
Copy link
Contributor

Hi @linzhp! It would be really helpful if you can provide a minimal reproduce case with Sample code, Thank you!

@keith
Copy link
Member

keith commented Oct 25, 2022

Repro would def be nice, I've been using macOS 13 for a while with repo patches without issue. At some point (not sure when) bazel started using an internal patch command, (maybe only in some cases?), so maybe somehow that difference is the key

@linzhp
Copy link
Contributor Author

linzhp commented Oct 26, 2022

Updated the ticket with repro

@ShreeM01 ShreeM01 added team-Rules-CPP Issues for C++ rules and removed more data needed labels Oct 26, 2022
@keith
Copy link
Member

keith commented Oct 27, 2022

Looking at this a bit it seems that macOS 13's version of patch is just more strict about the patch content than gpatch. The problem is your patch content doesn't appear to match what is generated in the BUILD file in that example before the patch:

% cat bazel-out/../external/com_github_gogo_protobuf/gogoproto/BUILD.bazel
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

proto_library(
    name = "gogoproto_proto",
    srcs = ["gogo.proto"],
    visibility = ["//visibility:public"],
    deps = ["@com_google_protobuf//:descriptor_proto"],
)

go_library(
    name = "gogoproto",
    srcs = [
        "doc.go",
        "gogo.pb.go",
        "helper.go",
    ],
    importpath = "github.com/gogo/protobuf/gogoproto",
    visibility = ["//visibility:public"],
    deps = [
        "//proto",
        "//protoc-gen-gogo/descriptor",
    ],
)

alias(
    name = "go_default_library",
    actual = ":gogoproto",
    visibility = ["//visibility:public"],
)

If you update the patch to have less content:

diff -urN a/gogoproto/BUILD.bazel b/gogoproto/BUILD.bazel
--- a/gogoproto/BUILD.bazel	2020-05-26 15:48:27.000000000 -0700
+++ b/gogoproto/BUILD.bazel	2020-05-26 16:06:33.000000000 -0700
@@ -30,0 +31,2 @@
+
+exports_files(["gogo.proto"])

Or to have the new correct content:

diff -urN a/gogoproto/BUILD.bazel b/gogoproto/BUILD.bazel
--- a/gogoproto/BUILD.bazel	2020-05-26 15:48:27.000000000 -0700
+++ b/gogoproto/BUILD.bazel	2020-05-26 16:06:33.000000000 -0700
@@ -28,3 +28,5 @@
     actual = ":gogoproto",
     visibility = ["//visibility:public"],
 )
+
+exports_files(["gogo.proto"])

Things do work as expected. I'm not sure what bazel should do about this case in general though.

@linzhp
Copy link
Contributor Author

linzhp commented Oct 27, 2022

I think in general, Bazel should not call local patch command. It should download and manage the version of patch command. We also run into some CI containers where patch command is not available at all.

@oquenchil oquenchil added team-Local-Exec Issues and PRs for the Execution (Local) team and removed team-Rules-CPP Issues for C++ rules labels Jan 23, 2023
@larsrc-google larsrc-google added team-OSS Issues for the Bazel OSS team: installation, release processBazel packaging, website and removed team-Local-Exec Issues and PRs for the Execution (Local) team labels Jan 24, 2023
@meteorcloudy meteorcloudy self-assigned this Mar 9, 2023
@meteorcloudy meteorcloudy added P2 We'll consider working on this in future. (Assignee optional) and removed untriaged labels Mar 9, 2023
@meteorcloudy
Copy link
Member

meteorcloudy commented Mar 9, 2023

In Bazel, we do use a native patch implementation by default and exposes this as an API for Starlark rules. I believe this is a problem in the go_repository implementation and is already fixed by bazel-contrib/bazel-gazelle@ce5a762.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 We'll consider working on this in future. (Assignee optional) team-OSS Issues for the Bazel OSS team: installation, release processBazel packaging, website type: bug
Projects
None yet
Development

No branches or pull requests

6 participants