From aae3e739f812da07fccd39097401afed9a34127d Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 18 Oct 2024 15:36:25 +0900 Subject: [PATCH] internal/debug: use go/build's Context to get the source directory --- internal/debug/caller.go | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/internal/debug/caller.go b/internal/debug/caller.go index edc9b6441..22bd120e7 100644 --- a/internal/debug/caller.go +++ b/internal/debug/caller.go @@ -15,11 +15,7 @@ package debug import ( - "bytes" - "errors" - "fmt" - "os" - "os/exec" + "go/build" "path" "path/filepath" "runtime" @@ -43,22 +39,13 @@ const ( // FirstCaller returns the file and line number of the first caller outside of Ebitengine. func FirstCaller() (file string, line int, callerType CallerType) { ebitengineFileDirOnce.Do(func() { - var stderr bytes.Buffer - cmd := exec.Command("go", "list", "-f", "{{.Dir}}", "github.com/hajimehoshi/ebiten/v2") - cmd.Stderr = &stderr - out, err := cmd.Output() - if errors.Is(err, exec.ErrNotFound) { - return - } + pkg, err := build.Default.Import("github.com/hajimehoshi/ebiten/v2", "", build.FindOnly) if err != nil { - fmt.Fprintf(os.Stderr, "debug: go list -f {{.Dir}} failed: %v\n", err) - fmt.Fprintf(os.Stderr, "%s\n", stderr.String()) return } - ebitengineFileDir = filepath.ToSlash(strings.TrimSpace(string(out))) + ebitengineFileDir = filepath.ToSlash(pkg.Dir) }) - // Go command is not found. if ebitengineFileDir == "" { return "", 0, CallerTypeNone }