Skip to content

Commit

Permalink
tocss: Fix the import resolving from absolute to relative assets paths
Browse files Browse the repository at this point in the history
Fixes #12137
  • Loading branch information
bep committed Feb 24, 2024
1 parent b2b7bfd commit 189b723
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
14 changes: 11 additions & 3 deletions hugofs/rootmapping_fs.go
Expand Up @@ -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)

Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion hugolib/filesystems/basefs.go
Expand Up @@ -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...)
}
}
Expand Down
30 changes: 30 additions & 0 deletions hugolib/filesystems/basefs_test.go
Expand Up @@ -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 --
Expand Down
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion resources/resource_transformers/tocss/scss/tocss.go
Expand Up @@ -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.
Expand Down

0 comments on commit 189b723

Please sign in to comment.