Backtestshugorun-20261002-200313#15208
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 have the same core fix. Each adds a `Namespace.Publish` method that type-asserts to `resource.Source`, calls `Publish()` and returns the original resource. Each registers it through `AddMethodMapping`. So both resolve the issue equally at the root, and `{{ ... | resources.Publish }}` works in both.
B is slightly more robust: it adds an explicit nil check, matching the "must provide a Resource object" convention used elsewhere in that namespace. Its doc comment explains the pipe use case.
On tests, A covers only a single happy path (minify, then publish, file exists). B tests several resource kinds (plain text, a minified transformation, an image, a copied resource, and an unpublished control) and checks the published file content. It also checks that a copied resource is published at its new path and not the original, and that unpublished resources are not written. B adds a negative test showing a non-Source (a Page) fails with the expected error.
B's test has some risk: the copied-resource `Name` assertion and the base64 PNG fixture depend on test-builder behavior. Even so, its coverage is much broader and more meaningful.
B also adds user-facing docs for the new function. This is neutral to slightly positive and adds no risk.
Overall the correctness is equivalent, and B wins on tests and edge-case 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
49m 19s
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
111vs 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+111 −0
docs/content/en/functions/resources/Publish.mdnew file mode 100644index 000000000..e7f58503b@@ -0,0 +1,26 @@+---+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 unchanged. This makes it possible to publish a resource at the end of a pipeline:++```go-html-template+{{ $js := resources.Get "main.js" | js.Build | resources.Publish }}+```++This is equivalent to calling the [`Publish`] method on the resource:++```go-html-template+{{ $js := resources.Get "main.js" | js.Build }}+{{ $js.Publish }}+```++[`Publish`]: /methods/resource/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..fd9e16f0a 100644@@ -296,6 +296,23 @@ func (ns *Namespace) Minify(r resources.ResourceTransformer) (resource.Resource, return ns.minifyClient.Minify(r) } +// Publish publishes the given Resource to the target path (if not already+// published) and returns it unchanged, allowing it to be used in a pipe, 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("must provide a Resource object")+ }+ 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..6148447df 100644@@ -296,3 +296,66 @@ 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 = ['page','rss','section','sitemap','taxonomy','term']+-- assets/a.txt --+I am a.txt+-- assets/b.txt --+I am b.txt+-- assets/c.txt --+I am c.txt+-- assets/js/main.js --+var a = 1;+-- assets/images/pixel.png --+iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==+-- layouts/home.html --+Home.+{{ $a := resources.Get "a.txt" | resources.Publish }}+a: {{ $a.Name }}|{{ $a.Content }}|+{{ $js := resources.Get "js/main.js" | minify | resources.Publish }}+js: {{ $js.Name }}|+{{ $img := resources.Get "images/pixel.png" | resources.Publish }}+img: {{ $img.Name }}|{{ $img.Width }}|+{{ $copy := resources.Get "b.txt" | resources.Copy "copied/b.txt" | resources.Publish }}+copy: {{ $copy.Name }}|+{{ $notPublished := resources.Get "c.txt" }}+c: {{ $notPublished.Name }}|+`++ b := hugolib.Test(t, files)++ b.AssertFileContent("public/index.html",+ "a: /a.txt|I am a.txt",+ "js: /js/main.js|",+ "img: /images/pixel.png|1|",+ "copy: /b.txt|",+ "c: /c.txt|",+ )+ b.AssertFileExists("public/a.txt", true)+ b.AssertFileContent("public/a.txt", "I am a.txt")+ b.AssertFileContent("public/js/main.min.js", "var a=1")+ b.AssertFileExists("public/images/pixel.png", true)+ b.AssertFileExists("public/copied/b.txt", true)+ b.AssertFileExists("public/b.txt", false)+ b.AssertFileExists("public/c.txt", false)+}++func TestPublishPageShouldFail(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+disableKinds = ['page','rss','section','sitemap','taxonomy','term']+-- layouts/home.html --+{{ $p := . | 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()