douba9425
2018-03-25 15:06
采纳率: 100%
浏览 192
已采纳

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

I'm on MacOS 10.13.2. Go 1.10. bazel 0.11.1

I need to compile a repo that has 2 projects (project1 and project2). project1 has 2 subpacakges. p1lib and dep1 p1lib uses dep1.

I generate BUILD files with gazelle, files look ok.

gazelle -go_prefix=github.com/BazelBuildForGo

But when I run build I get an error that says that I'm missing direct dependency.

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

My project can be found here https://github.com/wix-playground/BazelBuildForGo

图片转代码服务由CSDN问答提供 功能建议

我使用的是MacOS 10.13.2。 Go 1.10。 bazel 0.11.1

我需要编译一个具有2个项目(project1和project2)的存储库。 project1具有2个子封装。 p1lib和dep1 p1lib使用dep1。

我用瞪羚生成了BUILD文件,这些文件看起来还不错。

 瞪羚-go_prefix = github.com / BazelBuildForGo 
   
 
 

但是当我运行build时出现错误 表示我缺少直接依赖项。

  bazel build // project1 
INFO:分析目标// project1:project1(已加载3个程序包)。
INFO:找到1个目标 ... 
错误:/private/var/tmp/_bazel_user1/df78026a5ee0c7ed3d23dd05c3a3b1f7/external/com_github_wix_private_bazelbuildforgo/project1/p1lib/BUILD.bazel:3:1:GoCompileternal / com_github_library / com_github_library / com_github_library / com_github_library / com_github_library / library  /BazelBuildForGo/project1/p1lib.a失败(出口1)
2018 / 03/25 18:02:55缺少严格的依赖项:
 external / com_github_wix_private_bazelbuildforgo / project1 / p1lib / p1lib.go:导入github.com/wix-  private / BazelBuildForGo / project1 / dep1,这不是直接依赖项
Target // project1:project1无法构建
使用--verbose_failures可以查看失败的构建步骤的命令行。
INFO:经过时间:0.358s,严重 al路径:0.12s 
失败:构建未成功完成
   
 
 

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongxun8189 2018-03-26 15:31
    已采纳

    I think the problem is that the import prefix you passed to Gazelle on the command line (github.com/BazelBuildForGo) is different from the imports in the .go files (github.com/wix-private/BazelBuildForGo). When Gazelle sees imports that are outside the current prefix, it will generate external dependencies for those imports, and those dependencies will be missing:

    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"],
    )
    

    The fix for this is pretty easy though. Just run Gazelle with the prefix github.com/wix-private/BazelBuildForGo. You actually already have this in //:gazelle, so just run that, then rebuild.

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

    That will change the go_library rule above to this:

    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"],
    )
    
    打赏 评论

相关推荐 更多相似问题