Backtestshugorun-20261009-153657#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 fix. Each adds a `Publish` method on the resources namespace, registers it in `init.go`, type-asserts to `resource.Source`, calls `Publish()` and returns the resource. Either one lets `resources.Get "main.js" | js.Build | resources.Publish` work, so on correctness of the main path they are about equal. The edge cases differ. Patch A takes `any` and checks the input explicitly: - A nil input (for example, `resources.Get` on a missing file) gets a clear "must provide a Resource object" error. - A non-resource value such as a string gets "string is not a Resource". - A resource that cannot be published gets "can not be published". Patch B takes `resource.Resource` directly. A nil input therefore surfaces as "<nil> can not be published", and a string fails during template argument conversion with a less specific message. Both behave safely, but A's errors are clearer for the cases users are likely to hit. On tests, Patch A is much more thorough: - It covers a plain resource, a minified transformation, and an image resize. - It checks that a resource not passed through Publish is not written (`c.txt` absent), which shows publishing is actually triggered by the function. - It adds table-driven failure tests for nil, page, and string inputs. Patch B has a single happy-path test with minify. That test would fail before the change and is valid, but it is minimal. On scope, Patch A also adds user docs and a `docs.yaml` entry. This is neutral to slightly positive and adds no risk. Overall, Patch A wins on tests and on handling the edge cases.
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
14m 57s
Wall-clock time in the container
Full test suite
No new failures
18 passed · 1 skipped · 19 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
117vs 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+117 −0
docs/content/en/functions/resources/Publish.mdnew file mode 100644index 000000000..3a47b9b7a@@ -0,0 +1,23 @@+---+title: resources.Publish+description: Publishes the given resource to the target path and returns it.+categories: []+keywords: []+params:+ functions_and_methods:+ aliases: []+ returnType: resource.Resource+ signatures: [resources.Publish RESOURCE]+---++Hugo publishes a resource to the `publishDir` when you call its `RelPermalink` or `Permalink` method. Use the `resources.Publish` function to publish a resource without calling those methods, typically at the end of a resource pipeline. This is equivalent to calling the resource's `Publish` method, but returns the resource so you can keep using it:++```go-html-template+{{ $js := resources.Get "js/main.js" | js.Build | resources.Publish }}+{{ $js.Name }}+```++Because the function returns the resource, using it in a template action without assigning the result renders the resource's string representation. Assign the result to a variable as shown above.++> [!NOTE]+> Use the `resources.Publish` function with global, page, and remote resources. Passing a page or a nil value throws an error.docs/data/docs.yamlindex 19b950dd5..3882246c1 100644@@ -4038,6 +4038,14 @@ 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 and returns it,+ so it can be used at the end of a resource pipeline.+ 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..336110206 100644@@ -296,6 +296,26 @@ func (ns *Namespace) Minify(r resources.ResourceTransformer) (resource.Resource, return ns.minifyClient.Minify(r) } +// Publish publishes the given Resource to the target path and returns it,+// so it can be used at the end of a resource pipeline.+func (ns *Namespace) Publish(r any) (resource.Resource, error) {+ if r == nil {+ return nil, errors.New("must provide a Resource object")+ }+ res, ok := r.(resource.Resource)+ if !ok {+ return nil, fmt.Errorf("%T is not a Resource", r)+ }+ src, ok := res.(resource.Source)+ if !ok {+ return nil, fmt.Errorf("%T can not be published", r)+ }+ if err := src.Publish(); err != nil {+ return nil, err+ }+ return res, nil+}+ // PostProcess processes r after the build. // // Deprecated: Use templates.Defer instead.tpl/resources/resources_integration_test.goindex b7f86111c..3d1808e6a 100644@@ -296,3 +296,64 @@ 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.css --+body { color: red; }+-- assets/c.txt --+I am c.txt+-- assets/images/pixel.png --+iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==+-- layouts/home.html --+{{ $a := resources.Get "a.txt" | resources.Publish }}+a: {{ $a.Name }}|+{{ $b := resources.Get "b.css" | minify | resources.Publish }}+b: {{ $b.Name }}|+{{ $img := (resources.Get "images/pixel.png").Resize "2x2" | resources.Publish }}+img: {{ $img.Width }}x{{ $img.Height }}|+{{ $c := resources.Get "c.txt" }}+c: {{ $c.Name }}|+`++ b := hugolib.Test(t, files)++ b.AssertFileContent("public/index.html", "a: /a.txt|", "b: /b.css|", "img: 2x2|", "c: /c.txt|")+ b.AssertFileContent("public/a.txt", "I am a.txt")+ b.AssertFileContent("public/b.min.css", "body{color:red}")+ b.AssertFileExists("public/c.txt", false)+ b.AssertPublishDir("images/pixel_hu_")+}++func TestPublishShouldFail(t *testing.T) {+ t.Parallel()++ for _, tc := range []struct {+ name string+ template string+ expect string+ }{+ {"nil", `{{ resources.Get "notfound.txt" | resources.Publish }}`, "must provide a Resource object"},+ {"page", `{{ . | resources.Publish }}`, "can not be published"},+ {"string", `{{ "foo" | resources.Publish }}`, "string is not a Resource"},+ } {+ t.Run(tc.name, func(t *testing.T) {+ t.Parallel()+ files := `+-- hugo.toml --+disableKinds = ['page','rss','section','sitemap','taxonomy','term']+-- layouts/home.html --+` + tc.template++ b, err := hugolib.TestE(t, files)+ b.Assert(err, qt.IsNotNil)+ b.Assert(err.Error(), qt.Contains, tc.expect)+ })+ }+}
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()