mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-29 06:22:07 -05:00
7285e74090
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
388 lines
8 KiB
Go
388 lines
8 KiB
Go
// Copyright 2019 The Hugo Authors. All rights reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
// Package hugofs provides the file systems used by Hugo.
|
|
package hugofs
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"runtime"
|
|
"sort"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/gohugoio/hugo/hugofs/glob"
|
|
|
|
"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"
|
|
)
|
|
|
|
func NewFileMeta() *FileMeta {
|
|
return &FileMeta{}
|
|
}
|
|
|
|
type FileMeta struct {
|
|
PathInfo *paths.Path
|
|
Name string
|
|
Filename string
|
|
|
|
BaseDir string
|
|
SourceRoot string
|
|
Module string
|
|
ModuleOrdinal int
|
|
Component string
|
|
|
|
Weight int
|
|
IsProject bool
|
|
Watch bool
|
|
|
|
// 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
|
|
|
|
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 {
|
|
if m == nil {
|
|
return NewFileMeta()
|
|
}
|
|
c := *m
|
|
return &c
|
|
}
|
|
|
|
func (m *FileMeta) Merge(from *FileMeta) {
|
|
if m == nil || from == nil {
|
|
return
|
|
}
|
|
dstv := reflect.Indirect(reflect.ValueOf(m))
|
|
srcv := reflect.Indirect(reflect.ValueOf(from))
|
|
|
|
for i := 0; i < dstv.NumField(); i++ {
|
|
v := dstv.Field(i)
|
|
if !v.CanSet() {
|
|
continue
|
|
}
|
|
if !hreflect.IsTruthfulValue(v) {
|
|
v.Set(srcv.Field(i))
|
|
}
|
|
}
|
|
|
|
if m.InclusionFilter == nil {
|
|
m.InclusionFilter = from.InclusionFilter
|
|
}
|
|
}
|
|
|
|
func (f *FileMeta) Open() (afero.File, error) {
|
|
if f.OpenFunc == nil {
|
|
return nil, errors.New("OpenFunc not set")
|
|
}
|
|
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
|
|
}
|
|
return f.JoinStatFunc(name)
|
|
}
|
|
|
|
type FileMetaInfo interface {
|
|
fs.DirEntry
|
|
MetaProvider
|
|
|
|
// This is a real hybrid as it also implements the fs.FileInfo interface.
|
|
FileInfoOptionals
|
|
}
|
|
|
|
type MetaProvider interface {
|
|
Meta() *FileMeta
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func (fi *dirEntryMeta) Meta() *FileMeta {
|
|
return fi.m
|
|
}
|
|
|
|
// 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.(MetaProvider); ok {
|
|
m.Merge(fim.Meta())
|
|
}
|
|
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 {
|
|
name string
|
|
modTime time.Time
|
|
}
|
|
|
|
func (fi *dirNameOnlyFileInfo) Name() string {
|
|
return fi.name
|
|
}
|
|
|
|
func (fi *dirNameOnlyFileInfo) Size() int64 {
|
|
panic("not implemented")
|
|
}
|
|
|
|
func (fi *dirNameOnlyFileInfo) Mode() os.FileMode {
|
|
return os.ModeDir
|
|
}
|
|
|
|
func (fi *dirNameOnlyFileInfo) ModTime() time.Time {
|
|
return fi.modTime
|
|
}
|
|
|
|
func (fi *dirNameOnlyFileInfo) IsDir() bool {
|
|
return true
|
|
}
|
|
|
|
func (fi *dirNameOnlyFileInfo) Sys() any {
|
|
return nil
|
|
}
|
|
|
|
func newDirNameOnlyFileInfo(name string, meta *FileMeta, fileOpener func() (afero.File, error)) FileMetaInfo {
|
|
name = normalizeFilename(name)
|
|
_, base := filepath.Split(name)
|
|
|
|
m := meta.Copy()
|
|
if m.Filename == "" {
|
|
m.Filename = name
|
|
}
|
|
m.OpenFunc = fileOpener
|
|
|
|
return NewFileMetaInfo(
|
|
&dirNameOnlyFileInfo{name: base, modTime: htime.Now()},
|
|
m,
|
|
)
|
|
}
|
|
|
|
func decorateFileInfo(fi FileNameIsDir, opener func() (afero.File, error), filename string, inMeta *FileMeta) FileMetaInfo {
|
|
var meta *FileMeta
|
|
var fim FileMetaInfo
|
|
|
|
var ok bool
|
|
if fim, ok = fi.(FileMetaInfo); ok {
|
|
meta = fim.Meta()
|
|
} else {
|
|
meta = NewFileMeta()
|
|
fim = NewFileMetaInfo(fi, meta)
|
|
}
|
|
|
|
if opener != nil {
|
|
meta.OpenFunc = opener
|
|
}
|
|
|
|
nfilename := normalizeFilename(filename)
|
|
if nfilename != "" {
|
|
meta.Filename = nfilename
|
|
}
|
|
|
|
meta.Merge(inMeta)
|
|
|
|
return fim
|
|
}
|
|
|
|
func DirEntriesToFileMetaInfos(fis []fs.DirEntry) []FileMetaInfo {
|
|
fims := make([]FileMetaInfo, len(fis))
|
|
for i, v := range fis {
|
|
fim := v.(FileMetaInfo)
|
|
fims[i] = fim
|
|
}
|
|
return fims
|
|
}
|
|
|
|
func normalizeFilename(filename string) string {
|
|
if filename == "" {
|
|
return ""
|
|
}
|
|
if runtime.GOOS == "darwin" {
|
|
// When a file system is HFS+, its filepath is in NFD form.
|
|
return norm.NFC.String(filename)
|
|
}
|
|
return filename
|
|
}
|
|
|
|
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)
|
|
}
|