tocss: Fix the import resolving from absolute to relative assets paths

Fixes #12137
This commit is contained in:
Bjørn Erik Pedersen 2024-02-24 11:51:16 +01:00
parent 80e69344da
commit 6c7b85487d
No known key found for this signature in database
5 changed files with 44 additions and 6 deletions

View file

@ -338,12 +338,17 @@ func (c ComponentPath) ComponentPathJoined() string {
type ReverseLookupProvder interface { type ReverseLookupProvder interface {
ReverseLookup(filename string, checkExists bool) ([]ComponentPath, error) 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) ReverseStat(filename string) ([]FileMetaInfo, error)
func (fs *RootMappingFs) ReverseLookup(in string, checkExists bool) ([]ComponentPath, error) { func (fs *RootMappingFs) ReverseLookup(filename string, checkExists bool) ([]ComponentPath, error) {
in = fs.cleanName(in) return fs.ReverseLookupComponent("", filename, checkExists)
key := filepathSeparator + in }
func (fs *RootMappingFs) ReverseLookupComponent(component, filename string, checkExists bool) ([]ComponentPath, error) {
filename = fs.cleanName(filename)
key := filepathSeparator + filename
s, roots := fs.getRootsReverse(key) s, roots := fs.getRootsReverse(key)
@ -357,6 +362,9 @@ func (fs *RootMappingFs) ReverseLookup(in string, checkExists bool) ([]Component
dir, name := filepath.Split(base) dir, name := filepath.Split(base)
for _, first := range roots { for _, first := range roots {
if component != "" && first.FromBase != component {
continue
}
if first.Meta.Rename != nil { if first.Meta.Rename != nil {
name = first.Meta.Rename(name, true) name = first.Meta.Rename(name, true)
} }

View file

@ -362,7 +362,7 @@ func (d *SourceFilesystem) ReverseLookup(filename string, checkExists bool) ([]h
var cps []hugofs.ComponentPath var cps []hugofs.ComponentPath
hugofs.WalkFilesystems(d.Fs, func(fs afero.Fs) bool { hugofs.WalkFilesystems(d.Fs, func(fs afero.Fs) bool {
if rfs, ok := fs.(hugofs.ReverseLookupProvder); ok { 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...) cps = append(cps, c...)
} }
} }

View file

@ -478,6 +478,36 @@ Home.
_ = stat("blog/b1.md") _ = 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) { func TestStaticComposite(t *testing.T) {
files := ` files := `
-- hugo.toml -- -- hugo.toml --

View file

@ -144,7 +144,7 @@ func (t importResolver) CanonicalizeURL(url string) (string, error) {
var pathDir string var pathDir string
if isURL { if isURL {
var found bool 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 { if !found {
// Not a member of this filesystem, let Dart Sass handle it. // Not a member of this filesystem, let Dart Sass handle it.

View file

@ -86,7 +86,7 @@ func (t *toCSSTransformation) Transform(ctx *resources.ResourceTransformationCtx
if prev == "stdin" { if prev == "stdin" {
prevDir = baseDir prevDir = baseDir
} else { } else {
prevDir, _ = t.c.sfs.MakePathRelative(filepath.Dir(prev), false) prevDir, _ = t.c.sfs.MakePathRelative(filepath.Dir(prev), true)
if prevDir == "" { if prevDir == "" {
// Not a member of this filesystem. Let LibSASS handle it. // Not a member of this filesystem. Let LibSASS handle it.