From 3b51ba7f74cd7f068b17df89043c2f0a796a3e21 Mon Sep 17 00:00:00 2001 From: Moritz Kiefer Date: Fri, 14 Aug 2020 15:31:36 +0200 Subject: [PATCH] Add executable as a direct dependency (#1082) * Add executable as a direct dependency This fixes a bug introduced by https://github.com/bazelbuild/rules_scala/pull/958. Before #958, `rootpath` on a Scala binary resolved to the wrapper binary so other rules could treat it as an opaque executable. After that PR `rootpath` now resolves to the JAR instead of the wrapper binary. Adding the executable back as a direct dependency fixes that. * Add a testcase --- scala/private/phases/phase_default_info.bzl | 6 ++++-- test/BUILD | 20 ++++++++++++++++++++ test/rootpaths_binary.sh | 11 +++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100755 test/rootpaths_binary.sh diff --git a/scala/private/phases/phase_default_info.bzl b/scala/private/phases/phase_default_info.bzl index 53f056883..ad3678f7a 100644 --- a/scala/private/phases/phase_default_info.bzl +++ b/scala/private/phases/phase_default_info.bzl @@ -7,6 +7,7 @@ def phase_default_info(ctx, p): executable = None files = [] + direct = None runfiles = [] phase_names = dir(p) @@ -18,6 +19,7 @@ def phase_default_info(ctx, p): if hasattr(phase, "executable"): if executable == None: executable = phase.executable + direct = [executable] else: fail("only one executable may be provided") @@ -31,11 +33,11 @@ def phase_default_info(ctx, p): external_providers = { "DefaultInfo": DefaultInfo( executable = executable, - files = depset(transitive = files), + files = depset(direct = direct, 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), + runfiles = ctx.runfiles(transitive_files = depset(direct = direct, transitive = runfiles), collect_data = True), ), }, ) diff --git a/test/BUILD b/test/BUILD index 997c82e5c..0ecaa215d 100644 --- a/test/BUILD +++ b/test/BUILD @@ -478,6 +478,26 @@ scala_specs2_junit_test( "//test:test_scala_proto_server", ]] +# Generate a file containing the rootpaths of a Scala binary. +genrule( + name = "rootpath-script", + srcs = [":ScalaBinary"], + outs = ["rootpath-script.out"], + cmd = """ + rootpaths=($(rootpaths {})) + (IFS='\n'; echo "$${{rootpaths[*]}}") | sort > $@ + """.format(":ScalaBinary"), +) + +# Validate that the rootpaths point to both the binary +# and the JAR. +sh_test( + name = "ScalaBinaryRootpaths", + srcs = ["rootpaths_binary.sh"], + args = ["$(location :rootpath-script.out)"], + data = [":rootpath-script.out"], +) + sh_test( name = "test_binary_run_with_large_classpath", srcs = ["test_binary_run_with_large_classpath.sh"], diff --git a/test/rootpaths_binary.sh b/test/rootpaths_binary.sh new file mode 100755 index 000000000..ee4579eb5 --- /dev/null +++ b/test/rootpaths_binary.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +set -eou pipefail + +content="$(cat $1)" +expected=$'test/ScalaBinary\ntest/ScalaBinary.jar' +if [ "$content" != "$expected" ]; then + echo "Unexpected rootpaths: $content" + echo "$expected" + exit 1 +fi