Repository: gohugoio/hugo (language: go)

Issue:
dartsass: Unable to resolve directory paths to directory index files
Details: <https://discourse.gohugo.io/t/51528/15>

Applicable to Sass imported as a Hugo module, Hugo is unable to resolve directory paths to directory index files.

For example, this:

```
@use foo/bar
```

does not resolve to any of these:

- `foo/bar/_index.scss`
- `foo/bar/_index.sass`
- `foo/bar/_index.css`

This affects both `@use` and `@forward` rules. See:

https://github.com/gohugoio/hugo/blob/28f621d4a73ca7e97e23b33cbf3780ddab188d24/resources/resource_transformers/tocss/dartsass/transform.go#L137

Based on experimentation, we should resolve to a file instead of a directory when both exist with same base name:

```text
assets/
└── sass/
    ├── foo/
    │   └── _index.scss
    ├── foo.scss
    └── main.scss    <-- @use "foo";
```

In the above, we should resolve to `foo.scss` instead of `foo/_index.scss`.








Discussion / hints:
What is a "index file" and where is the spec?
<https://sass-lang.com/documentation/at-rules/use/#index-files>
To reiterate, the problem is limited to Sass imported as a Hugo module. If the files exist in the OS file system, directory imports work as expected.

Fix patch:
diff --git a/resources/resource_transformers/tocss/dartsass/transform.go b/resources/resource_transformers/tocss/dartsass/transform.go
index 99a5c3f683e..ddf28723bea 100644
--- a/resources/resource_transformers/tocss/dartsass/transform.go
+++ b/resources/resource_transformers/tocss/dartsass/transform.go
@@ -165,7 +165,11 @@ func (t importResolver) CanonicalizeURL(url string) (string, error) {
 	} else if strings.HasPrefix(name, "_") {
 		namePatterns = []string{"_%s.scss", "_%s.sass", "_%s.css"}
 	} else {
-		namePatterns = []string{"_%s.scss", "%s.scss", "_%s.sass", "%s.sass", "_%s.css", "%s.css"}
+		namePatterns = []string{
+			"_%s.scss", "%s.scss", "%s/_index.scss",
+			"_%s.sass", "%s.sass", "%s/_index.sass",
+			"_%s.css", "%s.css",
+		}
 	}
 
 	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 4d48b3b6a6b..7b2b03dc0a9 100644
--- a/resources/resource_transformers/tocss/dartsass/dartsass_integration_test.go
+++ b/resources/resource_transformers/tocss/dartsass/dartsass_integration_test.go
@@ -562,3 +562,43 @@ Styles: {{ $r.RelPermalink }}
 
 	b.AssertFileContent("public/index.html", "Styles: /scss/main.css")
 }
+
+// Issue 12849
+func TestDirectoryIndexes(t *testing.T) {
+	t.Parallel()
+
+	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" "dartsass" "outputStyle" "compressed" }}
+{{ (resources.Get "sass/main.scss" | toCSS $opts).Content }}
+-- assets/sass/main.scss --
+@use "foo"; // directory with index file from OS file system
+@use "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}")
+}