Skip to content

unify all of the default info phases implementations #958

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scala/private/phases/phase_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def _phase_compile(
class_jar = out.class_jar,
coverage = out.coverage.external,
full_jars = out.full_jars,
files = depset(out.full_jars),
Copy link
Contributor

Choose a reason for hiding this comment

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

having a depset here is probably not the best idea since depsets have their cost and especially since you want to customize this in other phases then you might have depsets which are nested for no reason.
Maybe pass array?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure. In some case we may wind up passing along existing depsets, but in many others we have just a few files that need to be added in. It's not clear to me which is more common and the impact to analysis time.

Copy link
Contributor

Choose a reason for hiding this comment

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

Are you replying in general or about files and runfiles? What we have now in these cases are arrays. If you’ll want to override these then you’ll need to have a nested depset.
In general I think it will depend on the case.

ijar = out.ijar,
ijars = out.ijars,
rjars = depset(out.full_jars, transitive = [rjars]),
Expand Down
1 change: 1 addition & 0 deletions scala/private/phases/phase_coverage_runfiles.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ def phase_coverage_runfiles(ctx, p):
coverage_runfiles = ctx.files._jacocorunner + ctx.files._lcov_merger + coverage_replacements.values()
return struct(
coverage_runfiles = coverage_runfiles,
runfiles = depset(coverage_runfiles),
rjars = rjars,
)
8 changes: 6 additions & 2 deletions scala/private/phases/phase_declare_executable.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ load(

def phase_declare_executable(ctx, p):
if (is_windows(ctx)):
return ctx.actions.declare_file("%s.exe" % ctx.label.name)
return struct(
executable = ctx.actions.declare_file("%s.exe" % ctx.label.name),
)
else:
return ctx.actions.declare_file(ctx.label.name)
return struct(
executable = ctx.actions.declare_file(ctx.label.name),
)
51 changes: 28 additions & 23 deletions scala/private/phases/phase_default_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,39 @@
#
# DOCUMENT THIS
#
def phase_default_info_binary(ctx, p):
return struct(
external_providers = {
"DefaultInfo": DefaultInfo(
executable = p.declare_executable,
files = depset([p.declare_executable] + p.compile.full_jars),
runfiles = p.runfiles.runfiles,
),
},
)

def phase_default_info_library(ctx, p):
return struct(
external_providers = {
"DefaultInfo": DefaultInfo(
files = depset(p.compile.full_jars),
runfiles = p.runfiles.runfiles,
),
},
)
def phase_default_info(ctx, p):
executable = None
files = []
runfiles = []

phase_names = dir(p)
phase_names.remove("to_json")
phase_names.remove("to_proto")
for phase_name in phase_names:
phase = getattr(p, phase_name)

if hasattr(phase, "executable"):
if executable == None:
executable = phase.executable
else:
fail("only one executable may be provided")

if hasattr(phase, "files"):
files.append(phase.files)

if hasattr(phase, "runfiles"):
runfiles.append(phase.runfiles)

def phase_default_info_scalatest(ctx, p):
return struct(
external_providers = {
"DefaultInfo": DefaultInfo(
executable = p.declare_executable,
files = depset([p.declare_executable] + p.compile.full_jars),
runfiles = ctx.runfiles(p.coverage_runfiles.coverage_runfiles, transitive_files = p.runfiles.runfiles.files),
executable = executable,
files = depset(transitive = files),
# TODO:
# Per Bazel documentation, we should avoid using collect_data. The core phases need to be updated
# before we can make the adjustment.
runfiles = ctx.runfiles(transitive_files = depset(transitive = runfiles), collect_data = True),
),
},
)
9 changes: 3 additions & 6 deletions scala/private/phases/phase_runfiles.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def phase_runfiles_scalatest(ctx, p):

args = struct(
transitive_files = depset(
[p.declare_executable, p.java_wrapper] + ctx.files._java_runtime + runfiles_ext,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as the below comment.

[p.java_wrapper] + ctx.files._java_runtime + runfiles_ext,
transitive = [p.compile.rjars],
),
args_file = args_file,
Expand All @@ -38,7 +38,7 @@ def _phase_runfiles_default(ctx, p, _args = struct()):
return _phase_runfiles(
ctx,
_args.transitive_files if hasattr(_args, "transitive_files") else depset(
[p.declare_executable, p.java_wrapper] + ctx.files._java_runtime,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Last I check, the DefaultInfo's executable field is assumed part of the overall runfiles output. So this wasn't needed here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think we can validate it somehow? Do you think tests should have been broken? Maybe it's good enough, not sure

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMHO it's good enough because otherwise most of the tests covering binary and test rules would have broken! Additionally, I wouldn't have been able to iterate on #959 which incorporates these changes.

If you want, I can test this against our codebase at Stripe to be extra safe.

Copy link
Contributor

@ittaiz ittaiz Jan 27, 2020

Choose a reason for hiding this comment

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

I think that’s a good idea (though you’re probably right)

[p.java_wrapper] + ctx.files._java_runtime,
transitive = [p.compile.rjars],
),
_args.args_file if hasattr(_args, "args_file") else None,
Expand All @@ -49,10 +49,7 @@ def _phase_runfiles(
transitive_files,
args_file):
return struct(
runfiles = ctx.runfiles(
transitive_files = transitive_files,
collect_data = True,
),
runfiles = transitive_files,
args_file = args_file,
)

Expand Down
2 changes: 1 addition & 1 deletion scala/private/phases/phase_write_executable.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def _phase_write_executable(
jvm_flags,
use_jacoco,
main_class):
executable = p.declare_executable
executable = p.declare_executable.executable
wrapper = p.java_wrapper

if (is_windows(ctx)):
Expand Down
11 changes: 2 additions & 9 deletions scala/private/phases/phases.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@ load(
_phase_runfiles_library = "phase_runfiles_library",
_phase_runfiles_scalatest = "phase_runfiles_scalatest",
)
load(
"@io_bazel_rules_scala//scala/private:phases/phase_default_info.bzl",
_phase_default_info_binary = "phase_default_info_binary",
_phase_default_info_library = "phase_default_info_library",
_phase_default_info_scalatest = "phase_default_info_scalatest",
)
load("@io_bazel_rules_scala//scala/private:phases/phase_default_info.bzl", _phase_default_info = "phase_default_info")
load("@io_bazel_rules_scala//scala/private:phases/phase_scalac_provider.bzl", _phase_scalac_provider = "phase_scalac_provider")
load("@io_bazel_rules_scala//scala/private:phases/phase_write_manifest.bzl", _phase_write_manifest = "phase_write_manifest")
load("@io_bazel_rules_scala//scala/private:phases/phase_collect_srcjars.bzl", _phase_collect_srcjars = "phase_collect_srcjars")
Expand Down Expand Up @@ -127,9 +122,7 @@ phase_runfiles_scalatest = _phase_runfiles_scalatest
phase_runfiles_common = _phase_runfiles_common

# default_info
phase_default_info_binary = _phase_default_info_binary
phase_default_info_library = _phase_default_info_library
phase_default_info_scalatest = _phase_default_info_scalatest
phase_default_info = _phase_default_info

# scalafmt
phase_scalafmt = _phase_scalafmt
4 changes: 2 additions & 2 deletions scala/private/rules/scala_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ load(
"phase_collect_jars_common",
"phase_compile_binary",
"phase_declare_executable",
"phase_default_info_binary",
"phase_default_info",
"phase_java_wrapper_common",
"phase_merge_jars",
"phase_runfiles_common",
Expand All @@ -42,7 +42,7 @@ def _scala_binary_impl(ctx):
("merge_jars", phase_merge_jars),
("runfiles", phase_runfiles_common),
("write_executable", phase_write_executable_common),
("default_info", phase_default_info_binary),
("default_info", phase_default_info),
],
)

Expand Down
4 changes: 2 additions & 2 deletions scala/private/rules/scala_junit_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ load(
"phase_collect_jars_junit_test",
"phase_compile_junit_test",
"phase_declare_executable",
"phase_default_info_binary",
"phase_default_info",
"phase_java_wrapper_common",
"phase_jvm_flags",
"phase_merge_jars",
Expand Down Expand Up @@ -47,7 +47,7 @@ def _scala_junit_test_impl(ctx):
("runfiles", phase_runfiles_common),
("jvm_flags", phase_jvm_flags),
("write_executable", phase_write_executable_junit_test),
("default_info", phase_default_info_binary),
("default_info", phase_default_info),
],
)

Expand Down
8 changes: 4 additions & 4 deletions scala/private/rules/scala_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ load(
"phase_compile_library",
"phase_compile_library_for_plugin_bootstrapping",
"phase_compile_macro_library",
"phase_default_info_library",
"phase_default_info",
"phase_merge_jars",
"phase_runfiles_library",
"phase_scalac_provider",
Expand Down Expand Up @@ -66,7 +66,7 @@ def _scala_library_impl(ctx):
("merge_jars", phase_merge_jars),
("runfiles", phase_runfiles_library),
("collect_exports_jars", phase_collect_exports_jars),
("default_info", phase_default_info_library),
("default_info", phase_default_info),
],
)

Expand Down Expand Up @@ -142,7 +142,7 @@ def _scala_library_for_plugin_bootstrapping_impl(ctx):
("merge_jars", phase_merge_jars),
("runfiles", phase_runfiles_library),
("collect_exports_jars", phase_collect_exports_jars),
("default_info", phase_default_info_library),
("default_info", phase_default_info),
],
)

Expand Down Expand Up @@ -197,7 +197,7 @@ def _scala_macro_library_impl(ctx):
("merge_jars", phase_merge_jars),
("runfiles", phase_runfiles_library),
("collect_exports_jars", phase_collect_exports_jars),
("default_info", phase_default_info_library),
("default_info", phase_default_info),
],
)

Expand Down
4 changes: 2 additions & 2 deletions scala/private/rules/scala_repl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ load(
"phase_collect_jars_repl",
"phase_compile_repl",
"phase_declare_executable",
"phase_default_info_binary",
"phase_default_info",
"phase_java_wrapper_repl",
"phase_merge_jars",
"phase_runfiles_common",
Expand Down Expand Up @@ -43,7 +43,7 @@ def _scala_repl_impl(ctx):
("merge_jars", phase_merge_jars),
("runfiles", phase_runfiles_common),
("write_executable", phase_write_executable_repl),
("default_info", phase_default_info_binary),
("default_info", phase_default_info),
],
)

Expand Down
4 changes: 2 additions & 2 deletions scala/private/rules/scala_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ load(
"phase_compile_scalatest",
"phase_coverage_runfiles",
"phase_declare_executable",
"phase_default_info_scalatest",
"phase_default_info",
"phase_java_wrapper_common",
"phase_merge_jars",
"phase_runfiles_scalatest",
Expand Down Expand Up @@ -44,7 +44,7 @@ def _scala_test_impl(ctx):
("runfiles", phase_runfiles_scalatest),
("coverage_runfiles", phase_coverage_runfiles),
("write_executable", phase_write_executable_scalatest),
("default_info", phase_default_info_scalatest),
("default_info", phase_default_info),
],
)

Expand Down