Repository: gohugoio/hugo (language: go)

Issue:
.Page.Resources.GetMatch returns nil when the image file name ends with `.en.png`
<!--
Please do not use the issue queue for questions or troubleshooting. Unless you are certain that your issue is a software defect, use the forum:

https://discourse.gohugo.io
-->

<!-- Please answer these questions before submitting your issue. Thanks! -->

### What version of Hugo are you using (`hugo version`)?

<pre>
$ hugo version
hugo v0.123.0+extended linux/amd64 BuildDate=unknown
</pre>

### Does this issue reproduce with the latest release?
Yes

After upgraded to latest v0.123.0, I can't build my site successfully, the error message is:

```bash
Error: error building site: render: failed to render pages: render of "page" failed: "/home/yuanji/Projects/nikki/themes/futu/layouts/_default/baseof.html:9:8": execute of template failed: template: _default/single.html:9:8: executing "_default/single.html" at <partial "head.html" .>: error calling partial: "/home/yuanji/Projects/nikki/themes/futu/layouts/partials/head.html:15:3": execute of template failed: template: partials/head.html:15:3: executing "partials/head.html" at <partial "schema_jsonld.html" .>: error calling partial: "/home/yuanji/Projects/nikki/themes/futu/layouts/partials/schema_jsonld.html:42:20": execute of template failed: template: partials/schema_jsonld.html:42:20: executing "partials/schema_jsonld.html" at <.Content>: error calling Content: "/home/yuanji/Projects/nikki/content/posts/my_post_title/index.en.md:1:1": "/home/yuanji/Projects/nikki/themes/futu/layouts/_default/_markup/render-image.html:4:15": execute of template failed: template: _default/_markup/render-image.html:4:15: executing "_default/_markup/render-image.html" at <$image.MediaType.SubType>: nil pointer evaluating resource.Resource.MediaType
```
I found if I rename the image file name from `cover.en.png` to `cover_en.png` (along with its reference in markdown), then the error will disappear.


Discussion / hints:
Please provide image render hook code.
Hi @jmooring , thanks for your reply. Here it is.
```
{{ $image := .Page.Resources.GetMatch .Destination }}
{{/* https://github.com/gohugoio/hugo/issues/5030 */}}
{{ if ne $image.MediaType.SubType "gif" }}
  {{/* https://discourse.gohugo.io/t/image-conversion-without-resizing/32429 */}}
  {{ $image = $image.Resize (print $image.Width "x" $image.Height " webp") }}
{{ end }}

{{ if .IsBlock }}
  <figure>
    <img
      src="{{ $image.RelPermalink }}"
      alt="{{ .Text }}"
      width="{{ $image.Width }}"
      height="{{ $image.Height }}"
      loading="lazy"
      {{ with .Title }}title="{{ . }}"{{ end }}
    />
    <figcaption>{{ with .Title }}{{ . | safeHTML }}{{ else }}{{ .Text | safeHTML }}{{ end }}</figcaption>
  </figure>
{{ else }}
  <img
    src="{{ $image.RelPermalink }}"
    alt="{{ .Text }}"
    width="{{ $image.Width }}"
    height="{{ $image.Height }}"
    loading="lazy"
    {{ with .Title }}title="{{ . }}"{{ end }}
  />
  <span>{{ with .Title }}{{ . | safeHTML }}{{ else }}{{ .Text | safeHTML }}{{ end }}</span>
{{ end }}
```
We could (should) apply the same fallback check as we do in `resources.Get` -- I assumed that all/most resources.GetMatch would use some kind of wildcard. 

But I suspect that this would be more correct:

```
{{ $image := .Page.Resources.Get .Destination }}
```


Fix patch:
diff --git a/resources/resource/resources.go b/resources/resource/resources.go
index 9f298b7a6c6..fb5f9a5ee13 100644
--- a/resources/resource/resources.go
+++ b/resources/resource/resources.go
@@ -106,6 +106,15 @@ func (r Resources) GetMatch(pattern any) Resource {
 		}
 	}
 
+	// Finally, check the original name.
+	for _, resource := range r {
+		if nop, ok := resource.(NameOriginalProvider); ok {
+			if g.Match(paths.NormalizePathStringBasic(nop.NameOriginal())) {
+				return resource
+			}
+		}
+	}
+
 	return nil
 }
 
@@ -135,6 +144,16 @@ func (r Resources) Match(pattern any) Resources {
 			matches = append(matches, resource)
 		}
 	}
+	if len(matches) == 0 {
+		// 	Fall back to the original name.
+		for _, resource := range r {
+			if nop, ok := resource.(NameOriginalProvider); ok {
+				if g.Match(strings.ToLower(nop.NameOriginal())) {
+					matches = append(matches, resource)
+				}
+			}
+		}
+	}
 	return matches
 }
 


Test patch:
diff --git a/hugolib/pagebundler_test.go b/hugolib/pagebundler_test.go
index 9f5255c7477..e9686706d0f 100644
--- a/hugolib/pagebundler_test.go
+++ b/hugolib/pagebundler_test.go
@@ -876,3 +876,20 @@ RegularPages: {{ range .RegularPages }}{{ .RelPermalink }}|File LogicalName: {{
 		"List: |/mysection|File LogicalName: _index.md|/mysection/|section|Resources: sectiondata.json: Secion data JSON.|sectiondata.txt: Section data TXT.|$",
 		"RegularPages: /mysection/foo/p2/|File LogicalName: p2.md|/mysection/mybundle/|File LogicalName: index.md|/mysection/p2/|File LogicalName: p2.md|$")
 }
+
+func TestBundleResourcesGetMatchOriginalName(t *testing.T) {
+	files := `
+-- hugo.toml --
+baseURL = "https://example.com"
+-- content/mybundle/index.md --
+-- content/mybundle/f1.en.txt --
+F1.
+-- layouts/_default/single.html --
+GetMatch: {{ with .Resources.GetMatch "f1.en.*" }}{{ .Name }}: {{ .Content }}|{{ end }}
+Match: {{ range .Resources.Match "f1.en.*" }}{{ .Name }}: {{ .Content }}|{{ end }}
+`
+
+	b := Test(t, files)
+
+	b.AssertFileContent("public/mybundle/index.html", "GetMatch: f1.txt: F1.|", "Match: f1.txt: F1.|")
+}