Skip to content

Commit

Permalink
bugfix: Properly escape jar: paths
Browse files Browse the repository at this point in the history
Should help with scalameta#6655
  • Loading branch information
tgodzik committed Nov 5, 2024
1 parent a482647 commit f7942aa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ object URIEncoderDecoder {
// Some encoding schemes encode `:` (although not the first one as that indicates scheme).
// Currently Metals doesn't encode `:` but does decode it
private val toEscape: Map[Char, String] =
Set('"', '<', '>', '&', '\'', '[', ']', '{', '}', ' ', '+', '!')
Set('"', '<', '>', '&', '\'', '[', ']', '{', '}', ' ', '+')
.map(char => char -> ("%" + char.toHexString))
.toMap

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ trait MtagsEnrichments extends ScalametaCommonEnrichments {
URLDecoder.decode(_, "UTF-8").toAbsolutePath(followSymlink)
)
else if (value.toUpperCase.startsWith("JAR")) {
try URI.create(value).toAbsolutePath(followSymlink)
try
URI
.create(URIEncoderDecoder.encode(value))
.toAbsolutePath(followSymlink)
catch {
case _: NoSuchFileException | _: FileSystemNotFoundException =>
withTryDecode(value.stripPrefix("jar:"))(
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/src/test/scala/tests/UriEncoderDecoderSuite.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tests

import scala.meta.internal.mtags.URIEncoderDecoder
import scala.meta.internal.metals.MetalsEnrichments.XtensionString

class UriEncoderDecoderSuite extends BaseSuite {

Expand Down Expand Up @@ -41,4 +42,11 @@ class UriEncoderDecoderSuite extends BaseSuite {
)
}

test("absolute-path-space") {
// Should be NoSuchFileException instead of URI creation one
intercept[java.nio.file.NoSuchFileException] {
"jar:file:///C:/Program Files/Java/jdk-21.0.3+9/lib/src.zip!/java.base/java/lang/String.java".toAbsolutePath
}
}

}

0 comments on commit f7942aa

Please sign in to comment.