rules_scala icon indicating copy to clipboard operation
rules_scala copied to clipboard

SemanticDB phases

Open andyscott opened this issue 5 years ago • 7 comments
trafficstars

Stripe's Scala team has an intern, and this quarter we're planning work that requires SemanticDB outputs!

I believe SemanticDB support can be easily encapsulated in a new optional phase that:

  1. adds the scalac plugin
  2. declares the new SemanticDB output files

This pattern was already prototyped in higherkindness/rules_scala, so I'm confident that it will work here too.

We'll circle back soon with an update once we can commit to this work.

andyscott avatar Jan 23 '20 05:01 andyscott

Sounds good. You're doing it so that you can utilize metals and vscode?

ittaiz avatar Jan 23 '20 10:01 ittaiz

Also if this is about metals then does this relate / duplicate #915 ?

ittaiz avatar Jan 24 '20 06:01 ittaiz

It's related in the sense that bloop uses SemanticDB, but this serves a different purpose: prebuilding SemanticDB files for large repos in the CI build.

andyscott avatar Jan 24 '20 07:01 andyscott

For what purpose? Asking out of curiosity of course as I’m not that familiar with SemanticDB itself.

ittaiz avatar Jan 24 '20 07:01 ittaiz

There's probably lots of possibilities from a tooling perspective, but one concrete thing I can think of is enabling us to apply tools like Scalafix.

https://scalameta.org/docs/semanticdb/guide.html#scalafix

long-stripe avatar Jan 25 '20 04:01 long-stripe

How I solved this (without direct rules_scala support):

  1. Added semanticdb as a dependency with rules_jvm_external: "org.scalameta:semanticdb-scalac_%s:4.7.1" % scala_version
  2. Set up your scalacopts and compiler plugins accordingly:
semanticdb = "@maven//:org_scalameta_semanticdb_scalac_%s" % scala_version_suffix

semanticdb_scalaopts = [
    "-Xplugin:$(location %s)" % semanticdb,
    "-Yrangepos",
]

semanticdb_deps = [
    semanticdb,
]

compiler_deps = semanticdb_deps

# Ensure your deps provider for the scala compiler includes the compiler deps:
declare_deps_provider(
    name = "my_scala_compile_classpath_provider",
    deps_id = "scala_compile_classpath",
    visibility = ["//visibility:public"],
    deps = [
        "@maven//:org_scala_lang_scala_compiler",
        "@maven//:org_scala_lang_scala_library",
        "@maven//:org_scala_lang_scala_reflect",
    ] + compiler_deps,
)

# In your targets ensure to provide scalaopts and deps:
scala_library(
  # ...
  scalacopts = semanticdb_scalaopts,
  deps = semanticdb_deps,
)

As an extra detail, how I managed to use Scalafix after the above was applied:

# Install coursier (`cs`) with whatever method.

# Install scalafix:
cs install scalafix
scalafix --version

# I wanted to use ZIO scalafix rules, so download and build ZIO scalafix rules:
git clone https://github.com/zio/zio/
cd zio
sbt scalafixRules/publishLocal

# In your repo:
# Build a deploy jar:
bazel build //some/custom/path:library_deploy.jar
# Run scalafix:
scalafix --tool-classpath $HOME/.ivy2/local/dev.zio/scalafixrules_2.13/2.0.5+38-7efadb73-SNAPSHOT/jars/scalafixrules_2.13.jar --rules Zio2Upgrade --files some/custom/path/src/main/scala/ --classpath bazel-bin/some/custom/path/library_deploy.jar

gergelyfabian avatar Jan 05 '23 12:01 gergelyfabian

This issue can probably be closed now. SemanticDB support was added in #1508

crt-31 avatar Nov 30 '23 22:11 crt-31