Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f1ac9dc

Browse files
committedFeb 12, 2025
Upgrade to Go 1.24
Fixes gohugoio#13381
1 parent 9b5f786 commit f1ac9dc

36 files changed

+666
-602
lines changed
 

‎.circleci/config.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ parameters:
44
defaults: &defaults
55
resource_class: large
66
docker:
7-
- image: bepsays/ci-hugoreleaser:1.22301.20401
7+
- image: bepsays/ci-hugoreleaser:1.22400.20000
88
environment: &buildenv
99
GOMODCACHE: /root/project/gomodcache
1010
version: 2
@@ -58,7 +58,7 @@ jobs:
5858
environment:
5959
<<: [*buildenv]
6060
docker:
61-
- image: bepsays/ci-hugoreleaser-linux-arm64:1.22301.20401
61+
- image: bepsays/ci-hugoreleaser-linux-arm64:1.22400.20000
6262
steps:
6363
- *restore-cache
6464
- &attach-workspace

‎.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
test:
1717
strategy:
1818
matrix:
19-
go-version: [1.22.x, 1.23.x]
19+
go-version: [1.23.x, 1.24.x]
2020
os: [ubuntu-latest, windows-latest] # macos disabled for now because of disk space issues.
2121
runs-on: ${{ matrix.os }}
2222
steps:

‎go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,4 @@ require (
170170
software.sslmate.com/src/go-pkcs12 v0.2.0 // indirect
171171
)
172172

173-
go 1.22.6
173+
go 1.23

‎scripts/fork_go_templates/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
)
1717

1818
func main() {
19-
// The current is built with 6885bad7dd86880be6929c02085e5c7a67ff2887 go1.23.0
19+
// The current is built with 3901409b5d [release-branch.go1.24] go1.24.0
2020
// TODO(bep) preserve the staticcheck.conf file.
2121
fmt.Println("Forking ...")
2222
defer fmt.Println("Done ...")
@@ -216,6 +216,7 @@ func rewrite(filename, rule string) {
216216
}
217217

218218
func goimports(dir string) {
219+
// Needs go install golang.org/x/tools/cmd/goimports@latest
219220
cmf, _ := hexec.SafeCommand("goimports", "-w", dir)
220221
out, err := cmf.CombinedOutput()
221222
if err != nil {

‎tpl/internal/go_templates/cfg/cfg.go

+2
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ const KnownEnv = `
3737
GOARCH
3838
GOARM
3939
GOARM64
40+
GOAUTH
4041
GOBIN
4142
GOCACHE
4243
GOCACHEPROG
4344
GOENV
4445
GOEXE
4546
GOEXPERIMENT
47+
GOFIPS140
4648
GOFLAGS
4749
GOGCCFLAGS
4850
GOHOSTARCH

‎tpl/internal/go_templates/htmltemplate/clone_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build !windows
6-
// +build !windows
5+
//go:build go1.13 && !windows
6+
// +build go1.13,!windows
77

88
package template
99

‎tpl/internal/go_templates/htmltemplate/content_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build !windows
6-
// +build !windows
5+
//go:build go1.13 && !windows
6+
// +build go1.13,!windows
77

88
package template
99

@@ -428,7 +428,7 @@ func TestStringer(t *testing.T) {
428428
if err := tmpl.Execute(b, s); err != nil {
429429
t.Fatal(err)
430430
}
431-
expect := "string=3"
431+
var expect = "string=3"
432432
if b.String() != expect {
433433
t.Errorf("expected %q got %q", expect, b.String())
434434
}

‎tpl/internal/go_templates/htmltemplate/css_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build !windows
6-
// +build !windows
5+
//go:build go1.13 && !windows
6+
// +build go1.13,!windows
77

88
package template
99

‎tpl/internal/go_templates/htmltemplate/escape.go

+6-13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"html"
1111
"io"
12+
"maps"
1213
"regexp"
1314

1415
template "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate"
@@ -145,7 +146,7 @@ func (e *escaper) escape(c context, n parse.Node) context {
145146
return c
146147
case *parse.ContinueNode:
147148
c.n = n
148-
e.rangeContext.continues = append(e.rangeContext.breaks, c)
149+
e.rangeContext.continues = append(e.rangeContext.continues, c)
149150
return context{state: stateDead}
150151
case *parse.IfNode:
151152
return e.escapeBranch(c, &n.BranchNode, "if")
@@ -588,22 +589,14 @@ func (e *escaper) escapeListConditionally(c context, n *parse.ListNode, filter f
588589
e1 := makeEscaper(e.ns)
589590
e1.rangeContext = e.rangeContext
590591
// Make type inferences available to f.
591-
for k, v := range e.output {
592-
e1.output[k] = v
593-
}
592+
maps.Copy(e1.output, e.output)
594593
c = e1.escapeList(c, n)
595594
ok := filter != nil && filter(&e1, c)
596595
if ok {
597596
// Copy inferences and edits from e1 back into e.
598-
for k, v := range e1.output {
599-
e.output[k] = v
600-
}
601-
for k, v := range e1.derived {
602-
e.derived[k] = v
603-
}
604-
for k, v := range e1.called {
605-
e.called[k] = v
606-
}
597+
maps.Copy(e.output, e1.output)
598+
maps.Copy(e.derived, e1.derived)
599+
maps.Copy(e.called, e1.called)
607600
for k, v := range e1.actionNodeEdits {
608601
e.editActionNode(k, v)
609602
}

‎tpl/internal/go_templates/htmltemplate/escape_test.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build !windows
6-
// +build !windows
5+
//go:build go1.13 && !windows
6+
// +build go1.13,!windows
77

88
package template
99

@@ -944,6 +944,7 @@ func TestEscapeSet(t *testing.T) {
944944
t.Errorf("want\n\t%q\ngot\n\t%q", test.want, got)
945945
}
946946
}
947+
947948
}
948949

949950
func TestErrors(t *testing.T) {
@@ -1064,6 +1065,10 @@ func TestErrors(t *testing.T) {
10641065
"{{range .Items}}<a{{if .X}}{{continue}}{{end}}>{{end}}",
10651066
"z:1:29: at range loop continue: {{range}} branches end in different contexts",
10661067
},
1068+
{
1069+
"{{range .Items}}{{if .X}}{{break}}{{end}}<a{{if .Y}}{{continue}}{{end}}>{{if .Z}}{{continue}}{{end}}{{end}}",
1070+
"z:1:54: at range loop continue: {{range}} branches end in different contexts",
1071+
},
10671072
{
10681073
"<a b=1 c={{.H}}",
10691074
"z: ends in a non-text context: {stateAttr delimSpaceOrTagEnd",
@@ -1193,6 +1198,7 @@ func TestErrors(t *testing.T) {
11931198
// Check that we get the same error if we call Execute again.
11941199
if err := tmpl.Execute(buf, nil); err == nil || err.Error() != got {
11951200
t.Errorf("input=%q: unexpected error on second call %q", test.input, err)
1201+
11961202
}
11971203
}
11981204
}

‎tpl/internal/go_templates/htmltemplate/example_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
//go:build go1.13
6+
// +build go1.13
7+
58
package template_test
69

710
import (
@@ -80,6 +83,7 @@ func Example() {
8083
// <div><strong>no rows</strong></div>
8184
// </body>
8285
// </html>
86+
8387
}
8488

8589
func Example_autoescaping() {
@@ -120,6 +124,7 @@ func Example_escape() {
120124
// \"Fran \u0026 Freddie\'s Diner\" \u003Ctasty@example.com\u003E
121125
// \"Fran \u0026 Freddie\'s Diner\"32\u003Ctasty@example.com\u003E
122126
// %22Fran+%26+Freddie%27s+Diner%2232%3Ctasty%40example.com%3E
127+
123128
}
124129

125130
func ExampleTemplate_Delims() {

‎tpl/internal/go_templates/htmltemplate/examplefiles_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
//go:build go1.13
6+
// +build go1.13
7+
58
package template_test
69

710
import (

‎tpl/internal/go_templates/htmltemplate/exec_test.go

+20-34
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
// Tests for template execution, copied from text/template.
66

7-
//go:build !windows
8-
// +build !windows
7+
//go:build go1.13 && !windows
8+
// +build go1.13,!windows
99

1010
package template
1111

@@ -325,16 +325,12 @@ var execTests = []execTest{
325325
{"$.U.V", "{{$.U.V}}", "v", tVal, true},
326326
{"declare in action", "{{$x := $.U.V}}{{$x}}", "v", tVal, true},
327327
{"simple assignment", "{{$x := 2}}{{$x = 3}}{{$x}}", "3", tVal, true},
328-
{
329-
"nested assignment",
328+
{"nested assignment",
330329
"{{$x := 2}}{{if true}}{{$x = 3}}{{end}}{{$x}}",
331-
"3", tVal, true,
332-
},
333-
{
334-
"nested assignment changes the last declaration",
330+
"3", tVal, true},
331+
{"nested assignment changes the last declaration",
335332
"{{$x := 1}}{{if true}}{{$x := 2}}{{if true}}{{$x = 3}}{{end}}{{end}}{{$x}}",
336-
"1", tVal, true,
337-
},
333+
"1", tVal, true},
338334

339335
// Type with String method.
340336
{"V{6666}.String()", "-{{.V0}}-", "-{6666}-", tVal, true}, // NOTE: -<6666>- in text/template
@@ -381,21 +377,15 @@ var execTests = []execTest{
381377
{".Method3(nil constant)", "-{{.Method3 nil}}-", "-Method3: &lt;nil&gt;-", tVal, true},
382378
{".Method3(nil value)", "-{{.Method3 .MXI.unset}}-", "-Method3: &lt;nil&gt;-", tVal, true},
383379
{"method on var", "{{if $x := .}}-{{$x.Method2 .U16 $x.X}}{{end}}-", "-Method2: 16 x-", tVal, true},
384-
{
385-
"method on chained var",
380+
{"method on chained var",
386381
"{{range .MSIone}}{{if $.U.TrueFalse $.True}}{{$.U.TrueFalse $.True}}{{else}}WRONG{{end}}{{end}}",
387-
"true", tVal, true,
388-
},
389-
{
390-
"chained method",
382+
"true", tVal, true},
383+
{"chained method",
391384
"{{range .MSIone}}{{if $.GetU.TrueFalse $.True}}{{$.U.TrueFalse $.True}}{{else}}WRONG{{end}}{{end}}",
392-
"true", tVal, true,
393-
},
394-
{
395-
"chained method on variable",
385+
"true", tVal, true},
386+
{"chained method on variable",
396387
"{{with $x := .}}{{with .SI}}{{$.GetU.TrueFalse $.True}}{{end}}{{end}}",
397-
"true", tVal, true,
398-
},
388+
"true", tVal, true},
399389
{".NilOKFunc not nil", "{{call .NilOKFunc .PI}}", "false", tVal, true},
400390
{".NilOKFunc nil", "{{call .NilOKFunc nil}}", "true", tVal, true},
401391
{"method on nil value from slice", "-{{range .}}{{.Method1 1234}}{{end}}-", "-1234-", tSliceOfNil, true},
@@ -481,14 +471,10 @@ var execTests = []execTest{
481471
{"printf lots", `{{printf "%d %s %g %s" 127 "hello" 7-3i .Method0}}`, "127 hello (7-3i) M0", tVal, true},
482472

483473
// HTML.
484-
{
485-
"html", `{{html "<script>alert(\"XSS\");</script>"}}`,
486-
"&lt;script&gt;alert(&#34;XSS&#34;);&lt;/script&gt;", nil, true,
487-
},
488-
{
489-
"html pipeline", `{{printf "<script>alert(\"XSS\");</script>" | html}}`,
490-
"&lt;script&gt;alert(&#34;XSS&#34;);&lt;/script&gt;", nil, true,
491-
},
474+
{"html", `{{html "<script>alert(\"XSS\");</script>"}}`,
475+
"&lt;script&gt;alert(&#34;XSS&#34;);&lt;/script&gt;", nil, true},
476+
{"html pipeline", `{{printf "<script>alert(\"XSS\");</script>" | html}}`,
477+
"&lt;script&gt;alert(&#34;XSS&#34;);&lt;/script&gt;", nil, true},
492478
{"html", `{{html .PS}}`, "a string", tVal, true},
493479
{"html typed nil", `{{html .NIL}}`, "&lt;nil&gt;", tVal, true},
494480
{"html untyped nil", `{{html .Empty0}}`, "&lt;nil&gt;", tVal, true}, // NOTE: "&lt;no value&gt;" in text/template
@@ -854,7 +840,7 @@ var delimPairs = []string{
854840

855841
func TestDelims(t *testing.T) {
856842
const hello = "Hello, world"
857-
value := struct{ Str string }{hello}
843+
var value = struct{ Str string }{hello}
858844
for i := 0; i < len(delimPairs); i += 2 {
859845
text := ".Str"
860846
left := delimPairs[i+0]
@@ -877,7 +863,7 @@ func TestDelims(t *testing.T) {
877863
if err != nil {
878864
t.Fatalf("delim %q text %q parse err %s", left, text, err)
879865
}
880-
b := new(strings.Builder)
866+
var b = new(strings.Builder)
881867
err = tmpl.Execute(b, value)
882868
if err != nil {
883869
t.Fatalf("delim %q exec err %s", left, err)
@@ -978,7 +964,7 @@ const treeTemplate = `
978964
`
979965

980966
func TestTree(t *testing.T) {
981-
tree := &Tree{
967+
var tree = &Tree{
982968
1,
983969
&Tree{
984970
2, &Tree{
@@ -1229,7 +1215,7 @@ var cmpTests = []cmpTest{
12291215

12301216
func TestComparison(t *testing.T) {
12311217
b := new(strings.Builder)
1232-
cmpStruct := struct {
1218+
var cmpStruct = struct {
12331219
Uthree, Ufour uint
12341220
NegOne, Three int
12351221
Ptr, NilPtr *int

‎tpl/internal/go_templates/htmltemplate/html_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build !windows
6-
// +build !windows
5+
//go:build go1.13 && !windows
6+
// +build go1.13,!windows
77

88
package template
99

‎tpl/internal/go_templates/htmltemplate/js.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
htmltemplate "html/template"
1212
"reflect"
13+
"regexp"
1314
"strings"
1415
"unicode/utf8"
1516
)
@@ -145,6 +146,8 @@ func indirectToJSONMarshaler(a any) any {
145146
return v.Interface()
146147
}
147148

149+
var scriptTagRe = regexp.MustCompile("(?i)<(/?)script")
150+
148151
// jsValEscaper escapes its inputs to a JS Expression (section 11.14) that has
149152
// neither side-effects nor free variables outside (NaN, Infinity).
150153
func jsValEscaper(args ...any) string {
@@ -182,9 +185,9 @@ func jsValEscaper(args ...any) string {
182185
// In particular we:
183186
// * replace "*/" comment end tokens with "* /", which does not
184187
// terminate the comment
185-
// * replace "</script" with "\x3C/script", and "<!--" with
186-
// "\x3C!--", which prevents confusing script block termination
187-
// semantics
188+
// * replace "<script" and "</script" with "\x3Cscript" and "\x3C/script"
189+
// (case insensitively), and "<!--" with "\x3C!--", which prevents
190+
// confusing script block termination semantics
188191
//
189192
// We also put a space before the comment so that if it is flush against
190193
// a division operator it is not turned into a line comment:
@@ -193,8 +196,8 @@ func jsValEscaper(args ...any) string {
193196
// x//* error marshaling y:
194197
// second line of error message */null
195198
errStr := err.Error()
199+
errStr = string(scriptTagRe.ReplaceAll([]byte(errStr), []byte(`\x3C${1}script`)))
196200
errStr = strings.ReplaceAll(errStr, "*/", "* /")
197-
errStr = strings.ReplaceAll(errStr, "</script", `\x3C/script`)
198201
errStr = strings.ReplaceAll(errStr, "<!--", `\x3C!--`)
199202
return fmt.Sprintf(" /* %s */null ", errStr)
200203
}

0 commit comments

Comments
 (0)
Please sign in to comment.