Skip to content

Commit

Permalink
Only use the new way to add semanticdb
Browse files Browse the repository at this point in the history
*except in test where we compile small virtual files
  • Loading branch information
mlachkar committed Oct 7, 2020
1 parent deab839 commit d31b5dc
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 65 deletions.
29 changes: 13 additions & 16 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ inThisBuild(
scalaVersion := scala213,
crossScalaVersions := List(scala213, scala212, scala211),
fork := true,
semanticdbEnabled := true,
semanticdbVersion := scalafixSemanticdb.revision,
scalacOptions ++= List("-P:semanticdb:synthetics:on"),
scalafixScalaBinaryVersion := scalaBinaryVersion.value,
scalafixDependencies += "com.github.liancheng" %% "organize-imports" % "0.4.0"
)
Expand Down Expand Up @@ -66,8 +69,7 @@ lazy val core = project
"com.geirsson" %% "metaconfig-typesafe-config" % metaconfigV,
"org.scala-lang" % "scala-reflect" % scalaVersion.value % Provided,
collectionCompat
),
scalafixSettings
)
)
.enablePlugins(BuildInfoPlugin)

Expand All @@ -86,8 +88,7 @@ lazy val rules = project
libraryDependencies ++= List(
"org.scalameta" % "semanticdb-scalac-core" % scalametaV cross CrossVersion.full,
collectionCompat
),
scalafixSettings
)
)
.dependsOn(core)
.enablePlugins(BuildInfoPlugin)
Expand All @@ -100,8 +101,7 @@ lazy val reflect = project
libraryDependencies ++= Seq(
"org.scala-lang" % "scala-compiler" % scalaVersion.value,
"org.scala-lang" % "scala-reflect" % scalaVersion.value
),
scalafixSettings
)
)
.dependsOn(core, rules)

Expand All @@ -118,8 +118,7 @@ lazy val cli = project
jgit,
"ch.qos.logback" % "logback-classic" % "1.2.3",
"org.apache.commons" % "commons-text" % "1.9"
),
scalafixSettings
)
)
.dependsOn(reflect, interfaces)

Expand Down Expand Up @@ -173,7 +172,6 @@ lazy val testkit = project
moduleName := "scalafix-testkit",
isFullCrossVersion,
libraryDependencies ++= Seq(
semanticdb,
googleDiff,
scalacheck,
scalatest
Expand All @@ -195,7 +193,6 @@ lazy val unit = project
libraryDependencies ++= testsDeps,
libraryDependencies ++= List(
jgit,
semanticdbPluginLibrary,
scalatest.withRevision("3.2.0"), // make sure testkit clients can use recent 3.x versions
"org.scalameta" %% "testkit" % scalametaV
),
Expand Down Expand Up @@ -223,6 +220,11 @@ lazy val unit = project
"inputClasspath",
fullClasspath.in(testsInput, Compile).value.map(_.data)
)

put(
"semanticdbRootDirectory",
Seq(semanticdbTargetRoot.in(testsInput, Compile).value)
)
put(
"inputSourceDirectories",
sourceDirectories.in(testsInput, Compile).value
Expand Down Expand Up @@ -253,7 +255,7 @@ lazy val unit = project
"testsInputResources" ->
sourceDirectory.in(testsInput, Compile).value / "resources",
"semanticClasspath" ->
classDirectory.in(testsInput, Compile).value,
semanticdbTargetRoot.in(testsShared, Compile).value,
"sharedSourceroot" ->
baseDirectory.in(ThisBuild).value /
"scalafix-tests" / "shared" / "src" / "main",
Expand Down Expand Up @@ -292,8 +294,3 @@ lazy val docs = project
.dependsOn(testkit, core, cli)
.enablePlugins(DocusaurusPlugin)
.disablePlugins(ScalafixPlugin)

lazy val scalafixSettings = Seq(
semanticdbEnabled := true,
semanticdbVersion := scalafixSemanticdb.revision
)
2 changes: 0 additions & 2 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ object Dependencies {
val jgit = "org.eclipse.jgit" % "org.eclipse.jgit" % "5.9.0.202009080501-r"

var testClasspath: String = "empty"
def semanticdb: ModuleID = "org.scalameta" % "semanticdb-scalac" % scalametaV cross CrossVersion.full
def metaconfig: ModuleID = "com.geirsson" %% "metaconfig-typesafe-config" % metaconfigV
def googleDiff = "com.googlecode.java-diff-utils" % "diffutils" % "1.3.0"

def metacp = "org.scalameta" %% "metacp" % scalametaV
def semanticdbPluginLibrary = "org.scalameta" % "semanticdb-scalac-core" % scalametaV cross CrossVersion.full
def scalameta = "org.scalameta" %% "scalameta" % scalametaV
def scalatest =
"org.scalatest" %% "scalatest" % "3.0.8" // don't bump, to avoid forcing breaking changes on clients via eviction
Expand Down
20 changes: 10 additions & 10 deletions project/ScalafixBuild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ object ScalafixBuild extends AutoPlugin with GhpagesKeys {
libraryDependencies ++= testsDeps
)

lazy val semanticdbSettings = Seq(
scalacOptions ++= List(
"-Yrangepos",
"-Xplugin-require:semanticdb",
"-P:semanticdb:synthetics:on"
),
addCompilerPlugin(
"org.scalameta" % "semanticdb-scalac" % scalametaV cross CrossVersion.full
)
)
// lazy val semanticdbSettings = Seq(
// scalacOptions ++= List(
// "-Yrangepos",
// "-Xplugin-require:semanticdb",
// "-P:semanticdb:synthetics:on"
// ),
// addCompilerPlugin(
// "org.scalameta" % "semanticdb-scalac" % scalametaV cross CrossVersion.full
// )
// )

// =======
// Website
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import scalafix.cli.ExitStatus
import scalafix.interfaces.ScalafixEvaluation
import scalafix.internal.config.PrintStreamReporter
import scalafix.internal.diff.DiffUtils
import scalafix.internal.interfaces.{ScalafixEvaluationImpl, ScalafixFileEvaluationImpl}
import scalafix.internal.interfaces.{
ScalafixEvaluationImpl,
ScalafixFileEvaluationImpl
}
import scalafix.internal.patch.PatchInternals
import scalafix.internal.patch.PatchInternals.tokenPatchApply
import scalafix.lint.{LintSeverity, RuleDiagnostic}
Expand Down
10 changes: 8 additions & 2 deletions scalafix-core/src/main/scala/scalafix/internal/v1/Rules.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ import scalafix.internal.util.SuppressOps
import scalafix.lint.Diagnostic
import scalafix.patch.Patch
import scalafix.rule.RuleName
import scalafix.v1.{Configuration, Rule, SemanticDocument, SemanticRule, SyntacticDocument, SyntacticRule}
import scalafix.v1.{
Configuration,
Rule,
SemanticDocument,
SemanticRule,
SyntacticDocument,
SyntacticRule
}

import scala.collection.mutable
import scala.meta.tokens.Tokens
import scala.util.control.NonFatal


case class Rules(rules: List[Rule] = Nil) {

def name: RuleName = RuleName(rules.flatMap(_.name.identifiers))
Expand Down
1 change: 0 additions & 1 deletion scalafix-core/src/main/scala/scalafix/v0/Rule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import scala.meta._
import metaconfig.Conf
import metaconfig.Configured


import scala.meta._
import scalafix.internal.config.MetaconfigOps
import scalafix.internal.config.ScalafixConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ object ClasspathOps {
new URLClassLoader(getURLs(classLoader), classLoader)
}

def thisClassLoaderWith(url: URL): URLClassLoader = {
val classLoader = this.getClass.getClassLoader
new URLClassLoader(url +: getURLs(classLoader), classLoader)
}

def thisClasspath: Classpath = {
Classpath(
getURLs(this.getClass().getClassLoader())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ object TestkitProperties {
case None => Nil
}
new TestkitProperties(
Classpath(sprops("inputClasspath")),
Classpath(sprops("semanticdbRootDirectory")) ++ Classpath(

This comment has been minimized.

Copy link
@bjaglin

bjaglin Nov 4, 2020

Collaborator

this is a breaking change, see scalacenter/sbt-scalafix#173 (comment)

This comment has been minimized.

Copy link
@mlachkar

mlachkar Nov 4, 2020

Author Collaborator

Ok. I didn't know that. I can fix that by putting semantidbInJAr = true

This comment has been minimized.

Copy link
@bjaglin

bjaglin Nov 4, 2020

Collaborator

I think that would just hide the problem, as mentioned in the comment over there, the best is to align the usage of the plugin.

This comment has been minimized.

Copy link
@bjaglin

bjaglin Nov 4, 2020

Collaborator
sprops("inputClasspath")
),
Classpath(sprops("inputSourceDirectories")).entries,
Classpath(sprops("outputSourceDirectories")).entries,
sourceroot,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class CliSemanticSuite extends BaseCliSuite {
checkSemantic(
name = "-P:semanticdb:targetroot",
args = {
val (targetroot :: Nil, jars) =
val (targetroot :: classDir :: Nil, jars) =
props.inputClasspath.entries.partition(_.isDirectory)
Array(
s"--scalacOptions",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,16 @@ import scalafix.interfaces.ScalafixMainCallback
import scalafix.interfaces.ScalafixMainMode
import scalafix.internal.reflect.ClasspathOps
import scalafix.internal.reflect.RuleCompiler
import scalafix.internal.rule.ProcedureSyntax
import scalafix.test.StringFS
import scalafix.testkit.DiffAssertions
import scalafix.tests.util.ScalaVersions
import scalafix.tests.util.{ScalaVersions, SemanticdbPlugin}
import scalafix.{interfaces => i}
import RulesBuildInfo.scalaVersion

import scala.util.Properties

class ScalafixImplSuite extends AnyFunSuite with DiffAssertions {
def semanticdbPluginPath(): String = {
val semanticdbscalac = ClasspathOps.thisClassLoader.getURLs.collectFirst {
case url if url.toString.contains("semanticdb-scalac_") =>
Paths.get(url.toURI).toString
}
semanticdbscalac.getOrElse {
throw new IllegalStateException(
"unable to auto-detect semanticdb-scalac compiler plugin"
)
}
}

def scalaLibrary: AbsolutePath =
RuleCompiler.defaultClasspathPaths
.find(_.toNIO.getFileName.toString.contains("scala-library"))
Expand Down Expand Up @@ -194,7 +184,7 @@ class ScalafixImplSuite extends AnyFunSuite with DiffAssertions {
)
val scalacOptions = Array[String](
"-Yrangepos",
s"-Xplugin:${semanticdbPluginPath()}",
s"-Xplugin:${SemanticdbPlugin.semanticdbPluginPath()}",
"-Xplugin-require:semanticdb",
"-classpath",
scalaLibrary.toString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object BaseSemanticSuite {
SemanticDocument.fromPath(
doc,
relpath,
ClasspathOps.thisClassLoader,
ClasspathOps.thisClassLoaderWith(BuildInfo.semanticClasspath.toURI.toURL),
symtab,
() => None
)
Expand All @@ -48,7 +48,7 @@ abstract class BaseSemanticSuite(filename: String)
}

override def beforeAll(): Unit = {
val dir = AbsolutePath(scalafix.tests.BuildInfo.sharedClasspath)
val dir = AbsolutePath(scalafix.tests.BuildInfo.semanticClasspath)
_db = LegacyInMemorySemanticdbIndex.load(
Classpaths.withDirectory(dir),
PathIO.workingDirectory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ object Classpaths {
def withDirectory(dir: AbsolutePath): Classpath =
Classpath(dir :: scalaLibrary.entries)

def withDirectories(dir: List[AbsolutePath]): Classpath =
Classpath(dir ++ scalaLibrary.entries)

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ import scalafix.internal.util.PrettyType

class BasePrettyTypeSuite extends BaseSemanticSuite("TypeToTreeInput") {
super.beforeAll()
val dir: m.AbsolutePath =
val classDir: m.AbsolutePath =
m.AbsolutePath(scalafix.tests.BuildInfo.sharedClasspath)
val classpath = Classpaths.withDirectory(dir)
val semanticdbTargetRoot =
m.AbsolutePath(scalafix.tests.BuildInfo.semanticClasspath)

val classpath =
Classpaths.withDirectories(List(semanticdbTargetRoot, classDir))
val table = GlobalSymbolTable(classpath, includeJdk = true)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import scalafix.internal.tests.utils.SkipWindows
import scalafix.test.StringFS
import scalafix.testkit.DiffAssertions
import scalafix.tests.core.Classpaths
import scalafix.tests.util.ScalaVersions
import scalafix.tests.util.{ScalaVersions, SemanticdbPlugin}
import scalafix.v1.SemanticRule

import scala.meta.io.AbsolutePath
Expand Down Expand Up @@ -62,7 +62,7 @@ class ScalafixArgumentsSuite extends AnyFunSuite with DiffAssertions {
val scalacOptions = Array[String](
"-Yrangepos",
removeUnused,
s"-Xplugin:${semanticdbPluginPath()}",
s"-Xplugin:${SemanticdbPlugin.semanticdbPluginPath()}",
"-Xplugin-require:semanticdb",
"-classpath",
s"${scalaLibrary.mkString(":")}",
Expand Down Expand Up @@ -220,15 +220,4 @@ class ScalafixArgumentsSuite extends AnyFunSuite with DiffAssertions {

def scalaLibrary: Seq[AbsolutePath] = Classpaths.scalaLibrary.entries

def semanticdbPluginPath(): String = {
val semanticdbscalac = ClasspathOps.thisClassLoader.getURLs.collectFirst {
case url if url.toString.contains("semanticdb-scalac_") =>
Paths.get(url.toURI).toString
}
semanticdbscalac.getOrElse {
throw new IllegalStateException(
"unable to auto-detect semanticdb-scalac compiler plugin"
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package scalafix.tests.util

import java.nio.file.Paths

import buildinfo.RulesBuildInfo.scalaVersion
import coursier.{Dependency, Fetch, Module, ModuleName, Organization}

object SemanticdbPlugin {
def semanticdbPluginPath(): String = {
val dep = Dependency(
Module(
Organization("org.scalameta"),
ModuleName(s"semanticdb-scalac_$scalaVersion")
),
"4.3.22"
)
val paths = Fetch()
.addDependencies(dep)
.run()
val semanticdbscalac = paths.collectFirst {
case path if path.toString.contains("semanticdb-scalac_") =>
Paths.get(path.toURI).toString
}
semanticdbscalac.getOrElse {
throw new IllegalStateException(
"unable to auto-detect semanticdb-scalac compiler plugin"
)
}
}
}

0 comments on commit d31b5dc

Please sign in to comment.