From 19ad7c914bdc10383081c542775085cebb289492 Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Tue, 9 Jan 2024 19:03:50 +0000 Subject: [PATCH] fix: work around https://github.com/golang/go/issues/61888 --- .version | 2 +- cmd/templ/generatecmd/main.go | 8 +++-- cmd/templ/generatecmd/main_test.go | 56 ++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 cmd/templ/generatecmd/main_test.go diff --git a/.version b/.version index d6ef7d4c0..b3695912a 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -0.2.525 \ No newline at end of file +0.2.528 \ No newline at end of file diff --git a/cmd/templ/generatecmd/main.go b/cmd/templ/generatecmd/main.go index 5c5f53c2f..7ab5fa6b4 100644 --- a/cmd/templ/generatecmd/main.go +++ b/cmd/templ/generatecmd/main.go @@ -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. @@ -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 { diff --git a/cmd/templ/generatecmd/main_test.go b/cmd/templ/generatecmd/main_test.go new file mode 100644 index 000000000..fdae8ec57 --- /dev/null +++ b/cmd/templ/generatecmd/main_test.go @@ -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) + } + }) + } +}