Skip to content

Commit

Permalink
Add rules for outputting bsp dependencies (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
aishfenton authored Dec 6, 2023
1 parent ec5c806 commit c6f03a5
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("//private:load_tool.bzl", "load_tool")
load("//private:bsp_workspace_deps.bzl", _bsp_workspace_deps = "bsp_workspace_deps")

# <--- Updated automatically by release job
_bsp4bazel_version = "0.0.30"
Expand All @@ -23,4 +24,10 @@ def _bsp4bazel_load(platform):

def bsp4bazel_setup():
_bsp4bazel_load("linux-x86")
_bsp4bazel_load("macos-x86")
_bsp4bazel_load("macos-x86")

def bsp_workspace_deps(name = "bsp_workspace_deps"):
if (name != "bsp_workspace_deps"):
fail("name must be 'bsp_workspace_deps'")

_bsp_workspace_deps(name = name)
2 changes: 1 addition & 1 deletion bazel_rules/bsp_target_info_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def bsp_target_info_aspect_impl(target, ctx):

return [
OutputGroupInfo(
bsp_output = depset([json_output_file] + semanticdb_classpath + compile_classpath),
bsp_output = depset([json_output_file]),
),
]

Expand Down
60 changes: 60 additions & 0 deletions bazel_rules/private/bsp_workspace_deps.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
load("@io_bazel_rules_scala//scala/private/toolchain_deps:toolchain_deps.bzl", "find_deps_info_on")

def _copy_files(ctx, files):
output_files = []

for file in files:
output_file = ctx.actions.declare_file(ctx.attr.name + "_" + file.basename)

ctx.actions.run_shell(
inputs = [file],
outputs = [output_file],
command = "cp {src} {out}".format(src = file.path, out = output_file.path),
)

output_files.append(output_file)

return output_files

def _bsp_workspace_deps_impl(ctx):
"""
This function copies the dependencies for the BSP workspace into a known output location.
Args:
ctx: The context object.
Returns:
A list of dependencies for the BSP workspace.
"""

compile_classpath = [
file
for deps in find_deps_info_on(ctx, "@io_bazel_rules_scala//scala:toolchain_type", "scala_compile_classpath").deps
for file in deps[JavaInfo].compile_jars.to_list()
]

# Why not use the class_jar in SemanticdbInfo? Because it's a string, not a File, so we can't pass
# pass it to outputs.
# https://github.com/bazelbuild/rules_scala/issues/1527
semanticdb_classpath = [
file
for deps in find_deps_info_on(ctx, "@io_bazel_rules_scala//scala:toolchain_type", "semanticdb").deps
for file in deps[JavaInfo].compile_jars.to_list()
]

scalac_output_files = _copy_files(ctx, compile_classpath)
semanticdb_output_files = _copy_files(ctx, semanticdb_classpath)

json_output_file = ctx.actions.declare_file(ctx.attr.name + ".json")
output_struct = struct(
scalac = [file.path for file in scalac_output_files],
semanticdb = [file.path for file in semanticdb_output_files],
)
ctx.actions.write(json_output_file, json.encode_indent(output_struct))

return [DefaultInfo(files = depset(scalac_output_files + semanticdb_output_files + [json_output_file]))]

bsp_workspace_deps = rule(
implementation = _bsp_workspace_deps_impl,
toolchains = ["@io_bazel_rules_scala//scala:toolchain_type"],
)
22 changes: 19 additions & 3 deletions bazel_rules/test/rules.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class BazelRulesTest extends munit.FunSuite:

def testBazelAspect[T](dir: os.Path)(implicit loc: munit.Location): Unit = {
def testBazelRules[T](dir: os.Path)(implicit loc: munit.Location): Unit = {
test(s"should output bsp_target_info.json for $dir") {
os.proc(
dir / "bazel",
Expand All @@ -20,6 +20,22 @@ class BazelRulesTest extends munit.FunSuite:
assert(os.exists(dir / "bazel-bin" / "src" / "example" / "example_bsp_target_info.json"))
assert(os.exists(dir / "bazel-bin" / "src" / "example" / "foo" / "foo_bsp_target_info.json"))
}

test(s"should output bsp_workspace_deps.json for $dir") {
os.proc(
dir / "bazel",
"build",
"//:bsp_workspace_deps",
).call(cwd = dir)

assert(os.exists(dir / "bazel-bin" / "bsp_workspace_deps.json"))

val json = os.read(dir / "bazel-bin" / "bsp_workspace_deps.json")
assert(json.contains("semanticdb-scalac"), "No semanticdb dep")
assert(json.contains("scala-reflect"), "No scala-reflect dep")
assert(json.contains("scala-library"), "No scala-library dep")
assert(json.contains("scala-compiler"), "No scala-compiler dep")
}
}

test("should build examples/simple-no-errors") {
Expand All @@ -39,5 +55,5 @@ class BazelRulesTest extends munit.FunSuite:
assertEquals(result.exitCode, 1)
}

testBazelAspect(os.pwd / "examples" / "simple-no-errors")
testBazelAspect(os.pwd / "examples" / "simple-with-errors")
testBazelRules(os.pwd / "examples" / "simple-no-errors")
testBazelRules(os.pwd / "examples" / "simple-with-errors")
3 changes: 3 additions & 0 deletions examples/simple-no-errors/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
load("@bsp4bazel-rules//:bsp4bazel.bzl", "bsp_workspace_deps")

bsp_workspace_deps()
2 changes: 1 addition & 1 deletion examples/simple-no-errors/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ local_repository(
path = "../../bazel_rules",
)

load("@bsp4bazel-rules//:bsp4bazel_setup.bzl", "bsp4bazel_setup")
load("@bsp4bazel-rules//:bsp4bazel.bzl", "bsp4bazel_setup")

bsp4bazel_setup()
3 changes: 3 additions & 0 deletions examples/simple-with-errors/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
load("@bsp4bazel-rules//:bsp4bazel.bzl", "bsp_workspace_deps")

bsp_workspace_deps()
2 changes: 1 addition & 1 deletion examples/simple-with-errors/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ local_repository(
path = "../../bazel_rules",
)

load("@bsp4bazel-rules//:bsp4bazel_setup.bzl", "bsp4bazel_setup")
load("@bsp4bazel-rules//:bsp4bazel.bzl", "bsp4bazel_setup")

bsp4bazel_setup()

0 comments on commit c6f03a5

Please sign in to comment.