Skip to content

Commit

Permalink
Set explicitly version of scalafmt in test
Browse files Browse the repository at this point in the history
  • Loading branch information
lwronski committed Apr 8, 2022
1 parent cfb9b51 commit 7c9549f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
1 change: 1 addition & 0 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
20 changes: 15 additions & 5 deletions modules/cli/src/main/scala/scala/cli/commands/Fmt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -172,7 +182,7 @@ object Fmt extends ScalaCommand[FmtOptions] {
logger,
allowExecve = true,
cwd = Some(workspace)
)
).waitFor()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
Expand Down
11 changes: 6 additions & 5 deletions website/docs/commands/fmt.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```

0 comments on commit 7c9549f

Please sign in to comment.