Skip to content

Commit fe00edd

Browse files
authored
Fix fmt to format the project.scala configuration file as any other Scala input (#3609)
1 parent 36c8b3e commit fe00edd

File tree

2 files changed

+20
-7
lines changed
  • modules

2 files changed

+20
-7
lines changed

modules/cli/src/main/scala/scala/cli/commands/fmt/Fmt.scala

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import caseapp.*
44
import caseapp.core.help.HelpFormat
55
import dependency.*
66

7-
import scala.build.input.{Inputs, Script, SourceScalaFile}
7+
import scala.build.input.{Inputs, ProjectScalaFile, Script, SourceScalaFile}
88
import scala.build.internal.{Constants, ExternalBinaryParams, FetchExternalBinary, Runner}
99
import scala.build.options.BuildOptions
1010
import scala.build.{Logger, Sources}
@@ -44,14 +44,11 @@ object Fmt extends ScalaCommand[FmtOptions] {
4444

4545
// TODO If no input is given, just pass '.' to scalafmt?
4646
val (sourceFiles, workspace, _) =
47-
if (args.all.isEmpty)
48-
(Seq(os.pwd), os.pwd, None)
47+
if args.all.isEmpty then (Seq(os.pwd), os.pwd, None)
4948
else {
5049
val i = options.shared.inputs(args.all).orExit(logger)
51-
val s = i.sourceFiles().collect {
52-
case sc: Script => sc.path
53-
case sc: SourceScalaFile => sc.path
54-
}
50+
type FormattableSourceFile = Script | SourceScalaFile | ProjectScalaFile
51+
val s = i.sourceFiles().collect { case sc: FormattableSourceFile => sc.path }
5552
(s, i.workspace, Some(i))
5653
}
5754
CurrentParams.workspaceOpt = Some(workspace)

modules/integration/src/test/scala/scala/cli/integration/FmtTests.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,20 @@ class FmtTests extends ScalaCliSuite {
216216
expect(fmtVersionHelp.contains(s"(${Constants.defaultScalafmtVersion} by default)"))
217217
}
218218
}
219+
220+
test("project.scala gets formatted correctly, as any other input") {
221+
val projectFileName = "project.scala"
222+
TestInputs(
223+
os.rel / projectFileName -> simpleInputsUnformattedContent,
224+
os.rel / confFileName ->
225+
s"""|version = "${Constants.defaultScalafmtVersion}"
226+
|runner.dialect = scala3
227+
|""".stripMargin
228+
)
229+
.fromRoot { root =>
230+
os.proc(TestUtil.cli, "fmt", ".").call(cwd = root)
231+
val updatedContent = noCrLf(os.read(root / projectFileName))
232+
expect(updatedContent == expectedSimpleInputsFormattedContent)
233+
}
234+
}
219235
}

0 commit comments

Comments
 (0)