Skip to content

Commit

Permalink
bugfix: Show exception from path matcher correctly
Browse files Browse the repository at this point in the history
Previously, we would just get a stack trace:
```
Exception in thread "main" java.lang.IllegalArgumentException
	at sun.nio.fs.UnixFileSystem.getPathMatcher(UnixFileSystem.java:286)
	at org.scalafmt.config.ProjectFiles$FileMatcher$Nio.<init>(ProjectFiles.scala:74)
	at org.scalafmt.config.ProjectFiles$FileMatcher$.$anonfun$nio$1(ProjectFiles.scala:70)
	at scala.collection.immutable.List.map(List.scala:250)
	at scala.collection.immutable.List.map(List.scala:79)
	at org.scalafmt.config.ProjectFiles$FileMatcher$.create(ProjectFiles.scala:69)
	at org.scalafmt.config.ProjectFiles$FileMatcher$.nio(ProjectFiles.scala:70)
	at org.scalafmt.config.ProjectFiles$FileMatcher$.apply(ProjectFiles.scala:64)
	at org.scalafmt.cli.ScalafmtCoreRunner$.$anonfun$run$2(ScalafmtCoreRunner.scala:27)
	at metaconfig.Configured$ConfiguredImplicit.fold(Configured.scala:111)
	at org.scalafmt.cli.ScalafmtCoreRunner$.run(ScalafmtCoreRunner.scala:23)
	at org.scalafmt.cli.Cli$.runWithRunner(Cli.scala:140)
	at org.scalafmt.cli.Cli$.run(Cli.scala:91)
	at org.scalafmt.cli.Cli$.mainWithOptions(Cli.scala:61)
	at org.scalafmt.cli.Cli$.main(Cli.scala:43)
	at org.scalafmt.cli.Cli.main(Cli.scala)
```

Reported in VirtusLab/scala-cli#2415
  • Loading branch information
tgodzik committed Jan 2, 2024
1 parent 75f2b96 commit 664364d
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import util.control.Breaks
import org.scalafmt.Error
import org.scalafmt.{Formatted, Scalafmt, Versions}
import org.scalafmt.config.{ProjectFiles, ScalafmtConfig}
import org.scalafmt.config.ScalafmtConfigException
import org.scalafmt.CompatCollections.ParConverters._

import scala.meta.parsers.ParseException
Expand All @@ -21,10 +22,18 @@ object ScalafmtCoreRunner extends ScalafmtRunner {
ExitCode.UnexpectedError
} { scalafmtConf =>
options.common.debug.println(s"parsed config (v${Versions.version})")
val filterMatcher = ProjectFiles.FileMatcher(
scalafmtConf.project,
options.customExcludes
)
val filterMatcher =
try {
ProjectFiles.FileMatcher(
scalafmtConf.project,
options.customExcludes
)
} catch {
case e: ScalafmtConfigException =>
options.common.err.println(e.getMessage())
return ExitCode.UnexpectedError // RETURNING EARLY!
}

val inputMethods = getInputMethods(options, filterMatcher.matchesPath)

val termDisplay =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,27 @@ object ProjectFiles {
private def regex(seq: Seq[String]) = create(seq, new Regex(_))

private final class Nio(pattern: String) extends PathMatcher {
private val matcher = fs.getPathMatcher(pattern)
private val matcher =
try { fs.getPathMatcher(pattern) }
catch {
case _: IllegalArgumentException =>
throw new ScalafmtConfigException(
s"Illegal pattern in configuration: $pattern"
)
}
def matches(path: file.Path): Boolean = matcher.matches(path)
}
private final class Regex(regex: String) extends PathMatcher {
private val pattern = java.util.regex.Pattern.compile(regex)
private val pattern =
try { java.util.regex.Pattern.compile(regex) }
catch {
case e: java.util.regex.PatternSyntaxException =>
throw new ScalafmtConfigException(
s"""|Illegal regex in configuration: $regex
|reason: ${e.getMessage()}""".stripMargin
)
}

def matches(path: file.Path): Boolean =
pattern.matcher(path.toString).find()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,37 @@ class DynamicSuite extends FunSuite {
)
}

check("regex-error") { f =>
f.setConfig(s"""|version = "$nightly"
|runner.dialect = "scala212"
|project.excludeFilters = [
| ".*foo("
|]
|""".stripMargin)
val err = f.assertThrows[ScalafmtDynamicError.ConfigParseError]().getMessage
assertNoDiff(
err.takeRight(120),
"""|Invalid config: Illegal regex in configuration: .*foo(
|reason: Unclosed group near index 6
|.*foo(
|""".stripMargin
)
}

check("path-error") { f =>
f.setConfig(s"""|version = "$nightly"
|runner.dialect = "scala212"
|project.excludePaths = [
| "foo.scala"
|]
|""".stripMargin)
val err = f.assertThrows[ScalafmtDynamicError.ConfigParseError]().getMessage
assertNoDiff(
err.takeRight(120),
"Invalid config: Illegal pattern in configuration: foo.scala"
)
}

check("config-cache") { f =>
f.setVersion(latest, "scala211")
f.assertFormat()
Expand Down
58 changes: 58 additions & 0 deletions scalafmt-tests/src/test/scala/org/scalafmt/cli/CliTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,64 @@ class CliTest extends AbstractCliTest with CliTestBehavior {
testCli("1.6.0-RC4") // test for runDynamic
testCli(stableVersion) // test for runScalafmt

test(s"path-error") {
val input =
s"""|/.scalafmt.conf
|version = "${stableVersion}"
|project.excludePaths = [
| "glob:**/src/main/scala/besom/rpc/**.scala",
| "foo.scala"
|]
|/foo.scala
|object A { foo( }
|""".stripMargin

noArgTest(
string2dir(input),
input,
Seq(Array("--test")),
ExitCode.UnexpectedError,
assertOut = out => {
assertContains(
out,
s"""Illegal pattern in configuration: foo.scala""".stripMargin
)
},
Some(ExitCode.UnexpectedError)
)

}

test(s"regex-error") {
val input =
s"""|/.scalafmt.conf
|version = "${stableVersion}"
|project.excludeFilters = [
| ".*foo("
|]
|/foo.scala
|object A { foo( }
|""".stripMargin

noArgTest(
string2dir(input),
input,
Seq(Array("--test")),
ExitCode.UnexpectedError,
assertOut = out => {
assertContains(
out,
"""|Illegal regex in configuration: .*foo(
|reason: Unclosed group near index 6
|.*foo(
|""".stripMargin
)
},
Some(ExitCode.UnexpectedError)
)

}

test("Fail if .scalafmt.conf is missing.") {
val input =
s"""|/foo.scala
Expand Down

0 comments on commit 664364d

Please sign in to comment.