Backtestshugorun-20261024-162305#15208
Add resources.Publish
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
The production code in the two patches is effectively identical: both register `Publish` on the resources namespace via AddMethodMapping and implement it by asserting the input to resource.Source, calling its Publish method, and returning the original resource (with a typed error for non-publishable inputs). Both satisfy the issue's ask, and all four independent tests pass on both. The differences are in tests, docs, and craft. Patch A's integration test covers more of the realistic surface: a plain asset, a minified (transformed) asset whose published name differs from its Name, a resources.Copy'd resource landing at the new target path, and a page resource in a bundle with publishResources disabled, plus a negative assertion that the untransformed copy source was not published. Patch B tests only the single minify pipeline case. Patch A also adds the user-facing docs page and the docs.yaml entry, which matches how the surrounding tpl functions are documented in this repository and is part of a complete feature addition; B adds none. A's doc comment is more descriptive and mirrors the issue's example. Neither patch adds risky scope. On tests and craft, A is the stronger patch.
Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.
Solve
19m 5s
Wall-clock time in the container
Full test suite
No new failures
15 passed · 0 skipped · 15 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
94vs 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..274d0fb42@@ -0,0 +1,25 @@+---+title: resources.Publish+description: Publishes the given resource to the publish directory and returns it.+categories: []+params:+ functions_and_methods:+ aliases: []+ returnType: resource.Resource+ signatures: [resources.Publish RESOURCE]+---++Use this function to publish a resource without rendering its `RelPermalink` or `Permalink`. It returns the given resource, so it can be used at the end of a pipeline:++```go-html-template+{{ resources.Get "main.js" | js.Build | resources.Publish }}+```++This is equivalent to calling the `Publish` method on the resource:++```go-html-template+{{ (resources.Get "main.js" | js.Build).Publish }}+```++> [!NOTE]+> Use the `resources.Publish` function with global, page, and remote resources.docs/data/docs.yamlindex 19b950dd5..e9c07640a 100644@@ -4038,6 +4038,15 @@ tpl: - r Description: PostProcess processes r after the build. Examples: []+ Publish:+ Aliases: null+ Args:+ - r+ Description: |-+ Publish publishes the given Resource to the target path (see RelPermalink)+ and returns it, so it can be used in a pipeline, e.g.+ {{ resources.Get "main.js" | js.Build | resources.Publish }}.+ Examples: [] safe: CSS: Aliases: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..fe34a37ac 100644@@ -296,6 +296,20 @@ func (ns *Namespace) Minify(r resources.ResourceTransformer) (resource.Resource, return ns.minifyClient.Minify(r) } +// Publish publishes the given Resource to the target path (see RelPermalink)+// and returns it, so it can be used in a pipeline, e.g.+// {{ resources.Get "main.js" | js.Build | resources.Publish }}.+func (ns *Namespace) Publish(r resource.Resource) (resource.Resource, error) {+ 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..09b2a42d3 100644@@ -296,3 +296,44 @@ 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 = ['rss','section','sitemap','taxonomy','term']+-- assets/a.txt --+I am a.txt+-- assets/b.css --+body { color: red; }+-- assets/c.txt --+I am c.txt+-- content/p1/index.md --+---+title: p1+build:+ publishResources: false+---+-- content/p1/d.txt --+I am d.txt+-- layouts/home.html --+Home.+{{ $a := resources.Get "a.txt" | resources.Publish }}+a: {{ $a.Name }}|+{{ with resources.Get "b.css" | minify | resources.Publish }}b: {{ .Name }}|{{ end }}+{{ with resources.Get "c.txt" | resources.Copy "copied/c.txt" | resources.Publish }}c: {{ .Name }}|{{ end }}+{{ with site.GetPage "p1" }}{{ with .Resources.Get "d.txt" | resources.Publish }}d: {{ .Name }}|{{ end }}{{ end }}+-- layouts/page.html --+{{ .Title }}+`++ b := hugolib.Test(t, files)++ b.AssertFileContent("public/index.html", "a: /a.txt|", "b: /b.css|", "c: /c.txt|", "d: d.txt|")+ b.AssertFileContent("public/a.txt", "I am a.txt")+ b.AssertFileContent("public/b.min.css", "body{color:red}")+ b.AssertFileContent("public/copied/c.txt", "I am c.txt")+ b.AssertFileContent("public/p1/d.txt", "I am d.txt")+ b.AssertFileExists("public/c.txt", false)+}
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()