Skip to content

Commit

Permalink
Fix readDir regression
Browse files Browse the repository at this point in the history
This reverts the `tpl/os` package, excluding new integration test, to `v0.92.2`.

We need some more tests and investigation into the old behaviour before we can change this.

Fixes gohugoio#9609
  • Loading branch information
bep committed Mar 7, 2022
1 parent 970f385 commit 8493662
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
15 changes: 11 additions & 4 deletions hugofs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,25 @@ func NewFrom(fs afero.Fs, cfg config.Provider) *Fs {
return newFs(fs, cfg)
}

func newFs(base afero.Fs, cfg config.Provider) *Fs {
func NewForWorkingDir(base afero.Fs, workingDir string) *Fs {
return &Fs{
Source: base,
Destination: base,
Os: &afero.OsFs{},
WorkingDir: getWorkingDirFs(base, cfg),
WorkingDir: getWorkingDirFs(base, workingDir),
}
}

func getWorkingDirFs(base afero.Fs, cfg config.Provider) *afero.BasePathFs {
workingDir := cfg.GetString("workingDir")
func newFs(base afero.Fs, cfg config.Provider) *Fs {
return &Fs{
Source: base,
Destination: base,
Os: &afero.OsFs{},
WorkingDir: getWorkingDirFs(base, cfg.GetString("workingDir")),
}
}

func getWorkingDirFs(base afero.Fs, workingDir string) *afero.BasePathFs {
if workingDir != "" {
return afero.NewBasePathFs(afero.NewReadOnlyFs(base), workingDir).(*afero.BasePathFs)
}
Expand Down
2 changes: 1 addition & 1 deletion hugolib/integrationtest_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func (s *IntegrationTestBuilder) initBuilder() {

logger := loggers.NewBasicLoggerForWriter(s.Cfg.LogLevel, &s.logBuff)

fs := hugofs.NewFrom(afs, config.New())
fs := hugofs.NewForWorkingDir(afs, s.Cfg.WorkingDir)

for _, f := range s.data.Files {
filename := filepath.Join(s.Cfg.WorkingDir, f.Name)
Expand Down
2 changes: 2 additions & 0 deletions tpl/os/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ START:|{{ range $entry := $entries }}{{ if not $entry.IsDir }}{{ $entry.Name }}|
START:|config.toml|myproject.txt|:END:
`)
}

// Issue 9609
26 changes: 12 additions & 14 deletions tpl/os/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,33 @@ import (
"errors"
"fmt"
_os "os"
"path/filepath"

"github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/deps"
"github.com/spf13/afero"
"github.com/spf13/cast"
)

// New returns a new instance of the os-namespaced template functions.
func New(d *deps.Deps) *Namespace {
var readFileFs, workFs afero.Fs
var rfs afero.Fs
if d.Fs != nil {
rfs = d.Fs.WorkingDir
if d.PathSpec != nil && d.PathSpec.BaseFs != nil {
rfs = afero.NewReadOnlyFs(afero.NewCopyOnWriteFs(d.PathSpec.BaseFs.Content.Fs, d.Fs.WorkingDir))
}

// The docshelper script does not have or need all the dependencies set up.
if d.PathSpec != nil {
readFileFs = afero.NewReadOnlyFs(afero.NewCopyOnWriteFs(d.PathSpec.BaseFs.Content.Fs, d.PathSpec.BaseFs.Work))
// See #9599
workFs = d.PathSpec.BaseFs.WorkDir
}

return &Namespace{
readFileFs: readFileFs,
workFs: workFs,
readFileFs: rfs,
deps: d,
}
}

// Namespace provides template functions for the "os" namespace.
type Namespace struct {
readFileFs afero.Fs
workFs afero.Fs
deps *deps.Deps
}

Expand All @@ -69,9 +67,8 @@ func (ns *Namespace) Getenv(key interface{}) (string, error) {
// readFile reads the file named by filename in the given filesystem
// and returns the contents as a string.
func readFile(fs afero.Fs, filename string) (string, error) {
filename = filepath.Clean(filename)
if filename == "" || filename == "." || filename == string(_os.PathSeparator) {
return "", errors.New("invalid filename")
if filename == "" {
return "", errors.New("readFile needs a filename")
}

b, err := afero.ReadFile(fs, filename)
Expand Down Expand Up @@ -100,12 +97,13 @@ func (ns *Namespace) ReadFile(i interface{}) (string, error) {

// ReadDir lists the directory contents relative to the configured WorkingDir.
func (ns *Namespace) ReadDir(i interface{}) ([]_os.FileInfo, error) {
defer herrors.Recover()
path, err := cast.ToStringE(i)
if err != nil {
return nil, err
}

list, err := afero.ReadDir(ns.workFs, path)
list, err := afero.ReadDir(ns.deps.Fs.WorkingDir, path)
if err != nil {
return nil, fmt.Errorf("failed to read directory %q: %s", path, err)
}
Expand Down

0 comments on commit 8493662

Please sign in to comment.