Bazel genrule: declared output 'grammar/hello_lexer.go' was not created by genrule

89 Views Asked by At

I am trying to generate lexer and parser for the ANTLR grammar using Bazel. When using genrule, it fails with the error declared output 'grammar/hello_lexer.go' was not created by genrule.

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

BUILD.bazel

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 fails with error

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

But when I manually run the exact command java -Xmx500M -cp grammar/antlr-4.13.1-complete.jar org.antlr.v4.Tool -Dlanguage=Go -no-visitor -package parsing grammar/Hello.g4 directly in shell, it works and generates the right files.

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

What am I doing wrong?

0

There are 0 best solutions below