diff --git a/internal/shaderlister/main.go b/internal/shaderlister/main.go index 191024d86706..f6937c74373c 100644 --- a/internal/shaderlister/main.go +++ b/internal/shaderlister/main.go @@ -25,9 +25,7 @@ import ( "go/types" "log/slog" "os" - "os/exec" "regexp" - "slices" "strings" "golang.org/x/tools/go/ast/inspector" @@ -57,34 +55,28 @@ func xmain() error { flag.Usage() } - pkg := flag.Arg(0) - deps, err := listDeps(pkg) + pkgs, err := packages.Load(&packages.Config{ + Mode: packages.NeedName | packages.NeedTypes | packages.NeedImports | packages.NeedDeps | packages.NeedSyntax | packages.NeedTypesInfo, + }, flag.Args()...) if err != nil { return err } - deps = slices.DeleteFunc(deps, func(name string) bool { + + var shaders []Shader + + packages.Visit(pkgs, func(pkg *packages.Package) bool { + path := pkg.PkgPath // A standard library should not have a directive for shaders. Skip them. - if isStandardImportPath(name) { + if isStandardImportPath(path) { return true } // A semi-standard library should not have a directive for shaders. Skip them. - if strings.HasPrefix(name, "golang.org/x/") { + if strings.HasPrefix(path, "golang.org/x/") { return true } - return false - }) - pkgs, err := packages.Load(&packages.Config{ - Mode: packages.NeedName | packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo, - }, append([]string{pkg}, deps...)...) - if err != nil { - return err - } - - var shaders []Shader - - for _, pkg := range pkgs { shaders = appendShaderSources(shaders, pkg) - } + return true + }, nil) w := bufio.NewWriter(os.Stdout) enc := json.NewEncoder(w) @@ -99,18 +91,6 @@ func xmain() error { return nil } -func listDeps(pkg string) ([]string, error) { - cmd := exec.Command("go", "list", "-f", `{{join .Deps ","}}`, pkg) - out, err := cmd.Output() - if err != nil { - if e, ok := err.(*exec.ExitError); ok { - return nil, fmt.Errorf("go list failed: %w\n%s", e, e.Stderr) - } - return nil, err - } - return strings.Split(strings.TrimSpace(string(out)), ","), nil -} - // isStandardImportPath reports whether $GOROOT/src/path should be considered part of the standard distribution. // // This is based on the implementation in the standard library (cmd/go/internal/search/search.go).