Skip to content

Commit

Permalink
CLI: add instructions when version is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed May 22, 2023
1 parent cba861d commit b42d62b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
8 changes: 7 additions & 1 deletion scalafmt-cli/src/main/scala/org/scalafmt/cli/Cli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,13 @@ object Cli {
// - `scalafmt-core` if the specified `version` setting match with build version
// (or if the `version` is not specified).
options.getVersionOpt match {
case None => Left(s"error: missing version (current $stableVersion)")
case None =>
Left(s"""error: missing Scalafmt version.
|Consider adding the following line to '${options.getProposedConfigFile}':
|```
|version = '$stableVersion'
|```
|""".stripMargin)
case Some(`stableVersion`) =>
options.common.debug.println(s"Using core runner [$stableVersion]")
Right(ScalafmtCoreRunner)
Expand Down
5 changes: 5 additions & 0 deletions scalafmt-cli/src/main/scala/org/scalafmt/cli/CliOptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ case class CliOptions(
private lazy val canonicalConfigFile: Option[Try[Path]] =
gitOps.getCanonicalConfigFile(cwd, config)

private[cli] def getProposedConfigFile: Path =
canonicalConfigFile.flatMap(_.toOption).getOrElse {
gitOps.getProposedConfigFile(cwd, config).path
}

/** Parse the scalafmt configuration and try to encode it to `ScalafmtConfig`.
* If `--config-str` is specified, this will parse the configuration string
* specified by `--config-str`. Otherwise, a contents of configuration file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import org.scalafmt.CompatCollections.JavaConverters._

object FileOps {

val defaultConfigFileName = ".scalafmt.conf"

def getLastModifiedMsec(file: Path): Long = {
val attributes = getAttributesNoLinks(file)
val mtime = attributes.lastModifiedTime().toMillis
Expand Down Expand Up @@ -134,7 +136,7 @@ object FileOps {
}

def tryGetConfigInDir(dir: AbsoluteFile): Option[Try[Path]] =
tryCheckConfigFile(dir / ".scalafmt.conf")
tryCheckConfigFile(dir / defaultConfigFileName)

private def tryCheckConfigFile(file: AbsoluteFile): Option[Try[Path]] =
if (!file.exists) None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ object GitOps {
def getRootConfigFile: Option[Try[Path]] =
obj.rootDir.flatMap(FileOps.tryGetConfigInDir)

def getProposedConfigFile(
cwd: AbsoluteFile,
config: Option[Path] = None
): AbsoluteFile =
config.fold {
obj.rootDir.getOrElse(cwd) / FileOps.defaultConfigFileName
}(cwd / _)

}

}
Expand Down
16 changes: 10 additions & 6 deletions scalafmt-tests/src/test/scala/org/scalafmt/cli/CliTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -813,9 +813,11 @@ class CliTest extends AbstractCliTest with CliTestBehavior {
),
ExitCode.UnsupportedVersion,
assertOut = out => {
val eol = System.lineSeparator()
val msg = s"error: missing version (current $stableVersion)$eol"
assertEquals(out, msg + msg) // two invocations
val out1 = out.substring(0, out.length / 2)
val out2 = out.substring(out1.length)
assertEquals(out1, out2) // two invocations
val msg = s"error: missing Scalafmt version."
assertEquals(out1.substring(0, msg.length), msg)
}
)
}
Expand All @@ -838,9 +840,11 @@ class CliTest extends AbstractCliTest with CliTestBehavior {
),
ExitCode.UnsupportedVersion,
assertOut = out => {
val eol = System.lineSeparator()
val msg = s"error: missing version (current $stableVersion)$eol"
assertEquals(out, msg + msg) // two invocations
val out1 = out.substring(0, out.length / 2)
val out2 = out.substring(out1.length)
assertEquals(out1, out2) // two invocations
val msg = s"error: missing Scalafmt version."
assertEquals(out1.substring(0, msg.length), msg)
}
)
}
Expand Down

0 comments on commit b42d62b

Please sign in to comment.