Skip to content

Commit 0ef2def

Browse files
committed
fix #9281 URI encode semanticdb TextDocument uri
1 parent 5cdfd31 commit 0ef2def

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

compiler/src/dotty/tools/dotc/semanticdb/ExtractSemanticDB.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ object ExtractSemanticDB:
626626
val doc: TextDocument = TextDocument(
627627
schema = Schema.SEMANTICDB4,
628628
language = Language.SCALA,
629-
uri = relPath.toString,
629+
uri = Tools.mkURIstring(relPath),
630630
text = "",
631631
md5 = internal.MD5.compute(String(source.content)),
632632
symbols = symbolInfos,

compiler/src/dotty/tools/dotc/semanticdb/Tools.scala

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ import dotty.tools.dotc.semanticdb.Scala3.{_, given _}
88

99
object Tools:
1010

11+
/** Converts a Path to a String that is URI encoded, without forcing absolute paths.
12+
*
13+
* Calling `.toUri` on a relative path will convert it to absolute. Iteration through its parts instead preserves
14+
* the resulting URI as relative.
15+
*/
16+
def mkURIstring(path: Path): String =
17+
val uriParts = for part <- path.asScala yield new java.net.URI(null, null, part.toString, null)
18+
uriParts.mkString("/")
19+
1120
/** Load SemanticDB TextDocument for a single Scala source file
1221
*
1322
* @param scalaAbsolutePath Absolute path to a Scala source file.
@@ -19,16 +28,16 @@ object Tools:
1928
scalaRelativePath: Path,
2029
semanticdbAbsolutePath: Path
2130
): TextDocument =
22-
val reluri = scalaRelativePath.toString
31+
val reluri = mkURIstring(scalaRelativePath)
2332
val sdocs = parseTextDocuments(semanticdbAbsolutePath)
2433
sdocs.documents.find(_.uri == reluri) match
25-
case None => throw new NoSuchElementException(reluri)
34+
case None => throw new NoSuchElementException(s"$scalaRelativePath")
2635
case Some(document) =>
2736
val text = new String(Files.readAllBytes(scalaAbsolutePath), StandardCharsets.UTF_8)
2837
// Assert the SemanticDB payload is in-sync with the contents of the Scala file on disk.
2938
val md5FingerprintOnDisk = internal.MD5.compute(text)
3039
if document.md5 != md5FingerprintOnDisk
31-
throw new IllegalArgumentException("stale semanticdb: " + reluri)
40+
throw new IllegalArgumentException(s"stale semanticdb: $scalaRelativePath")
3241
else
3342
// Update text document to include full text contents of the file.
3443
document.copy(text = text)

tests/semanticdb/metac.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2997,7 +2997,7 @@ expect/filename with spaces.scala
29972997

29982998
Summary:
29992999
Schema => SemanticDB v4
3000-
Uri => filename with spaces.scala
3000+
Uri => filename%20with%20spaces.scala
30013001
Text => empty
30023002
Language => Scala
30033003
Symbols => 2 entries

0 commit comments

Comments
 (0)