简体   繁体   English

使用 new_git_repository 规则时无法使用 bazel 0.17.1 导入第三方非 bazel 包

[英]Failed to import third-part non-bazel package with bazel 0.17.1 while using new_git_repository rule

My Workspace我的工作区

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

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

I run bazel using Bazel containers .我使用Bazel 容器运行 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

And I got errors like我得到了类似的错误

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'

I see the docs of bazel, and found the description of build_file attr.我看到了 bazel 的文档,并找到了build_file attr 的描述。

String;细绳; optional可选的

A file to use as a BUILD file for this directory.用作此目录的 BUILD 文件的文件。 Either build_file or build_file_content must be specified.必须指定 build_file 或 build_file_content。

This attribute is a label relative to the main workspace.此属性是相对于主工作区的标签。 The file does not need to be named BUILD, but can be.该文件不需要命名为 BUILD,但可以。 (Something like BUILD.new-repo-name may work well for distinguishing it from the repository's actual BUILD files.) (像 BUILD.new-repo-name 这样的东西可以很好地将它与存储库的实际 BUILD 文件区分开来。)

It seems like that third-party/gorocksdb.BUILD is a correct relative path from WORKSPACE, and it works in some lower version bazel.似乎third-party/gorocksdb.BUILD是来自 WORKSPACE 的正确相对路径,它适用于一些较低版本的 bazel。 I don't know why bazel try to find this file under __workspace_dir__/external/ , I really not declare anything named external, is it a new feature in bazel and not documented?我不知道为什么 bazel 试图在__workspace_dir__/external/下找到这个文件,我真的没有声明任何名为 external 的东西,它是 bazel 中的一个新功能并且没有记录吗?

I'm sure all other parts in my project is correct because when I copy the whole content of gorocksdb.BUILD and paste it with build_file_content attr, it works very well.我确信我的项目中的所有其他部分都是正确的,因为当我复制gorocksdb.BUILD的全部内容并将其粘贴到build_file_content attr 时,它工作得很好。

I resolved it by removing the following line我通过删除以下行来解决它

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

The behavior of new_git_repository in bazel tools in bazel 0.17 is not the same as the builtin method. bazel 0.17 中 bazel 工具中 new_git_repository 的行为与内置方法不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM