Backtestshugorun-20261007-020926#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 `Namespace.Publish(r resource.Resource)`, which type-asserts to `resource.Source`, calls `Publish()` and returns `r`. Both register the method mapping in `init.go`. So correctness is essentially equal and both resolve the issue's ask, letting `{{ ... | js.Build | resources.Publish }}` work.
The difference is mainly in testing. Patch B's single test covers one minified JS resource and checks that the file is written. Patch A's test covers a much wider range of resource kinds:
- a transformed JS resource
- a plain global CSS resource
- `resources.FromString`
- an image
- a resized image
- a page bundle resource when `publishResources` is false, with a negative check that a sibling resource is not published
Patch A also adds a failure-path test confirming that passing a non-resource produces a sensible error. These tests would fail before the change, because the function is missing from the namespace, and they exercise more real-world scenarios.
On craft, Patch A adds an explicit nil check and prefixes its error messages with `resources.Publish:`, which makes template errors clearer. Its doc comment shows the intended pipeline usage. The documentation page is extra scope but harmless and appropriate for a new template function.
One small risk is whether `errors` is already imported in `resources.go`; neither diff shows import changes, so I treat this as neutral. Overall Patch A is the stronger, better-tested fix.
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
23m 18s
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..fb66e3c40@@ -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]+---++The `resources.Publish` function publishes the given resource without rendering its URL, and returns the resource. This is useful at the end of a pipeline:++```go-html-template+{{ resources.Get "main.js" | js.Build | resources.Publish }}+```++The example above is equivalent to:++```go-html-template+{{ (resources.Get "main.js" | js.Build).Publish }}+```++> [!NOTE]+> Use the `resources.Publish` function with global, page, and remote resources.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..3e408d0eb 100644@@ -296,6 +296,23 @@ func (ns *Namespace) Minify(r resources.ResourceTransformer) (resource.Resource, return ns.minifyClient.Minify(r) } +// Publish publishes r to the public directory and returns it, so it can be+// used at the end of 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("resources.Publish: resource is nil")+ }+ src, ok := r.(resource.Source)+ if !ok {+ return nil, fmt.Errorf("resources.Publish: %T cannot 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..9ce5a83e2 100644@@ -296,3 +296,67 @@ 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 = ["taxonomy", "term", "section", "RSS", "sitemap", "robotsTXT", "404"]+-- assets/js/main.js --+let foo = 1;+-- assets/css/main.css --+body { color: red; }+-- assets/images/pixel.png --+iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==+-- content/p1/index.md --+---+title: p1+build:+ publishResources: false+---+-- content/p1/data.txt --+data+-- content/p1/other.txt --+other+-- layouts/home.html --+{{ $js := resources.Get "js/main.js" | minify | resources.Publish }}+{{ $css := resources.Get "css/main.css" | resources.Publish }}+{{ "let bar;" | resources.FromString "js/fromstring.js" | resources.Publish }}+{{ resources.Get "images/pixel.png" | resources.Publish }}+{{ (resources.Get "images/pixel.png").Resize "2x2" | resources.Publish }}+{{ (site.GetPage "/p1").Resources.Get "data.txt" | resources.Publish }}+JS: {{ $js.Name }}|{{ $js.MediaType }}|+CSS: {{ $css.Name }}|+-- layouts/page.html --+{{ .Title }}+`++ b := hugolib.Test(t, files)++ // No permalink is used for any of the resources above, so they are+ // published by resources.Publish alone.+ b.AssertFileContent("public/js/main.min.js", "let foo=1")+ b.AssertFileContent("public/css/main.css", "body { color: red; }")+ b.AssertFileContent("public/js/fromstring.js", "let bar;")+ b.AssertFileExists("public/images/pixel.png", true)+ b.AssertFileExists("public/p1/data.txt", true)+ b.AssertFileExists("public/p1/other.txt", false)+ b.AssertFileContent("public/index.html", "JS: /js/main.js|text/javascript|", "CSS: /css/main.css|")+ b.AssertPublishDir("pixel_hu_")+}++func TestPublishNotAResourceShouldFail(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+-- layouts/home.html --+{{ . | resources.Publish }}+`++ _, err := hugolib.TestE(t, files)++ qt.Assert(t, err, qt.IsNotNil)+ qt.Assert(t, err.Error(), qt.Contains, "cannot be published")+}
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()