Backtestshugorun-20260930-140149#15208
Add resources.Publish
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Both patches implement `Namespace.Publish` the same way. Each type-asserts to `resource.Source`, calls `Publish()`, returns the resource, and registers the method mapping in init.go. On correctness they are equivalent. The difference is in testing. Patch A's test only checks a minify pipeline, asserting the name and that `main.min.js` exists. Patch B's test covers more: a plain `resources.Get` asset, a `resources.FromString` resource, a minified resource (asserting `other.min.js` is written and `other.js` is not), and the exact `js.Build | resources.Publish` scenario from the issue. It also includes a control resource that is fetched but never published and asserts it is absent from `public/`. That control shows publishing comes from the new function and not from incidental access, so the test would clearly fail without the change. Patch B also adds a documentation page for the new template function, consistent with how Hugo documents functions. This adds no risk. Since the implementations are equal, Patch B wins on the second criterion, tests, with docs as a minor plus.
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
11m 22s16 turns
Wall-clock time in the container
Full test suite
No new failures
3,698 passed · 99 skipped · 3,806 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
88vs 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
docs/content/en/functions/resources/Publish.mdnew file mode 100644index 000000000..0a093d6c8@@ -0,0 +1,17 @@+---+title: resources.Publish+description: Publishes the given resource to the publish directory and returns it.+categories: []+keywords: []+params:+ functions_and_methods:+ aliases: []+ returnType: resource.Resource+ signatures: [resources.Publish RESOURCE]+---++```go-html-template+{{ resources.Get "js/main.js" | js.Build | resources.Publish }}+```++The resources.Publish function publishes the given resource to the publish directory without needing to call its `RelPermalink`, `Permalink`, or `Publish` method. It returns the resource it receives, so it can be used at the end of a pipeline.tpl/resources/init.goindex 9ce9bee20..087f3c5de 100644@@ -95,6 +95,11 @@ func init() { [][2]string{}, ) + ns.AddMethodMapping(ctx.Publish,+ nil,+ [][2]string{},+ )+ return ns } tpl/resources/resources.goindex aae6ac1f3..3ad63c175 100644@@ -296,6 +296,18 @@ func (ns *Namespace) Minify(r resources.ResourceTransformer) (resource.Resource, return ns.minifyClient.Minify(r) } +// Publish publishes r to the target path(s) and returns r.+func (ns *Namespace) Publish(r resource.Resource) (resource.Resource, error) {+ p, ok := r.(resource.Source)+ if !ok {+ return nil, fmt.Errorf("resource of type %T cannot be published", r)+ }+ if err := p.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..f2ddb9027 100644@@ -74,6 +74,60 @@ Copy3: /blog/js/copies/moo.a677329fc6c4ad947e0c7116d91f37a2.min.js|text/javascri b.AssertFileExists("public/images/copy3.png", false) } +func TestPublish(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+baseURL = "http://example.com/blog"+-- assets/js/main.js --+let foo = 1;+-- assets/js/other.js --+let bar = 2;+-- assets/js/notpublished.js --+let baz = 3;+-- assets/js/built.js --+console.log("built");+-- layouts/home.html --+{{/* Plain asset, published without using its permalink. */}}+{{ $main := resources.Get "js/main.js" | resources.Publish }}+Main: {{ $main.Name }}|{{ $main.MediaType }}|{{ $main.Content | safeJS }}|++{{/* Resource created from a string. */}}+{{ $fromString := "let s = 4;" | resources.FromString "js/fromstring.js" | resources.Publish }}+FromString: {{ $fromString.Name }}|{{ $fromString.MediaType }}|++{{/* Transformed resource. */}}+{{ $min := resources.Get "js/other.js" | minify | resources.Publish }}+Min: {{ $min.Name }}|{{ $min.MediaType }}|{{ $min.Content | safeJS }}|++{{/* The scenario from the issue. */}}+{{ $built := resources.Get "js/built.js" | js.Build | resources.Publish }}+Built: {{ $built.MediaType }}|++{{/* Control: fetched but neither published nor permalinked. */}}+{{ $np := resources.Get "js/notpublished.js" }}+NotPublished: {{ $np.Name }}|+`++ b := hugolib.Test(t, files)++ b.AssertFileContent("public/index.html",+ "Main: /js/main.js|text/javascript|let foo = 1;|",+ "FromString: /js/fromstring.js|text/javascript|",+ "Min: /js/other.js|text/javascript|let bar=2|",+ "NotPublished: /js/notpublished.js|",+ "Built: text/javascript|",+ )++ b.AssertFileContent("public/js/main.js", "let foo = 1;")+ b.AssertFileContent("public/js/fromstring.js", "let s = 4;")+ b.AssertFileContent("public/js/other.min.js", "let bar=2")+ b.AssertFileExists("public/js/other.js", false)+ b.AssertFileExists("public/js/notpublished.js", false)+ b.AssertFileContent("public/js/built.js", `console.log("built");`)+}+ func TestCopyPageShouldFail(t *testing.T) { t.Parallel()
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()
What the agent reported
I added resources.Publish, so {{ resources.Get "main.js" | js.Build | resources.Publish }} now works and returns the same resource it was given. The new test passes, but ./bt-test could not build the package in this container: it tries to download Go modules and the container has no DNS (lookup proxy.golang.org ... server misbehaving, "setup failed"). All results below come from go test run against the offline module cache at /bt/cache/go-mod instead.
What was wrong: the resources template namespace had no Publish function. The only way to publish a resource was to call its .Publish method, which can't go at the end of a pipe.
What I changed:
tpl/resources/resources.go: newNamespace.Publish(r resource.Resource) (resource.Resource, error). It calls the resource's ownPublish()and returns the resource unchanged. It works for plain assets, images and transformed resources (js.Build,minify, and so on). Anything that can't be published, including nil (for example whenresources.Getfinds nothing), gets an error saying so.tpl/resources/init.go: registersctx.Publish, with no alias, so a barepublishisn't confused with the method.docs/content/en/functions/resources/Publish.md: a new docs page in the same format asMinify.md. I did not editdocs/data/docs.yaml, because it is generated from the Go source byhugo gen docshelper.tpl/resources/resources_integration_test.go: newTestPublish. Each case publishes a resource without ever using its permalink, then checks both the rendered output and which files land inpublic/:- the exact case from the report,
resources.Get "js/built.js" | js.Build | resources.Publish, which should writepublic/js/built.js; - a plain asset;
- a
resources.FromStringresource; - a
minifypipeline, which should writeother.min.jsand notother.js; - a resource that is fetched but never published, which must not appear in
public/.
Test evidence:
- Before the change:
TestPublishfails withcan't evaluate field Publish in type interface {}. I checked this by stashing the two production files and rerunning. - After the change:
TestPublishpasses, and the whole./tpl/resources/package passes, includingTestCopyandTestCopyPageShouldFail../tpl/internal/...passes, andgo vetandgofmtare clean. ./tpl/tplimpl/: onlyTestVimeoShortcodefails, and it fails the same way with and without my change, so it is unrelated.- Test fix: the test-writer's first draft expected resource names without a leading slash. Hugo renders them as
/js/main.js, so I corrected those strings in the new test before it passed. No existing test was changed.
Requirements:
- Pipeable
resources.Publish: done, and tested with the report's exactjs.Buildpipeline. - Returns the input resource: done. The test checks the returned value's Name, MediaType and Content.
- It actually publishes, with no
.RelPermalinkor.Publishcall: done. The test checks the files inpublic/and confirms an unpublished resource is not written.
A reviewer checked the final diff against the report and found nothing blocking and no unrelated changes. AGENTS.md and CLAUDE.md were already deleted before I started, and I left them alone. Everything is uncommitted in the working tree.