Backtestshugorun-20260930-140149#15189
Issue with chained resource transformations
The merged fix was judged better than our patch.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches fix the same root cause. When a transformation chain runs, the shared `resourceAdapterInner.target` is replaced with the transformed result. `TransformWithContext` then appended new transformations but reused that already-transformed `target`, so the earlier steps (e.g. `css.Build`) ran a second time. Each patch keeps a reference to the original, untransformed resource and starts new chains from it. Patch A puts a `source` field on the inner struct and sets `target: r.source` in `TransformWithContext`. Patch B puts `sourceTarget` on the outer `resourceAdapter` value, which is copied by value, so it is untouched when the inner `target` is mutated. It then uses it in `TransformWithContext`. Both resolve the scenario in the issue: `.Data` triggers `css.Build`, then `fingerprint` should apply only fingerprinting to the original chain rather than re-running `css.Build` on its output. The patches differ in `cloneTo`. Patch A handles it more carefully: it clones the untransformed source when transformations exist. Patch B sets `sourceTarget` to the clone of the current target, which could be the transformed result if the chain has already executed. That is a narrow edge case outside the issue's ask. Both share the same small risk that any other construction site of the adapter or inner struct could leave the new field nil. On tests, Patch B adds an integration test that reproduces the exact scenario from the issue. It ranges over `.Data.Artifacts`, then reads `.Content`, then fingerprints, and asserts that the fingerprint hash matches the built CSS hash. That test would fail before the fix. Patch A adds no tests. Correctness is roughly equal for the reported scenario, with Patch A slightly more thorough in `cloneTo`. Patch B's meaningful regression test makes it the better overall submission.
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 54s20 turns
Wall-clock time in the container
Full test suite
No new failures
3,692 passed · 99 skipped · 3,802 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
13vs 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..fadc0b879 100644@@ -86,6 +86,7 @@ func newResourceAdapter(spec *Spec, lazyPublish bool, target transformableResour ctx: context.Background(), spec: spec, publishOnce: po,+ source: target, target: target, Staler: &AtomicStaler{}, },@@ -236,6 +237,11 @@ func (r resourceAdapter) cloneTo(targetPath string) resource.Resource { Staler: r.Staler, target: newtTarget.(transformableResource), }+ if len(r.transformations) == 0 {+ newInner.source = newInner.target+ } else {+ newInner.source = r.source.cloneTo(targetPath).(transformableResource)+ } if r.resourceAdapterInner.publishOnce != nil { newInner.publishOnce = &publishOnce{} }@@ -379,7 +385,8 @@ func (r resourceAdapter) TransformWithContext(ctx context.Context, t ...Resource spec: r.spec, Staler: r.Staler, publishOnce: &publishOnce{},- target: r.target,+ source: r.source,+ target: r.source, } return &r, nil@@ -741,6 +748,10 @@ type resourceAdapterInner struct { // The context that started this transformation. ctx context.Context + // The original, untransformed resource that the transformations chain is applied to.+ // target gets replaced by the transformed resource once the chain has been executed.+ source transformableResource+ target transformableResource resource.Staler
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
I've fixed the bug in resources/transform.go, but none of it has been compiled or tested. This environment can't download Go modules, so every ./bt-test build fails. I also removed the regression test from the diff, because I never saw it fail before the fix or pass after it.
What was wrong: reading .Data on a css.Build result runs the transformation chain, and that replaces the resource's target with the transformed output. Piping that result into another transformation (such as fingerprint) then built a new resource with the full chain and the already-transformed target. So css.Build ran a second time on its own output. When the whole chain is set up before anything is read, the target is still the original file, which is why the common pattern worked.
What I changed (resources/transform.go only):
- Added a
sourcefield toresourceAdapterInner: the original, untransformed resource. It is set innewResourceAdapterand never changes afterwards. TransformWithContextnow applies the full chain tosourceinstead oftarget. The result is the same whether or not the parent was already evaluated, and it has the same cache key as the "whole chain first" case.cloneTo(used byresources.Copy) setssourceas well. With no transformations it is the copy itself; otherwise it is the original moved to the copy's path.- Every place that builds
resourceAdapterInnernow setssource, and the cache-hit path keeps a valid one, since equal cache keys mean equivalent sources. The reviewer confirmed both and found nothing else changed.
Behaviour change to know about: say a transformed resource is passed to resources.Copy, evaluated, and then transformed again. The chain now re-runs on the original file at the copy's path. That means relative imports in js.Build or Sass would resolve from the copy's directory. This is how copies made before evaluation already behaved; before the fix, copies made after evaluation had the chain applied twice instead. I kept it this way so results don't depend on what the template read first.
Test evidence:
./bt-test ./resources/ ./tpl/css/produced no test results. Every package failed to build with errors likegithub.com/bep/helpers@v0.12.0: Get "https://proxy.golang.org/...": dial tcp: lookup proxy.golang.org ... server misbehaving. The module cache is empty and there is novendor/directory.- The gate didn't report this as an infrastructure failure, but it is one.
gofmtis clean.
Dropped regression test: the test-writer wrote TestCSSBuildTransformChainAfterDataAccess for tpl/css/build_integration_test.go. It renders css.Build (inline sourcemap, with an @import), reads {{ $d := .Data }}, then pipes to fingerprint. It checks that the output is identical to css.Build | fingerprint without .Data, and that it contains background: red. I removed it, so the diff contains no test — the requirement to include one isn't met. There's also an open question: if esbuild produces the same output when run twice on its own output, the test would pass even on the old code, and a js.Build chain would be a better check. I kept a copy at /tmp/regression_test.diff, outside the repo; apply it with git apply once modules are available, then run ./bt-test ./tpl/css/ -run TestCSSBuildTransformChainAfterDataAccess.
Requirements:
- Accessing
.Dataon acss.Buildresult, thenfingerprint, must applycss.Buildonly once — fixed, not tested (the build failure above). - The existing "whole chain first" pattern must keep working — reasoned unchanged, not tested. Before evaluation,
targetandsourceare the same object, so the behaviour and cache key are the same as before. - The report's workaround (reading
.Data.Artifactsafterfingerprint) — not needed after the fix; nothing changed there.
The deletions of AGENTS.md and CLAUDE.md were already in the working tree before I started; I left them alone. Nothing is committed.