Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single-jar pack_sources should conform to Bazel behavior #1378

Merged
merged 10 commits into from
Apr 6, 2022
30 changes: 20 additions & 10 deletions scala/private/phases/phase_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -309,16 +309,26 @@ def _create_scala_compilation_provider(ctx, ijar, source_jar, deps_providers):
)

def _pack_source_jar(ctx, scala_srcs, in_srcjars):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was confused a bit at the beginning as in_srcjars implies a bool, but looks like it's meant to be a list of src jars.

so nit possibly to rename it scala_src_jars. feel free to do so as a follow up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, though we should rename to input_srcjars imo (as followup).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sgtm. thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too lazy for followup. Renamed it and also changed the .-src.jar naming into -src.jar.

output_jar = ctx.outputs.jar
source_jar_name = output_jar.basename[:-len(output_jar.extension)] + "-src.jar"
output_source_jar = ctx.actions.declare_file(source_jar_name, sibling = output_jar)
return java_common.pack_sources(
ctx.actions,
output_source_jar = output_source_jar,
sources = scala_srcs,
source_jars = in_srcjars,
java_toolchain = find_java_toolchain(ctx, ctx.attr._java_toolchain),
)
# https://github.com/bazelbuild/bazel/blob/ff6c0333e4f957bb9f7ab5401b01dbf3e9b515b1/src/main/java/com/google/devtools/build/lib/rules/java/JavaInfoBuildHelper.java#L180-L183
# java_common.pack_sources checks for no srcs and only a single input jar
# if so, it checks that output_source_jar is null
# passing that, it will return the input source jar directly
# However, pack_sources will FAIL if both output_source_jar and
# the deprecated output_jar field are BOTH null
# Therefore, we can return the single input jar ourselves
if not scala_srcs and len(in_srcjars) == 1:
return in_srcjars[0]
else:
output_jar = ctx.outputs.jar
source_jar_name = output_jar.basename[:-len(output_jar.extension)] + "-src.jar"
output_source_jar = ctx.actions.declare_file(source_jar_name, sibling = output_jar)
return java_common.pack_sources(
ctx.actions,
output_source_jar = output_source_jar,
sources = scala_srcs,
source_jars = in_srcjars,
java_toolchain = find_java_toolchain(ctx, ctx.attr._java_toolchain),
)

def _try_to_compile_java_jar(
ctx,
Expand Down