bazel-gazelle icon indicating copy to clipboard operation
bazel-gazelle copied to clipboard

BUILD files patching broken in gazelle v.0.29.0

Open dpoluyanov opened this issue 1 year ago • 11 comments

What version of gazelle are you using?

v.0.29.0

What version of rules_go are you using?

v.0.37.0

What version of Bazel are you using?

6.0.0

Does this issue reproduce with the latest releases of all the above?

Yes

What operating system and processor architecture are you using?

darwin/aarch64

What did you do?

I'm about to patch BUILD files for github.com/google/gopacket, replacing dynamic cgo link to libpcap with our internal prebuilt binary I've created an repro repository in: https://github.com/dpoluyanov/bazel-gazelle-gh-1381

What did you expect to see?

As from v.0.28.0 I expected to see successfully built com_github_google_gopacket library linked with our internal libpcap binary

What did you see instead?

Starting from v.0.29.0 I'm hitting an error:

ERROR: /Users/dpoluyanov/github.com/dpoluyanov/bazel-gazelle-gh-1381/BUILD.bazel:23:11: //:workspace depends on @com_github_google_gopacket//pcap:pcap in repository @com_github_google_gopacket which failed to fetch. no such package '@com_github_google_gopacket//pcap': Error applying patch /Users/dpoluyanov/github.com/dpoluyanov/bazel-gazelle-gh-1381/third_party/com_github_google_gopacket/2022_04_09_prebuilt_libpcap.patch: Cannot find file to patch (near line 1), old file name (pcap/BUILD.bazel      2022-04-09 21:14:18.000000000 +0300) doesn't exist, new file name (pcap/BUILD.bazel      2022-04-09 21:14:10.000000000 +0300) doesn't exist.

dpoluyanov avatar Jan 23 '23 07:01 dpoluyanov

bisect points me exactly to ce5a7621b6a984d12e7c0ed8d0f04a5662d10c89:

ce5a7621b6a984d12e7c0ed8d0f04a5662d10c89 is the first bad commit
commit ce5a7621b6a984d12e7c0ed8d0f04a5662d10c89
Author: Tyler French <[email protected]>
Date:   Sat Dec 31 14:53:05 2022 -0500

    Use patch from @bazel_tools//tools/build_defs/repo:utils.bzl (#1381)

 internal/go_repository.bzl | 39 +++++++--------------------------------
 repository.md              |  2 +-
 2 files changed, 8 insertions(+), 33 deletions(-)
bisect found first bad commit%                          

dpoluyanov avatar Jan 23 '23 07:01 dpoluyanov

Hey! I was able to resolve this using a patch arg of p1, and refreshing the patch to use temporary a/ and b/ directories. I am not super familiar with how these patching strategies work--maybe someone with more context can help here.

I opened this PR which builds successfully for me https://github.com/dpoluyanov/bazel-gazelle-gh-1381/pull/1

I am still unsure why the patch is failing to apply because the patch call inside gazelle should be occurring after the BUILD files are fully generated.

Let me know if this helps (cc @linzhp)

tyler-french avatar Jan 23 '23 21:01 tyler-french

The other fix to try is to add patch_args = ["-p0"] with the original patch

linzhp avatar Jan 23 '23 22:01 linzhp

patch_args = ["-p0"] doesn't help a lot, but patch_args = ["-p1"] with changes to patch file proposed by @tyler-french let avoid an error and continue gazelle update process

dpoluyanov avatar Jan 24 '23 07:01 dpoluyanov

If -p1 works, it means repository_ctx.patch can see BUILD.bazel files generated by Gazelle (cc. @sluongng). I think this a bug of repository_ctx.patch, which is implemented in Java, that it supports -p1, but not -p0 or no args.

Can you open a bug to Bazel instead?

linzhp avatar Jan 24 '23 18:01 linzhp

I think support for -p0 is there. 🤔

Context:

  • This is where repository_ctx.patch() is implemented https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/bazel/repository/PatchUtil.java;l=494;drc=851c3304b88a3ea5352e387edb7adb7943f84fc8
  • There are a bunch of corresponding tests with strip == 0 here https://cs.opensource.google/bazel/bazel/+/master:src/test/java/com/google/devtools/build/lib/bazel/repository/PatchUtilTest.java;l=138-156;drc=851c3304b88a3ea5352e387edb7adb7943f84fc8

I think @dpoluyanov went down the same rabbit hole as I did previously with a previous customer. We don't have the option of patching Bazel / investigating what was going on in Bazel, so we did the next best thing to unblock us: patching Gazelle with a revert of the PR.

Would love it if somebody could get to the bottom of this pesky bug 🐛

sluongng avatar Jan 25 '23 12:01 sluongng

I see this too, arguments -p1 and -p0 don't help. I can clearly see the files do exist, so I don't understand why Gazelle is upset.

uhthomas avatar Feb 01 '23 15:02 uhthomas

I have a user reporting the same thing today:

//third_party/com_github_containers_image_v5/signature_BUILD_bazel.patch:signature_BUILD_bazel.patch: BUILD file not found in any of the following directories. Add a BUILD file to a directory to mark it as a package.

with this simple patch

--- signature/BUILD.bazel	2023-02-07 15:09:59.149818892 +0100
+++ signature/BUILD.bazel	2023-02-07 15:35:29.219882279 +0100
@@ -20,6 +20,7 @@
         "policy_types.go",
         "simple.go",
     ],
+    cgo = False,
     importpath = "github.com/containers/image/v5/signature",
     importpath_aliases = ["github.com/containers/image/signature"],
     visibility = ["//visibility:public"],

The patch is simple enough, but BUILD file generated by Gazelle was not found

sluongng avatar Feb 07 '23 15:02 sluongng

According to the error message, you should refer to the patch file as //third_party/com_github_containers_image_v5:signature_BUILD_bazel.patch instead of //third_party/com_github_containers_image_v5/signature_BUILD_bazel.patch. You may also need to add a BUILD.bazel file in //third_party/com_github_containers_image_v5.

linzhp avatar Feb 07 '23 15:02 linzhp

I think this bug is reintroduced somehow, and I'm still seeing this in 0.31.0 , changing to -p0 doesn't help as patching the source code works, but not the BUILD.bzl files

thefallentree avatar May 30 '23 16:05 thefallentree

        patch_tool = "patch",

adding this work

thefallentree avatar May 30 '23 16:05 thefallentree