简体   繁体   English

使用 bazel 构建 Envoy 时,genrule cmd 会导致“缺少终止 '”' 字符

[英]genrule cmd causes "Missing Terminating '"' character when building Envoy w/ bazel

I am trying to compile an open source project called Envoy using Bazel (located here: https://github.com/envoyproxy/envoy ).我正在尝试使用 Bazel(位于此处: https : //github.com/envoyproxy/envoy )编译一个名为 Envoy 的开源项目。 I am getting this error message when trying to run the build script:尝试运行构建脚本时收到此错误消息:

在此处输入图片说明

The genrule code causing this issue is located in the file source/common/version/BUILD and looks like this:导致此问题的规则代码位于文件 source/common/version/BUILD 中,如下所示:

    genrule(
    name = "generate_version_number",
    srcs = ["//:VERSION"],
    outs = ["version_number.h"],
    cmd = """echo "#define BUILD_VERSION_NUMBER \\"$$(cat $<)\\"" >$@""",
    visibility = ["//visibility:private"],
)

The error "missing terminating '"' is coming from somewhere in that cmd argument to genrule, but I can't figure it out.错误“缺少终止 '”' 来自 cmd 参数中的某个地方给 genrule,但我无法弄清楚。 I have tried moving the quotes around about 1,000 different ways and I continue to get the same error.我曾尝试以大约 1,000 种不同的方式移动引号,但仍然出现相同的错误。

It looks like the error is saying that there are two missing terminating quotes: on line 1, and on line 2. So the file probably looks like this:看起来错误是说缺少两个终止引号:第 1 行和第 2 行。因此文件可能如下所示:

#define BUILD_VERSION_NUMBER "1.20.0-dev
"

The VERSION file doesn't appear to have a new line at the end, so what I guess is happening is that echo is adding a newline. VERSION文件最后似乎没有换行,所以我猜正在发生的是echo添加了一个换行符。 Try changing echo to echo -n .尝试将echo更改为echo -n

What's kind of strange though is that I can see the same behavior (echo printing a newline at the end) on the terminal in linux, but not within a bazel build:有点奇怪的是,我可以在 linux 的终端上看到相同的行为(在末尾回显打印换行符),但不能在 bazel 构建中看到:

$ echo -n 1.0.0 > version.txt

$ hexdump -C version.txt 
00000000  31 2e 30 2e 30                                    |1.0.0|
00000005

$ echo "$(cat version.txt)" > txt ; hexdump -C txt
00000000  31 2e 30 2e 30 0a                                 |1.0.0.|
00000006

$ echo -n "$(cat version.txt)" > txt ; hexdump -C txt
00000000  31 2e 30 2e 30                                    |1.0.0|
00000005

$ cat BUILD
genrule(
    name = "generate_version_number",
    srcs = ["version.txt"],
    outs = ["version_number.h"],
    cmd = """echo "#define BUILD_VERSION_NUMBER \\"$$(cat $<)\\"" >$@""",
    visibility = ["//visibility:private"],
)

$ bazel build version_number.h
INFO: Analyzed target //:version_number.h (5 packages loaded, 9 targets configured).
INFO: Found 1 target...
Target //:version_number.h up-to-date:
  bazel-bin/version_number.h
INFO: Elapsed time: 0.236s, Critical Path: 0.01s
INFO: 2 processes: 1 internal, 1 linux-sandbox.
INFO: Build completed successfully, 2 total actions

$ cat bazel-bin/version_number.h
#define BUILD_VERSION_NUMBER "1.0.0"

So not sure why offhand.所以不知道为什么会手忙脚乱。

I'm guessing you have git configured to translate the \\n line ending in VERSION to \\r\\n .我猜你已经将 git 配置为将以VERSION结尾的\\n行转换为\\r\\n $() command substitution strips the \\n in the intended configuration, but with yours there is still a \\r left over. $()命令替换删除了预期配置中的\\n ,但您的配置中仍然有一个\\r剩余。 You can check by looking at how many bytes the VERSION file has, it should be exactly the number of printable characters + 1 (11 right now for 1.21.0-dev ).您可以通过查看VERSION文件有多少字节来检查,它应该正好是可打印字符数 + 1(现在为1.21.0-dev为 11)。

Try cloning the repository with git from WSL, which will default to \\r line endings.尝试使用 WSL 中的 git 克隆存储库,这将默认为\\r行结尾。 I'm guessing you used a git client from outside WSL, which will default to \\r\\n Windows newlines.我猜你使用了 WSL 外部的 git 客户端,默认为\\r\\n Windows 换行符。

Instead, you might have core.eol set to crlf instead of native in your WSL gitconfig.相反,你可能有core.eol设置为crlf ,而不是native在WSL gitconfig。 That seems kind of unusual, but if so just set it to native for either this repository or globally.这看起来有点不寻常,但如果是这样,只需将此存储库或全局设置native

https://unix.stackexchange.com/a/383411/415337 has more details about this behavior with command substitution if you're curious.如果您好奇, https://unix.stackexchange.com/a/383411/415337 会提供有关此行为的更多详细信息以及命令替换。

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

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