Skip to content

Commit 6bafe28

Browse files
fjlSahil-4555
authored andcommitted
build: module-aware FindMainPackages (ethereum#32736)
This fixes `go run build/ci.go install`. It was failing because we resolved all main packages by parsing sources, which fails when the source directory contains multiple modules.
1 parent fe2402d commit 6bafe28

File tree

2 files changed

+11
-23
lines changed

2 files changed

+11
-23
lines changed

build/ci.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func doInstall(cmdline []string) {
221221
// Default: collect all 'main' packages in cmd/ and build those.
222222
packages := flag.Args()
223223
if len(packages) == 0 {
224-
packages = build.FindMainPackages("./cmd")
224+
packages = build.FindMainPackages(&tc, "./cmd/...")
225225
}
226226

227227
// Do the build!

internal/build/util.go

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import (
2121
"bytes"
2222
"flag"
2323
"fmt"
24-
"go/parser"
25-
"go/token"
2624
"io"
2725
"log"
2826
"os"
@@ -219,28 +217,18 @@ func UploadSFTP(identityFile, host, dir string, files []string) error {
219217

220218
// FindMainPackages finds all 'main' packages in the given directory and returns their
221219
// package paths.
222-
func FindMainPackages(dir string) []string {
223-
var commands []string
224-
cmds, err := os.ReadDir(dir)
220+
func FindMainPackages(tc *GoToolchain, pattern string) []string {
221+
list := tc.Go("list", "-f", `{{if eq .Name "main"}}{{.ImportPath}}{{end}}`, pattern)
222+
output, err := list.Output()
225223
if err != nil {
226-
log.Fatal(err)
224+
log.Fatal("go list failed:", err)
227225
}
228-
for _, cmd := range cmds {
229-
pkgdir := filepath.Join(dir, cmd.Name())
230-
if !cmd.IsDir() {
231-
continue
232-
}
233-
pkgs, err := parser.ParseDir(token.NewFileSet(), pkgdir, nil, parser.PackageClauseOnly)
234-
if err != nil {
235-
log.Fatal(err)
236-
}
237-
for name := range pkgs {
238-
if name == "main" {
239-
path := "./" + filepath.ToSlash(pkgdir)
240-
commands = append(commands, path)
241-
break
242-
}
226+
var result []string
227+
for l := range bytes.Lines(output) {
228+
l = bytes.TrimSpace(l)
229+
if len(l) > 0 {
230+
result = append(result, string(l))
243231
}
244232
}
245-
return commands
233+
return result
246234
}

0 commit comments

Comments
 (0)