question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bazel fails to patch external repos on macOS 13

See original GitHub issue

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: https://github.com/bazelbuild/bazel/blob/b422719f35b1ae5ed5f7f83d05cd778e9c58d793/tools/build_defs/repo/utils.bzl#L139

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
linzhpcommented, 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.

0reactions
keithcommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

patching sources of external repo - Google Groups
to bazel-discuss. Hi,. I have an external repo downloading and compiling just fine. Problem is I need to apply a patch to it...
Read more >
Installing Bazel on Ubuntu
On this page · Step 1: Add Bazel distribution URI as a package source · Using Bazel's apt repository. Step 2: Install and...
Read more >
How to run mediapipe with c++ on M1 mac - Stack Overflow
In root repository "mediapipe", fix ".bazelversion" file as following. 4.1.0. Then, retried lesson 6 and the download started. bug.
Read more >
MediaPipe install fails on OS X - gr33nonline - WordPress.com
usually indicates that Bazel fails to download necessary dependency repositories that MediaPipe needs. MedaiPipe has several dependency ...
Read more >
Bazel build fails with "" errors [126764883] - Visible to Public
[exec] ERROR: /mnt/d/studio-master-dev/tools/base/bazel/repositories.bzl:206:13: //external:android/crosstool depends on @androidndk//:default_crosstool in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found