Skip to content
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

Fix scaladoc crash on Windows - illegal path character #20311

Merged
merged 1 commit into from
May 7, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions scaladoc/src/dotty/tools/scaladoc/renderers/SiteRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import java.nio.file.Path
import java.nio.file.Files
import java.io.File
import scala.util.chaining._
import dotty.tools.scaladoc.util.Escape.escapeFilename

case class ResolvedTemplate(template: LoadedTemplate, ctx: StaticSiteContext):
val resolved = template.resolveToHtml(ctx)
Expand Down Expand Up @@ -55,11 +56,16 @@ trait SiteRenderer(using DocContext) extends Locations:
val staticSiteRootPath = content.ctx.root.toPath.toAbsolutePath
def asValidURL: Option[String] = Try(URI(str).toURL).toOption.map(_ => str)
def asAsset: Option[String] = Option.when(
Files.exists(staticSiteRootPath.resolve("_assets").resolve(str.stripPrefix("/")))
Try(
Files.exists(staticSiteRootPath.resolve("_assets").resolve(str.stripPrefix("/")))
).getOrElse(false)
)(
resolveLink(pageDri, str.stripPrefix("/"))
)
def asStaticSite: Option[String] = tryAsDriPlain(str).orElse(tryAsDri(str))
def asStaticSite: Option[String] =
tryAsDriPlain(str)
.orElse(tryAsDri(str))
.orElse(tryAsDriPlain(escapeFilename(str)))

/* Link resolving checks performs multiple strategies with following priority:
1. We check if the link is a valid URL e.g. http://dotty.epfl.ch
Expand Down
14 changes: 8 additions & 6 deletions scaladoc/src/dotty/tools/scaladoc/site/StaticSiteContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import java.nio.file.FileVisitOption
import java.nio.file.Path
import java.nio.file.Paths

import scala.util.Try
import scala.jdk.CollectionConverters._
import scala.annotation.static
import scala.util.control.NonFatal

class StaticSiteContext(
val root: File,
Expand Down Expand Up @@ -75,10 +74,13 @@ class StaticSiteContext(
val templateSourceLocation = staticSiteRoot.reverseSiteMappings.get(templateDestLocation)

// Check if link is relative or absolute
if link.startsWith("/")
then Seq(root.toPath.resolve(link.drop(1)))
else Seq(templateDestLocation.getParent.resolve(link).normalize) ++
templateSourceLocation.map(_.getParent.resolve(link).normalize)
try
if link.startsWith("/")
then Seq(root.toPath.resolve(link.drop(1)))
else Seq(templateDestLocation.getParent.resolve(link).normalize) ++
templateSourceLocation.map(_.getParent.resolve(link).normalize)
catch
case NonFatal(_) => Seq.empty

// Try to strip site extension and create all possible file paths
val fileNames = if siteExtensions.exists(link.endsWith(_))
Expand Down
17 changes: 17 additions & 0 deletions scaladoc/src/dotty/tools/scaladoc/util/escape.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,24 @@ object Escape:
.replace("#","%23")

def escapeFilename(filename: String) =
// from compiler/src/dotty/tools/dotc/util/NameTransformer.scala
val escaped = filename
.replace("~", "$tilde")
.replace("=", "$eq")
.replace("<", "$less")
.replace(">", "$greater")
.replace("!", "$bang")
.replace("#", "$hash")
.replace("%", "$percent")
.replace("^", "$up")
.replace("&", "$amp")
.replace("|", "$bar")
.replace("*", "$times")
.replace("/", "$div")
.replace("+", "$plus")
.replace("-", "$minus")
.replace(":", "$colon")
.replace("\\", "$bslash")
.replace("?", "$qmark")
.replace("@", "$at")
if escaped != filename then escaped + "$" else escaped