Skip to content

Commit 179f498

Browse files
committed
Reformat changed files in all build roots
`ScalafmtAlg#reformatChanged` is working on the `Repo`-level but we're reading the Scalafmt version in `ScalafmtAlg#getScalafmtVersion` on the `BuildRoot`-level. This means we detect a dependency on Scalafmt in a build root but when we reformat changed files, we'll do it in the root directory of the repo. If the root directory contains no `.scalafmt.conf`, reformating fails with an error as shown in scala-steward-org/scala-steward-action#475 (comment) With this change we now run Scalafmt in all build roots instead of only the root directory. This fixes the error mentioned above. In the future we could restrict running Scalafmt in only those build roots where we did changes by matching the `updateReplacements` (that have been determined in `EditAlg`) with the build roots.
1 parent 0706345 commit 179f498

File tree

5 files changed

+14
-11
lines changed

5 files changed

+14
-11
lines changed

modules/core/src/main/scala/org/scalasteward/core/buildtool/BuildToolDispatcher.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ final class BuildToolDispatcher[F[_]](implicit
5353
buildTools.traverse_(_.runMigration(buildRoot, migration))
5454
})
5555

56-
private def getBuildRoots(repo: Repo, repoConfig: RepoConfig): List[BuildRoot] =
57-
repoConfig.buildRootsOrDefault.map(buildRootCfg => BuildRoot(repo, buildRootCfg.relativePath))
58-
5956
private val allBuildTools = List(mavenAlg, millAlg, sbtAlg, scalaCliAlg)
6057
private val fallbackBuildTool = List(sbtAlg)
6158

@@ -69,5 +66,5 @@ final class BuildToolDispatcher[F[_]](implicit
6966
repo: Repo,
7067
repoConfig: RepoConfig
7168
): F[List[(BuildRoot, List[BuildToolAlg[F]])]] =
72-
getBuildRoots(repo, repoConfig).traverse(findBuildTools)
69+
repoConfig.buildRootsOrDefault(repo).traverse(findBuildTools)
7370
}

modules/core/src/main/scala/org/scalasteward/core/edit/EditAlg.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,10 @@ final class EditAlg[F[_]](implicit
118118
val reformat =
119119
data.config.scalafmt.runAfterUpgradingOrDefault && data.cache.dependsOn(List(scalafmtModule))
120120
F.whenA(reformat) {
121-
logger.attemptWarn.log_("Reformatting changed files failed") {
122-
scalafmtAlg.reformatChanged(data.repo)
121+
data.config.buildRootsOrDefault(data.repo).traverse_ { buildRoot =>
122+
logger.attemptWarn.log_(s"Reformatting changed files failed in ${buildRoot.relativePath}") {
123+
scalafmtAlg.reformatChanged(buildRoot)
124+
}
123125
}
124126
}
125127
}

modules/core/src/main/scala/org/scalasteward/core/repoconfig/RepoConfig.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import io.circe.Codec
2222
import io.circe.generic.extras.Configuration
2323
import io.circe.generic.extras.semiauto._
2424
import io.circe.syntax._
25+
import org.scalasteward.core.buildtool.BuildRoot
26+
import org.scalasteward.core.data.Repo
2527
import org.scalasteward.core.edit.hooks.PostUpdateHook
2628

2729
final case class RepoConfig(
@@ -36,10 +38,11 @@ final case class RepoConfig(
3638
reviewers: List[String] = List.empty,
3739
dependencyOverrides: List[GroupRepoConfig] = List.empty
3840
) {
39-
def buildRootsOrDefault: List[BuildRootConfig] =
41+
def buildRootsOrDefault(repo: Repo): List[BuildRoot] =
4042
buildRoots
4143
.map(_.filterNot(_.relativePath.contains("..")))
4244
.getOrElse(List(BuildRootConfig.repoRoot))
45+
.map(cfg => BuildRoot(repo, cfg.relativePath))
4346

4447
def postUpdateHooksOrDefault: List[PostUpdateHook] =
4548
postUpdateHooks.getOrElse(Nil).map(_.toHook)

modules/core/src/main/scala/org/scalasteward/core/scalafmt/ScalafmtAlg.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import cats.syntax.all._
2222
import io.circe.ParsingFailure
2323
import org.scalasteward.core.application.Config
2424
import org.scalasteward.core.buildtool.BuildRoot
25-
import org.scalasteward.core.data.{Repo, Scope, Version}
25+
import org.scalasteward.core.data.{Scope, Version}
2626
import org.scalasteward.core.io.process.SlurpOptions
2727
import org.scalasteward.core.io.{FileAlg, ProcessAlg, WorkspaceAlg}
2828
import org.scalasteward.core.scalafmt.ScalafmtAlg.{opts, parseScalafmtConf}
@@ -52,9 +52,9 @@ final class ScalafmtAlg[F[_]](config: Config)(implicit
5252
.map(version => Scope(List(scalafmtDependency(version)), List(config.defaultResolver)))
5353
.value
5454

55-
def reformatChanged(repo: Repo): F[Unit] =
55+
def reformatChanged(buildRoot: BuildRoot): F[Unit] =
5656
for {
57-
repoDir <- workspaceAlg.repoDir(repo)
57+
repoDir <- workspaceAlg.buildRootDir(buildRoot)
5858
cmd = Nel.of(scalafmtBinary, opts.nonInteractive) ++ opts.modeChanged
5959
_ <- processAlg.exec(cmd, repoDir, slurpOptions = SlurpOptions.ignoreBufferOverflow)
6060
} yield ()

modules/core/src/test/scala/org/scalasteward/core/repoconfig/RepoConfigAlgTest.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,9 @@ class RepoConfigAlgTest extends FunSuite {
243243
}
244244

245245
test("build root with '..'") {
246+
val repo = Repo("typelevel", "cats")
246247
val content = """buildRoots = [ "../../../etc" ]"""
247-
val config = RepoConfigAlg.parseRepoConfig(content).map(_.buildRootsOrDefault)
248+
val config = RepoConfigAlg.parseRepoConfig(content).map(_.buildRootsOrDefault(repo))
248249
assertEquals(config, Right(Nil))
249250
}
250251

0 commit comments

Comments
 (0)