代码之家  ›  专栏  ›  技术社区  ›  JackDaniels

Bazel genrule:声明的输出“grammar/hello_lexer.go”不是由genrule创建的

  •  0
  • JackDaniels  · 技术社区  · 2 年前

    我正在尝试使用Bazel为ANTLR语法生成lexer和解析器。使用genrule时,它失败并出现错误 declared output 'grammar/hello_lexer.go' was not created by genrule.

    ls grammar 
    BUILD.bazel         Hello.g4            antlr-4.13.1-complete.jar
    

    BUILD.ZEL

    load("@rules_java//java:defs.bzl", "java_library", "java_import")
    
    java_import(
        name = "antlr_jar",
        jars = ["antlr-4.13.1-complete.jar"],
    )
    
    genrule(
        name = "generate_parser",
        srcs = glob(["Hello.g4"]),
    
        tools = [":antlr_jar"],
        cmd = "java -Xmx500M -cp $(location :antlr_jar) org.antlr.v4.Tool -Dlanguage=Go -no-visitor -package parsing  $(SRCS)",
        outs = ["hello_lexer.go"],
    )
    

    bazel build 出现错误而失败

    bazel build --subcommands --verbose_failures --sandbox_debug //grammar:generate_parser
    INFO: Analyzed target //grammar:generate_parser (0 packages loaded, 0 targets configured).
    SUBCOMMAND: # //grammar:generate_parser [action 'Executing genrule //grammar:generate_parser', configuration: 7baf2462b8dc082d7868aad895e80c01f6d9dba247a3296e4fd6dd318f4433c1, execution platform: @@local_config_platform//:host, mnemonic: Genrule]
    (cd /private/var/tmp/_bazel_jp/64463e3d7652188cb285edbcf54b686c/execroot/_main && \
      exec env - \
        PATH=/Users/jp/Library/Caches/bazelisk/downloads/sha256/b1cf1c5783fa3eac60942bf2ec6169cfccecd2fa5a355587d8d35983bc1e1310/bin:/opt/homebrew/opt/openjdk/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/Library/Frameworks/Python.framework/Versions/3.12/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/opt/homebrew/opt/coreutils/libexec/gnubin:/usr/local/go/bin:/Applications/GoLand.app/Contents/MacOS:/Users/jp/go/bin/ \
      /bin/bash -c 'source external/bazel_tools/tools/genrule/genrule-setup.sh; java -Xmx500M -cp grammar/antlr-4.13.1-complete.jar org.antlr.v4.Tool -Dlanguage=Go -no-visitor -package parsing  grammar/Hello.g4')
    # Configuration: 7baf2462b8dc082d7868aad895e80c01f6d9dba247a3296e4fd6dd318f4433c1
    # Execution platform: @@local_config_platform//:host
    ERROR: /Users/jp/src/fizzbee/grammar/BUILD.bazel:8:8: declared output 'grammar/hello_lexer.go' was not created by genrule. This is probably because the genrule actually didn't create this output, or because the output was a directory and the genrule was run remotely (note that only the contents of declared file outputs are copied from genrules run remotely)
    ERROR: /Users/jp/src/fizzbee/grammar/BUILD.bazel:8:8: Executing genrule //grammar:generate_parser failed: not all outputs were created or valid
    Target //grammar:generate_parser failed to build
    INFO: Elapsed time: 0.481s, Critical Path: 0.35s
    INFO: 2 processes: 1 internal, 1 darwin-sandbox.
    ERROR: Build did NOT complete successfully
    

    但是当我手动运行确切的命令时 java -Xmx500M -cp grammar/antlr-4.13.1-complete.jar org.antlr.v4.Tool -Dlanguage=Go -no-visitor -package parsing grammar/Hello.g4 它直接在shell中工作并生成正确的文件。

    ls grammar/ 
    BUILD.bazel         Hello.tokens            antlr-4.13.1-complete.jar   hello_listener.go
    Hello.g4            HelloLexer.interp       hello_base_listener.go      hello_parser.go
    Hello.interp            HelloLexer.tokens       hello_lexer.go
    

    我做错了什么?

    0 回复  |  直到 2 年前