-
-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: infinite loop if go.mod not found, add warning on missing templ …
- Loading branch information
Showing
13 changed files
with
560 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.2.529 | ||
0.2.532 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package modcheck | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
|
||
"github.com/a-h/templ" | ||
"golang.org/x/mod/modfile" | ||
"golang.org/x/mod/semver" | ||
) | ||
|
||
// WalkUp the directory tree, starting at dir, until we find a directory containing | ||
// a go.mod file. | ||
func WalkUp(dir string) (string, error) { | ||
dir, err := filepath.Abs(dir) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to get absolute path: %w", err) | ||
} | ||
|
||
var modFile string | ||
for { | ||
modFile = filepath.Join(dir, "go.mod") | ||
_, err := os.Stat(modFile) | ||
if err != nil && !os.IsNotExist(err) { | ||
return "", fmt.Errorf("failed to stat go.mod file: %w", err) | ||
} | ||
if os.IsNotExist(err) { | ||
// Move up. | ||
prev := dir | ||
dir = filepath.Dir(dir) | ||
if dir == prev { | ||
break | ||
} | ||
continue | ||
} | ||
break | ||
} | ||
|
||
// No file found. | ||
if modFile == "" { | ||
return dir, fmt.Errorf("could not find go.mod file") | ||
} | ||
return dir, nil | ||
} | ||
|
||
// 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`) | ||
|
||
func patchGoVersion(moduleFileContents []byte) []byte { | ||
return goVersionRegexp.ReplaceAll(moduleFileContents, []byte("\ngo $1\n")) | ||
} | ||
|
||
func Check(dir string) error { | ||
dir, err := WalkUp(dir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Found a go.mod file. | ||
// Read it and find the templ version. | ||
modFile := filepath.Join(dir, "go.mod") | ||
m, err := os.ReadFile(modFile) | ||
if err != nil { | ||
return fmt.Errorf("failed to read go.mod file: %w", err) | ||
} | ||
|
||
// Replace "go 1.21.x" with "go 1.21". | ||
m = patchGoVersion(m) | ||
|
||
mf, err := modfile.Parse(modFile, m, nil) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse go.mod file: %w", err) | ||
} | ||
if mf.Module.Mod.Path == "github.com/a-h/templ" { | ||
// The go.mod file is for templ itself. | ||
return nil | ||
} | ||
for _, r := range mf.Require { | ||
if r.Mod.Path == "github.com/a-h/templ" { | ||
cmp := semver.Compare(r.Mod.Version, templ.Version()) | ||
if cmp < 0 { | ||
return fmt.Errorf("generator %v is newer than templ version %v found in go.mod file, consider running `go get -u github.com/a-h/templ` to upgrade", templ.Version(), r.Mod.Version) | ||
} | ||
if cmp > 0 { | ||
return fmt.Errorf("generator %v is older than templ version %v found in go.mod file, consider upgrading templ CLI", templ.Version(), r.Mod.Version) | ||
} | ||
return nil | ||
} | ||
} | ||
return fmt.Errorf("templ not found in go.mod file, run `go get github.com/a-h/templ to install it`") | ||
} |
2 changes: 1 addition & 1 deletion
2
cmd/templ/generatecmd/main_test.go → ...mpl/generatecmd/modcheck/modcheck_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package generatecmd | ||
package modcheck | ||
|
||
import ( | ||
"testing" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module templ/testproject | ||
|
||
go 1.21 | ||
|
||
require github.com/a-h/templ v0.2.513 // indirect | ||
|
||
replace github.com/a-h/templ => {moduleRoot} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= | ||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= |
Oops, something went wrong.