简体   繁体   中英

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 ). 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:

    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. I have tried moving the quotes around about 1,000 different ways and I continue to get the same error.

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:

#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. Try changing echo to 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:

$ 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 . $() command substitution strips the \\n in the intended configuration, but with yours there is still a \\r left over. 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 ).

Try cloning the repository with git from WSL, which will default to \\r line endings. I'm guessing you used a git client from outside WSL, which will default to \\r\\n Windows newlines.

Instead, you might have core.eol set to crlf instead of native in your WSL gitconfig. That seems kind of unusual, but if so just set it to native for either this repository or globally.

https://unix.stackexchange.com/a/383411/415337 has more details about this behavior with command substitution if you're curious.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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