2018-10-03 08:58:09 -04:00
package hugolib
import (
"fmt"
2022-05-02 10:07:52 -04:00
"os"
2018-10-21 06:20:21 -04:00
"path/filepath"
2018-10-03 08:58:09 -04:00
"strings"
"testing"
2019-01-02 06:33:26 -05:00
2019-08-10 15:05:17 -04:00
qt "github.com/frankban/quicktest"
2018-10-03 08:58:09 -04:00
"github.com/gohugoio/hugo/common/herrors"
)
type testSiteBuildErrorAsserter struct {
2019-08-10 15:05:17 -04:00
name string
c * qt . C
2018-10-03 08:58:09 -04:00
}
2022-05-02 10:07:52 -04:00
func ( t testSiteBuildErrorAsserter ) getFileError ( err error ) herrors . FileError {
2019-08-10 15:05:17 -04:00
t . c . Assert ( err , qt . Not ( qt . IsNil ) , qt . Commentf ( t . name ) )
2022-05-02 10:07:52 -04:00
fe := herrors . UnwrapFileError ( err )
t . c . Assert ( fe , qt . Not ( qt . IsNil ) )
return fe
2018-10-03 08:58:09 -04:00
}
func ( t testSiteBuildErrorAsserter ) assertLineNumber ( lineNumber int , err error ) {
2022-05-02 10:07:52 -04:00
t . c . Helper ( )
2018-10-03 08:58:09 -04:00
fe := t . getFileError ( err )
2020-01-15 09:59:56 -05:00
t . c . Assert ( fe . Position ( ) . LineNumber , qt . Equals , lineNumber , qt . Commentf ( err . Error ( ) ) )
2018-10-21 06:20:21 -04:00
}
func ( t testSiteBuildErrorAsserter ) assertErrorMessage ( e1 , e2 string ) {
// The error message will contain filenames with OS slashes. Normalize before compare.
e1 , e2 = filepath . ToSlash ( e1 ) , filepath . ToSlash ( e2 )
2019-08-10 15:05:17 -04:00
t . c . Assert ( e2 , qt . Contains , e1 )
2018-10-03 08:58:09 -04:00
}
func TestSiteBuildErrors ( t * testing . T ) {
const (
yamlcontent = "yamlcontent"
2018-10-21 06:20:21 -04:00
tomlcontent = "tomlcontent"
2018-11-01 06:28:30 -04:00
jsoncontent = "jsoncontent"
2018-10-03 08:58:09 -04:00
shortcode = "shortcode"
base = "base"
single = "single"
)
// TODO(bep) add content tests after https://github.com/gohugoio/hugo/issues/5324
// is implemented.
tests := [ ] struct {
name string
fileType string
fileFixer func ( content string ) string
assertCreateError func ( a testSiteBuildErrorAsserter , err error )
assertBuildError func ( a testSiteBuildErrorAsserter , err error )
} {
{
name : "Base template parse failed" ,
fileType : base ,
fileFixer : func ( content string ) string {
return strings . Replace ( content , ".Title }}" , ".Title }" , 1 )
} ,
2020-07-13 07:40:35 -04:00
// Base templates gets parsed at build time.
assertBuildError : func ( a testSiteBuildErrorAsserter , err error ) {
2018-10-21 06:20:21 -04:00
a . assertLineNumber ( 4 , err )
2018-10-03 08:58:09 -04:00
} ,
} ,
{
name : "Base template execute failed" ,
fileType : base ,
fileFixer : func ( content string ) string {
return strings . Replace ( content , ".Title" , ".Titles" , 1 )
} ,
assertBuildError : func ( a testSiteBuildErrorAsserter , err error ) {
2018-10-21 06:20:21 -04:00
a . assertLineNumber ( 4 , err )
2018-10-03 08:58:09 -04:00
} ,
} ,
{
name : "Single template parse failed" ,
fileType : single ,
fileFixer : func ( content string ) string {
return strings . Replace ( content , ".Title }}" , ".Title }" , 1 )
} ,
assertCreateError : func ( a testSiteBuildErrorAsserter , err error ) {
2018-10-21 06:20:21 -04:00
fe := a . getFileError ( err )
2019-08-10 15:05:17 -04:00
a . c . Assert ( fe . Position ( ) . LineNumber , qt . Equals , 5 )
a . c . Assert ( fe . Position ( ) . ColumnNumber , qt . Equals , 1 )
2020-07-13 07:40:35 -04:00
a . assertErrorMessage ( "\"layouts/foo/single.html:5:1\": parse failed: template: foo/single.html:5: unexpected \"}\" in operand" , fe . Error ( ) )
2018-10-03 08:58:09 -04:00
} ,
} ,
{
name : "Single template execute failed" ,
fileType : single ,
fileFixer : func ( content string ) string {
return strings . Replace ( content , ".Title" , ".Titles" , 1 )
} ,
assertBuildError : func ( a testSiteBuildErrorAsserter , err error ) {
2018-10-21 06:20:21 -04:00
fe := a . getFileError ( err )
2019-08-10 15:05:17 -04:00
a . c . Assert ( fe . Position ( ) . LineNumber , qt . Equals , 5 )
a . c . Assert ( fe . Position ( ) . ColumnNumber , qt . Equals , 14 )
2018-10-22 14:20:48 -04:00
a . assertErrorMessage ( "\"layouts/_default/single.html:5:14\": execute of template failed" , fe . Error ( ) )
2018-10-03 08:58:09 -04:00
} ,
} ,
2018-10-24 07:32:46 -04:00
{
name : "Single template execute failed, long keyword" ,
fileType : single ,
fileFixer : func ( content string ) string {
return strings . Replace ( content , ".Title" , ".ThisIsAVeryLongTitle" , 1 )
} ,
assertBuildError : func ( a testSiteBuildErrorAsserter , err error ) {
fe := a . getFileError ( err )
2019-08-10 15:05:17 -04:00
a . c . Assert ( fe . Position ( ) . LineNumber , qt . Equals , 5 )
a . c . Assert ( fe . Position ( ) . ColumnNumber , qt . Equals , 14 )
2018-10-24 07:32:46 -04:00
a . assertErrorMessage ( "\"layouts/_default/single.html:5:14\": execute of template failed" , fe . Error ( ) )
} ,
} ,
2018-10-03 08:58:09 -04:00
{
name : "Shortcode parse failed" ,
fileType : shortcode ,
fileFixer : func ( content string ) string {
return strings . Replace ( content , ".Title }}" , ".Title }" , 1 )
} ,
assertCreateError : func ( a testSiteBuildErrorAsserter , err error ) {
2018-10-21 06:20:21 -04:00
a . assertLineNumber ( 4 , err )
2018-10-03 08:58:09 -04:00
} ,
} ,
2018-10-20 13:09:03 -04:00
{
2022-05-02 10:07:52 -04:00
name : "Shortcode execute failed" ,
2018-10-20 13:09:03 -04:00
fileType : shortcode ,
fileFixer : func ( content string ) string {
return strings . Replace ( content , ".Title" , ".Titles" , 1 )
} ,
assertBuildError : func ( a testSiteBuildErrorAsserter , err error ) {
2018-10-22 14:20:48 -04:00
fe := a . getFileError ( err )
// Make sure that it contains both the content file and template
2022-05-02 10:07:52 -04:00
a . assertErrorMessage ( ` "content/myyaml.md:7:10": failed to render shortcode "sc": failed to process shortcode: "layouts/shortcodes/sc.html:4:22": execute of template failed: template: shortcodes/sc.html:4:22: executing "shortcodes/sc.html" at <.Page.Titles>: can't evaluate field Titles in type page.Page ` , fe . Error ( ) )
a . c . Assert ( fe . Position ( ) . LineNumber , qt . Equals , 7 )
2018-10-20 13:09:03 -04:00
} ,
} ,
2018-10-21 06:20:21 -04:00
{
name : "Shortode does not exist" ,
fileType : yamlcontent ,
fileFixer : func ( content string ) string {
return strings . Replace ( content , "{{< sc >}}" , "{{< nono >}}" , 1 )
} ,
assertBuildError : func ( a testSiteBuildErrorAsserter , err error ) {
fe := a . getFileError ( err )
2019-08-10 15:05:17 -04:00
a . c . Assert ( fe . Position ( ) . LineNumber , qt . Equals , 7 )
a . c . Assert ( fe . Position ( ) . ColumnNumber , qt . Equals , 10 )
2019-01-02 06:33:26 -05:00
a . assertErrorMessage ( ` "content/myyaml.md:7:10": failed to extract shortcode: template for shortcode "nono" not found ` , fe . Error ( ) )
2018-10-21 06:20:21 -04:00
} ,
} ,
{
name : "Invalid YAML front matter" ,
fileType : yamlcontent ,
fileFixer : func ( content string ) string {
2022-05-02 10:07:52 -04:00
return ` -- -
title : "My YAML Content"
foo bar
-- -
`
2018-10-21 06:20:21 -04:00
} ,
assertBuildError : func ( a testSiteBuildErrorAsserter , err error ) {
2022-05-02 10:07:52 -04:00
a . assertLineNumber ( 3 , err )
2018-10-21 06:20:21 -04:00
} ,
} ,
{
name : "Invalid TOML front matter" ,
fileType : tomlcontent ,
fileFixer : func ( content string ) string {
return strings . Replace ( content , "description = " , "description &" , 1 )
} ,
assertBuildError : func ( a testSiteBuildErrorAsserter , err error ) {
fe := a . getFileError ( err )
2019-08-10 15:05:17 -04:00
a . c . Assert ( fe . Position ( ) . LineNumber , qt . Equals , 6 )
2018-10-21 06:20:21 -04:00
} ,
} ,
2018-10-23 02:54:10 -04:00
{
name : "Invalid JSON front matter" ,
2018-11-01 06:28:30 -04:00
fileType : jsoncontent ,
2018-10-23 02:54:10 -04:00
fileFixer : func ( content string ) string {
return strings . Replace ( content , "\"description\":" , "\"description\"" , 1 )
} ,
assertBuildError : func ( a testSiteBuildErrorAsserter , err error ) {
fe := a . getFileError ( err )
2019-08-10 15:05:17 -04:00
a . c . Assert ( fe . Position ( ) . LineNumber , qt . Equals , 3 )
2018-10-23 02:54:10 -04:00
} ,
} ,
2018-10-17 02:24:45 -04:00
{
2018-10-24 08:02:34 -04:00
// See https://github.com/gohugoio/hugo/issues/5327
2018-10-17 02:24:45 -04:00
name : "Panic in template Execute" ,
fileType : single ,
fileFixer : func ( content string ) string {
return strings . Replace ( content , ".Title" , ".Parent.Parent.Parent" , 1 )
} ,
2018-10-24 08:02:34 -04:00
2018-10-17 02:24:45 -04:00
assertBuildError : func ( a testSiteBuildErrorAsserter , err error ) {
2019-08-10 15:05:17 -04:00
a . c . Assert ( err , qt . Not ( qt . IsNil ) )
2019-09-04 03:50:32 -04:00
fe := a . getFileError ( err )
a . c . Assert ( fe . Position ( ) . LineNumber , qt . Equals , 5 )
a . c . Assert ( fe . Position ( ) . ColumnNumber , qt . Equals , 21 )
2018-10-17 02:24:45 -04:00
} ,
} ,
2018-10-03 08:58:09 -04:00
}
for _ , test := range tests {
2022-05-02 10:07:52 -04:00
if test . name != "Invalid JSON front matter" {
continue
}
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
test := test
2019-01-02 06:33:26 -05:00
t . Run ( test . name , func ( t * testing . T ) {
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
t . Parallel ( )
2019-08-10 15:05:17 -04:00
c := qt . New ( t )
2019-01-02 06:33:26 -05:00
errorAsserter := testSiteBuildErrorAsserter {
2019-08-10 15:05:17 -04:00
c : c ,
name : test . name ,
2019-01-02 06:33:26 -05:00
}
2018-10-03 08:58:09 -04:00
2019-01-02 06:33:26 -05:00
b := newTestSitesBuilder ( t ) . WithSimpleConfigFile ( )
2018-10-03 08:58:09 -04:00
2019-01-02 06:33:26 -05:00
f := func ( fileType , content string ) string {
if fileType != test . fileType {
return content
}
return test . fileFixer ( content )
2018-10-03 08:58:09 -04:00
}
2019-01-02 06:33:26 -05:00
b . WithTemplatesAdded ( "layouts/shortcodes/sc.html" , f ( shortcode , ` SHORTCODE L1
2018-10-03 08:58:09 -04:00
SHORTCODE L2
SHORTCODE L3 :
SHORTCODE L4 : { { . Page . Title } }
` ) )
2019-01-02 06:33:26 -05:00
b . WithTemplatesAdded ( "layouts/_default/baseof.html" , f ( base , ` BASEOF L1
2018-10-03 08:58:09 -04:00
BASEOF L2
BASEOF L3
BASEOF L4 { { if . Title } } { { end } }
{ { block "main" . } } This is the main content . { { end } }
BASEOF L6
` ) )
2019-01-02 06:33:26 -05:00
b . WithTemplatesAdded ( "layouts/_default/single.html" , f ( single , ` { { define "main" } }
2018-10-03 08:58:09 -04:00
SINGLE L2 :
SINGLE L3 :
SINGLE L4 :
SINGLE L5 : { { . Title } } { { . Content } }
{ { end } }
2020-01-15 09:59:56 -05:00
` ) )
b . WithTemplatesAdded ( "layouts/foo/single.html" , f ( single , `
SINGLE L2 :
SINGLE L3 :
SINGLE L4 :
SINGLE L5 : { { . Title } } { { . Content } }
2018-10-03 08:58:09 -04:00
` ) )
2019-01-02 06:33:26 -05:00
b . WithContent ( "myyaml.md" , f ( yamlcontent , ` -- -
2018-10-03 08:58:09 -04:00
title : "The YAML"
-- -
Some content .
2018-10-21 06:20:21 -04:00
{ { < sc > } }
2018-10-03 08:58:09 -04:00
Some more text .
The end .
2018-10-21 06:20:21 -04:00
` ) )
2019-01-02 06:33:26 -05:00
b . WithContent ( "mytoml.md" , f ( tomlcontent , ` ++ +
2018-10-21 06:20:21 -04:00
title = "The TOML"
p1 = "v"
p2 = "v"
p3 = "v"
description = "Descriptioon"
++ +
Some content .
2018-10-23 02:54:10 -04:00
` ) )
2019-01-02 06:33:26 -05:00
b . WithContent ( "myjson.md" , f ( jsoncontent , ` {
2018-10-23 02:54:10 -04:00
"title" : "This is a title" ,
"description" : "This is a description."
}
Some content .
2018-10-03 08:58:09 -04:00
` ) )
2019-01-02 06:33:26 -05:00
createErr := b . CreateSitesE ( )
if test . assertCreateError != nil {
test . assertCreateError ( errorAsserter , createErr )
2018-10-03 08:58:09 -04:00
} else {
2019-08-10 15:05:17 -04:00
c . Assert ( createErr , qt . IsNil )
2018-10-03 08:58:09 -04:00
}
2019-01-02 06:33:26 -05:00
if createErr == nil {
buildErr := b . BuildE ( BuildCfg { } )
if test . assertBuildError != nil {
test . assertBuildError ( errorAsserter , buildErr )
} else {
2019-08-10 15:05:17 -04:00
c . Assert ( buildErr , qt . IsNil )
2019-01-02 06:33:26 -05:00
}
}
} )
2018-10-03 08:58:09 -04:00
}
2022-05-02 10:07:52 -04:00
}
// Issue 9852
func TestErrorMinify ( t * testing . T ) {
t . Parallel ( )
files := `
-- config . toml --
minify = true
-- layouts / index . html --
< body >
< script >= ; < / script >
< / body >
`
b , err := NewIntegrationTestBuilder (
IntegrationTestConfig {
T : t ,
TxtarString : files ,
} ,
) . BuildE ( )
2023-01-04 12:24:36 -05:00
b . Assert ( err , qt . IsNotNil )
2022-05-02 10:07:52 -04:00
fe := herrors . UnwrapFileError ( err )
b . Assert ( fe , qt . IsNotNil )
b . Assert ( fe . Position ( ) . LineNumber , qt . Equals , 2 )
b . Assert ( fe . Position ( ) . ColumnNumber , qt . Equals , 9 )
b . Assert ( fe . Error ( ) , qt . Contains , "unexpected = in expression on line 2 and column 9" )
b . Assert ( filepath . ToSlash ( fe . Position ( ) . Filename ) , qt . Contains , "hugo-transform-error" )
b . Assert ( os . Remove ( fe . Position ( ) . Filename ) , qt . IsNil )
}
2022-05-12 05:43:20 -04:00
func TestErrorNestedRender ( t * testing . T ) {
2022-05-02 10:07:52 -04:00
t . Parallel ( )
files := `
-- config . toml --
2022-05-12 05:43:20 -04:00
-- content / _index . md --
-- -
title : "Home"
-- -
-- layouts / index . html --
line 1
line 2
1 { { . Render "myview" } }
-- layouts / _default / myview . html --
line 1
12 { { partial "foo.html" . } }
line 4
line 5
-- layouts / partials / foo . html --
line 1
line 2
123 { { . ThisDoesNotExist } }
line 4
`
b , err := NewIntegrationTestBuilder (
IntegrationTestConfig {
T : t ,
TxtarString : files ,
} ,
) . BuildE ( )
b . Assert ( err , qt . IsNotNil )
errors := herrors . UnwrapFileErrorsWithErrorContext ( err )
b . Assert ( errors , qt . HasLen , 4 )
b . Assert ( errors [ 0 ] . Position ( ) . LineNumber , qt . Equals , 3 )
b . Assert ( errors [ 0 ] . Position ( ) . ColumnNumber , qt . Equals , 4 )
b . Assert ( errors [ 0 ] . Error ( ) , qt . Contains , filepath . FromSlash ( ` "/layouts/index.html:3:4": execute of template failed ` ) )
b . Assert ( errors [ 0 ] . ErrorContext ( ) . Lines , qt . DeepEquals , [ ] string { "line 1" , "line 2" , "1{{ .Render \"myview\" }}" } )
b . Assert ( errors [ 2 ] . Position ( ) . LineNumber , qt . Equals , 2 )
b . Assert ( errors [ 2 ] . Position ( ) . ColumnNumber , qt . Equals , 5 )
b . Assert ( errors [ 2 ] . ErrorContext ( ) . Lines , qt . DeepEquals , [ ] string { "line 1" , "12{{ partial \"foo.html\" . }}" , "line 4" , "line 5" } )
b . Assert ( errors [ 3 ] . Position ( ) . LineNumber , qt . Equals , 3 )
b . Assert ( errors [ 3 ] . Position ( ) . ColumnNumber , qt . Equals , 6 )
b . Assert ( errors [ 3 ] . ErrorContext ( ) . Lines , qt . DeepEquals , [ ] string { "line 1" , "line 2" , "123{{ .ThisDoesNotExist }}" , "line 4" } )
}
2023-02-11 10:20:24 -05:00
func TestErrorNestedShortcode ( t * testing . T ) {
2022-05-12 05:43:20 -04:00
t . Parallel ( )
files := `
-- config . toml --
-- content / _index . md --
-- -
title : "Home"
-- -
# # Hello
{ { < hello > } }
2022-05-02 10:07:52 -04:00
-- layouts / index . html --
line 1
2022-05-12 05:43:20 -04:00
line 2
{ { . Content } }
line 5
-- layouts / shortcodes / hello . html --
line 1
2022-05-02 10:07:52 -04:00
12 { { partial "foo.html" . } }
line 4
line 5
-- layouts / partials / foo . html --
line 1
line 2
123 { { . ThisDoesNotExist } }
line 4
`
b , err := NewIntegrationTestBuilder (
IntegrationTestConfig {
T : t ,
TxtarString : files ,
} ,
) . BuildE ( )
b . Assert ( err , qt . IsNotNil )
errors := herrors . UnwrapFileErrorsWithErrorContext ( err )
2022-05-12 05:43:20 -04:00
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
b . Assert ( errors , qt . HasLen , 4 )
2022-05-12 05:43:20 -04:00
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
b . Assert ( errors [ 1 ] . Position ( ) . LineNumber , qt . Equals , 6 )
b . Assert ( errors [ 1 ] . Position ( ) . ColumnNumber , qt . Equals , 1 )
b . Assert ( errors [ 1 ] . ErrorContext ( ) . ChromaLexer , qt . Equals , "md" )
b . Assert ( errors [ 1 ] . Error ( ) , qt . Contains , filepath . FromSlash ( ` "/content/_index.md:6:1": failed to render shortcode "hello": failed to process shortcode: "/layouts/shortcodes/hello.html:2:5": ` ) )
b . Assert ( errors [ 1 ] . ErrorContext ( ) . Lines , qt . DeepEquals , [ ] string { "" , "## Hello" , "{{< hello >}}" , "" } )
b . Assert ( errors [ 2 ] . ErrorContext ( ) . Lines , qt . DeepEquals , [ ] string { "line 1" , "12{{ partial \"foo.html\" . }}" , "line 4" , "line 5" } )
b . Assert ( errors [ 3 ] . ErrorContext ( ) . Lines , qt . DeepEquals , [ ] string { "line 1" , "line 2" , "123{{ .ThisDoesNotExist }}" , "line 4" } )
2022-05-12 05:43:20 -04:00
}
func TestErrorRenderHookHeading ( t * testing . T ) {
t . Parallel ( )
files := `
-- config . toml --
-- content / _index . md --
-- -
title : "Home"
-- -
# # Hello
-- layouts / index . html --
line 1
line 2
{ { . Content } }
line 5
-- layouts / _default / _markup / render - heading . html --
line 1
12 { { . Levels } }
line 4
line 5
`
b , err := NewIntegrationTestBuilder (
IntegrationTestConfig {
T : t ,
TxtarString : files ,
} ,
) . BuildE ( )
b . Assert ( err , qt . IsNotNil )
errors := herrors . UnwrapFileErrorsWithErrorContext ( err )
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
b . Assert ( errors , qt . HasLen , 3 )
2022-05-12 05:43:20 -04:00
b . Assert ( errors [ 0 ] . Error ( ) , qt . Contains , filepath . FromSlash ( ` "/content/_index.md:1:1": "/layouts/_default/_markup/render-heading.html:2:5": execute of template failed ` ) )
}
func TestErrorRenderHookCodeblock ( t * testing . T ) {
t . Parallel ( )
files := `
-- config . toml --
-- content / _index . md --
-- -
title : "Home"
-- -
# # Hello
§ § § foo
bar
§ § §
-- layouts / index . html --
line 1
line 2
{ { . Content } }
line 5
-- layouts / _default / _markup / render - codeblock - foo . html --
line 1
12 { { . Foo } }
line 4
line 5
`
b , err := NewIntegrationTestBuilder (
IntegrationTestConfig {
T : t ,
TxtarString : files ,
} ,
) . BuildE ( )
b . Assert ( err , qt . IsNotNil )
errors := herrors . UnwrapFileErrorsWithErrorContext ( err )
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
b . Assert ( errors , qt . HasLen , 3 )
2022-05-12 05:43:20 -04:00
first := errors [ 0 ]
b . Assert ( first . Error ( ) , qt . Contains , filepath . FromSlash ( ` "/content/_index.md:7:1": "/layouts/_default/_markup/render-codeblock-foo.html:2:5": execute of template failed ` ) )
}
func TestErrorInBaseTemplate ( t * testing . T ) {
t . Parallel ( )
filesTemplate := `
-- config . toml --
-- content / _index . md --
-- -
title : "Home"
-- -
-- layouts / baseof . html --
line 1 base
line 2 base
{ { block "main" . } } empty { { end } }
line 4 base
{ { block "toc" . } } empty { { end } }
-- layouts / index . html --
{ { define "main" } }
line 2 index
line 3 index
line 4 index
{ { end } }
{ { define "toc" } }
TOC : { { partial "toc.html" . } }
{ { end } }
-- layouts / partials / toc . html --
toc line 1
toc line 2
toc line 3
toc line 4
`
t . Run ( "base template" , func ( t * testing . T ) {
files := strings . Replace ( filesTemplate , "line 4 base" , "123{{ .ThisDoesNotExist \"abc\" }}" , 1 )
b , err := NewIntegrationTestBuilder (
IntegrationTestConfig {
T : t ,
TxtarString : files ,
} ,
) . BuildE ( )
b . Assert ( err , qt . IsNotNil )
b . Assert ( err . Error ( ) , qt . Contains , filepath . FromSlash ( ` render of "home" failed: "/layouts/baseof.html:4:6" ` ) )
} )
t . Run ( "index template" , func ( t * testing . T ) {
files := strings . Replace ( filesTemplate , "line 3 index" , "1234{{ .ThisDoesNotExist \"abc\" }}" , 1 )
b , err := NewIntegrationTestBuilder (
IntegrationTestConfig {
T : t ,
TxtarString : files ,
} ,
) . BuildE ( )
b . Assert ( err , qt . IsNotNil )
b . Assert ( err . Error ( ) , qt . Contains , filepath . FromSlash ( ` render of "home" failed: "/layouts/index.html:3:7" ` ) )
} )
t . Run ( "partial from define" , func ( t * testing . T ) {
files := strings . Replace ( filesTemplate , "toc line 2" , "12345{{ .ThisDoesNotExist \"abc\" }}" , 1 )
b , err := NewIntegrationTestBuilder (
IntegrationTestConfig {
T : t ,
TxtarString : files ,
} ,
) . BuildE ( )
b . Assert ( err , qt . IsNotNil )
b . Assert ( err . Error ( ) , qt . Contains , filepath . FromSlash ( ` render of "home" failed: "/layouts/index.html:7:8": execute of template failed ` ) )
b . Assert ( err . Error ( ) , qt . Contains , ` execute of template failed: template: partials/toc.html:2:8: executing "partials/toc.html" ` )
} )
2018-10-03 08:58:09 -04:00
}
2018-10-30 06:15:15 -04:00
// https://github.com/gohugoio/hugo/issues/5375
func TestSiteBuildTimeout ( t * testing . T ) {
b := newTestSitesBuilder ( t )
b . WithConfigFile ( "toml" , `
timeout = 5
` )
b . WithTemplatesAdded ( "_default/single.html" , `
{ { . WordCount } }
` , "shortcodes/c.html", `
{ { range . Page . Site . RegularPages } }
{ { . WordCount } }
{ { end } }
` )
for i := 1 ; i < 100 ; i ++ {
b . WithContent ( fmt . Sprintf ( "page%d.md" , i ) , ` -- -
title : "A page"
-- -
{ { < c > } } ` )
}
2019-01-02 06:33:26 -05:00
b . CreateSites ( ) . BuildFail ( BuildCfg { } )
2018-10-30 06:15:15 -04:00
}
2024-06-08 05:52:22 -04:00
func TestErrorTemplateRuntime ( t * testing . T ) {
t . Parallel ( )
files := `
-- hugo . toml --
-- layouts / index . html --
Home .
{ { . ThisDoesNotExist } }
`
b , err := TestE ( t , files )
b . Assert ( err , qt . Not ( qt . IsNil ) )
b . Assert ( err . Error ( ) , qt . Contains , filepath . FromSlash ( ` /layouts/index.html:2:3 ` ) )
b . Assert ( err . Error ( ) , qt . Contains , ` can't evaluate field ThisDoesNotExist ` )
}