mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-30 22:39:58 +02: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
This commit is contained in:
@@ -16,21 +16,25 @@ package hugofs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gohugoio/hugo/hugofs/glob"
|
||||
|
||||
"github.com/gohugoio/hugo/hugofs/files"
|
||||
"golang.org/x/text/unicode/norm"
|
||||
|
||||
"github.com/gohugoio/hugo/common/herrors"
|
||||
"github.com/gohugoio/hugo/common/hreflect"
|
||||
"github.com/gohugoio/hugo/common/htime"
|
||||
"github.com/gohugoio/hugo/common/paths"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
)
|
||||
@@ -39,48 +43,37 @@ func NewFileMeta() *FileMeta {
|
||||
return &FileMeta{}
|
||||
}
|
||||
|
||||
// PathFile returns the relative file path for the file source.
|
||||
func (f *FileMeta) PathFile() string {
|
||||
if f.BaseDir == "" {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimPrefix(strings.TrimPrefix(f.Filename, f.BaseDir), filepathSeparator)
|
||||
}
|
||||
|
||||
type FileMeta struct {
|
||||
Name string
|
||||
Filename string
|
||||
Path string
|
||||
PathWalk string
|
||||
OriginalFilename string
|
||||
BaseDir string
|
||||
PathInfo *paths.Path
|
||||
Name string
|
||||
Filename string
|
||||
|
||||
SourceRoot string
|
||||
MountRoot string
|
||||
Module string
|
||||
BaseDir string
|
||||
SourceRoot string
|
||||
Module string
|
||||
ModuleOrdinal int
|
||||
Component string
|
||||
|
||||
Weight int
|
||||
IsOrdered bool
|
||||
IsSymlink bool
|
||||
IsRootFile bool
|
||||
IsProject bool
|
||||
Watch bool
|
||||
Weight int
|
||||
IsProject bool
|
||||
Watch bool
|
||||
|
||||
Classifier files.ContentClass
|
||||
// The lang associated with this file. This may be
|
||||
// either the language set in the filename or
|
||||
// the language defined in the source mount configuration.
|
||||
Lang string
|
||||
// The language index for the above lang. This is the index
|
||||
// in the sorted list of languages/sites.
|
||||
LangIndex int
|
||||
|
||||
SkipDir bool
|
||||
|
||||
Lang string
|
||||
TranslationBaseName string
|
||||
TranslationBaseNameWithExt string
|
||||
Translations []string
|
||||
|
||||
Fs afero.Fs
|
||||
OpenFunc func() (afero.File, error)
|
||||
JoinStatFunc func(name string) (FileMetaInfo, error)
|
||||
|
||||
// Include only files or directories that match.
|
||||
InclusionFilter *glob.FilenameFilter
|
||||
|
||||
// Rename the name part of the file (not the directory).
|
||||
Rename func(name string, toFrom bool) string
|
||||
}
|
||||
|
||||
func (m *FileMeta) Copy() *FileMeta {
|
||||
@@ -120,6 +113,15 @@ func (f *FileMeta) Open() (afero.File, error) {
|
||||
return f.OpenFunc()
|
||||
}
|
||||
|
||||
func (f *FileMeta) ReadAll() ([]byte, error) {
|
||||
file, err := f.Open()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
return io.ReadAll(file)
|
||||
}
|
||||
|
||||
func (f *FileMeta) JoinStat(name string) (FileMetaInfo, error) {
|
||||
if f.JoinStatFunc == nil {
|
||||
return nil, os.ErrNotExist
|
||||
@@ -128,50 +130,123 @@ func (f *FileMeta) JoinStat(name string) (FileMetaInfo, error) {
|
||||
}
|
||||
|
||||
type FileMetaInfo interface {
|
||||
os.FileInfo
|
||||
// Meta is for internal use.
|
||||
fs.DirEntry
|
||||
MetaProvider
|
||||
|
||||
// This is a real hybrid as it also implements the fs.FileInfo interface.
|
||||
FileInfoOptionals
|
||||
}
|
||||
|
||||
type MetaProvider interface {
|
||||
Meta() *FileMeta
|
||||
}
|
||||
|
||||
type fileInfoMeta struct {
|
||||
os.FileInfo
|
||||
type FileInfoOptionals interface {
|
||||
Size() int64
|
||||
Mode() fs.FileMode
|
||||
ModTime() time.Time
|
||||
Sys() any
|
||||
}
|
||||
|
||||
type FileNameIsDir interface {
|
||||
Name() string
|
||||
IsDir() bool
|
||||
}
|
||||
|
||||
type FileInfoProvider interface {
|
||||
FileInfo() FileMetaInfo
|
||||
}
|
||||
|
||||
// DirOnlyOps is a subset of the afero.File interface covering
|
||||
// the methods needed for directory operations.
|
||||
type DirOnlyOps interface {
|
||||
io.Closer
|
||||
Name() string
|
||||
Readdir(count int) ([]os.FileInfo, error)
|
||||
Readdirnames(n int) ([]string, error)
|
||||
Stat() (os.FileInfo, error)
|
||||
}
|
||||
|
||||
type dirEntryMeta struct {
|
||||
fs.DirEntry
|
||||
m *FileMeta
|
||||
|
||||
fi fs.FileInfo
|
||||
fiInit sync.Once
|
||||
}
|
||||
|
||||
type filenameProvider interface {
|
||||
Filename() string
|
||||
}
|
||||
|
||||
var _ filenameProvider = (*fileInfoMeta)(nil)
|
||||
|
||||
// Filename returns the full filename.
|
||||
func (fi *fileInfoMeta) Filename() string {
|
||||
return fi.m.Filename
|
||||
}
|
||||
|
||||
// Name returns the file's name. Note that we follow symlinks,
|
||||
// if supported by the file system, and the Name given here will be the
|
||||
// name of the symlink, which is what Hugo needs in all situations.
|
||||
func (fi *fileInfoMeta) Name() string {
|
||||
if name := fi.m.Name; name != "" {
|
||||
return name
|
||||
}
|
||||
return fi.FileInfo.Name()
|
||||
}
|
||||
|
||||
func (fi *fileInfoMeta) Meta() *FileMeta {
|
||||
func (fi *dirEntryMeta) Meta() *FileMeta {
|
||||
return fi.m
|
||||
}
|
||||
|
||||
func NewFileMetaInfo(fi os.FileInfo, m *FileMeta) FileMetaInfo {
|
||||
// Filename returns the full filename.
|
||||
func (fi *dirEntryMeta) Filename() string {
|
||||
return fi.m.Filename
|
||||
}
|
||||
|
||||
func (fi *dirEntryMeta) fileInfo() fs.FileInfo {
|
||||
var err error
|
||||
fi.fiInit.Do(func() {
|
||||
fi.fi, err = fi.DirEntry.Info()
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return fi.fi
|
||||
}
|
||||
|
||||
func (fi *dirEntryMeta) Size() int64 {
|
||||
return fi.fileInfo().Size()
|
||||
}
|
||||
|
||||
func (fi *dirEntryMeta) Mode() fs.FileMode {
|
||||
return fi.fileInfo().Mode()
|
||||
}
|
||||
|
||||
func (fi *dirEntryMeta) ModTime() time.Time {
|
||||
return fi.fileInfo().ModTime()
|
||||
}
|
||||
|
||||
func (fi *dirEntryMeta) Sys() any {
|
||||
return fi.fileInfo().Sys()
|
||||
}
|
||||
|
||||
// Name returns the file's name.
|
||||
func (fi *dirEntryMeta) Name() string {
|
||||
if name := fi.m.Name; name != "" {
|
||||
return name
|
||||
}
|
||||
return fi.DirEntry.Name()
|
||||
}
|
||||
|
||||
// dirEntry is an adapter from os.FileInfo to fs.DirEntry
|
||||
type dirEntry struct {
|
||||
fs.FileInfo
|
||||
}
|
||||
|
||||
var _ fs.DirEntry = dirEntry{}
|
||||
|
||||
func (d dirEntry) Type() fs.FileMode { return d.FileInfo.Mode().Type() }
|
||||
|
||||
func (d dirEntry) Info() (fs.FileInfo, error) { return d.FileInfo, nil }
|
||||
|
||||
func NewFileMetaInfo(fi FileNameIsDir, m *FileMeta) FileMetaInfo {
|
||||
if m == nil {
|
||||
panic("FileMeta must be set")
|
||||
}
|
||||
if fim, ok := fi.(FileMetaInfo); ok {
|
||||
if fim, ok := fi.(MetaProvider); ok {
|
||||
m.Merge(fim.Meta())
|
||||
}
|
||||
return &fileInfoMeta{FileInfo: fi, m: m}
|
||||
switch v := fi.(type) {
|
||||
case fs.DirEntry:
|
||||
return &dirEntryMeta{DirEntry: v, m: m}
|
||||
case fs.FileInfo:
|
||||
return &dirEntryMeta{DirEntry: dirEntry{v}, m: m}
|
||||
case nil:
|
||||
return &dirEntryMeta{DirEntry: dirEntry{}, m: m}
|
||||
default:
|
||||
panic(fmt.Sprintf("Unsupported type: %T", fi))
|
||||
}
|
||||
}
|
||||
|
||||
type dirNameOnlyFileInfo struct {
|
||||
@@ -212,7 +287,6 @@ func newDirNameOnlyFileInfo(name string, meta *FileMeta, fileOpener func() (afer
|
||||
m.Filename = name
|
||||
}
|
||||
m.OpenFunc = fileOpener
|
||||
m.IsOrdered = false
|
||||
|
||||
return NewFileMetaInfo(
|
||||
&dirNameOnlyFileInfo{name: base, modTime: htime.Now()},
|
||||
@@ -220,16 +294,10 @@ func newDirNameOnlyFileInfo(name string, meta *FileMeta, fileOpener func() (afer
|
||||
)
|
||||
}
|
||||
|
||||
func decorateFileInfo(
|
||||
fi os.FileInfo,
|
||||
fs afero.Fs, opener func() (afero.File, error),
|
||||
filename, filepath string, inMeta *FileMeta,
|
||||
) FileMetaInfo {
|
||||
func decorateFileInfo(fi FileNameIsDir, opener func() (afero.File, error), filename string, inMeta *FileMeta) FileMetaInfo {
|
||||
var meta *FileMeta
|
||||
var fim FileMetaInfo
|
||||
|
||||
filepath = strings.TrimPrefix(filepath, filepathSeparator)
|
||||
|
||||
var ok bool
|
||||
if fim, ok = fi.(FileMetaInfo); ok {
|
||||
meta = fim.Meta()
|
||||
@@ -241,14 +309,8 @@ func decorateFileInfo(
|
||||
if opener != nil {
|
||||
meta.OpenFunc = opener
|
||||
}
|
||||
if fs != nil {
|
||||
meta.Fs = fs
|
||||
}
|
||||
nfilepath := normalizeFilename(filepath)
|
||||
|
||||
nfilename := normalizeFilename(filename)
|
||||
if nfilepath != "" {
|
||||
meta.Path = nfilepath
|
||||
}
|
||||
if nfilename != "" {
|
||||
meta.Filename = nfilename
|
||||
}
|
||||
@@ -258,14 +320,11 @@ func decorateFileInfo(
|
||||
return fim
|
||||
}
|
||||
|
||||
func isSymlink(fi os.FileInfo) bool {
|
||||
return fi != nil && fi.Mode()&os.ModeSymlink == os.ModeSymlink
|
||||
}
|
||||
|
||||
func fileInfosToFileMetaInfos(fis []os.FileInfo) []FileMetaInfo {
|
||||
func DirEntriesToFileMetaInfos(fis []fs.DirEntry) []FileMetaInfo {
|
||||
fims := make([]FileMetaInfo, len(fis))
|
||||
for i, v := range fis {
|
||||
fims[i] = v.(FileMetaInfo)
|
||||
fim := v.(FileMetaInfo)
|
||||
fims[i] = fim
|
||||
}
|
||||
return fims
|
||||
}
|
||||
@@ -281,17 +340,49 @@ func normalizeFilename(filename string) string {
|
||||
return filename
|
||||
}
|
||||
|
||||
func fileInfosToNames(fis []os.FileInfo) []string {
|
||||
names := make([]string, len(fis))
|
||||
for i, d := range fis {
|
||||
names[i] = d.Name()
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
func sortFileInfos(fis []os.FileInfo) {
|
||||
func sortDirEntries(fis []fs.DirEntry) {
|
||||
sort.Slice(fis, func(i, j int) bool {
|
||||
fimi, fimj := fis[i].(FileMetaInfo), fis[j].(FileMetaInfo)
|
||||
return fimi.Meta().Filename < fimj.Meta().Filename
|
||||
})
|
||||
}
|
||||
|
||||
// AddFileInfoToError adds file info to the given error.
|
||||
func AddFileInfoToError(err error, fi FileMetaInfo, fs afero.Fs) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
meta := fi.Meta()
|
||||
filename := meta.Filename
|
||||
|
||||
// Check if it's already added.
|
||||
for _, ferr := range herrors.UnwrapFileErrors(err) {
|
||||
pos := ferr.Position()
|
||||
errfilename := pos.Filename
|
||||
if errfilename == "" {
|
||||
pos.Filename = filename
|
||||
ferr.UpdatePosition(pos)
|
||||
}
|
||||
|
||||
if errfilename == "" || errfilename == filename {
|
||||
if filename != "" && ferr.ErrorContext() == nil {
|
||||
f, ioerr := fs.Open(filename)
|
||||
if ioerr != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
ferr.UpdateContent(f, nil)
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
lineMatcher := herrors.NopLineMatcher
|
||||
|
||||
if textSegmentErr, ok := err.(*herrors.TextSegmentError); ok {
|
||||
lineMatcher = herrors.ContainsMatcher(textSegmentErr.Segment)
|
||||
}
|
||||
|
||||
return herrors.NewFileErrorFromFile(err, filename, fs, lineMatcher)
|
||||
}
|
||||
|
Reference in New Issue
Block a user