Backtestshugorun-20260917-015623#15189
Issue with chained resource transformations
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches produce correct content for the reported scenario, but they take different structural approaches and A is the cleaner root-cause fix. The bug is that once a chain runs, the inner `target` is replaced by the transformed resource, yet TransformWithContext still appends the new transform to the FULL chain (`append(r.transformations, t...)`), so the already-applied transforms (e.g. css.Build) get re-applied on top of an already-transformed target — exactly the 'wrongly including the full chain' the issue describes. Patch A recognizes the transformed state (atomic `transformed` flag set in doInit) and, when set, starts a fresh chain of ONLY the new transforms against the already-transformed target: [css.Build] is dropped, [fingerprint] is applied once to the built CSS. This is the conceptually correct model (the target already embodies the prior chain) and it avoids re-executing the prior transforms and re-publishing their side effects — a real concern here since the trigger (`.Data.Artifacts`) already published artifacts. Patch B instead keeps the full [t1,t2] chain but redirects `target` back to a newly-tracked original `sourceTarget`, re-running the entire chain (including css.Build) from scratch. That yields correct content but re-does expensive work and risks duplicate publishing of the artifacts that were already emitted; it also adds a latent hazard: cloneTo sets `sourceTarget = newtTarget` from `r.target`, which if cloneTo runs after a transform captures a transformed target as the supposed 'source'. On tests, A's unit test is at the exact transformation layer, fails-before (would yield 'start-t1-t1-t2') / passes-after, and crucially asserts `assertNoDuplicateWrites`, guarding the side-effect concern; B's integration test faithfully reproduces the issue with SHA assertions and also fails-before/passes-after, but does not guard against duplicate writes. Scope and craft are comparable in size, but A uses atomic access (appropriate given Hugo's concurrent rendering) and clear issue-referencing comments, while B's cloneTo mutation and re-run semantics are riskier. A resolves the issue at the root with less wasted work and a safer side-effect profile.
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
21m 7s20 turns
Wall-clock time in the container
Full test suite
No new failures
3,691 passed · 99 skipped · 3,803 total
Contamination audit
Unknown
See the audit notes for this run
Patch size
61vs 53 merged
Changed lines, ours vs merged
The issue
View on GitHubSee the fix in #15188.
This has been an issue since we added this feature, but we haven't noticed it (or gotten reports about it) because the common setup is to set up the entire chain before starting to use it:
{{- with . | css.Build $opts }}
{{- if hugo.IsDevelopment }}
<link rel="stylesheet" href="{{ .RelPermalink }}">
{{- else }}
{{- with . | fingerprint }}
<link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}">
{{- end }}
{{- end }}
{{- end }}
In the above:
.RelPermalinktriggers the transformation of the entire chain in a streaming way.
With the recently added .Data.Artifacts in css.Build I discovered this issue by accident when doing something ala:
{{- with . | css.Build $opts }}
{{ range .Data.Artifacts }}
{{ if eq .MediaType.MainType "font" }}
<link rel="preload" href="{{ .RelPermalink }}" as="font" type="{{ .MediaType.Type }}">
{{ end }}
{{ end }}
{{- if hugo.IsDevelopment }}
<link rel="stylesheet" href="{{ .RelPermalink }}">
{{- else }}
{{- with . | fingerprint }}
<link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}">
{{- end }}
{{- end }}
{{- end }}
In the above:
.Datatriggers thecss.Buildtransformation.- Then
fingerprintstarts a new transformation with correct.Content, but wrongly including the full chain, includingcss.Build
The above could be rewritten to the below and it would work as expected:
{{- with . | css.Build $opts }}
{{- if hugo.IsDevelopment }}
<link rel="stylesheet" href="{{ .RelPermalink }}">
{{- else }}
{{- with . | fingerprint }}
{{ range .Data.Artifacts }}
{{ if eq .MediaType.MainType "font" }}
<link rel="preload" href="{{ .RelPermalink }}" as="font" type="{{ .MediaType.Type }}">
{{ end }}
{{ end }}
<link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}">
{{- end }}
{{- end }}
{{- end }}
{{- end }}The two patches
resources/transform.goindex 18c768b36..7d9dc94eb 100644@@ -22,6 +22,7 @@ import ( "path" "strings" "sync"+ "sync/atomic" "github.com/gohugoio/hugo/common/constants" "github.com/gohugoio/hugo/common/hashing"@@ -370,8 +371,19 @@ func (r resourceAdapter) Transform(t ...ResourceTransformation) (ResourceTransfo } func (r resourceAdapter) TransformWithContext(ctx context.Context, t ...ResourceTransformation) (ResourceTransformer, error) {+ var transformations []ResourceTransformation+ if r.transformed.Load() {+ // The existing transformations have already been applied to the+ // target; start a new chain from the transformed target with only+ // the newly supplied transformations. Otherwise the entire chain+ // would be re-applied. See issue #15189.+ transformations = t+ } else {+ transformations = append(r.transformations, t...)+ }+ r.resourceTransformations = &resourceTransformations{- transformations: append(r.transformations, t...),+ transformations: transformations, } r.resourceAdapterInner = &resourceAdapterInner{@@ -728,6 +740,7 @@ func (r *resourceAdapter) doInit(publish, setContent bool) error { } r.transformationsErr = r.getOrTransform(publish, setContent)+ r.transformed.Store(true) }) if publish && r.publishOnce != nil {@@ -764,6 +777,10 @@ type resourceTransformations struct { transformationsInit sync.Once transformationsErr error transformations []ResourceTransformation++ // transformed reports whether the transformations in this chain have+ // already been applied to the target.+ transformed atomic.Bool } // hasTransformationPermalinkHash reports whether any of the transformationsresources/transform_test.goindex feae43e41..53d3c0f16 100644@@ -329,6 +329,48 @@ func TestTransform(t *testing.T) { assertNoDuplicateWrites(c, spec) }) + c.Run("Content read then chained transform", func(c *qt.C) {+ c.Parallel()++ spec := newTestResourceSpec(specDescriptor{c: c})++ createContentAppender := func(name, suffix string) resources.ResourceTransformation {+ return &testTransformation{+ name: name,+ transform: func(ctx *resources.ResourceTransformationCtx) error {+ in := helpers.ReaderToString(ctx.From)+ fmt.Fprint(ctx.To, in+suffix)+ return nil+ },+ }+ }++ t1 := createContentAppender("t1", "-t1")+ t2 := createContentAppender("t2", "-t2")++ r := createTransformer(c, spec, "f1.txt", "start")++ tr1, err := r.Transform(t1)+ c.Assert(err, qt.IsNil)++ // Trigger the first transformation (as .Content or .Data would) before+ // chaining the next transformation onto it.+ content1, err := tr1.(resource.ContentProvider).Content(context.Background())+ c.Assert(err, qt.IsNil)+ c.Assert(content1, qt.Equals, "start-t1")++ tr2, err := tr1.Transform(t2)+ c.Assert(err, qt.IsNil)++ content2, err := tr2.(resource.ContentProvider).Content(context.Background())+ c.Assert(err, qt.IsNil)+ // t1 must NOT be applied a second time: the new transformation applies+ // only to the already-transformed content.+ c.Assert(content2, qt.Equals, "start-t1-t2")++ assertNoDuplicateWrites(c, spec)+ })+ c.Run("Content many", func(c *qt.C) { c.Parallel()
resources/transform.goindex 18c768b36..cee11f8ba 100644@@ -82,6 +82,7 @@ func newResourceAdapter(spec *Spec, lazyPublish bool, target transformableResour return &resourceAdapter{ resourceTransformations: &resourceTransformations{}, metaProvider: target,+ sourceTarget: target, resourceAdapterInner: &resourceAdapterInner{ ctx: context.Background(), spec: spec,@@ -186,6 +187,12 @@ type resourceAdapter struct { commonResource *resourceTransformations *resourceAdapterInner++ // The original untransformed target. The inner target is replaced with the+ // transformed resource once the transformation chain has run, so any new+ // transformations appended to the chain must start from this.+ sourceTarget transformableResource+ metaProvider resource.ResourceMetaProvider } @@ -230,6 +237,7 @@ func (r *resourceAdapter) GetDependencyManager() identity.Manager { func (r resourceAdapter) cloneTo(targetPath string) resource.Resource { newtTarget := r.target.cloneTo(targetPath)+ r.sourceTarget = newtTarget.(transformableResource) newInner := &resourceAdapterInner{ ctx: r.ctx, spec: r.spec,@@ -379,7 +387,7 @@ func (r resourceAdapter) TransformWithContext(ctx context.Context, t ...Resource spec: r.spec, Staler: r.Staler, publishOnce: &publishOnce{},- target: r.target,+ target: r.sourceTarget, } return &r, niltpl/css/build_integration_test.goindex 6ddb23331..e72a1bb03 100644@@ -537,6 +537,49 @@ ARTIFACT: {{ .RelPermalink }}|{{ .Permalink }}|{{ .MediaType.Type }} b.AssertFileExists("public/css/main.css.map", true) } +func TestCSSBuildDataArtifactsAndThenFingerprint(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+baseURL = "https://example.org/mysite/"+disableKinds = ['page','rss','section','sitemap','taxonomy','term']+-- assets/css/main.css --+@import "/fonts/fonts.css";+body { color: #222; }+-- assets/fonts/fonts.css --+@font-face {+ font-family: 'Comic Neue';+ src: url(ComicNeue-Regular.woff2) format('woff2'), url(ComicNeue-Regular.ttf) format('truetype');+}+-- assets/fonts/ComicNeue-Regular.ttf --+fakefontdata+-- assets/fonts/ComicNeue-Regular.woff2 --+fakefontdata2+-- layouts/home.html --+{{ with resources.Get "css/main.css" }}+ORIG CSS SHA256: {{ .Content | crypto.Hash "sha256" }}|+{{ with . | css.Build (dict "minify" true) }}+{{ range .Data.Artifacts }}+ARTIFACT: {{ .RelPermalink }}|{{ .Permalink }}|{{ .MediaType.Type }}+{{ end }}+BUILT CSS SHA256: {{ .Content | crypto.Hash "sha256" }}|+{{ with . | fingerprint }}+FINGERPRINTED: {{ .RelPermalink }}|+{{ end }}+{{ end }}+{{ end }}+`++ b := hugolib.Test(t, files, hugolib.TestOptOsFs())+ b.AssertFileContent("public/index.html",+ "ARTIFACT: /mysite/css/ComicNeue-Regular-UA4ODE7N.ttf|",+ "ORIG CSS SHA256: a2b878c05df0f346c3854caebd21f00bb0965b46628440b8ccb31ce675af6bfe|",+ "BUILT CSS SHA256: 5f8757d5579386b8755e0df759c7eecfd069c385294dfe7bc3695862d7f218b1|",+ "FINGERPRINTED: /mysite/css/main.5f8757d5579386b8755e0df759c7eecfd069c385294dfe7bc3695862d7f218b1.css|",+ )+}+ // Issue #14623 func TestCSSBuildLoadersPartial(t *testing.T) { t.Parallel()
What the agent reported
The background task's exit code 1 was just the final grep finding no failures (grep returns 1 on no match) — the results I already captured confirm everything passed. The reviewer confirmed the fix is correct, minimal, and complete.
Summary
What was wrong
In resources/transform.go, TransformWithContext unconditionally did append(r.transformations, t...) when chaining a new transformation onto a resource. That is fine when the whole chain is built before first use (the common pattern), but broken when the chain is executed partway — e.g. accessing .Data/.Content runs css.Build and bakes the result into the adapter's target, yet the transformations slice still lists css.Build. Chaining | fingerprint then produced a chain of [css.Build, fingerprint] applied to the already-built target, wrongly re-running css.Build.
What I changed (resources/transform.go, minimal 3-part fix)
- Added a
transformed atomic.Boolfield toresourceTransformations(and thesync/atomicimport). Since that struct is embedded as a pointer, the flag is shared across the value-copied adapter. - In
doInit, setr.transformed.Store(true)inside thetransformationsInit.Doclosure once the chain has actually executed. - In
TransformWithContext, whenr.transformed.Load()is true, start a fresh chain from the already-transformed target using only the newly supplied transformationst; otherwise keep the original append behavior. This leaves the "build chain before first use" path (existingContent two/Content two chainedsubtests) unchanged.
Test evidence (resources/transform_test.go)
Added regression subtest TestTransform/Content read then chained transform, using non-idempotent append transformations and reading .Content between the two Transform calls to trigger the bug.
- Before the fix: FAIL — got
start-t1-t1-t2(t1 applied twice), wantedstart-t1-t2. - After the fix: PASS. The full
TestTransformand the entireresourcespackage pass, with 0 failures across the whole-run TestTransform ./...build.
Reviewer verdict: correct, minimal, and complete; no unrelated changes. One low-risk out-of-scope observation was noted (a rebuild-granularity optimization in hasTransformationPermalinkHash for an unusual reverse ordering; no effect on output correctness or the reported bug) — I intentionally left it untouched to keep the change minimal.
All changes are left uncommitted in the working tree. (The AGENTS.md/CLAUDE.md deletions shown in git diff were pre-existing before this task and are unrelated.)