Skip to content

Commit 9c2902d

Browse files
authored
Handle empty files and truncated YAML front matter (#17527)
Scaladoc crashes when a static file is empty: ``` [error] Caused by: java.util.NoSuchElementException: head of empty list [error] at scala.collection.immutable.Nil$.head(List.scala:662) [error] at scala.collection.immutable.Nil$.head(List.scala:661) [error] at dotty.tools.scaladoc.site.common$package$.loadTemplateFile(common.scala:66) [error] at dotty.tools.scaladoc.site.StaticSiteLoader.loadRecursively(StaticSiteLoader.scala:191) [error] at dotty.tools.scaladoc.site.StaticSiteLoader.loadRecursively$$anonfun$1(StaticSiteLoader.scala:188) ``` See sbt/sbt#7256 for more context. This PR solves this issue, and also handles unended YAML front matter.
2 parents 51d69e8 + 44fed3d commit 9c2902d

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

scaladoc/src/dotty/tools/scaladoc/site/common.scala

+8-3
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,16 @@ def yamlParser(using ctx: StaticSiteContext): Parser = Parser.builder(defaultMar
6363
def loadTemplateFile(file: File, defaultTitle: Option[TemplateName] = None)(using ctx: StaticSiteContext): TemplateFile = {
6464
val lines = Files.readAllLines(file.toPath).asScala.toList
6565

66-
val (config, content) = if (lines.head == ConfigSeparator) {
66+
val (config, content) = if (!lines.isEmpty && lines.head == ConfigSeparator) {
6767
// Taking the second occurrence of ConfigSeparator.
6868
// The rest may appear within the content.
69-
val index = lines.drop(1).indexOf(ConfigSeparator) + 2
70-
(lines.take(index), lines.drop(index))
69+
val secondSeparatorIndex = lines.drop(1).indexOf(ConfigSeparator)
70+
if secondSeparatorIndex != -1 then
71+
(lines.take(secondSeparatorIndex + 2), lines.drop(secondSeparatorIndex + 2))
72+
else
73+
// If there is no second occurrence of ConfigSeparator, we assume that the
74+
// whole file is config.
75+
(lines.tail, Nil)
7176
} else (Nil, lines)
7277

7378
val configParsed = yamlParser.parse(config.mkString(LineSeparator))

scaladoc/test-documentations/emptyPage/_docs/hello.md

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
title: My page
3+
foo: bar

scaladoc/test/dotty/tools/scaladoc/site/SiteGeneratationTest.scala

+16
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,22 @@ class SiteGeneratationTest extends BaseHtmlTest:
9595
testApiPages(mainTitle = projectName, parents = Nil, hasToplevelIndexIndex = false)
9696
}
9797

98+
@Test
99+
def emptyPage() = withGeneratedSite(testDocPath.resolve("emptyPage")){
100+
withHtmlFile("docs/hello.html") { content =>
101+
// There should be no content as the page body is empty.
102+
content.assertTextsIn("#content", Nil*)
103+
}
104+
}
105+
106+
@Test
107+
def noConfigEnd() = withGeneratedSite(testDocPath.resolve("noConfigEnd")){
108+
withHtmlFile("docs/hello.html") { content =>
109+
// There should be no content as the page body is empty.
110+
content.assertTextsIn("#content", Nil*)
111+
}
112+
}
113+
98114
@Test
99115
def staticLinking() = withGeneratedSite(testDocPath.resolve("static-links")){
100116

0 commit comments

Comments
 (0)