From 7c9549f4705f5d664265a4a04eb659b4be529ff1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wro=C5=84ski?= Date: Tue, 5 Apr 2022 10:43:06 +0200 Subject: [PATCH] Set explicitly version of scalafmt in test --- build.sc | 1 + .../main/scala/scala/cli/commands/Fmt.scala | 20 ++++++++++++++----- .../scala/cli/integration/FmtTests.scala | 5 +++-- website/docs/commands/fmt.md | 11 +++++----- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/build.sc b/build.sc index 4a1710b9d7..66b2f240e0 100644 --- a/build.sc +++ b/build.sc @@ -632,6 +632,7 @@ trait CliIntegrationBase extends SbtModule with ScalaCliPublishModule with HasTe | def scalaSnapshot213 = "${TestDeps.scalaSnapshot213}" | def scala3 = "${Scala.scala3}" | def defaultScala = "${Scala.defaultUser}" + | def defaultScalafmtVersion = "${Deps.scalafmtCli.dep.version}" | def bloopVersion = "${Deps.bloopConfig.dep.version}" | def pprintVersion = "${TestDeps.pprint.dep.version}" | def munitVersion = "${TestDeps.munit.dep.version}" diff --git a/modules/cli/src/main/scala/scala/cli/commands/Fmt.scala b/modules/cli/src/main/scala/scala/cli/commands/Fmt.scala index 2a7fb456ef..caf1958eff 100644 --- a/modules/cli/src/main/scala/scala/cli/commands/Fmt.scala +++ b/modules/cli/src/main/scala/scala/cli/commands/Fmt.scala @@ -77,11 +77,10 @@ object Fmt extends ScalaCommand[FmtOptions] { private implicit class FmtOptionsOps(v: FmtOptions) { import v._ - def binaryUrl(versionMaybe: Option[String]): (String, Boolean) = { - val defaultVersion = versionMaybe.getOrElse(Constants.defaultScalafmtVersion) + def binaryUrl(version: String): (String, Boolean) = { val osArchSuffix0 = osArchSuffix.map(_.trim).filter(_.nonEmpty) .getOrElse(FetchExternalBinary.platformSuffix()) - val tag0 = scalafmtTag.getOrElse("v" + defaultVersion) + val tag0 = scalafmtTag.getOrElse("v" + version) val gitHubOrgName0 = scalafmtGithubOrgName.getOrElse("alexarchambault/scalafmt-native-image") val extension0 = if (Properties.isWin) ".zip" else ".gz" val url = @@ -116,6 +115,17 @@ object Fmt extends ScalaCommand[FmtOptions] { val (versionMaybe, confExists) = readVersionFromFile(workspace, logger) val cache = options.shared.buildOptions(false, None).archiveCache + val version = versionMaybe match { + case Some(v) => v + case None => + System.err.println( + s"""|Scalafmt requires explicitly specified version. + |To configure the scalafmt version add the following line into .scalafmt.conf: + | version = ${Constants.defaultScalafmtVersion} """".stripMargin + ) + sys.exit(1) + } + if (sourceFiles.isEmpty) logger.debug("No source files, not formatting anything") else { @@ -155,7 +165,7 @@ object Fmt extends ScalaCommand[FmtOptions] { case Some(launcher) => os.Path(launcher, os.pwd) case None => - val (url, changing) = options.binaryUrl(versionMaybe) + val (url, changing) = options.binaryUrl(version) FetchExternalBinary.fetch(url, changing, cache, logger, "scalafmt") .orExit(logger) } @@ -172,7 +182,7 @@ object Fmt extends ScalaCommand[FmtOptions] { logger, allowExecve = true, cwd = Some(workspace) - ) + ).waitFor() } } } diff --git a/modules/integration/src/test/scala/scala/cli/integration/FmtTests.scala b/modules/integration/src/test/scala/scala/cli/integration/FmtTests.scala index e40501de67..0a395f3b95 100644 --- a/modules/integration/src/test/scala/scala/cli/integration/FmtTests.scala +++ b/modules/integration/src/test/scala/scala/cli/integration/FmtTests.scala @@ -14,8 +14,9 @@ class FmtTests extends munit.FunSuite { val simpleInputs = TestInputs( Seq( os.rel / ".scalafmt.conf" -> - """runner.dialect = scala213 - |""".stripMargin, + s"""|version = "${Constants.defaultScalafmtVersion}" + |runner.dialect = scala213 + |""".stripMargin, os.rel / "Foo.scala" -> simpleInputsUnformattedContent ) ) diff --git a/website/docs/commands/fmt.md b/website/docs/commands/fmt.md index 9032365f4b..068bb8b097 100644 --- a/website/docs/commands/fmt.md +++ b/website/docs/commands/fmt.md @@ -29,11 +29,12 @@ For a list of all possible values, consult the [official Scala Dialects document scala-cli fmt --dialect scala212 ``` -### Current limitations +### Scalafmt version -At this time, `scala-cli` doesn't read a `scalafmt` version from `.scalafmt.conf` files. -Therefore, in some scenarios, wrong version may be used. In such situations, it is possible to set the version manually if you encounter any issues: +At this time, `scala-cli` read a `scalafmt` version from `.scalafmt.conf` files. If the version is missing, `scala-cli` throws an error, that user should declare explicitly the Scalafmt version. From Scalafmt `3.5.0` it is a mandatory parameter. -```bash -scala-cli fmt --scalafmt-tag v3.0.3 +To configure the Scalafmt version add the following config into `.scalafmt.conf`. For example, to set version `3.5.0` you should add the following line: + +``` +version = "3.5.0" ```