代码之家  ›  专栏  ›  技术社区  ›  AlexS

当项目具有内部依赖关系时,bazel golang构建失败

  •  4
  • AlexS  · 技术社区  · 7 年前

    我在MacOS 10.13.2上。 转到1.10。 bazel 0.11.1

    我需要编制一份包含2个项目(project1和project2)的回购协议。 项目1有2个子主题。p1lib和dep1 p1lib使用dep1。

    我用gazelle生成构建文件,文件看起来不错。

    gazelle -go_prefix=github.com/BazelBuildForGo
    

    但当我运行build时,我得到一个错误,表明我缺少直接依赖关系。

    bazel build //project1
    INFO: Analysed target //project1:project1 (3 packages loaded).
    INFO: Found 1 target...
    ERROR: /private/var/tmp/_bazel_user1/df78026a5ee0c7ed3d23dd05c3a3b1f7/external/com_github_wix_private_bazelbuildforgo/project1/p1lib/BUILD.bazel:3:1: GoCompile external/com_github_wix_private_bazelbuildforgo/project1/p1lib/darwin_amd64_stripped/go_default_library~/github.com/BazelBuildForGo/project1/p1lib.a failed (Exit 1)
    2018/03/25 18:02:55 missing strict dependencies:
        external/com_github_wix_private_bazelbuildforgo/project1/p1lib/p1lib.go: import of github.com/wix-private/BazelBuildForGo/project1/dep1, which is not a direct dependency
    Target //project1:project1 failed to build
    Use --verbose_failures to see the command lines of failed build steps.
    INFO: Elapsed time: 0.358s, Critical Path: 0.12s
    FAILED: Build did NOT complete successfully
    

    我的项目可以在这里找到 https://github.com/wix-playground/BazelBuildForGo

    1 回复  |  直到 7 年前
        1
  •  1
  •   Jay Conrod    7 年前

    我认为问题是您在命令行上传递给Gazelle的导入前缀( github.com/BazelBuildForGo )与中的进口不同。go文件( github.com/wix-private/BazelBuildForGo )。当Gazelle看到当前前缀之外的导入时,它将为这些导入生成外部依赖项,并且这些依赖项将丢失:

    go_library(
        name = "go_default_library",
        srcs = ["p1lib.go"],
        importpath = "github.com/BazelBuildForGo/project1/p1lib",
        visibility = ["//visibility:public"],
        deps = ["@com_github_wix_private_bazelbuildforgo//project1/dep1:go_default_library"],
    )
    

    不过,解决这个问题相当容易。只需运行带有前缀的Gazelle github。com/wix private/BazelBuildForGo 。实际上你已经有这个了 //:gazelle ,所以只需运行它,然后重建。

    $ bazel run //:gazelle
    $ bazel build //...
    

    这将改变 go_library 上述规则:

    go_library(
        name = "go_default_library",
        srcs = ["p1lib.go"],
        importpath = "github.com/wix-private/BazelBuildForGo/project1/p1lib",
        visibility = ["//visibility:public"],
        deps = ["//project1/dep1:go_default_library"],
    )