]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
hugofs: Fix vertical mount merge issue
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 1 Mar 2024 13:42:56 +0000 (14:42 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 1 Mar 2024 16:10:13 +0000 (17:10 +0100)
Fixes #12175

hugofs/rootmapping_fs.go
hugolib/filesystems/basefs_test.go

index 336c8b4e71a2707e20f75f66375396409b2b0f30..a304986688002d9e119425706eca22758d50f514 100644 (file)
@@ -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]
@@ -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
 
@@ -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 {
index 4fdeba7655abb8c65214e99fc069b2708eb57969..10b4a4cb296490b27087b2be524a76495fab3e4f 100644 (file)
@@ -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 --