Ittai
Ittai

Reputation: 5915

Bazel- can a skylark action read a command-line flag (strict_java_deps)

I'm working on implementing a feature like Strict Java Deps for rules_scala.
I'd really like to have the ability to configure in runtime if this uses warn or error.

I seem to recall skylark rules can't create and access command-line flags but I don't recall if they can access existing ones?

Main difference is that existing ones are already parsed so maybe they are also passed in some ctx.

Upvotes: 3

Views: 504

Answers (1)

kris
kris

Reputation: 23592

The flag you want (strict_java_deps) isn't available through Skylark at the moment. There's no reason we can't add it, though, filed #3295 to track.

For other flags, the context can access the configuration fragments, which can access some of the parsed command line flags. I think what you'd want is ctx.fragments, then use the fragments to get the java fragments, and then get the default_javac_flags from that:

# rules.bzl
def _impl(ctx):
  print("flags: %s" % ctx.fragments.java.default_javac_flags)
  ...

frag = rule(
    implementation = _impl,
    fragments = ["java"],  # Declare that this rule uses java fragments
)

Then:

$ bazel build --javacopt="-g:source,lines" :x
WARNING: /home/kchodorow/test/a/tester.bzl:2:3: flags: ["-g:source,lines"].

Upvotes: 2

Related Questions