Skip to content

Commit

Permalink
hugofs: Fix vertical mount merge issue
Browse files Browse the repository at this point in the history
Fixes #12175
  • Loading branch information
bep committed Mar 1, 2024
1 parent 0d6e593 commit 2b2f2b7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
28 changes: 27 additions & 1 deletion hugofs/rootmapping_fs.go
Expand Up @@ -475,6 +475,11 @@ func (fs *RootMappingFs) newUnionFile(fis ...FileMetaInfo) (afero.File, error) {
return fis[0].Meta().Open()
}

if !fis[0].IsDir() {
// Pick the last file mount.
return fis[len(fis)-1].Meta().Open()
}

openers := make([]func() (afero.File, error), len(fis))
for i := len(fis) - 1; i >= 0; i-- {
fi := fis[i]
Expand Down Expand Up @@ -647,6 +652,28 @@ func (rfs *RootMappingFs) collectDirEntries(prefix string) ([]iofs.DirEntry, err
}

func (fs *RootMappingFs) doStat(name string) ([]FileMetaInfo, error) {
fis, err := fs.doDoStat(name)
if err != nil {
return nil, err
}
// Sanity check. Check that all is either file or directories.
var isDir, isFile bool
for _, fi := range fis {
if fi.IsDir() {
isDir = true
} else {
isFile = true
}
}
if isDir && isFile {
// For now.
return nil, os.ErrNotExist
}

return fis, nil
}

func (fs *RootMappingFs) doDoStat(name string) ([]FileMetaInfo, error) {
name = fs.cleanName(name)
key := filepathSeparator + name

Expand All @@ -669,7 +696,6 @@ func (fs *RootMappingFs) doStat(name string) ([]FileMetaInfo, error) {
var fis []FileMetaInfo

for _, rm := range roots {

var fi FileMetaInfo
fi, err = fs.statRoot(rm, name)
if err == nil {
Expand Down
34 changes: 34 additions & 0 deletions hugolib/filesystems/basefs_test.go
Expand Up @@ -509,6 +509,40 @@ l2
}
}

func TestAssetsIssue12175(t *testing.T) {
files := `
-- hugo.toml --
baseURL = "https://example.com/"
[module]
[[module.mounts]]
source = "node_modules/@foo/core/assets"
target = "assets"
[[module.mounts]]
source = "assets"
target = "assets"
-- node_modules/@foo/core/assets/js/app.js --
JS.
-- node_modules/@foo/core/assets/scss/app.scss --
body { color: red; }
-- assets/scss/app.scss --
body { color: blue; }
-- layouts/index.html --
Home.
SCSS: {{ with resources.Get "scss/app.scss" }}{{ .RelPermalink }}|{{ .Content }}{{ end }}|
# Note that the pattern below will match 2 resources, which doesn't make much sense,
# but is how the current (and also < v0.123.0) merge logic works, and for most practical purposes, it doesn't matter.
SCSS Match: {{ with resources.Match "**.scss" }}{{ . | len }}|{{ range .}}{{ .RelPermalink }}|{{ end }}{{ end }}|
`

b := hugolib.Test(t, files)

b.AssertFileContent("public/index.html", `
SCSS: /scss/app.scss|body { color: blue; }|
SCSS Match: 2|
`)
}

func TestStaticComposite(t *testing.T) {
files := `
-- hugo.toml --
Expand Down

0 comments on commit 2b2f2b7

Please sign in to comment.