Skip to content

Commit 004606c

Browse files
authored
Merge branch 'main' into scala-wasm-support
2 parents 0022b06 + 27f0821 commit 004606c

2 files changed

Lines changed: 99 additions & 16 deletions

File tree

  • modules

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,29 @@ object Fmt extends ScalaCommand[FmtOptions] {
7676
case _ => "default"
7777
}
7878

79-
val entry = {
80-
val dialect = ScalafmtDialect.fromString(dialectString)
81-
val prevConfMaybe = pathMaybe.map(p => os.read(p))
82-
scalafmtConfigWithFields(prevConfMaybe.getOrElse(""), Some(version), dialect)
83-
}
84-
val scalaFmtConfPath = {
85-
val confFileName = ".scalafmt.conf"
86-
val path =
87-
if (options.saveScalafmtConf) pathMaybe.getOrElse(workspace / confFileName)
88-
else workspace / Constants.workspaceDirName / confFileName
89-
os.write.over(path, entry, createFolders = true)
90-
path
91-
}
79+
val dialect = ScalafmtDialect.fromString(dialectString)
80+
val prevConfMaybe = pathMaybe.map(os.read(_))
81+
val entry = scalafmtConfigWithFields(prevConfMaybe.getOrElse(""), Some(version), dialect)
82+
83+
val confFileName = ".scalafmt.conf"
84+
val canUseDiscoveredInPlace =
85+
!options.saveScalafmtConf
86+
&& options.scalafmtConfStr.isEmpty
87+
&& options.scalafmtConf.isEmpty
88+
&& pathMaybe.isDefined
89+
&& versionMaybe.isDefined
90+
&& dialectMaybe.isDefined
91+
&& options.scalafmtVersion.forall(versionMaybe.contains)
92+
&& options.scalafmtDialect.forall(dialectMaybe.contains)
93+
94+
val scalaFmtConfPath =
95+
if canUseDiscoveredInPlace then pathMaybe.get
96+
else
97+
val path =
98+
if options.saveScalafmtConf then pathMaybe.getOrElse(workspace / confFileName)
99+
else workspace / Constants.workspaceDirName / confFileName
100+
os.write.over(path, entry, createFolders = true)
101+
path
92102

93103
val fmtCommand = options.scalafmtLauncher.filter(_.nonEmpty) match {
94104
case Some(launcher) =>

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

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ class FmtTests extends ScalaCliSuite {
6363
os.rel / "Foo.scala" -> simpleInputsUnformattedContent
6464
)
6565

66+
val simpleInputsWithoutConf: TestInputs = TestInputs(
67+
os.rel / "Foo.scala" -> simpleInputsUnformattedContent
68+
)
69+
70+
private def workspaceConfPath(root: os.Path): os.Path =
71+
root / Constants.workspaceDirName / confFileName
72+
73+
private def expectNoWorkspaceScalafmtConf(root: os.Path): Unit = {
74+
expect(!os.exists(workspaceConfPath(root)))
75+
expect(!os.exists(root / Constants.workspaceDirName))
76+
}
77+
6678
private def noCrLf(input: String): String =
6779
input.replaceAll("\r\n", "\n")
6880

@@ -175,12 +187,73 @@ class FmtTests extends ScalaCliSuite {
175187
}
176188
}
177189

190+
test("complete .scalafmt.conf is not duplicated under .scala-build") {
191+
simpleInputs.fromRoot { root =>
192+
os.proc(TestUtil.cli, "fmt", ".").call(cwd = root)
193+
val updatedContent = noCrLf(os.read(root / "Foo.scala"))
194+
expect(updatedContent == expectedSimpleInputsFormattedContent)
195+
expectNoWorkspaceScalafmtConf(root)
196+
}
197+
}
198+
199+
test("complete .scalafmt.conf + matching --scalafmt-version still avoids .scala-build") {
200+
simpleInputs.fromRoot { root =>
201+
os.proc(
202+
TestUtil.cli,
203+
"fmt",
204+
".",
205+
"--scalafmt-version",
206+
Constants.defaultScalafmtVersion
207+
).call(cwd = root)
208+
val updatedContent = noCrLf(os.read(root / "Foo.scala"))
209+
expect(updatedContent == expectedSimpleInputsFormattedContent)
210+
expectNoWorkspaceScalafmtConf(root)
211+
}
212+
}
213+
214+
test("complete .scalafmt.conf + matching --scalafmt-dialect still avoids .scala-build") {
215+
simpleInputs.fromRoot { root =>
216+
os.proc(TestUtil.cli, "fmt", ".", "--scalafmt-dialect", "scala213").call(cwd = root)
217+
val updatedContent = noCrLf(os.read(root / "Foo.scala"))
218+
expect(updatedContent == expectedSimpleInputsFormattedContent)
219+
expectNoWorkspaceScalafmtConf(root)
220+
}
221+
}
222+
223+
test("complete .scalafmt.conf in git root is used in place") {
224+
simpleInputs.fromRoot { root =>
225+
TestUtil.initializeGit(root)
226+
val subdir = root / "subdir"
227+
os.makeDir.all(subdir)
228+
os.move(root / "Foo.scala", subdir / "Foo.scala")
229+
os.proc(TestUtil.cli, "fmt", ".").call(cwd = subdir)
230+
val updatedContent = noCrLf(os.read(subdir / "Foo.scala"))
231+
expect(updatedContent == expectedSimpleInputsFormattedContent)
232+
expectNoWorkspaceScalafmtConf(subdir)
233+
expectNoWorkspaceScalafmtConf(root)
234+
}
235+
}
236+
237+
test("no .scalafmt.conf still triggers .scala-build") {
238+
simpleInputsWithoutConf.fromRoot { root =>
239+
// Isolate from the enclosing scala-cli git repo
240+
// (whose .scalafmt.conf would otherwise be discovered)
241+
TestUtil.initializeGit(root)
242+
val confPath = workspaceConfPath(root)
243+
expect(!os.exists(confPath))
244+
os.proc(TestUtil.cli, "fmt", ".").call(cwd = root)
245+
expect(os.exists(confPath))
246+
val updatedContent = noCrLf(os.read(root / "Foo.scala"))
247+
expect(updatedContent == expectedSimpleInputsFormattedContent)
248+
}
249+
}
250+
178251
test("creating workspace conf file") {
179252
simpleInputsWithDialectOnly.fromRoot { root =>
180-
val workspaceConfPath = root / Constants.workspaceDirName / confFileName
181-
expect(!os.exists(workspaceConfPath))
253+
val confPath = workspaceConfPath(root)
254+
expect(!os.exists(confPath))
182255
os.proc(TestUtil.cli, "fmt", ".").call(cwd = root)
183-
expect(os.exists(workspaceConfPath))
256+
expect(os.exists(confPath))
184257
val updatedContent = noCrLf(os.read(root / "Foo.scala"))
185258
expect(updatedContent == expectedSimpleInputsFormattedContent)
186259
}

0 commit comments

Comments
 (0)