Skip to content

Commit 0e87b18

Browse files
committed
hugolib: Fix handling of zero-length files
This was a regression in Hugo 0.20. This commit makes sure that zeron-length files are not rendered to file. Fixes #3355
1 parent e98f885 commit 0e87b18

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

‎hugolib/site.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,6 +1939,10 @@ func (s *Site) renderAndWritePage(name string, dest string, p *PageOutput, layou
19391939
return nil
19401940
}
19411941

1942+
if renderBuffer.Len() == 0 {
1943+
return nil
1944+
}
1945+
19421946
outBuffer := bp.GetBuffer()
19431947
defer bp.PutBuffer(outBuffer)
19441948

‎hugolib/site_render.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ func (s *Site) renderRobotsTXT() error {
340340
return nil
341341
}
342342

343+
if outBuffer.Len() == 0 {
344+
return nil
345+
}
346+
343347
return s.publish("robots.txt", outBuffer)
344348
}
345349

‎hugolib/site_test.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,20 @@ func TestNewSiteDefaultLang(t *testing.T) {
376376
require.Equal(t, hugofs.Os, s.Fs.Destination)
377377
}
378378

379+
// Issue #3355
380+
func TestShouldNotWriteZeroLengthFilesToDestination(t *testing.T) {
381+
cfg, fs := newTestCfg()
382+
383+
writeSource(t, fs, filepath.Join("content", "simple.html"), "simple")
384+
writeSource(t, fs, filepath.Join("layouts", "_default/single.html"), "{{.Content}}")
385+
writeSource(t, fs, filepath.Join("layouts", "_default/list.html"), "")
386+
387+
s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
388+
th := testHelper{s.Cfg, s.Fs, t}
389+
390+
th.assertFileNotExist(filepath.Join("public", "index.html"))
391+
}
392+
379393
// Issue #1176
380394
func TestSectionNaming(t *testing.T) {
381395
t.Parallel()

‎hugolib/testhelpers_test.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ func (th testHelper) assertFileContentRegexp(filename string, matches ...string)
6161
}
6262
}
6363

64+
func (th testHelper) assertFileNotExist(filename string) {
65+
exists, err := helpers.Exists(filename, th.Fs.Destination)
66+
require.NoError(th.T, err)
67+
require.False(th.T, exists)
68+
}
69+
6470
func (th testHelper) replaceDefaultContentLanguageValue(value string) string {
6571
defaultInSubDir := th.Cfg.GetBool("defaultContentLanguageInSubDir")
6672
replace := th.Cfg.GetString("defaultContentLanguage") + "/"

0 commit comments

Comments
 (0)