From 14aa43d7b384196e6fea75e83e6243eaab51ad2c Mon Sep 17 00:00:00 2001 From: Tomasz Godzik Date: Tue, 5 Nov 2024 19:04:55 +0100 Subject: [PATCH] bugfix: Properly escape jar: paths Should help with https://github.com/scalameta/metals/issues/6655 --- .../scala/meta/internal/metals/URIEncoderDecoder.scala | 2 +- .../scala/meta/internal/mtags/MtagsEnrichments.scala | 5 ++++- .../src/test/scala/tests/UriEncoderDecoderSuite.scala | 8 ++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/mtags/src/main/scala-2/scala/meta/internal/metals/URIEncoderDecoder.scala b/mtags/src/main/scala-2/scala/meta/internal/metals/URIEncoderDecoder.scala index 091fcad4497..65035ad7dd6 100644 --- a/mtags/src/main/scala-2/scala/meta/internal/metals/URIEncoderDecoder.scala +++ b/mtags/src/main/scala-2/scala/meta/internal/metals/URIEncoderDecoder.scala @@ -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 diff --git a/mtags/src/main/scala-2/scala/meta/internal/mtags/MtagsEnrichments.scala b/mtags/src/main/scala-2/scala/meta/internal/mtags/MtagsEnrichments.scala index 63c29f3b390..155add980c9 100644 --- a/mtags/src/main/scala-2/scala/meta/internal/mtags/MtagsEnrichments.scala +++ b/mtags/src/main/scala-2/scala/meta/internal/mtags/MtagsEnrichments.scala @@ -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:"))( diff --git a/tests/unit/src/test/scala/tests/UriEncoderDecoderSuite.scala b/tests/unit/src/test/scala/tests/UriEncoderDecoderSuite.scala index 2fa7519d7a4..4e73f11b3b9 100644 --- a/tests/unit/src/test/scala/tests/UriEncoderDecoderSuite.scala +++ b/tests/unit/src/test/scala/tests/UriEncoderDecoderSuite.scala @@ -1,5 +1,6 @@ package tests +import scala.meta.internal.metals.MetalsEnrichments.XtensionString import scala.meta.internal.mtags.URIEncoderDecoder class UriEncoderDecoderSuite extends BaseSuite { @@ -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 + } + } + }