Invalid YAML Anchor Syntax in transform.Remarshal
Hugo Bug Report: Invalid YAML Anchor Syntax in transform.Remarshal
Summary
transform.Remarshal "yaml" generates invalid YAML syntax when marshalling data structures with repeated empty arrays. It creates self-referencing anchors (options: &options *options) which violates YAML specification and causes parse errors.
Affected Versions
- Broken: Hugo v0.155.0, v0.155.1, v0.155.2, v0.155.3, v0.156.0, v0.157.0
- Works: Hugo v0.147.7 and earlier
The bug appears to have been introduced between v0.147.7 and v0.155.0.
Impact
Any Hugo site using transform.Remarshal "yaml" to generate YAML files will produce invalid YAML that cannot be parsed by standard YAML parsers (yq, Python PyYAML, Go yaml.v3, etc.).
Description
When Hugo's transform.Remarshal "yaml" encounters repeated empty arrays in a data structure, it attempts to optimize by creating YAML anchors. However, it generates syntactically invalid YAML:
Invalid output (Hugo v0.155.0+):
slug:
options: &options *options # ❌ INVALID: Can't define and reference on same line
pattern: /^[a-z0-9]+$/
Valid output (Hugo v0.147.7):
slug:
options: [] # ✓ VALID: Simple empty array
pattern: /^[a-z0-9]+$/
Correct anchor syntax would be:
slug:
options: &options [] # Define anchor with value
pattern: /^[a-z0-9]+$/
title:
options: *options # Reference anchor elsewhere
pattern: /^.+$/
Minimal Reproducible Example
1. Create Hugo site
hugo new site yaml-bug-test
cd yaml-bug-test
2. Create go.mod
module example.com/yaml-bug-test
go 1.18
3. Create config.yml
title: YAML Bug Test
outputFormats:
TestSchema:
baseName: test-schema
mediaType: text/yaml
isPlainText: true
outputs:
home:
- TestSchema
params:
testData:
field1:
options: []
pattern: /^test1$/
field2:
options: []
pattern: /^test2$/
field3:
options: []
pattern: /^test3$/
4. Create layouts/index.testschema.yaml
{{ .Site.Params.testData | transform.Remarshal "yaml" -}}
5. Build and verify
hugo --gc
# Check for invalid anchors
grep "options:.*\*" public/test-schema.yaml
# Attempt to parse (will fail with v0.155.0+)
yq eval public/test-schema.yaml
Expected output (Hugo v0.147.7):
field1:
options: []
pattern: /^test1$/
field2:
options: []
pattern: /^test2$/
field3:
options: []
pattern: /^test3$/
Actual output (Hugo v0.155.0+):
field1:
options: &options []
pattern: /^test1$/
field2:
options: *options *options # ❌ INVALID SYNTAX
pattern: /^test2$/
field3:
options: *options *options # ❌ INVALID SYNTAX
pattern: /^test3$/
Error When Parsing
$ yq eval public/test-schema.yaml
Error: bad file 'public/test-schema.yaml': yaml: line X: could not find expected ':'
Root Cause Analysis
The bug appears to be in Hugo's YAML marshaller when it attempts to create anchors for repeated values. The logic incorrectly generates:
key: &anchor *anchor
Instead of the correct two-step process:
- Define anchor:
key: &anchor value - Reference anchor elsewhere:
key: *anchor
Workarounds
Temporary Fix 1: Downgrade Hugo
Use Hugo v0.147.7 or earlier:
# In CI/CD workflows
hugo-version: "0.147.7"
Temporary Fix 2: Use JSON + yq
Avoid Hugo's YAML marshaller entirely:
Change template from:
{{ $data | transform.Remarshal "yaml" -}}
To:
{{ $data | jsonify -}}
Then convert JSON to YAML externally:
hugo --gc
yq eval -P public/schema.json > public/schema.yaml
Environment
- OS: macOS (ARM64), Linux (AMD64) - affects all platforms
- Hugo Installation: Binary, Docker (hugomods/hugo), all installation methods
- Go Version: Not relevant (Hugo binary issue)
Additional Context
This bug breaks any workflow that:
- Uses
transform.Remarshal "yaml"to generate YAML configuration files - Has data structures with repeated empty arrays or slices
- Needs to process the generated YAML with external tools (yq, Ansible, Kubernetes, etc.)
The bug appears to be in Hugo's underlying YAML library or its usage of it. The self-referencing anchor syntax &anchor *anchor has never been valid YAML according to the YAML 1.2 specification.
Reproduction Repository
[Link to minimal reproduction repo if you create one]
Related Issues
[Search results from Hugo issues if any similar issues exist]
This bug makes transform.Remarshal "yaml" unusable for production YAML generation in Hugo v0.155.0+
Source: gohugoio/hugo