Bazel对依赖库进行patch遇到的问题
Tag bazel, patch, 依赖, on by view 22

最近使用bazel需要对一个go第三方依赖进行patch,这个依赖是gorm/v1,原作者对v1已经放弃了,我们项目中使用了v1,但是因为v1到v2是不兼容的升级,所以我们项目中无法升级。针对v1的优化和bug修复,就只能使用patch的方式进行了。

第一步将github.com/jinzhu/gorm克隆下来,check出最后一个版本v1.9.16,然后做修改。修改完毕之后,不要进行git提交,执行git diff >> new1.patch将会生成这次变更的patch文件。

将patch文件存储如下0001-go-errors.patch

third_party_patches
└── com_github_jinzhu_gorm
    ├── 0001-go-errors.patch
    ├── 0002-fix-sqlite.patch
    └── BUILD.bazel

其中,BUILD.bazel空文件即可。另外deps.bzl中的引用如下

go_repository(
    name = "com_github_jinzhu_gorm",
    build_file_proto_mode = "disable",
    importpath = "github.com/jinzhu/gorm",
    patch_args = ["-p1"],
    patches = [
        "//third_party_patches/com_github_jinzhu_gorm:0001-go-errors.patch",
        "//third_party_patches/com_github_jinzhu_gorm:0002-fix-sqlite.patch",
    ],
    sum = "h1:+IyIjPEABKRpsu/F8OvDPy9fyQlgsg2luMV2ZIH5i5o=",
    version = "v1.9.16",
)

但是我编译的时候,却报错了

➜  code git:(feature/migrate-flow) ✗ bazel build //...
INFO: Analyzed 74 targets (3 packages loaded, 141 targets configured).
INFO: Found 74 targets...
ERROR: /root/.cache/bazel/_bazel_root/aa05a35ce6b6cd2ce6b5a504fd9d9e22/external/com_github_jinzhu_gorm/BUILD.bazel:3:11: GoCompilePkg external/com_github_jinzhu_gorm/gorm.a failed: (Exit 1): builder failed: error executing command bazel-out/host/bin/external/go_sdk/builder compilepkg -sdk external/go_sdk -installsuffix linux_amd64 -tags sqlite -src external/com_github_jinzhu_gorm/association.go -src ... (remaining 71 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
compilepkg: missing strict dependencies:
        /root/.cache/bazel/_bazel_root/aa05a35ce6b6cd2ce6b5a504fd9d9e22/sandbox/linux-sandbox/4877/execroot/ias-admin/external/com_github_jinzhu_gorm/main.go: import of "github.com/go-errors/errors"
No dependencies were provided.
Check that imports in Go sources match importpath attributes in deps.
INFO: Elapsed time: 0.843s, Critical Path: 0.06s
INFO: 4 processes: 4 internal.
FAILED: Build did NOT complete successfully

按照报错打开bazel cache路径,发现bazel为依赖生成的BUILD.bazel文件中,并没有添加我patch中引入的依赖。于是我将cache中的BUILD.bazel拷贝出来,手动加入依赖,然后git diff,得到BUILD.bazel的patch文件如下


diff --git a/BUILD.bazel b/BUILD.bazel
index 0849476..d26e9f7 100644
--- a/BUILD.bazel
+++ b/BUILD.bazel
@@ -32,7 +32,10 @@ go_library(
     ],
     importpath = "github.com/jinzhu/gorm",
     visibility = ["//visibility:public"],
-    deps = ["@com_github_jinzhu_inflection//:inflection"],
+    deps = [
+        "@com_github_jinzhu_inflection//:inflection",
+        "@com_github_go_errors_errors//:go_default_library",
+    ],
 )
 
 alias(
@@ -76,5 +79,6 @@ go_test(
         "//dialects/sqlite",
         "@com_github_erikstmartin_go_testdb//:go-testdb",
         "@com_github_jinzhu_now//:now",
+        "@com_github_go_errors_errors//:go_default_library",
     ],
 )

加入到我们的patch中,然后再次编译,就正常了。

我推断bazel patch这里面的逻辑是,先拉下原始包,第二步生成BUILD.bazel,最后再patch。google的研发bazel的人简直就是没长脑子,这样的步骤我还要它生成BUILD.bazel干啥,还不如直接把BUILD.bazel生成到patch文件里。总之,折腾了许久总算解决了,记录一下。