使用new_git_repository规则无法导入带有bazel 0.17.1的第三方非bazel软件包

时间:2019-01-15 09:20:48

标签: bazel

我的工作区

cat WORKSPACE
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository", "new_git_repository")

http_archive(
    name = "io_bazel_rules_go",
    urls = ["https://github.com/bazelbuild/rules_go/releases/download/0.16.5/rules_go-0.16.5.tar.gz"],
    sha256 = "7be7dc01f1e0afdba6c8eb2b43d2fa01c743be1b9273ab1eaf6c233df078d705",
)

load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains")
go_rules_dependencies()
go_register_toolchains()

# ... rocksdb and etc

new_git_repository(
    name = "com_github_tecbot_gorocksdb",
    remote = "https://github.com/tecbot/gorocksdb.git",
    commit = "3e476152774442234f9a9f747386a48a1d82a515",
    build_file = "third-party/gorocksdb.BUILD",
)

和我的gorocksdb.BUILD

cat third-party/gorocksdb.BUILD
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
    name = "go_default_library",
    srcs = [
        "backup.go",
        "cache.go",
        "cf_handle.go",
        # ...
        "write_batch.go",
        "gorocksdb.c",
        "gorocksdb.h",
    ],
    importpath = "github.com/tecbot/gorocksdb",
    cgo = True,
    visibility = ["//visibility:public"],
    cdeps = [
        "@com_github_facebook_rocksdb//:rocksdb",
    ],
)

我使用Bazel containers运行bazel。

docker run -e USER=(id -u) -u=(id -u) -v $PWD:/src/workspace -v /tmp/build_output:/tmp/build_output -w /src/workspace l.gcr.io/google/bazel:0.17.1 --output_user_root=/tmp/build_output run --verbose_failures //:helloworld

我遇到了类似的错误

ERROR: /src/workspace/BUILD.bazel:3:1: no such package '@com_github_tecbot_gorocksdb//': Traceback (most recent call last):
    File "/tmp/build_output/a08c2e4811c846650b733c6fc815a920/external/bazel_tools/tools/build_defs/repo/git.bzl", line 160
        workspace_and_buildfile(ctx)
    File "/tmp/build_output/a08c2e4811c846650b733c6fc815a920/external/bazel_tools/tools/build_defs/repo/utils.bzl", line 60, in workspace_and_buildfile
        ctx.symlink(ctx.attr.build_file, "BUILD.bazel")
Not a regular file: /src/workspace/external/third-party/gorocksdb.BUILD and referenced by '//:helloworld'

我看到bazel的docs,找到了build_file attr的描述。

  

String;可选

     

用作此目录的BUILD文件的文件。   必须指定build_file或build_file_content。

     

此属性是相对于主工作空间的标签。该文件无需命名为BUILD,但可以命名为。 (类似于BUILD.new-repo-name可能会使其与存储库的实际BUILD文件区分开来。)

看来third-party/gorocksdb.BUILD是来自WORKSPACE的正确相对路径,并且可以在较低版本的bazel中使用。我不知道为什么bazel尝试在__workspace_dir__/external/下找到该文件,我真的没有声明任何名为external的东西,这是bazel中的新功能并且没有记录吗?

我确定项目中的所有其他部分都是正确的,因为当我复制gorocksdb.BUILD的全部内容并将其粘贴到build_file_content attr时,效果很好。

1 个答案:

答案 0 :(得分:0)

已通过删除以下行来解决

load(“ @ bazel_tools // tools / build_defs / repo:git.bzl”,“ git_repository”,“ new_git_repository”)

bazel 0.17中的bazel工具中new_git_repository的行为与内置方法不同。