internal/debug: bug fix: panic when Go command is not found with -tags=ebitenginedebug

This commit is contained in:
Hajime Hoshi 2024-10-15 21:28:38 +09:00
parent 9de408a25c
commit 74765c995e
2 changed files with 9 additions and 2 deletions

View File

@ -15,6 +15,7 @@
package debug
import (
"errors"
"fmt"
"os/exec"
"path"
@ -34,12 +35,20 @@ func FirstCaller() (file string, line int, ok bool) {
ebitengineFileDirOnce.Do(func() {
cmd := exec.Command("go", "list", "-f", "{{.Dir}}", "github.com/hajimehoshi/ebiten/v2")
out, err := cmd.Output()
if errors.Is(err, exec.ErrNotFound) {
return
}
if err != nil {
panic(fmt.Sprintf("debug: go list -f {{.Dir}} failed: %v", err))
}
ebitengineFileDir = filepath.ToSlash(strings.TrimSpace(string(out)))
})
// Go command is not found.
if ebitengineFileDir == "" {
return "", 0, false
}
// Relying on a caller stacktrace is very fragile, but this is fine as this is only for debugging.
var ebitenPackageReached bool
for i := 0; ; i++ {

View File

@ -166,8 +166,6 @@ func (q *commandQueue) EnqueueDrawTrianglesCommand(dst *Image, srcs [graphics.Sh
if debug.IsDebug {
if file, line, ok := debug.FirstCaller(); ok {
c.firstCaller = fmt.Sprintf("%s:%d", file, line)
} else {
c.firstCaller = "(internal)"
}
}
q.commands = append(q.commands, c)