Skip to content

URI encode semanticdb TextDocument uri #9365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ object ExtractSemanticDB:
val doc: TextDocument = TextDocument(
schema = Schema.SEMANTICDB4,
language = Language.SCALA,
uri = relPath.toString,
uri = Tools.mkURIstring(relPath),
text = "",
md5 = internal.MD5.compute(String(source.content)),
symbols = symbolInfos,
Expand Down
13 changes: 10 additions & 3 deletions compiler/src/dotty/tools/dotc/semanticdb/Tools.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import dotty.tools.dotc.semanticdb.Scala3.{_, given _}

object Tools:

/** Converts a Path to a String that is URI encoded, without forcing absolute paths. */
def mkURIstring(path: Path): String =
// Calling `.toUri` on a relative path will convert it to absolute. Iteration through its parts instead preserves
// the resulting URI as relative.
val uriParts = for part <- path.asScala yield new java.net.URI(null, null, part.toString, null)
uriParts.mkString("/")

/** Load SemanticDB TextDocument for a single Scala source file
*
* @param scalaAbsolutePath Absolute path to a Scala source file.
Expand All @@ -19,16 +26,16 @@ object Tools:
scalaRelativePath: Path,
semanticdbAbsolutePath: Path
): TextDocument =
val reluri = scalaRelativePath.toString
val reluri = mkURIstring(scalaRelativePath)
val sdocs = parseTextDocuments(semanticdbAbsolutePath)
sdocs.documents.find(_.uri == reluri) match
case None => throw new NoSuchElementException(reluri)
case None => throw new NoSuchElementException(s"$scalaRelativePath")
case Some(document) =>
val text = new String(Files.readAllBytes(scalaAbsolutePath), StandardCharsets.UTF_8)
// Assert the SemanticDB payload is in-sync with the contents of the Scala file on disk.
val md5FingerprintOnDisk = internal.MD5.compute(text)
if document.md5 != md5FingerprintOnDisk
throw new IllegalArgumentException("stale semanticdb: " + reluri)
throw new IllegalArgumentException(s"stale semanticdb: $scalaRelativePath")
else
// Update text document to include full text contents of the file.
document.copy(text = text)
Expand Down
2 changes: 1 addition & 1 deletion tests/semanticdb/metac.expect
Original file line number Diff line number Diff line change
Expand Up @@ -2997,7 +2997,7 @@ expect/filename with spaces.scala

Summary:
Schema => SemanticDB v4
Uri => filename with spaces.scala
Uri => filename%20with%20spaces.scala
Text => empty
Language => Scala
Symbols => 2 entries
Expand Down