Backtestshugorun-20261005-133023#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 find the same root cause. `TransformWithContext` reused `r.target`, but once the chain has run (for example, after `.Data` is accessed), that target holds the already-transformed resource. Re-applying the full chain to it double-transforms. Both fix this by keeping the original untransformed source and starting new chains from it. The patches differ in two places. 1. **`cloneTo` (e.g. `resources.Copy`):** - Patch B sets `sourceTarget = r.target.cloneTo(...)`. If the adapter has already been transformed, `r.target` is the transformed resource, so the new source becomes transformed content. The transformation list is still attached, so the same double-application bug comes back through Copy. - Patch A handles this explicitly. With no transformations, the source is the new target. Otherwise it clones the untransformed `chainSource`. - Patch A's test covers this path: fingerprint, access `.Data`, Copy, access `.Data`, then minify. 2. **Slice aliasing:** - Patch A builds a fresh slice instead of `append(r.transformations, t...)`. This prevents two branches chained off the same base from sharing a backing array and overwriting each other's appended transformations. - Patch B keeps the aliasing-prone append. **Tests:** - Patch A adds a generic integration test. It checks that triggering via `.Data`, `.Content` or `.RelPermalink` before chaining, and the Copy case, all give the same output as the never-initialized chain. It also adds a css.Build `.Data.Artifacts` then fingerprint test, matching the issue's scenario. - Patch B adds only the css.Build scenario, which relies on hard-coded hashes. Scope is comparable and focused in both. Patch A is more correct at the edges and better tested.
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
47m 26s
Wall-clock time in the container
Full test suite
No new failures
3,696 passed · 99 skipped · 3,804 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
110vs 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/resources_integration_test.goindex 71216e304..06c1b7beb 100644@@ -258,3 +258,47 @@ This was assets/a.txt b.Assert(err, qt.IsNil) b.AssertFileContent("public/p1/b.txt", "This was assets/a.txt") }++// Issue 15189+func TestTransformChainAfterTransformationIsApplied(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+disableKinds = ['page','rss','section','sitemap','taxonomy','term']+-- assets/a.css --+body { color: red; }+-- layouts/home.html --+{{ $data := resources.Get "a.css" | fingerprint "md5" }}+{{ $data.Data.Integrity }}+{{ $data = $data | minify }}+Data: {{ $data.RelPermalink }}|{{ $data.Data.Integrity }}|{{ $data.Content }}|+{{ $content := resources.Get "a.css" | fingerprint "md5" }}+{{ $content.Content }}+{{ $content = $content | minify }}+Content: {{ $content.RelPermalink }}|{{ $content.Data.Integrity }}|{{ $content.Content }}|+{{ $permalink := resources.Get "a.css" | fingerprint "md5" }}+{{ $permalink.RelPermalink }}+{{ $permalink = $permalink | minify }}+Permalink: {{ $permalink.RelPermalink }}|{{ $permalink.Data.Integrity }}|{{ $permalink.Content }}|+{{ $copy := resources.Get "a.css" | fingerprint "md5" }}+{{ $copy.Data.Integrity }}+{{ $copy = $copy | resources.Copy "b.css" }}+{{ $copy.Data.Integrity }}+{{ $copy = $copy | minify }}+Copy: {{ $copy.RelPermalink }}|{{ $copy.Content }}|+{{ $notInitialized := resources.Get "a.css" | fingerprint "md5" | minify }}+NotInitialized: {{ $notInitialized.RelPermalink }}|{{ $notInitialized.Data.Integrity }}|{{ $notInitialized.Content }}|+`++ b := hugolib.Test(t, files)++ const expect = "/a.8121084da82fefa730a83a486ced7426.min.css|md5-gSEITagv76cwqDpIbO10Jg==|body{color:red}|"+ b.AssertFileContent("public/index.html",+ "NotInitialized: "+expect,+ "Data: "+expect,+ "Content: "+expect,+ "Permalink: "+expect,+ "Copy: /b.8121084da82fefa730a83a486ced7426.min.css|body{color:red}|",+ )+}resources/transform.goindex 18c768b36..4e0ff3732 100644@@ -82,6 +82,7 @@ func newResourceAdapter(spec *Spec, lazyPublish bool, target transformableResour return &resourceAdapter{ resourceTransformations: &resourceTransformations{}, metaProvider: target,+ chainSource: target, resourceAdapterInner: &resourceAdapterInner{ ctx: context.Background(), spec: spec,@@ -187,6 +188,12 @@ type resourceAdapter struct { *resourceTransformations *resourceAdapterInner metaProvider resource.ResourceMetaProvider++ // chainSource is the untransformed resource that the transformations+ // in resourceTransformations are applied to.+ // Note that resourceAdapterInner.target gets replaced with the+ // transformed resource once the transformations are applied.+ chainSource transformableResource } var _ identity.ForEeachIdentityByNameProvider = (*resourceAdapter)(nil)@@ -240,6 +247,13 @@ func (r resourceAdapter) cloneTo(targetPath string) resource.Resource { newInner.publishOnce = &publishOnce{} } r.resourceAdapterInner = newInner+ if len(r.transformations) == 0 {+ r.chainSource = newInner.target+ } else {+ // The target may already be transformed, so any further+ // transformations must start out from a copy of the untransformed source.+ r.chainSource = r.chainSource.cloneTo(targetPath).(transformableResource)+ } return &r } @@ -370,16 +384,22 @@ func (r resourceAdapter) Transform(t ...ResourceTransformation) (ResourceTransfo } func (r resourceAdapter) TransformWithContext(ctx context.Context, t ...ResourceTransformation) (ResourceTransformer, error) {+ transformations := make([]ResourceTransformation, 0, len(r.transformations)+len(t))+ transformations = append(transformations, r.transformations...)+ transformations = append(transformations, t...) r.resourceTransformations = &resourceTransformations{- transformations: append(r.transformations, t...),+ transformations: transformations, } + // Note that we start out from the untransformed source and not r.target,+ // which may already be transformed (e.g. by accessing .Data) by the+ // transformations we're appending to. r.resourceAdapterInner = &resourceAdapterInner{ ctx: ctx, spec: r.spec, Staler: r.Staler, publishOnce: &publishOnce{},- target: r.target,+ target: r.chainSource, } return &r, niltpl/css/build_integration_test.goindex 6ddb23331..87bc1dbaa 100644@@ -960,3 +960,45 @@ CSS: {{ $css.RelPermalink }}| @media screen and (min-width:768px){body{color:orange}} `) }++// Issue 15189+func TestCSSBuildDataArtifactsThenFingerprint(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+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');+}+-- assets/fonts/ComicNeue-Regular.woff2 --+fakefontdata2+-- layouts/home.html --+{{ with resources.Get "css/main.css" }}+{{ with . | css.Build (dict "minify" true) }}+{{ range .Data.Artifacts }}{{ if eq .MediaType.MainType "font" }}+<link rel="preload" href="{{ .RelPermalink }}" as="font" type="{{ .MediaType.Type }}">+{{ end }}{{ end }}+{{ with . | fingerprint "md5" }}+<link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}">|Artifacts: {{ len .Data.Artifacts }}|+{{ end }}+{{ end }}+{{ end }}+{{ with resources.Get "css/main.css" | css.Build (dict "minify" true) | fingerprint "md5" }}+Chain: {{ .RelPermalink }}|Artifacts: {{ len .Data.Artifacts }}|+{{ end }}+`++ b := hugolib.Test(t, files, hugolib.TestOptOsFs())+ b.AssertFileContent("public/index.html",+ `as="font" type="font/woff2"`,+ `Chain: /css/main.222d6fa376dfed9b506387bebcd2847f.css|Artifacts: 1|`,+ `<link rel="stylesheet" href="/css/main.222d6fa376dfed9b506387bebcd2847f.css"`,+ `|Artifacts: 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()