Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SBT: don't build dynamic for NativePlatform #4741

Merged
merged 1 commit into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ commands += Command.command("ci-test-native") { s =>
s"++$scalaVersion" :: "testsNative/test" :: s
}

lazy val dynamic = crossProject(JVMPlatform, NativePlatform)
lazy val dynamic = crossProject(JVMPlatform) // don't build for NativePlatform
.withoutSuffixFor(JVMPlatform).in(file("scalafmt-dynamic")).settings(
moduleName := "scalafmt-dynamic",
description := "Implementation of scalafmt-interfaces",
Expand Down Expand Up @@ -217,7 +217,8 @@ lazy val cli = crossProject(JVMPlatform, NativePlatform)
}
},
).nativeSettings(scalaNativeConfig).jvmEnablePlugins(NativeImagePlugin)
.dependsOn(core, dynamic).aggregate(core, dynamic)
.dependsOn(core, interfaces).aggregate(core)
.jvmConfigure(_.dependsOn(dynamic.jvm).aggregate(dynamic.jvm))

lazy val tests = crossProject(JVMPlatform, NativePlatform)
.withoutSuffixFor(JVMPlatform).in(file("scalafmt-tests")).settings(
Expand All @@ -233,8 +234,7 @@ lazy val tests = crossProject(JVMPlatform, NativePlatform)
.get
}),
).enablePlugins(BuildInfoPlugin)
.jvmSettings(javaOptions += "-Dfile.encoding=UTF8")
.dependsOn(core, dynamic, cli)
.jvmSettings(javaOptions += "-Dfile.encoding=UTF8").dependsOn(core, cli)

lazy val sharedTestSettings = Seq(libraryDependencies += munit.value % Test)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ private[scalafmt] trait CliUtils {
nGContext.exit(exit.code)
}

protected def getDynamicRunner(): ScalafmtRunner = ScalafmtDynamicRunner
protected def getDynamicRunner: Option[ScalafmtRunner] =
Some(ScalafmtDynamicRunner)

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.scalafmt.sysops.FileOps
import java.nio.file.Path
import java.util.concurrent.atomic.AtomicReference

import util.control.Breaks._
import scala.util.control.Breaks._

object ScalafmtDynamicRunner extends ScalafmtRunner {
override private[cli] def run(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package org.scalafmt.cli
private[scalafmt] trait CliUtils {
protected val isScalaNative: Boolean = true

protected def getDynamicRunner(): ScalafmtRunner = ???
protected def getDynamicRunner: Option[ScalafmtRunner] = None
}
40 changes: 21 additions & 19 deletions scalafmt-cli/shared/src/main/scala/org/scalafmt/cli/Cli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,29 @@ object Cli extends CliUtils {
case Right(runner) => runWithRunner(options, runner)
}

private val isNative: Boolean = isScalaNative ||
"true" == System.getProperty("scalafmt.native-image", "false")
private val isNativeImage: Boolean = "true" ==
System.getProperty("scalafmt.native-image", "false")

private def getProposedConfigVersion(options: CliOptions): String =
s"version = $stableVersion"

private type MaybeRunner = Either[String, ScalafmtRunner]

private def noDynamicRunner(version: String, options: CliOptions) = {
val path = options.configPath
s"""|error: invalid Scalafmt version.
|
|This Scalafmt installation has version '$stableVersion' and the version configured in '$path' is '$version'.
|To fix this problem, add the following line to .scalafmt.conf:
|```
|version = $stableVersion
|```
|
|NOTE: this error happens only when running a native Scalafmt binary.
|Scalafmt automatically installs and invokes the correct version of Scalafmt when running on the JVM.
|""".stripMargin
}

private def findRunner(options: CliOptions): MaybeRunner = options.hoconOpt
.fold[MaybeRunner](Left(
s"""|error: missing Scalafmt configuration file.
Expand Down Expand Up @@ -98,24 +113,11 @@ object Cli extends CliUtils {
case Right(`stableVersion`) =>
options.common.debug.println(s"Using core runner [$stableVersion]")
Right(ScalafmtCoreRunner)
case Right(v) if isNative =>
Left {
s"""|error: invalid Scalafmt version.
|
|This Scalafmt installation has version '$stableVersion' and the version configured in '${options
.configPath}' is '$v'.
|To fix this problem, add the following line to .scalafmt.conf:
|```
|version = $stableVersion
|```
|
|NOTE: this error happens only when running a native Scalafmt binary.
|Scalafmt automatically installs and invokes the correct version of Scalafmt when running on the JVM.
|""".stripMargin
}
case Right(v) =>
options.common.debug.println(s"Using dynamic runner [$v]")
Right(getDynamicRunner())
val runnerOpt = if (isNativeImage) None else getDynamicRunner
if (runnerOpt.isDefined) options.common.debug
.println(s"Using dynamic runner [$v]")
runnerOpt.toRight(noDynamicRunner(v, options))
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.scalafmt.cli

import org.scalafmt.Error._
import org.scalafmt.dynamic.exceptions.ScalafmtException
import org.scalafmt.interfaces.PositionException
import org.scalafmt.interfaces.ScalafmtReporter
import org.scalafmt.interfaces._

import java.io.OutputStreamWriter
import java.io.PrintWriter
Expand Down Expand Up @@ -32,8 +30,11 @@ class ScalafmtCliReporter(options: CliOptions) extends ScalafmtReporter {
case MisformattedFile(_, diff) =>
options.common.err.println(diff)
exitCode.getAndUpdate(ExitCode.merge(ExitCode.TestError, _))
case ScalafmtException(_, cause) => error(file, cause)
case _ if !options.ignoreWarnings =>
case e: ScalafmtException => error(file, e.getCause)
case _ if e.getClass.getSimpleName.contains("ScalafmtException") =>
error(file, e.getCause)
case _ if options.ignoreWarnings =>
case _ =>
new FailedToFormat(file.toString, e).printStackTrace(options.common.err)
exitCode.getAndUpdate(ExitCode.merge(ExitCode.UnexpectedError, _))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.scalafmt.dynamic.exceptions

import org.scalafmt.interfaces

import scala.util.control.NoStackTrace

case class ScalafmtException(message: String, cause: Throwable)
extends Exception(message, cause) with NoStackTrace
extends interfaces.ScalafmtException(message, cause) with NoStackTrace
Loading