Skip to content

Commit

Permalink
Exposing the value of strict_java_deps flag to Skylark.
Browse files Browse the repository at this point in the history
Fixes bazelbuild#3295.

Users requested this [1] and it is useful for example to pass it as a value to java_common.compile#strict_deps.

[1] https://stackoverflow.com/questions/44827704/bazel-can-a-skylark-action-read-a-command-line-flag-strict-java-deps

PiperOrigin-RevId: 160886151
  • Loading branch information
iirina authored and katre committed Jul 5, 2017
1 parent 94530f5 commit d381624
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,15 @@ public ImmutableList<String> getDefaultJavacFlags() {
return commandLineJavacFlags;
}

@SkylarkCallable(
name = "strict_java_deps",
structField = true,
doc = "The value of the strict_java_deps flag."
)
public String getStrictJavaDepsName() {
return strictJavaDeps.name().toLowerCase();
}

@Override
public void reportInvalidOptions(EventHandler reporter, BuildOptions buildOptions) {
if ((bundleTranslations == TriState.YES) && translationTargets.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,53 @@ public void strictDepsDisabled() throws Exception {
assertThat(directJars).containsExactly("foo/liba.jar", "foo/libb.jar");
}

@Test
public void strictJavaDepsFlagExposed_default() throws Exception {
scratch.file(
"foo/rule.bzl",
"result = provider()",
"def _impl(ctx):",
" return [result(strict_java_deps=ctx.fragments.java.strict_java_deps)]",
"myrule = rule(",
" implementation=_impl,",
" fragments = ['java']",
")"
);
scratch.file(
"foo/BUILD",
"load(':rule.bzl', 'myrule')",
"myrule(name='myrule')"
);
ConfiguredTarget configuredTarget = getConfiguredTarget("//foo:myrule");
SkylarkClassObject skylarkClassObject =
configuredTarget.get(new SkylarkKey(Label.parseAbsolute("//foo:rule.bzl"), "result"));
assertThat(((String) skylarkClassObject.getValue("strict_java_deps"))).isEqualTo("default");
}

@Test
public void strictJavaDepsFlagExposed_error() throws Exception {
scratch.file(
"foo/rule.bzl",
"result = provider()",
"def _impl(ctx):",
" return [result(strict_java_deps=ctx.fragments.java.strict_java_deps)]",
"myrule = rule(",
" implementation=_impl,",
" fragments = ['java']",
")"
);
scratch.file(
"foo/BUILD",
"load(':rule.bzl', 'myrule')",
"myrule(name='myrule')"
);
useConfiguration("--strict_java_deps=ERROR");
ConfiguredTarget configuredTarget = getConfiguredTarget("//foo:myrule");
SkylarkClassObject skylarkClassObject =
configuredTarget.get(new SkylarkKey(Label.parseAbsolute("//foo:rule.bzl"), "result"));
assertThat(((String) skylarkClassObject.getValue("strict_java_deps"))).isEqualTo("error");
}

private static boolean javaCompilationArgsHaveTheSameParent(
JavaCompilationArgs args, JavaCompilationArgs otherArgs) {
if (!nestedSetsOfArtifactHaveTheSameParent(
Expand Down

0 comments on commit d381624

Please sign in to comment.