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

add sbt 2.x cross build setting #332

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- run:
# for git tests
git config --global user.email "scalafmt@scalameta.org" && git config --global user.name "scalafmt"
- run: sbt plugin/scripted
- run: sbt plugin/scripted "++ 3.x" compile

formatting:
runs-on: ubuntu-latest
Expand Down
20 changes: 17 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ inThisBuild(List(
),
resolvers ++= Resolver.sonatypeOssRepos("public"),
scalaVersion := "2.12.20",
crossScalaVersions += "3.3.4",
packageDoc / publishArtifact := insideCI.value,
packageSrc / publishArtifact := insideCI.value,
))
Expand All @@ -47,14 +48,27 @@ lazy val plugin = project.enablePlugins(SbtPlugin).settings(
moduleName := "sbt-scalafmt",
libraryDependencies ++= List(
"com.googlecode.java-diff-utils" % "diffutils" % "1.3.0",
"org.scalameta" %% "scalafmt-sysops" % scalafmtVersion,
"org.scalameta" %% "scalafmt-dynamic" % scalafmtVersion,
"org.scalameta" %% "scalafmt-sysops" % scalafmtVersion cross
CrossVersion.for3Use2_13,
"org.scalameta" %% "scalafmt-dynamic" % scalafmtVersion cross
CrossVersion.for3Use2_13,
),
scriptedBufferLog := false,
scriptedLaunchOpts += s"-Dplugin.version=${version.value}",
// For compat reasons we have this in here to ensure we are testing against 1.2.8
// We honestly probably don't need to, so if this ever causes issues, rip it out.
pluginCrossBuild / sbtVersion := "1.2.8",
pluginCrossBuild / sbtVersion := {
scalaBinaryVersion.value match {
case "2.12" => "1.2.8"
case _ => "2.0.0-M2"
}
},
conflictWarning := {
scalaBinaryVersion.value match {
case "3" => ConflictWarning("warn", Level.Warn, false)
case _ => conflictWarning.value
}
},
)

// For some reason, it doesn't work if this is defined in globalSettings in PublishPlugin.
Expand Down
14 changes: 8 additions & 6 deletions plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import java.io.OutputStreamWriter
import java.nio.file.Path

import sbt.Keys._
import sbt._
// format: off
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this necessary? if this is scala3 syntax, better to modify scalafmt configuration, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imports {
expand = true

without this comment expand import

- import sbt.{given, _}
+ import sbt._
+ import sbt.given

but error if Scala 2.12

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this is an unreported bug in scalafmt?

import sbt.{given, _}
// format: on
import sbt.librarymanagement.MavenRepository
import sbt.util.CacheImplicits._
import sbt.util.CacheStoreFactory
Expand Down Expand Up @@ -396,8 +398,8 @@ object ScalafmtPlugin extends AutoPlugin {
private def getScalafmtSourcesTask(
f: (Seq[File], Seq[File], FormatSession) => InitTask,
) = Def.taskDyn[Unit] {
val sources = unmanagedSources.in(scalafmt).?.value.getOrElse(Seq.empty)
val dirs = unmanagedSourceDirectories.in(scalafmt).?.value.getOrElse(Nil)
val sources = (scalafmt / unmanagedSources).?.value.getOrElse(Seq.empty)
val dirs = (scalafmt / unmanagedSourceDirectories).?.value.getOrElse(Nil)
getScalafmtTask(f)(sources, dirs, scalaConfig.value)
}

Expand Down Expand Up @@ -462,10 +464,10 @@ object ScalafmtPlugin extends AutoPlugin {
scalafmtCheck := getScalafmtSourcesTask(scalafmtCheckTask).value,
scalafmtSbtCheck := getScalafmtSbtTasks(scalafmtSbtCheckTask).value,
scalafmtDoFormatOnCompile := Def.settingDyn {
if (scalafmtOnCompile.value) scalafmt.in(resolvedScoped.value.scope)
if (scalafmtOnCompile.value) resolvedScoped.value.scope / scalafmt
else Def.task(())
}.value,
sources.in(Compile) := sources.in(Compile)
Compile / sources := (Compile / sources)
.dependsOn(scalafmtDoFormatOnCompile).value,
scalafmtOnly := {
val files = spaceDelimited("<files>").parsed
Expand Down Expand Up @@ -509,7 +511,7 @@ object ScalafmtPlugin extends AutoPlugin {
)

override def buildSettings: Seq[Def.Setting[_]] =
Seq(scalafmtConfig := baseDirectory.in(ThisBuild).value / ".scalafmt.conf")
Seq(scalafmtConfig := (ThisBuild / baseDirectory).value / ".scalafmt.conf")

override def globalSettings: Seq[Def.Setting[_]] = Seq(
scalafmtFilter := "",
Expand Down