bazelbuild / rules_scala

Scala rules for Bazel
Apache License 2.0
363 stars 277 forks source link

SemanticDB phases #952

Open andyscott opened 4 years ago

andyscott commented 4 years ago

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.

ittaiz commented 4 years ago

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

ittaiz commented 4 years ago

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

andyscott commented 4 years ago

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.

ittaiz commented 4 years ago

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

long-stripe commented 4 years ago

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

gergelyfabian commented 1 year ago

How I solved this (without direct rules_scala support):

  1. Added semanticdb as a dependency with rules_jvmexternal: `"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:

```shell
# 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
crt-31 commented 11 months ago

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