From 6c7b85487db64449918cb3d20113c90a6648ffbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sat, 24 Feb 2024 11:51:16 +0100 Subject: [PATCH] tocss: Fix the import resolving from absolute to relative assets paths Fixes #12137 --- hugofs/rootmapping_fs.go | 14 +++++++-- hugolib/filesystems/basefs.go | 2 +- hugolib/filesystems/basefs_test.go | 30 +++++++++++++++++++ .../tocss/dartsass/transform.go | 2 +- .../resource_transformers/tocss/scss/tocss.go | 2 +- 5 files changed, 44 insertions(+), 6 deletions(-) diff --git a/hugofs/rootmapping_fs.go b/hugofs/rootmapping_fs.go index ce4243fbb..9a89914be 100644 --- a/hugofs/rootmapping_fs.go +++ b/hugofs/rootmapping_fs.go @@ -338,12 +338,17 @@ func (c ComponentPath) ComponentPathJoined() string { type ReverseLookupProvder interface { ReverseLookup(filename string, checkExists bool) ([]ComponentPath, error) + ReverseLookupComponent(component, filename string, checkExists bool) ([]ComponentPath, error) } // func (fs *RootMappingFs) ReverseStat(filename string) ([]FileMetaInfo, error) -func (fs *RootMappingFs) ReverseLookup(in string, checkExists bool) ([]ComponentPath, error) { - in = fs.cleanName(in) - key := filepathSeparator + in +func (fs *RootMappingFs) ReverseLookup(filename string, checkExists bool) ([]ComponentPath, error) { + return fs.ReverseLookupComponent("", filename, checkExists) +} + +func (fs *RootMappingFs) ReverseLookupComponent(component, filename string, checkExists bool) ([]ComponentPath, error) { + filename = fs.cleanName(filename) + key := filepathSeparator + filename s, roots := fs.getRootsReverse(key) @@ -357,6 +362,9 @@ func (fs *RootMappingFs) ReverseLookup(in string, checkExists bool) ([]Component dir, name := filepath.Split(base) for _, first := range roots { + if component != "" && first.FromBase != component { + continue + } if first.Meta.Rename != nil { name = first.Meta.Rename(name, true) } diff --git a/hugolib/filesystems/basefs.go b/hugolib/filesystems/basefs.go index 25c58d516..5479e2266 100644 --- a/hugolib/filesystems/basefs.go +++ b/hugolib/filesystems/basefs.go @@ -362,7 +362,7 @@ func (d *SourceFilesystem) ReverseLookup(filename string, checkExists bool) ([]h var cps []hugofs.ComponentPath hugofs.WalkFilesystems(d.Fs, func(fs afero.Fs) bool { if rfs, ok := fs.(hugofs.ReverseLookupProvder); ok { - if c, err := rfs.ReverseLookup(filename, checkExists); err == nil { + if c, err := rfs.ReverseLookupComponent(d.Name, filename, checkExists); err == nil { cps = append(cps, c...) } } diff --git a/hugolib/filesystems/basefs_test.go b/hugolib/filesystems/basefs_test.go index f5b7b6170..f50bdb09f 100644 --- a/hugolib/filesystems/basefs_test.go +++ b/hugolib/filesystems/basefs_test.go @@ -478,6 +478,36 @@ Home. _ = stat("blog/b1.md") } +func TestReverseLookupShouldOnlyConsiderFilesInCurrentComponent(t *testing.T) { + files := ` +-- hugo.toml -- +baseURL = "https://example.com/" +[module] +[[module.mounts]] +source = "files/layouts" +target = "layouts" +[[module.mounts]] +source = "files/layouts/assets" +target = "assets" +-- files/layouts/l1.txt -- +l1 +-- files/layouts/assets/l2.txt -- +l2 +` + b := hugolib.Test(t, files) + + assetsFs := b.H.Assets + + for _, checkExists := range []bool{false, true} { + cps, err := assetsFs.ReverseLookup(filepath.FromSlash("files/layouts/assets/l2.txt"), checkExists) + b.Assert(err, qt.IsNil) + b.Assert(cps, qt.HasLen, 1) + cps, err = assetsFs.ReverseLookup(filepath.FromSlash("files/layouts/l2.txt"), checkExists) + b.Assert(err, qt.IsNil) + b.Assert(cps, qt.HasLen, 0) + } +} + func TestStaticComposite(t *testing.T) { files := ` -- hugo.toml -- diff --git a/resources/resource_transformers/tocss/dartsass/transform.go b/resources/resource_transformers/tocss/dartsass/transform.go index a9600f380..17f16a688 100644 --- a/resources/resource_transformers/tocss/dartsass/transform.go +++ b/resources/resource_transformers/tocss/dartsass/transform.go @@ -144,7 +144,7 @@ func (t importResolver) CanonicalizeURL(url string) (string, error) { var pathDir string if isURL { var found bool - prevDir, found = t.c.sfs.MakePathRelative(filepath.Dir(filePath), false) + prevDir, found = t.c.sfs.MakePathRelative(filepath.Dir(filePath), true) if !found { // Not a member of this filesystem, let Dart Sass handle it. diff --git a/resources/resource_transformers/tocss/scss/tocss.go b/resources/resource_transformers/tocss/scss/tocss.go index a3f2a5289..3a46e6016 100644 --- a/resources/resource_transformers/tocss/scss/tocss.go +++ b/resources/resource_transformers/tocss/scss/tocss.go @@ -86,7 +86,7 @@ func (t *toCSSTransformation) Transform(ctx *resources.ResourceTransformationCtx if prev == "stdin" { prevDir = baseDir } else { - prevDir, _ = t.c.sfs.MakePathRelative(filepath.Dir(prev), false) + prevDir, _ = t.c.sfs.MakePathRelative(filepath.Dir(prev), true) if prevDir == "" { // Not a member of this filesystem. Let LibSASS handle it.