Backtestshugorun-20261013-011256#15208

Issue #15208 Merged fix PR #15209 Run 13 Oct 2026 · run-20261013-011256 gohugoio/hugo

Add resources.Publish

Better

Our patch was judged better than the merged fix.

In this judgment, Patch B = our agent, Patch A = the merged fix.

Both patches add the same core change: a `Publish(r resource.Resource) (resource.Resource, error)` function in `tpl/resources`, registered via `AddMethodMapping`. It type-asserts to `resource.Source`, calls `Publish()` and returns the resource. That resolves the issue's ask in both cases, so correctness is effectively equal.

B adds an explicit nil check with a clearer message, and prefixes its errors with `resources.Publish:`. A would still fail gracefully on nil because the type assertion fails, but its error would read `<nil> can not be published`, which is less helpful. This is a minor craft gain for B.

Testing is where the patches differ meaningfully. A's single test only covers `minify | resources.Publish` and never exercises `js.Build`, the exact pipeline in the issue. B's tests cover:
- plain `resources.Get`, `minify`, `js.Build`, `resources.FromString` and a copied page-bundle resource, checking both the returned resource's properties and the published files on disk;
- the error path for passing a Page;
- the error path for a nil resource from a missing asset.

One caveat: B's assertion `p: p.txt|` for the copied bundle resource relies on naming behaviour I can't verify from the diff, so that test carries slight risk. B's docs are neutral scope that adds no risk.

Overall B is the stronger patch, mainly for test coverage of the issue's actual scenario and its edge cases, plus slightly better error handling.

The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.

Solve

54m 53s

Wall-clock time in the container

Full test suite

No new failures

16 passed · 1 skipped · 17 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

161vs 37 merged

Changed lines, ours vs merged

So you can write:

{{ resources.Get "main.js" | js.Build | resources.Publish }}

Instead of:

{{ (resources.Get "main.js" | js.Build ).Publish }}

The first one reads much nicer in my eyes.

resources.Publish would return the Resource it gets as input.

The two patches

Our patch+161 −0
docs/content/en/functions/resources/Publish.mdnew file mode 100644index 000000000..33880e7a7@@ -0,0 +1,38 @@+---+title: resources.Publish+description: Publishes the given resource and returns it.+categories: []+keywords: []+params:+  functions_and_methods:+    aliases: []+    returnType: resource.Resource+    signatures: [resources.Publish RESOURCE]+---++The `resources.Publish` function writes the given resource to the publish directory, typically `public`, and returns the resource. Use it at the end of a pipeline:++```go-html-template+{{ $js := resources.Get "js/main.js" | js.Build | resources.Publish }}+```++This publishes the resource in the same way as the [`Publish`] method:++```go-html-template+{{ (resources.Get "js/main.js" | js.Build).Publish }}+```++Unlike the method, which returns nothing, the function returns the resource. Printing the result directly, without assigning it to a variable, renders the resource's string representation in the page.++Because the function returns the resource, you can keep using it:++```go-html-template+{{ with resources.Get "css/main.css" | minify | resources.Publish }}+  {{ .Name }}+{{ end }}+```++> [!NOTE]+> Use the `resources.Publish` function with global, page, and remote resources. Passing a page, or a nil value (for example when `resources.Get` does not find the file), returns an error.++[`Publish`]: /methods/resource/publish/docs/content/en/methods/resource/Publish.mdindex 0ecdf7e74..6fbb28d89 100644@@ -30,3 +30,11 @@ Instead of this: ```go-html-template {{ $noop := $resource.Permalink }} ```++To publish a resource at the end of a pipeline, use the [`resources.Publish`] function, which returns the resource:++```go-html-template+{{ $js := resources.Get "js/main.js" | js.Build | resources.Publish }}+```++[`resources.Publish`]: /functions/resources/publish/tpl/resources/init.goindex 9ce9bee20..0cda68c4c 100644@@ -90,6 +90,11 @@ func init() { 			[][2]string{}, 		) +		ns.AddMethodMapping(ctx.Publish,+			nil,+			[][2]string{},+		)+ 		ns.AddMethodMapping(ctx.PostProcess, 			nil, 			[][2]string{},tpl/resources/resources.goindex aae6ac1f3..7d144c88a 100644@@ -296,6 +296,22 @@ func (ns *Namespace) Minify(r resources.ResourceTransformer) (resource.Resource, 	return ns.minifyClient.Minify(r) } +// Publish publishes r to the publish directory and returns it, so it can be+// used at the end of a pipeline, e.g. {{ resources.Get "main.js" | js.Build | resources.Publish }}.+func (ns *Namespace) Publish(r resource.Resource) (resource.Resource, error) {+	if r == nil {+		return nil, errors.New("resources.Publish: resource is nil")+	}+	src, ok := r.(resource.Source)+	if !ok {+		return nil, fmt.Errorf("resources.Publish: %T can not be published", r)+	}+	if err := src.Publish(); err != nil {+		return nil, err+	}+	return r, nil+}+ // PostProcess processes r after the build. // // Deprecated: Use templates.Defer instead.tpl/resources/resources_integration_test.goindex b7f86111c..18b3791de 100644@@ -296,3 +296,97 @@ body { color: red; }  	b.AssertLogContains("resources.PostProcess was deprecated in Hugo v0.164.0") }++func TestPublish(t *testing.T) {+	t.Parallel()++	files := `+-- hugo.toml --+disableKinds = ['rss','section','sitemap','taxonomy','term']+-- assets/a.txt --+I am a.txt+-- assets/css/main.css --+body {+	color: red;+}+-- assets/js/main.js --+var a = 1 + 2;+console.log(a);+-- content/mybundle/index.md --+---+title: My Bundle+---+-- content/mybundle/p.txt --+I am p.txt+-- layouts/home.html --+Home.+{{ $a := resources.Get "a.txt" | resources.Publish }}+a: {{ $a.Name }}|{{ $a.Content }}|+{{ $css := resources.Get "css/main.css" | minify | resources.Publish }}+css: {{ $css.Name }}|+{{ $js := resources.Get "js/main.js" | js.Build | resources.Publish }}+js: {{ $js.Name }}|+{{ $s := resources.FromString "s/s.txt" "I am s.txt" | resources.Publish }}+s: {{ $s.Name }}|+{{ with site.GetPage "mybundle" }}+  {{ $p := .Resources.Get "p.txt" | resources.Copy "copied/p.txt" | resources.Publish }}+  p: {{ $p.Name }}|+{{ end }}+-- layouts/single.html --+{{ .Title }}+`++	b := hugolib.Test(t, files)++	b.AssertFileContent("public/index.html",+		"a: /a.txt|I am a.txt|",+		"css: /css/main.css|",+		"js: /js/main.js|",+		"s: /s/s.txt|",+		"p: p.txt|",+	)++	b.AssertFileContent("public/a.txt", "I am a.txt")+	b.AssertFileContent("public/css/main.min.css", "body{color:red}")+	b.AssertFileContent("public/js/main.js", "console.log(")+	b.AssertFileContent("public/s/s.txt", "I am s.txt")+	b.AssertFileContent("public/copied/p.txt", "I am p.txt")+}++func TestPublishPageShouldFail(t *testing.T) {+	t.Parallel()++	files := `+-- hugo.toml --+disableKinds = ['rss','section','sitemap','taxonomy','term']+-- content/p1.md --+---+title: P1+---+-- layouts/home.html --+{{ with site.GetPage "p1" }}{{ $p := resources.Publish . }}{{ end }}+-- layouts/single.html --+{{ .Title }}+`++	b, err := hugolib.TestE(t, files)++	b.Assert(err, qt.IsNotNil)+	b.Assert(err.Error(), qt.Contains, "can not be published")+}++func TestPublishNilShouldFail(t *testing.T) {+	t.Parallel()++	files := `+-- hugo.toml --+disableKinds = ['page','rss','section','sitemap','taxonomy','term']+-- layouts/home.html --+{{ $r := resources.Get "doesnotexist.txt" | resources.Publish }}+`++	b, err := hugolib.TestE(t, files)++	b.Assert(err, qt.IsNotNil)+	b.Assert(err.Error(), qt.Contains, "resources.Publish: resource is nil")+} 
Merged fix · PR #15209+37 −0
tpl/resources/init.goindex 9ce9bee20..0cda68c4c 100644@@ -90,6 +90,11 @@ func init() { 			[][2]string{}, 		) +		ns.AddMethodMapping(ctx.Publish,+			nil,+			[][2]string{},+		)+ 		ns.AddMethodMapping(ctx.PostProcess, 			nil, 			[][2]string{},tpl/resources/resources.goindex aae6ac1f3..88722fb07 100644@@ -296,6 +296,18 @@ func (ns *Namespace) Minify(r resources.ResourceTransformer) (resource.Resource, 	return ns.minifyClient.Minify(r) } +// Publish publishes r to the destination and returns it.+func (ns *Namespace) Publish(r resource.Resource) (resource.Resource, error) {+	s, ok := r.(resource.Source)+	if !ok {+		return nil, fmt.Errorf("%T can not be published", r)+	}+	if err := s.Publish(); err != nil {+		return nil, err+	}+	return r, nil+}+ // PostProcess processes r after the build. // // Deprecated: Use templates.Defer instead.tpl/resources/resources_integration_test.goindex b7f86111c..b5c611d52 100644@@ -278,6 +278,26 @@ disableKinds = ['page','section','rss','sitemap','taxonomy','term'] 	b.AssertLogContains("! WARN  Dart Sass: hugo:vars") } +// See issue 15208.+func TestPublish(t *testing.T) {+	t.Parallel()++	files := `+-- hugo.toml --+disableKinds = ["taxonomy", "term", "section", "RSS", "sitemap", "robotsTXT", "404"]+-- assets/js/main.js --+let foo;+-- layouts/home.html --+{{ $r := resources.Get "js/main.js" | minify | resources.Publish }}+Name: {{ $r.Name }}|+`++	b := hugolib.Test(t, files)++	b.AssertFileContent("public/index.html", "Name: /js/main.js|")+	b.AssertFileExists("public/js/main.min.js", true)+}+ // See issue 15086. func TestPostProcessDeprecated(t *testing.T) { 	t.Parallel()