Skip to content

Commit

Permalink
fix: work around golang/go#61888
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed Jan 9, 2024
1 parent 239aa8b commit 19ad7c9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.525
0.2.528
8 changes: 6 additions & 2 deletions cmd/templ/generatecmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,11 @@ func logWithDecoration(w io.Writer, decoration string, col color.Attribute, form
}

// Replace "go 1.21.3" with "go 1.21" until https://github.com/golang/go/issues/61888 is fixed, see templ issue https://github.com/a-h/templ/issues/355
var goVersionRegexp = regexp.MustCompile(`\ngo (\d+\.\d+)\.\d+\n`)
var goVersionRegexp = regexp.MustCompile(`\ngo (\d+\.\d+)(?:\D.+)\n`)

func patchGoVersion(moduleFileContents []byte) []byte {
return goVersionRegexp.ReplaceAll(moduleFileContents, []byte("\ngo $1\n"))
}

func checkTemplVersion(dir string) error {
// Walk up the directory tree, starting at dir, until we find a go.mod file.
Expand Down Expand Up @@ -556,7 +560,7 @@ func checkTemplVersion(dir string) error {
}

// Replace "go 1.21.x" with "go 1.21".
m = goVersionRegexp.ReplaceAll(m, []byte("\ngo $1\n"))
m = patchGoVersion(m)

mf, err := modfile.Parse(current, m, nil)
if err != nil {
Expand Down
56 changes: 56 additions & 0 deletions cmd/templ/generatecmd/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package generatecmd

import (
"testing"

"golang.org/x/mod/modfile"
)

func TestPatchGoVersion(t *testing.T) {
tests := []struct {
input string
expected string
}{
{
input: "go 1.20",
expected: "1.20",
},
{
input: "go 1.20.123",
expected: "1.20",
},
{
input: "go 1.20.1",
expected: "1.20",
},
{
input: "go 1.20rc1",
expected: "1.20",
},
{
input: "go 1.15",
expected: "1.15",
},
{
input: "go 1.15-something-something",
expected: "1.15",
},
{
input: "go 1.23.23.23",
expected: "1.23",
},
}
for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
input := "module github.com/a-h/templ\n\n" + string(test.input) + "\n"
actual := patchGoVersion([]byte(input))
mf, err := modfile.Parse("go.mod", actual, nil)
if err != nil {
t.Errorf("failed to parse go.mod: %v", err)
}
if test.expected != mf.Go.Version {
t.Errorf("expected %q, got %q", test.expected, mf.Go.Version)
}
})
}
}

0 comments on commit 19ad7c9

Please sign in to comment.