Skip to content

Commit

Permalink
fix: return an error when trying to decode tasty in Scala 2
Browse files Browse the repository at this point in the history
Since this isn't supported for now we should correctly return an error
for this. Right now you can still trigger this in Scala 2 and it just
returns an empty string not really giving the user much information
about why it didn't work. This way clients can now handle this situation
better.
  • Loading branch information
ckipp01 committed Mar 18, 2022
1 parent c2d8baa commit b47c60d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,27 @@ final class FileDecoderProvider(
else DecoderResponse.failed(path.toURI, "Unsupported extension")
}

private def isScala3(path: AbsolutePath): Boolean = {
buildTargets
.scalaVersion(path)
.exists(version => ScalaVersions.isScala3Version(version))
}

private def decodeTasty(
path: AbsolutePath
): Future[DecoderResponse] = {
if (path.isScala)
if (path.isScala && isScala3(path)) {
selectClassFromScalaFileAndDecode(path.toURI, path, false)(
decodeFromTastyFile
)
else if (path.isTasty) {
} else if (path.isScala) {
Future.successful(
DecoderResponse.failed(
path.toURI,
"Decoding tasty is only supported in Scala 3 for now."
)
)
} else if (path.isTasty) {
findPathInfoForClassesPathFile(path) match {
case Some(pathInfo) => decodeFromTastyFile(pathInfo)
case None =>
Expand All @@ -305,8 +318,11 @@ final class FileDecoderProvider(
)
)
}
} else
Future.successful(DecoderResponse.failed(path.toURI, "Invalid extension"))
} else {
Future.successful(
DecoderResponse.failed(path.toURI, "Invalid extension")
)
}
}

private def findPathInfoFromJavaSource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ class FileDecoderProviderLspSuite
Right(FileDecoderProviderLspSuite.tastySingle)
)

check(
"tasty-single-not-for-scala2",
s"""|/metals.json
|{
| "app": {
| "scalaVersion": "${V.scala213}"
| }
|}
|/app/src/main/scala/Main.scala
|package foo.bar.example
|object Main
|""".stripMargin,
"app/src/main/scala/Main.scala",
None,
"tasty-decoded",
Left("Decoding tasty is only supported in Scala 3 for now.")
)

check(
"decode-jar",
s"""
Expand Down

0 comments on commit b47c60d

Please sign in to comment.