Skip to content

Commit

Permalink
Upgrade to Go 1.23
Browse files Browse the repository at this point in the history
Fixes #12763
  • Loading branch information
bep committed Aug 15, 2024
1 parent b3ad58f commit 2168c5b
Show file tree
Hide file tree
Showing 34 changed files with 616 additions and 402 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ parameters:
defaults: &defaults
resource_class: large
docker:
- image: bepsays/ci-hugoreleaser:1.22200.20501
- image: bepsays/ci-hugoreleaser:1.22300.20000
environment: &buildenv
GOMODCACHE: /root/project/gomodcache
version: 2
Expand Down Expand Up @@ -60,7 +60,7 @@ jobs:
environment:
<<: [*buildenv]
docker:
- image: bepsays/ci-hugoreleaser-linux-arm64:1.22200.20501
- image: bepsays/ci-hugoreleaser-linux-arm64:1.22300.20000
steps:
- *restore-cache
- &attach-workspace
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.21.x, 1.22.x]
go-version: [1.22.x, 1.23.x]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,4 @@ require (
software.sslmate.com/src/go-pkcs12 v0.2.0 // indirect
)

go 1.21.8
go 1.22.6
3 changes: 2 additions & 1 deletion hugofs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ func isWrite(flag int) bool {
// TODO(bep) move this to a more suitable place.
func MakeReadableAndRemoveAllModulePkgDir(fs afero.Fs, dir string) (int, error) {
// Safe guard
if !strings.Contains(dir, "pkg") {
// Note that the base directory changed from pkg to gomod_cache in Go 1.23.
if !strings.Contains(dir, "pkg") && !strings.Contains(dir, "gomod") {
panic(fmt.Sprint("invalid dir:", dir))
}

Expand Down
12 changes: 0 additions & 12 deletions modules/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,18 +365,6 @@ func (c *Client) Get(args ...string) error {
}

func (c *Client) get(args ...string) error {
var hasD bool
for _, arg := range args {
if arg == "-d" {
hasD = true
break
}
}
if !hasD {
// go get without the -d flag does not make sense to us, as
// it will try to build and install go packages.
args = append([]string{"-d"}, args...)
}
if err := c.runGo(context.Background(), c.logger.Out(), append([]string{"get"}, args...)...); err != nil {
return fmt.Errorf("failed to get %q: %w", args, err)
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/fork_go_templates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

func main() {
// The current is built with 8e1fdea8316d840fd07e9d6e026048e53290948b go1.22.5
// The current is built with 6885bad7dd86880be6929c02085e5c7a67ff2887 go1.23.0
// TODO(bep) preserve the staticcheck.conf file.
fmt.Println("Forking ...")
defer fmt.Println("Done ...")
Expand Down
3 changes: 2 additions & 1 deletion testscripts/commands/mod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ hugo mod clean
! stderr .
stdout 'hugo: removed 1 dirs in module cache for \"github.com/bep/empty-hugo-module\"'
hugo mod clean --all
stdout 'Deleted 2\d{2} files from module cache\.'
# Currently this is 299 on MacOS and 301 on Linux.
stdout 'Deleted (2|3)\d{2} files from module cache\.'
cd submod
hugo mod init testsubmod
cmpenv go.mod $WORK/golden/go.mod.testsubmod
Expand Down
2 changes: 2 additions & 0 deletions testscripts/commands/mod_npm.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

dostounix golden/package.json


hugo mod npm pack
cmp package.json golden/package.json

Expand Down Expand Up @@ -41,3 +42,4 @@ path="github.com/gohugoio/hugoTestModule2"
}
-- go.mod --
module github.com/gohugoio/hugoTestModule
go 1.20
1 change: 1 addition & 0 deletions testscripts/commands/mod_npm_withexisting.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ path="github.com/gohugoio/hugoTestModule2"
}
-- go.mod --
module github.com/gohugoio/hugoTestModule
go 1.20
2 changes: 2 additions & 0 deletions tpl/internal/go_templates/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const KnownEnv = `
GOAMD64
GOARCH
GOARM
GOARM64
GOBIN
GOCACHE
GOCACHEPROG
Expand All @@ -57,6 +58,7 @@ const KnownEnv = `
GOPPC64
GOPRIVATE
GOPROXY
GORISCV64
GOROOT
GOSUMDB
GOTMPDIR
Expand Down
113 changes: 24 additions & 89 deletions tpl/internal/go_templates/fmtsort/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,23 @@
package fmtsort

import (
"cmp"
"reflect"
"sort"
"slices"
)

// Note: Throughout this package we avoid calling reflect.Value.Interface as
// it is not always legal to do so and it's easier to avoid the issue than to face it.

// SortedMap represents a map's keys and values. The keys and values are
// aligned in index order: Value[i] is the value in the map corresponding to Key[i].
type SortedMap struct {
Key []reflect.Value
Value []reflect.Value
}
// SortedMap is a slice of KeyValue pairs that simplifies sorting
// and iterating over map entries.
//
// Each KeyValue pair contains a map key and its corresponding value.
type SortedMap []KeyValue

func (o *SortedMap) Len() int { return len(o.Key) }
func (o *SortedMap) Less(i, j int) bool { return compare(o.Key[i], o.Key[j]) < 0 }
func (o *SortedMap) Swap(i, j int) {
o.Key[i], o.Key[j] = o.Key[j], o.Key[i]
o.Value[i], o.Value[j] = o.Value[j], o.Value[i]
// KeyValue holds a single key and value pair found in a map.
type KeyValue struct {
Key, Value reflect.Value
}

// Sort accepts a map and returns a SortedMap that has the same keys and
Expand All @@ -48,26 +46,22 @@ func (o *SortedMap) Swap(i, j int) {
// Otherwise identical arrays compare by length.
// - interface values compare first by reflect.Type describing the concrete type
// and then by concrete value as described in the previous rules.
func Sort(mapValue reflect.Value) *SortedMap {
func Sort(mapValue reflect.Value) SortedMap {
if mapValue.Type().Kind() != reflect.Map {
return nil
}
// Note: this code is arranged to not panic even in the presence
// of a concurrent map update. The runtime is responsible for
// yelling loudly if that happens. See issue 33275.
n := mapValue.Len()
key := make([]reflect.Value, 0, n)
value := make([]reflect.Value, 0, n)
sorted := make(SortedMap, 0, n)
iter := mapValue.MapRange()
for iter.Next() {
key = append(key, iter.Key())
value = append(value, iter.Value())
}
sorted := &SortedMap{
Key: key,
Value: value,
sorted = append(sorted, KeyValue{iter.Key(), iter.Value()})
}
sort.Stable(sorted)
slices.SortStableFunc(sorted, func(a, b KeyValue) int {
return compare(a.Key, b.Key)
})
return sorted
}

Expand All @@ -82,43 +76,19 @@ func compare(aVal, bVal reflect.Value) int {
}
switch aVal.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
a, b := aVal.Int(), bVal.Int()
switch {
case a < b:
return -1
case a > b:
return 1
default:
return 0
}
return cmp.Compare(aVal.Int(), bVal.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
a, b := aVal.Uint(), bVal.Uint()
switch {
case a < b:
return -1
case a > b:
return 1
default:
return 0
}
return cmp.Compare(aVal.Uint(), bVal.Uint())
case reflect.String:
a, b := aVal.String(), bVal.String()
switch {
case a < b:
return -1
case a > b:
return 1
default:
return 0
}
return cmp.Compare(aVal.String(), bVal.String())
case reflect.Float32, reflect.Float64:
return floatCompare(aVal.Float(), bVal.Float())
return cmp.Compare(aVal.Float(), bVal.Float())
case reflect.Complex64, reflect.Complex128:
a, b := aVal.Complex(), bVal.Complex()
if c := floatCompare(real(a), real(b)); c != 0 {
if c := cmp.Compare(real(a), real(b)); c != 0 {
return c
}
return floatCompare(imag(a), imag(b))
return cmp.Compare(imag(a), imag(b))
case reflect.Bool:
a, b := aVal.Bool(), bVal.Bool()
switch {
Expand All @@ -130,28 +100,12 @@ func compare(aVal, bVal reflect.Value) int {
return -1
}
case reflect.Pointer, reflect.UnsafePointer:
a, b := aVal.Pointer(), bVal.Pointer()
switch {
case a < b:
return -1
case a > b:
return 1
default:
return 0
}
return cmp.Compare(aVal.Pointer(), bVal.Pointer())
case reflect.Chan:
if c, ok := nilCompare(aVal, bVal); ok {
return c
}
ap, bp := aVal.Pointer(), bVal.Pointer()
switch {
case ap < bp:
return -1
case ap > bp:
return 1
default:
return 0
}
return cmp.Compare(aVal.Pointer(), bVal.Pointer())
case reflect.Struct:
for i := 0; i < aVal.NumField(); i++ {
if c := compare(aVal.Field(i), bVal.Field(i)); c != 0 {
Expand Down Expand Up @@ -198,22 +152,3 @@ func nilCompare(aVal, bVal reflect.Value) (int, bool) {
}
return 0, false
}

// floatCompare compares two floating-point values. NaNs compare low.
func floatCompare(a, b float64) int {
switch {
case isNaN(a):
return -1 // No good answer if b is a NaN so don't bother checking.
case isNaN(b):
return 1
case a < b:
return -1
case a > b:
return 1
}
return 0
}

func isNaN(a float64) bool {
return a != a
}
17 changes: 7 additions & 10 deletions tpl/internal/go_templates/fmtsort/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
package fmtsort_test

import (
"cmp"
"fmt"
"github.com/gohugoio/hugo/tpl/internal/go_templates/fmtsort"
"math"
"reflect"
"runtime"
"sort"
"slices"
"strings"
"testing"
"unsafe"
Expand Down Expand Up @@ -67,10 +68,6 @@ func TestCompare(t *testing.T) {
switch {
case i == j:
expect = 0
// NaNs are tricky.
if typ := v0.Type(); (typ.Kind() == reflect.Float32 || typ.Kind() == reflect.Float64) && math.IsNaN(v0.Float()) {
expect = -1
}
case i < j:
expect = -1
case i > j:
Expand Down Expand Up @@ -142,13 +139,13 @@ func sprint(data any) string {
return "nil"
}
b := new(strings.Builder)
for i, key := range om.Key {
for i, m := range om {
if i > 0 {
b.WriteRune(' ')
}
b.WriteString(sprintKey(key))
b.WriteString(sprintKey(m.Key))
b.WriteRune(':')
fmt.Fprint(b, om.Value[i])
fmt.Fprint(b, m.Value)
}
return b.String()
}
Expand Down Expand Up @@ -200,8 +197,8 @@ func makeChans() []chan int {
for i := range cs {
pin.Pin(reflect.ValueOf(cs[i]).UnsafePointer())
}
sort.Slice(cs, func(i, j int) bool {
return uintptr(reflect.ValueOf(cs[i]).UnsafePointer()) < uintptr(reflect.ValueOf(cs[j]).UnsafePointer())
slices.SortFunc(cs, func(a, b chan int) int {
return cmp.Compare(reflect.ValueOf(a).Pointer(), reflect.ValueOf(b).Pointer())
})
return cs
}
Expand Down
5 changes: 2 additions & 3 deletions tpl/internal/go_templates/htmltemplate/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const (

// indirect returns the value, after dereferencing as many times
// as necessary to reach the base type (or nil).
// Signature modified by Hugo. TODO(bep) script this.
func doIndirect(a any) any {
if a == nil {
return nil
Expand All @@ -46,8 +45,8 @@ func doIndirect(a any) any {
}

var (
errorType = reflect.TypeOf((*error)(nil)).Elem()
fmtStringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
errorType = reflect.TypeFor[error]()
fmtStringerType = reflect.TypeFor[fmt.Stringer]()
)

// indirectToStringerOrError returns the value, after dereferencing as many times
Expand Down
10 changes: 4 additions & 6 deletions tpl/internal/go_templates/htmltemplate/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,9 @@ Least Surprise Property:
knows that contextual autoescaping happens should be able to look at a {{.}}
and correctly infer what sanitization happens."
As a consequence of the Least Surprise Property, template actions within an
ECMAScript 6 template literal are disabled by default.
Handling string interpolation within these literals is rather complex resulting
in no clear safe way to support it.
To re-enable template actions within ECMAScript 6 template literals, use the
GODEBUG=jstmpllitinterp=1 environment variable.
Previously, ECMAScript 6 template literal were disabled by default, and could be
enabled with the GODEBUG=jstmpllitinterp=1 environment variable. Template
literals are now supported by default, and setting jstmpllitinterp has no
effect.
*/
package template
3 changes: 0 additions & 3 deletions tpl/internal/go_templates/htmltemplate/examplefiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build go1.13
// +build go1.13

package template_test

import (
Expand Down
Loading

0 comments on commit 2168c5b

Please sign in to comment.