Skip to content

Commit

Permalink
Output a SemanticDbInfo provider in the "semanticdb" phase
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaden Peterson committed Nov 12, 2024
1 parent 6919531 commit f71a2ba
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 22 deletions.
58 changes: 40 additions & 18 deletions rules/private/phases/phase_semanticdb.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@rules_scala_annex//rules:providers.bzl", _ScalaConfiguration = "ScalaConfiguration")
load(
"@rules_scala_annex//rules:providers.bzl",
_ScalaConfiguration = "ScalaConfiguration",
_SemanticDbInfo = "SemanticDbInfo",
)

def _semanticdb_directory_from_file(file):
return file.path[:file.path.find("META-INF") - 1]

#
# PHASE: semanticdb
Expand All @@ -11,32 +18,47 @@ def phase_semanticdb(ctx, g):
scala_configuration = ctx.attr.scala[_ScalaConfiguration]

if scala_configuration.semanticdb_bundle:
return struct(outputs = [], scalacopts = [])
return struct(outputs = [], arguments_modifier = lambda _: None)

directory_name = "{}/semanticdb".format(ctx.label.name)
outputs = []
semanticdb_directory = paths.join("_semanticdb/", ctx.label.name)
semanticdb_target_root = paths.join(paths.dirname(ctx.outputs.jar.path), semanticdb_directory)

for source in ctx.files.srcs:
if source.extension == "scala":
output_filename = paths.join(
semanticdb_directory,
path = paths.join(
directory_name,
"META-INF",
"semanticdb",
"{}.semanticdb".format(source.path),
)

outputs.append(ctx.actions.declare_file(output_filename))
outputs.append(ctx.actions.declare_file(path))

def add_scalacopts(arguments):
if len(outputs) == 0:
return

if scala_configuration.version.startswith("2"):
arguments.add("--compiler_option=-P:semanticdb:failures:error")
arguments.add_all(
[outputs[0]],
format_each = "--compiler_option=-P:semanticdb:targetroot:%s",
map_each = _semanticdb_directory_from_file,
)
else:
arguments.add_all(
[outputs[0]],
format_each = "--compiler_option=-semanticdb-target:%s",
map_each = _semanticdb_directory_from_file,
)

arguments.add("--compiler_option=-Ysemanticdb")

if scala_configuration.version.startswith("2"):
scalacopts = [
"-P:semanticdb:failures:error",
"-P:semanticdb:targetroot:{}".format(semanticdb_target_root),
]
else:
scalacopts = [
"-semanticdb-target:{}".format(semanticdb_target_root),
"-Ysemanticdb",
]
g.out.providers.append(
_SemanticDbInfo(
target_root = "{}/{}".format(ctx.label.package, directory_name),
semanticdb_files = outputs,
),
)

return struct(outputs = outputs, scalacopts = scalacopts)
return struct(outputs = outputs, arguments_modifier = add_scalacopts)
7 changes: 4 additions & 3 deletions rules/private/phases/phase_zinc_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ def phase_zinc_compile(ctx, g):
]

zincs = [dep[_ZincInfo] for dep in ctx.attr.deps if _ZincInfo in dep]
common_scalacopts = scala_configuration.global_scalacopts + ctx.attr.scalacopts + g.semanticdb.scalacopts
common_scalacopts = scala_configuration.global_scalacopts + ctx.attr.scalacopts

args = ctx.actions.args()

args.add_all(depset(transitive = [zinc.deps for zinc in zincs]), map_each = _compile_analysis)
args.add("--compiler_bridge", zinc_configuration.compiler_bridge)
args.add_all("--compiler_classpath", g.classpaths.compiler)
Expand All @@ -55,8 +54,10 @@ def phase_zinc_compile(ctx, g):
args.add_all("--plugins", g.classpaths.plugin)
args.add_all("--source_jars", g.classpaths.src_jars)
args.add("--tmp", tmp.path)

args.add("--log_level", zinc_configuration.log_level)

g.semanticdb.arguments_modifier(args)

args.add_all("--", g.classpaths.srcs)
args.set_param_file_format("multiline")
args.use_param_file("@%s", use_always = True)
Expand Down
8 changes: 8 additions & 0 deletions rules/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,11 @@ LabeledJars = provider(
"values": "The preorder depset of label and jars.",
},
)

SemanticDbInfo = provider(
doc = "Provided by the Scala rules when `semanticdb_bundle` is set to `False`.",
fields = {
"target_root": "The directory in which SemanticDB files were outputted.",
"semanticdb_files": "The SemanticDB files.",
},
)
18 changes: 18 additions & 0 deletions tests/plugins/semanticdb/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@rules_scala_annex//rules:scala.bzl", "configure_zinc_scala", "scala_library")
load("@rules_scala_annex//rules/scala:workspace.bzl", "scala_2_13_version", "scala_3_version")
load(":rule.bzl", "read_semanticdb_info")

configure_zinc_scala(
name = "scala_2_13_with_semanticdb",
Expand Down Expand Up @@ -39,9 +40,26 @@ scala_library(
tags = ["manual"],
)

read_semanticdb_info(
name = "semanticdb-2_13-semanticdb-info",
scala_target = ":semanticdb-2_13",
)

scala_library(
name = "semanticdb-3",
srcs = glob(["*.scala"]),
scala = ":scala_3_with_semanticdb",
tags = ["manual"],
)

read_semanticdb_info(
name = "semanticdb-3-semanticdb-info",
scala_target = ":semanticdb-3",
)

scala_library(
name = "semanticdb-empty",
srcs = [],
scala = ":scala_2_13_with_semanticdb",
tags = ["manual"],
)
25 changes: 25 additions & 0 deletions tests/plugins/semanticdb/rule.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
load("@rules_scala_annex//rules:providers.bzl", "SemanticDbInfo")

def _read_semanticdb_info_impl(ctx):
semanticdb_info = ctx.attr.scala_target[SemanticDbInfo]
output = ctx.actions.declare_file("{}.txt".format(ctx.label.name))

ctx.actions.write(
output,
json.encode({
"targetRoot": semanticdb_info.target_root,
"semanticDbFiles": sorted([file.path for file in semanticdb_info.semanticdb_files]),
}),
)

return DefaultInfo(files = depset([output]))

read_semanticdb_info = rule(
attrs = {
"scala_target": attr.label(
mandatory = True,
providers = [SemanticDbInfo],
),
},
implementation = _read_semanticdb_info_impl,
)
19 changes: 18 additions & 1 deletion tests/plugins/semanticdb/test
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/bin/bash -e
. "$(dirname "$0")"/../../common.sh

bazel_bin="$(bazel info bazel-bin)"

check_for_semanticdb_files() {
for filename in "A.scala.semanticdb" "B.scala.semanticdb"; do
path="../../bazel-bin/plugins/semanticdb/_semanticdb/semanticdb-$1/META-INF/semanticdb/plugins/semanticdb/$filename"
path="../../bazel-bin/plugins/semanticdb/semanticdb-$1/semanticdb/META-INF/semanticdb/plugins/semanticdb/$filename"

if [ ! -f "$path" ]; then
echo "Error: $path doesn't exist"
Expand All @@ -12,7 +14,22 @@ check_for_semanticdb_files() {
done
}

check_semanticdb_info() {
bazel build ":semanticdb-$1-semanticdb-info"

output_path="$bazel_bin/plugins/semanticdb/semanticdb-$1-semanticdb-info.txt"
semanticdb_file_directory="bazel-out/k8-fastbuild/bin/plugins/semanticdb/semanticdb-$1/semanticdb/META-INF/semanticdb/plugins/semanticdb"

[ "$(jq ".targetRoot" "$output_path")" = "\"plugins/semanticdb/semanticdb-$1/semanticdb\"" ]
[ "$(jq -c ".semanticDbFiles" "$output_path")" = "[\"$semanticdb_file_directory/A.scala.semanticdb\",\"$semanticdb_file_directory/B.scala.semanticdb\"]" ]
}

bazel build :semanticdb-2_13
check_for_semanticdb_files 2_13
check_semanticdb_info 2_13

bazel build :semanticdb-3
check_for_semanticdb_files '3'
check_semanticdb_info '3'

bazel build :semanticdb-empty

0 comments on commit f71a2ba

Please sign in to comment.