Backtestshugorun-20261005-133023#15208
Add resources.Publish
Better
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches implement the same core change: a `resources.Publish` template function, registered via AddMethodMapping, that type-asserts to `resource.Source`, calls `Publish()`, and returns the resource. Both resolve the issue's ask equally well. The differences are in testing and edge handling. Patch A's tests cover more: (1) a plain `resources.Get` resource piped straight to `resources.Publish`, (2) a transformed (minified) resource, and (3) a non-publishable input (`site.Home`), asserting the error message. Its templates access only `.Name`, not `.RelPermalink`, so the assertions that `public/a.txt` and `public/css/main.min.css` exist genuinely depend on the new function; they would fail without it. Patch B has a single test with a minified JS resource. It also uses only `.Name`, so it is a valid test, but it does not cover the error path or an untransformed resource. Patch A adds an explicit nil check with a clearer message. In Go a nil interface would already fail the type assertion, so this is only a marginal craft improvement. Patch A also adds a documentation page, which is neutral but consistent with how Hugo documents template functions. Given equal correctness, Patch A wins on more thorough tests, including the failure case.
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
57m 21s
Wall-clock time in the container
Full test suite
No new failures
3,699 passed · 99 skipped · 3,807 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
80vs 37 merged
Changed lines, ours vs merged
The issue
View on GitHubSo 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+80 −0
docs/content/en/functions/resources/Publish.mdnew file mode 100644index 000000000..7733f215e@@ -0,0 +1,17 @@+---+title: resources.Publish+description: Publishes the given resource to the public directory and returns it.+categories: []+keywords: []+params:+ functions_and_methods:+ aliases: []+ returnType: resource.Resource+ signatures: [resources.Publish RESOURCE]+---++```go-html-template+{{ $js := resources.Get "main.js" | js.Build | resources.Publish }}+```++This is equivalent to calling the `Publish` method on the resource, but returns the resource so that it can be used at the end of a pipeline.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..e48842ed9 100644@@ -296,6 +296,24 @@ func (ns *Namespace) Minify(r resources.ResourceTransformer) (resource.Resource, return ns.minifyClient.Minify(r) } +// Publish publishes r to the public directory and returns it, which makes it+// possible to use it in 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("resource is nil")+ }+ src, ok := r.(resource.Source)+ if !ok {+ return nil, fmt.Errorf("%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..c4daf6ac2 100644@@ -296,3 +296,43 @@ body { color: red; } b.AssertLogContains("resources.PostProcess was deprecated in Hugo v0.164.0") }++func TestResourcesPublish(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+disableKinds = ['page','rss','section','sitemap','taxonomy','term']+-- assets/a.txt --+I am a.txt+-- assets/css/main.css --+body {+ color: red;+}+-- layouts/home.html --+Home.+{{ with resources.Get "a.txt" | resources.Publish }}a: {{ .Name }}|{{ end }}+{{ with resources.Get "css/main.css" | minify | resources.Publish }}css: {{ .Name }}|{{ end }}+`++ b := hugolib.Test(t, files)++ b.AssertFileContent("public/index.html", "a: /a.txt|", "css: /css/main.css|")+ b.AssertFileContent("public/a.txt", "I am a.txt")+ b.AssertFileContent("public/css/main.min.css", "body{color:red}")+}++func TestResourcesPublishNotPublishable(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+disableKinds = ['page','rss','section','sitemap','taxonomy','term']+-- layouts/home.html --+{{ $m := site.Home | resources.Publish }}+`++ b, err := hugolib.TestE(t, files)+ b.Assert(err, qt.IsNotNil)+ b.Assert(err.Error(), qt.Contains, "can not be published")+}
Merged fix · PR #+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()