Backtestshugorun-20260922-141813#15189

Issue #15189 Merged fix PR # Run 22 Sep 2026 · run-20260922-141813 gohugoio/hugo

Issue with chained resource transformations

Better

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 output and both ship a test that fails before and passes after, so the decision turns on which resolves the root cause more faithfully and cleanly.

Root cause: after a chain is materialized (e.g. via .Data.Artifacts), doInit replaces the inner target with the transformed result, but TransformWithContext still re-appends the *full* original chain (append(r.transformations, t...)) and applies it — so css.Build runs again on an already-built resource. There are two ways to fix the mismatch: make the chain match the (now-transformed) target by carrying only the new transformations forward (Patch A), or make the target match the full chain by reverting to the original source (Patch B).

Patch A matches the issue's own description of the intended behavior: fingerprint should 'start a new transformation with correct .Content ... [not] wrongly including the full chain, including css.Build.' Its `transformed` flag makes the adapter apply only the appended transformation to the already-transformed target, so css.Build's expensive work is reused rather than repeated. It is confined to transform.go, and its test is well-crafted: non-idempotent appender transformations make a wrongly re-applied chain directly visible (content2 would be x-t1-t1-t2 instead of x-t1-t2), and it additionally asserts assertNoDuplicateWrites — an edge the .Data.Artifacts materialization/publish path directly implies.

Patch B is also correct, and its integration test is a higher-fidelity reproduction of the exact reported css.Build+fingerprint scenario. But it fixes correctness by keeping the full chain (append unchanged) and pointing the target back to a new `sourceTarget` — i.e. it re-runs the entire chain, including css.Build, from the original source every time you branch off a materialized resource. That contradicts the issue's framing ('wrongly including the full chain'), is a latent performance regression (and a latent non-determinism/duplicate-publish risk, which its test does not guard against, unlike A's). It also has a wider blast radius: a new field, initialization in newResourceAdapter, and an unchecked type assertion added to cloneTo.

A is at least as correct, more efficient, aligned with the stated root cause, tighter in scope, and tests the duplicate-write edge. That outweighs B's more realistic integration test.

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

29m 6s18 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

62vs 53 merged

Changed lines, ours vs merged

See 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:

  • .RelPermalink triggers 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:

  • .Data triggers the css.Build transformation.
  • Then fingerprint starts a new transformation with correct .Content, but wrongly including the full chain, including css.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

Our patch+61 −1
resources/transform.goindex 18c768b36..43c497c83 100644@@ -370,8 +370,15 @@ func (r resourceAdapter) Transform(t ...ResourceTransformation) (ResourceTransfo }  func (r resourceAdapter) TransformWithContext(ctx context.Context, t ...ResourceTransformation) (ResourceTransformer, error) {+	transformations := t+	if !r.transformed {+		// The current transformations have not been applied to the target yet,+		// so carry them forward and let the whole chain run at once.+		transformations = append(r.transformations, t...)+	}+ 	r.resourceTransformations = &resourceTransformations{-		transformations: append(r.transformations, t...),+		transformations: transformations, 	}  	r.resourceAdapterInner = &resourceAdapterInner{@@ -728,6 +735,7 @@ func (r *resourceAdapter) doInit(publish, setContent bool) error { 		}  		r.transformationsErr = r.getOrTransform(publish, setContent)+		r.transformed = true 	})  	if publish && r.publishOnce != nil {@@ -764,6 +772,11 @@ type resourceTransformations struct { 	transformationsInit sync.Once 	transformationsErr  error 	transformations     []ResourceTransformation+	// transformed reports whether the transformations above have already been+	// applied to the target. Once applied, the target holds the transformed+	// result, so any further transformation must start a fresh chain from it+	// rather than re-applying the whole chain.+	transformed bool }  // hasTransformationPermalinkHash reports whether any of the transformationsresources/transform_test.goindex feae43e41..676e93020 100644@@ -329,6 +329,53 @@ func TestTransform(t *testing.T) { 		assertNoDuplicateWrites(c, spec) 	}) +	c.Run("Access Data first, then chain", func(c *qt.C) {+		c.Parallel()++		spec := newTestResourceSpec(specDescriptor{c: c})++		// Non-idempotent transformations: each appends its own name to the+		// content. This makes a wrongly re-applied chain visible in the output.+		appender := func(name string) resources.ResourceTransformation {+			return &testTransformation{+				name: name,+				transform: func(ctx *resources.ResourceTransformationCtx) error {+					in := helpers.ReaderToString(ctx.From)+					ctx.AddOutPathIdentifier("." + name)+					fmt.Fprint(ctx.To, in+"-"+name)+					return nil+				},+			}+		}++		t1 := appender("t1")+		t2 := appender("t2")++		r := createTransformer(c, spec, "f1.txt", "x")++		tr1, err := r.Transform(t1)+		c.Assert(err, qt.IsNil)++		// Materialize the first transformation early by accessing .Data,+		// mirroring how css.Build's .Data.Artifacts triggers the chain.+		_ = tr1.Data()++		// Chaining a further transformation must apply only t2 to the+		// already-transformed result, not re-run the whole chain.+		tr2, err := tr1.Transform(t2)+		c.Assert(err, qt.IsNil)++		content1, err := tr1.(resource.ContentProvider).Content(context.Background())+		c.Assert(err, qt.IsNil)+		content2, err := tr2.(resource.ContentProvider).Content(context.Background())+		c.Assert(err, qt.IsNil)++		c.Assert(content1, qt.Equals, "x-t1")+		c.Assert(content2, qt.Equals, "x-t1-t2")++		assertNoDuplicateWrites(c, spec)+	})+ 	c.Run("Content many", func(c *qt.C) { 		c.Parallel()  
Merged fix · PR #+52 −1
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 fix is complete and verified. Here is my final report.

What was wrong

In resources/transform.go, a resourceAdapter holds a chain of transformations (r.transformations) and a target. When a new transformation is chained via TransformWithContext, it did append(r.transformations, t...) and re-ran the entire chain against target.

That is only correct while the chain has not yet been materialized. But accessing .Data() / .Content() / .RelPermalink() runs the chain and replaces r.target with the transformed result (transform(): r.target = newTarget) without clearing r.transformations. So a subsequent .Transform(...) re-appended the already-applied transformations and re-ran them on top of the already-transformed target — double-applying the whole chain. This is exactly the reported symptom: accessing css.Build's .Data.Artifacts and then piping through fingerprint wrongly re-ran css.Build. The common "build the whole chain, then use it" pattern avoided the bug only because the chain wasn't materialized before the next .Transform was appended.

What I changed

resources/transform.go (production, 3 edits):

  • Added a transformed bool field to resourceTransformations.
  • Set r.transformed = true inside doInit's transformationsInit.Do closure, right after getOrTransform runs.
  • In TransformWithContext, when r.transformed is true (chain already materialized, target already transformed), start a fresh chain from just the new transformations; otherwise append as before. The returned adapter gets a brand-new resourceTransformations (so transformed resets to false).

resources/transform_test.go (test, 1 added sub-test Access Data first, then chain): chains two non-idempotent (name-appending) transformations, accesses .Data() to materialize the first, then chains the second — asserting the user-visible content is x-t1-t2, not the doubled x-t1-t1-t2.

Test evidence

  • Regression test before fix: FAIL — content2 was "x-t1-t1-t2" (whole chain re-applied) vs. wanted "x-t1-t2".
  • After fix, ./bt-test ./resources/ -run TestTransform: exit 0 — all 13 entries pass, including the new Access_Data_first,_then_chain and the existing chaining cases (Content_two_chained, Content_two, Content_many, Access_RelPermalink_first, File-cached_transformation, Memory-cached_transformation, All_values, Concurrent).
  • Blast radius, ./bt-test ./resources/... (whole tree): the only failures were four resources/images golden-image byte-comparison tests (TestImagesGoldenFiltersMask, TestImagesGoldenMethods, TestImagesGoldenProcessMisc, TestImagesGoldenProcessWebP). I stashed my change and re-ran that package on the baseline: the exact same four fail without my change — pre-existing environment/encoder-version failures, unrelated to transformation chaining (which touches no image golden bytes).
  • Reviewer: confirmed the fix is correct, minimal, and free of aliasing bugs. It flagged a theoretical unsynchronized read of the new transformed flag; I judged this out of scope because the adjacent line already reads r.target — mutated inside the very same sync.Once — unsynchronized, so the fix introduces no new race class. Making only the one field atomic would be an inconsistent half-measure.

Requirements disposition

  1. Fix chained transformations so a materialized chain (.Data) followed by a new transformation (fingerprint) does not re-run the prior chain — ✅ Implemented in TransformWithContext; regression test fails before / passes after.
  2. Preserve the working common-case behavior (chain built before use, triggered via .RelPermalink/.Content) — ✅ Preserved: when not yet materialized (transformed == false), the old append behavior is unchanged; verified by the still-passing Content_two_chained, Access_RelPermalink_first, and other existing cases.
  3. The alternative rewrite the report offers (moving range .Data.Artifacts inside the fingerprint block) is described only as a user-side workaround, not an ask — no code change required; noted here with the diff as evidence that the original (broken) template ordering now works.

All changes are left uncommitted in the working tree.