Repository: gohugoio/hugo (language: go)

Issue:
libsass: Unable to resolve directory paths to directory index files
Similar to https://github.com/gohugoio/hugo/issues/12849, but for LibSass.

When implementing https://github.com/gohugoio/hugo/pull/12850 I didn't notice that LibSass 3.6.0 and later support index files with `@import` rules.

See <https://sass-lang.com/documentation/at-rules/import/#index-files>.


Discussion / hints:

Fix patch:
diff --git a/resources/resource_transformers/tocss/dartsass/transform.go b/resources/resource_transformers/tocss/dartsass/transform.go
index ddf28723bea..f0bd6634af5 100644
--- a/resources/resource_transformers/tocss/dartsass/transform.go
+++ b/resources/resource_transformers/tocss/dartsass/transform.go
@@ -166,9 +166,10 @@ func (t importResolver) CanonicalizeURL(url string) (string, error) {
 		namePatterns = []string{"_%s.scss", "_%s.sass", "_%s.css"}
 	} else {
 		namePatterns = []string{
-			"_%s.scss", "%s.scss", "%s/_index.scss",
-			"_%s.sass", "%s.sass", "%s/_index.sass",
+			"_%s.scss", "%s.scss",
+			"_%s.sass", "%s.sass",
 			"_%s.css", "%s.css",
+			"%s/_index.scss", "%s/_index.sass",
 		}
 	}
 
diff --git a/resources/resource_transformers/tocss/scss/tocss.go b/resources/resource_transformers/tocss/scss/tocss.go
index 3a46e601691..36ef2a77db4 100644
--- a/resources/resource_transformers/tocss/scss/tocss.go
+++ b/resources/resource_transformers/tocss/scss/tocss.go
@@ -105,7 +105,11 @@ func (t *toCSSTransformation) Transform(ctx *resources.ResourceTransformationCtx
 		} else if strings.HasPrefix(name, "_") {
 			namePatterns = []string{"_%s.scss", "_%s.sass"}
 		} else {
-			namePatterns = []string{"_%s.scss", "%s.scss", "_%s.sass", "%s.sass"}
+			namePatterns = []string{
+				"_%s.scss", "%s.scss",
+				"_%s.sass", "%s.sass",
+				"%s/_index.scss", "%s/_index.sass",
+			}
 		}
 
 		name = strings.TrimPrefix(name, "_")


Test patch:
diff --git a/resources/resource_transformers/tocss/dartsass/dartsass_integration_test.go b/resources/resource_transformers/tocss/dartsass/dartsass_integration_test.go
index 7b2b03dc0a9..2aac2c5fb54 100644
--- a/resources/resource_transformers/tocss/dartsass/dartsass_integration_test.go
+++ b/resources/resource_transformers/tocss/dartsass/dartsass_integration_test.go
@@ -566,6 +566,9 @@ Styles: {{ $r.RelPermalink }}
 // Issue 12849
 func TestDirectoryIndexes(t *testing.T) {
 	t.Parallel()
+	if !dartsass.Supports() {
+		t.Skip()
+	}
 
 	files := `
 -- hugo.toml --
diff --git a/resources/resource_transformers/tocss/scss/scss_integration_test.go b/resources/resource_transformers/tocss/scss/scss_integration_test.go
index 46946387202..bd140cc88cf 100644
--- a/resources/resource_transformers/tocss/scss/scss_integration_test.go
+++ b/resources/resource_transformers/tocss/scss/scss_integration_test.go
@@ -111,7 +111,7 @@ moo {
 
 @import "another.css";
 /* foo */
-        
+
 `)
 }
 
@@ -262,7 +262,7 @@ body {
 	body {
 		background: url($image) no-repeat center/cover;
 		font-family: $font;
-	  }	  
+	  }
 }
 
 p {
@@ -417,3 +417,46 @@ h3 {
 
 	b.AssertFileContent("public/index.html", `b.46b2d77c7ffe37ee191678f72df991ecb1319f849957151654362f09b0ef467f.css`)
 }
+
+// Issue 12851
+func TestDirectoryIndexes(t *testing.T) {
+	t.Parallel()
+	if !scss.Supports() {
+		t.Skip()
+	}
+
+	files := `
+-- hugo.toml --
+disableKinds = ['page','section','rss','sitemap','taxonomy','term']
+
+[[module.mounts]]
+source = 'assets'
+target = 'assets'
+
+[[module.imports]]
+path = "github.com/gohugoio/hugoTestModule2"
+
+[[module.imports.mounts]]
+source = "miscellaneous/sass"
+target = "assets/sass"
+-- go.mod --
+module hugo-github-issue-12849
+-- layouts/index.html --
+{{ $opts := dict "transpiler" "libsass" "outputStyle" "compressed" }}
+{{ (resources.Get "sass/main.scss" | toCSS $opts).Content }}
+-- assets/sass/main.scss --
+@import "foo"; // directory with index file from OS file system
+@import "bar"; // directory with index file from module mount
+-- assets/sass/foo/_index.scss --
+.foo {color: red;}
+`
+
+	b := hugolib.NewIntegrationTestBuilder(
+		hugolib.IntegrationTestConfig{
+			T:           t,
+			NeedsOsFS:   true,
+			TxtarString: files,
+		}).Build()
+
+	b.AssertFileContent("public/index.html", ".foo{color:red}.bar{color:green}")
+}