From d9982ef0e76003c71b2ca09ebabab931fbeef30f Mon Sep 17 00:00:00 2001 From: Tobias Roeser Date: Tue, 1 Mar 2022 11:47:43 +0100 Subject: [PATCH 1/3] Support newer scalafmt versions which require a config file Removed deprecated API calls. Version bump scalafmt-dynamic to 3.4.3 --- build.sc | 5 ++-- scalalib/src/scalafmt/ScalafmtModule.scala | 25 ++++++++++++++++--- scalalib/src/scalafmt/ScalafmtWorker.scala | 1 - .../test/src/scalafmt/ScalafmtTests.scala | 8 ++++++ 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/build.sc b/build.sc index 1e184f196e4..8642c888533 100755 --- a/build.sc +++ b/build.sc @@ -124,7 +124,7 @@ object Deps { val sbtTestInterface = ivy"org.scala-sbt:test-interface:1.0" val scalaCheck = ivy"org.scalacheck::scalacheck:1.15.4" def scalaCompiler(scalaVersion: String) = ivy"org.scala-lang:scala-compiler:${scalaVersion}" - val scalafmtDynamic = ivy"org.scalameta::scalafmt-dynamic:3.0.8" + val scalafmtDynamic = ivy"org.scalameta::scalafmt-dynamic:3.4.3" val scalametaTrees = ivy"org.scalameta::trees:4.5.0" def scalaReflect(scalaVersion: String) = ivy"org.scala-lang:scala-reflect:${scalaVersion}" def scalacScoveragePlugin = ivy"org.scoverage:::scalac-scoverage-plugin:1.4.11" @@ -428,7 +428,8 @@ object scalalib extends MillModule { Seq( "-Djna.nosys=true", "-DMILL_BUILD_LIBRARIES=" + genIdeaArgs.map(_.path).mkString(","), - "-DMILL_SCALA_LIB=" + runClasspath().map(_.path).mkString(",") + "-DMILL_SCALA_LIB=" + runClasspath().map(_.path).mkString(","), + s"-DTEST_SCALAFMT_VERSION=${Deps.scalafmtDynamic.dep.version}" ) } object backgroundwrapper extends MillPublishModule { diff --git a/scalalib/src/scalafmt/ScalafmtModule.scala b/scalalib/src/scalafmt/ScalafmtModule.scala index 67768929188..1e9080ca713 100644 --- a/scalalib/src/scalafmt/ScalafmtModule.scala +++ b/scalalib/src/scalafmt/ScalafmtModule.scala @@ -1,6 +1,7 @@ package mill.scalalib.scalafmt import mill._ +import mill.api.Result import mill.define._ import mill.scalalib._ @@ -11,7 +12,7 @@ trait ScalafmtModule extends JavaModule { .worker() .reformat( filesToFormat(sources()), - scalafmtConfig().head + resolvedScalafmtConfig() ) } @@ -20,12 +21,28 @@ trait ScalafmtModule extends JavaModule { .worker() .checkFormat( filesToFormat(sources()), - scalafmtConfig().head + resolvedScalafmtConfig() ) } def scalafmtConfig: Sources = T.sources(T.workspace / ".scalafmt.conf") + // TODO: Do we want provide some defaults or write a default file? + private[ScalafmtModule] def resolvedScalafmtConfig: Task[PathRef] = T.task { + val locs = scalafmtConfig() + locs.find(p => os.exists(p.path)) match { + case None => Result.Failure(s"None of the specified `scalafmtConfig` locations exist. Searched in: ${locs.map(_.path).mkString(", ")}") + case Some(c) if (os.read.lines.stream(c.path).find(_.trim.startsWith("version")).isEmpty) => + Result.Failure( + s"""Found scalafmtConfig file does not specify to scalafmt version to use. + |Please specify the scalafmt version in ${c} + |Example: + |version = "2.4.3" + |""".stripMargin) + case Some(c) => Result.Success(c) + } + } + protected def filesToFormat(sources: Seq[PathRef]) = { for { pathRef <- sources if os.exists(pathRef.path) @@ -52,7 +69,7 @@ object ScalafmtModule extends ExternalModule with ScalafmtModule { .worker() .reformat( files, - scalafmtConfig().head + resolvedScalafmtConfig() ) } @@ -63,7 +80,7 @@ object ScalafmtModule extends ExternalModule with ScalafmtModule { .worker() .checkFormat( files, - scalafmtConfig().head + resolvedScalafmtConfig() ) } diff --git a/scalalib/src/scalafmt/ScalafmtWorker.scala b/scalalib/src/scalafmt/ScalafmtWorker.scala index 8e6eabc6c2b..d82498367d6 100644 --- a/scalalib/src/scalafmt/ScalafmtWorker.scala +++ b/scalalib/src/scalafmt/ScalafmtWorker.scala @@ -66,7 +66,6 @@ private[scalafmt] class ScalafmtWorker { val scalafmt = Scalafmt .create(this.getClass.getClassLoader) - .withRespectVersion(false) def readDefaultScalafmtConfig(): Try[Path] = { Try(JPaths.get(getClass.getResource("default.scalafmt.conf").toURI)) diff --git a/scalalib/test/src/scalafmt/ScalafmtTests.scala b/scalalib/test/src/scalafmt/ScalafmtTests.scala index c04a10d523e..40e1ebfd0a7 100644 --- a/scalalib/test/src/scalafmt/ScalafmtTests.scala +++ b/scalalib/test/src/scalafmt/ScalafmtTests.scala @@ -10,6 +10,8 @@ import utest.framework.TestPath object ScalafmtTests extends TestSuite { + val scalafmtTestVersion = sys.props.getOrElse("TEST_SCALAFMT_VERSION", ???) + trait TestBase extends TestUtil.BaseModule { def millSourcePath = TestUtil.getSrcPathBase() / millOuterCtx.enclosing.split('.') @@ -43,6 +45,12 @@ object ScalafmtTests extends TestSuite { os.remove.all(eval.outPath) os.makeDir.all(m.millSourcePath / os.up) os.copy(resourcePath, m.millSourcePath) + os.write( + m.millSourcePath / ".scalafmt.conf", + s"""version = $scalafmtTestVersion + |runner.dialect = scala213 + |""".stripMargin + ) t(eval) } From 5b2656bbe7676dde9112d32264504061f14bb05e Mon Sep 17 00:00:00 2001 From: Tobias Roeser Date: Tue, 1 Mar 2022 11:55:43 +0100 Subject: [PATCH 2/3] Removed support to read a `default.scalafmt.conf` from classpath --- scalalib/src/scalafmt/ScalafmtWorker.scala | 28 +--------------------- 1 file changed, 1 insertion(+), 27 deletions(-) diff --git a/scalalib/src/scalafmt/ScalafmtWorker.scala b/scalalib/src/scalafmt/ScalafmtWorker.scala index d82498367d6..5fa9822c309 100644 --- a/scalalib/src/scalafmt/ScalafmtWorker.scala +++ b/scalalib/src/scalafmt/ScalafmtWorker.scala @@ -1,7 +1,5 @@ package mill.scalalib.scalafmt -import java.nio.file.{Paths => JPaths} - import mill._ import mill.define.{Discover, ExternalModule, Worker} import mill.api.Ctx @@ -9,11 +7,6 @@ import org.scalafmt.interfaces.Scalafmt import scala.collection.mutable import mill.api.Result -import java.nio.file.Files -import scala.util.Try -import java.nio.file.Path -import scala.util.Failure -import scala.util.Success object ScalafmtWorkerModule extends ExternalModule { def worker: Worker[ScalafmtWorker] = T.worker { new ScalafmtWorker() } @@ -67,22 +60,7 @@ private[scalafmt] class ScalafmtWorker { val scalafmt = Scalafmt .create(this.getClass.getClassLoader) - def readDefaultScalafmtConfig(): Try[Path] = { - Try(JPaths.get(getClass.getResource("default.scalafmt.conf").toURI)) - } - - val (configPath, tmpFileCreated) = - if (os.exists(scalafmtConfig.path)) - (scalafmtConfig.path.toNIO, false) - else { - readDefaultScalafmtConfig() match { - case Success(defaultConfig) => (defaultConfig, false) - case Failure(exception) => { - ctx.log.debug(s"Read default scalafmt config fail, create a temp one") - (JPaths.get(Files.createTempFile("temp", ".scalafmt.conf").toUri), true) - } - } - } + val configPath = scalafmtConfig.path.toNIO // keeps track of files that are misformatted val misformatted = mutable.ListBuffer.empty[PathRef] @@ -108,10 +86,6 @@ private[scalafmt] class ScalafmtWorker { } - if (tmpFileCreated) { - Files.delete(configPath) - } - configSig = scalafmtConfig.sig misformatted.toList } else { From ba34bac5a8cf60caf99674b83cb306339b1dd70e Mon Sep 17 00:00:00 2001 From: Tobias Roeser Date: Tue, 1 Mar 2022 14:32:53 +0100 Subject: [PATCH 3/3] Fixed a type and a missing .path --- scalalib/src/scalafmt/ScalafmtModule.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scalalib/src/scalafmt/ScalafmtModule.scala b/scalalib/src/scalafmt/ScalafmtModule.scala index 1e9080ca713..467b33005e3 100644 --- a/scalalib/src/scalafmt/ScalafmtModule.scala +++ b/scalalib/src/scalafmt/ScalafmtModule.scala @@ -34,8 +34,8 @@ trait ScalafmtModule extends JavaModule { case None => Result.Failure(s"None of the specified `scalafmtConfig` locations exist. Searched in: ${locs.map(_.path).mkString(", ")}") case Some(c) if (os.read.lines.stream(c.path).find(_.trim.startsWith("version")).isEmpty) => Result.Failure( - s"""Found scalafmtConfig file does not specify to scalafmt version to use. - |Please specify the scalafmt version in ${c} + s"""Found scalafmtConfig file does not specify the scalafmt version to use. + |Please specify the scalafmt version in ${c.path} |Example: |version = "2.4.3" |""".stripMargin)