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

Formatting error in complex agrument types #4318

Closed
skitanoooo opened this issue Sep 24, 2024 · 0 comments · Fixed by #4415
Closed

Formatting error in complex agrument types #4318

skitanoooo opened this issue Sep 24, 2024 · 0 comments · Fixed by #4415

Comments

@skitanoooo
Copy link

skitanoooo commented Sep 24, 2024

Configuration (required)

version = 3.8.3
runner.dialect = scala213source3
preset = default

newlines.source = keep
align.openParenCallSite = true

Command-line parameters (required)

When I run scalafmt via CLI like this: scalafmt A.scala

Steps

Given code like this:

package foo

object A {
  def f(a: (
             Seq[Int],
               Seq[Int]) =
           (Seq(), Seq())): Unit = ???
}

Problem

Scalafmt formats code like this:

> scalafmt <path>/A.scala
org.scalafmt.cli.FailedToFormat: <path>/A.scala
Caused by: org.scalafmt.Error$PreciseIncomplete: <path>/A.scala:4: error: Unable to format file due to bug in scalafmt
  def f(a: (
           ^
	at org.scalafmt.Scalafmt$.$anonfun$doFormatOne$6(Scalafmt.scala:123)
	at scala.util.Success.flatMap(Try.scala:266)
	at org.scalafmt.Scalafmt$.$anonfun$doFormatOne$3(Scalafmt.scala:118)
	at scala.meta.parsers.Parsed.fold(Errors.scala:11)
	at scala.meta.parsers.Parsed.fold$(Errors.scala:10)
	at scala.meta.parsers.Parsed$Success.fold(Errors.scala:25)
	at org.scalafmt.Scalafmt$.doFormatOne(Scalafmt.scala:114)
	at org.scalafmt.Scalafmt$.doFormat(Scalafmt.scala:90)
	at org.scalafmt.Scalafmt$.$anonfun$formatCode$3(Scalafmt.scala:78)
	at scala.util.Success.fold(Try.scala:289)
	at org.scalafmt.Scalafmt$.formatCode(Scalafmt.scala:78)
	at org.scalafmt.Scalafmt$.format(Scalafmt.scala:57)
	at org.scalafmt.Scalafmt.format(Scalafmt.scala)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at org.scalafmt.dynamic.ScalafmtReflect.$anonfun$tryFormat$1(ScalafmtReflect.scala:121)
	at scala.util.Try$.apply(Try.scala:217)
	at org.scalafmt.dynamic.ScalafmtReflect.tryFormat(ScalafmtReflect.scala:117)
	at org.scalafmt.dynamic.ScalafmtReflectConfig.tryFormat(ScalafmtReflectConfig.scala:95)
	at org.scalafmt.dynamic.ScalafmtDynamicSession.$anonfun$tryForceFormat$1(ScalafmtDynamicSession.scala:49)
	at scala.util.Success.flatMap(Try.scala:266)
	at org.scalafmt.dynamic.ScalafmtDynamicSession.tryForceFormat(ScalafmtDynamicSession.scala:49)
	at org.scalafmt.dynamic.ScalafmtDynamicSession.tryFormat(ScalafmtDynamicSession.scala:36)
	at org.scalafmt.dynamic.ScalafmtDynamicSession.format(ScalafmtDynamicSession.scala:18)
	at org.scalafmt.cli.ScalafmtDynamicRunner$.handleFile(ScalafmtDynamicRunner.scala:74)
	at org.scalafmt.cli.ScalafmtDynamicRunner$.$anonfun$run$7(ScalafmtDynamicRunner.scala:48)
	at org.scalafmt.cli.ScalafmtDynamicRunner$.$anonfun$run$7$adapted(ScalafmtDynamicRunner.scala:46)
	at scala.collection.IterableOnceOps.foreach(IterableOnce.scala:619)
	at scala.collection.IterableOnceOps.foreach$(IterableOnce.scala:617)
	at scala.collection.AbstractIterator.foreach(Iterator.scala:1303)
	at scala.collection.parallel.ParIterableLike$Foreach.leaf(ParIterableLike.scala:938)
	at scala.collection.parallel.Task.$anonfun$tryLeaf$1(Tasks.scala:52)
	at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
	at scala.util.control.Breaks$$anon$1.catchBreak(Breaks.scala:97)
	at scala.collection.parallel.Task.tryLeaf(Tasks.scala:55)
	at scala.collection.parallel.Task.tryLeaf$(Tasks.scala:49)
	at scala.collection.parallel.ParIterableLike$Foreach.tryLeaf(ParIterableLike.scala:935)
	at scala.collection.parallel.AdaptiveWorkStealingTasks$AWSTWrappedTask.compute(Tasks.scala:152)
	at scala.collection.parallel.AdaptiveWorkStealingTasks$AWSTWrappedTask.compute$(Tasks.scala:148)
	at scala.collection.parallel.AdaptiveWorkStealingForkJoinTasks$AWSFJTWrappedTask.compute(Tasks.scala:304)
	at java.base/java.util.concurrent.RecursiveAction.exec(RecursiveAction.java:194)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:387)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1312)
	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1843)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1808)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:188)

Expectation

Workaround

If either of these lines are removed, it will not reproduce

newlines.source = keep
align.openParenCallSite = true

Notes

This code also raises an error.

package foo

object A {
//  def f(a: (
//             Seq[Int],
//               Seq[Int]) =
//           (Seq(), Seq())): Unit = ???

  private def g: Either[
    (
      Int, Option[Int]),
    (Int, Option[Int])
  ] = ???
}
> scalafmt <path>/A.scala
org.scalafmt.cli.FailedToFormat: <path>/A.scala
Caused by: org.scalafmt.Error$PreciseIncomplete: <path>/A.scala:10: error: Unable to format file due to bug in scalafmt
    (
    ^
	at org.scalafmt.Scalafmt$.$anonfun$doFormatOne$6(Scalafmt.scala:123)
	at scala.util.Success.flatMap(Try.scala:266)
	at org.scalafmt.Scalafmt$.$anonfun$doFormatOne$3(Scalafmt.scala:118)
	at scala.meta.parsers.Parsed.fold(Errors.scala:11)
	at scala.meta.parsers.Parsed.fold$(Errors.scala:10)
	at scala.meta.parsers.Parsed$Success.fold(Errors.scala:25)
	at org.scalafmt.Scalafmt$.doFormatOne(Scalafmt.scala:114)
	at org.scalafmt.Scalafmt$.doFormat(Scalafmt.scala:90)
	at org.scalafmt.Scalafmt$.$anonfun$formatCode$3(Scalafmt.scala:78)
	at scala.util.Success.fold(Try.scala:289)
	at org.scalafmt.Scalafmt$.formatCode(Scalafmt.scala:78)
	at org.scalafmt.Scalafmt$.format(Scalafmt.scala:57)
	at org.scalafmt.Scalafmt.format(Scalafmt.scala)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at org.scalafmt.dynamic.ScalafmtReflect.$anonfun$tryFormat$1(ScalafmtReflect.scala:121)
	at scala.util.Try$.apply(Try.scala:217)
	at org.scalafmt.dynamic.ScalafmtReflect.tryFormat(ScalafmtReflect.scala:117)
	at org.scalafmt.dynamic.ScalafmtReflectConfig.tryFormat(ScalafmtReflectConfig.scala:95)
	at org.scalafmt.dynamic.ScalafmtDynamicSession.$anonfun$tryForceFormat$1(ScalafmtDynamicSession.scala:49)
	at scala.util.Success.flatMap(Try.scala:266)
	at org.scalafmt.dynamic.ScalafmtDynamicSession.tryForceFormat(ScalafmtDynamicSession.scala:49)
	at org.scalafmt.dynamic.ScalafmtDynamicSession.tryFormat(ScalafmtDynamicSession.scala:36)
	at org.scalafmt.dynamic.ScalafmtDynamicSession.format(ScalafmtDynamicSession.scala:18)
	at org.scalafmt.cli.ScalafmtDynamicRunner$.handleFile(ScalafmtDynamicRunner.scala:74)
	at org.scalafmt.cli.ScalafmtDynamicRunner$.$anonfun$run$7(ScalafmtDynamicRunner.scala:48)
	at org.scalafmt.cli.ScalafmtDynamicRunner$.$anonfun$run$7$adapted(ScalafmtDynamicRunner.scala:46)
	at scala.collection.IterableOnceOps.foreach(IterableOnce.scala:619)
	at scala.collection.IterableOnceOps.foreach$(IterableOnce.scala:617)
	at scala.collection.AbstractIterator.foreach(Iterator.scala:1303)
	at scala.collection.parallel.ParIterableLike$Foreach.leaf(ParIterableLike.scala:938)
	at scala.collection.parallel.Task.$anonfun$tryLeaf$1(Tasks.scala:52)
	at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
	at scala.util.control.Breaks$$anon$1.catchBreak(Breaks.scala:97)
	at scala.collection.parallel.Task.tryLeaf(Tasks.scala:55)
	at scala.collection.parallel.Task.tryLeaf$(Tasks.scala:49)
	at scala.collection.parallel.ParIterableLike$Foreach.tryLeaf(ParIterableLike.scala:935)
	at scala.collection.parallel.AdaptiveWorkStealingTasks$AWSTWrappedTask.compute(Tasks.scala:152)
	at scala.collection.parallel.AdaptiveWorkStealingTasks$AWSTWrappedTask.compute$(Tasks.scala:148)
	at scala.collection.parallel.AdaptiveWorkStealingForkJoinTasks$AWSFJTWrappedTask.compute(Tasks.scala:304)
	at java.base/java.util.concurrent.RecursiveAction.exec(RecursiveAction.java:194)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:387)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1312)
	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1843)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1808)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:188)

Please note that this/these problem was not caused by intentionally giving pathological code to cause errors, but was accidental due to the corrupted formatting that existed in our project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant