diff --git a/extractor/extractor.go b/extractor/extractor.go index 339c51a3583..6a2c994d2cb 100644 --- a/extractor/extractor.go +++ b/extractor/extractor.go @@ -42,6 +42,13 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error { } pkgs, err := packages.Load(cfg, patterns...) + var modFlag string + for _, flag := range buildFlags { + if strings.HasPrefix(flag, "-mod=") { + modFlag = flag + } + } + if err != nil { return err } @@ -59,8 +66,8 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error { // root directories of packages that we want to extract wantedRoots := make(map[string]bool) for _, pkg := range pkgs { - mdir := util.GetModDir(pkg.PkgPath) - pdir := util.GetPkgDir(pkg.PkgPath) + mdir := util.GetModDir(pkg.PkgPath, modFlag) + pdir := util.GetPkgDir(pkg.PkgPath, modFlag) if mdir == "" { mdir = pdir } @@ -79,8 +86,8 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error { return true }, func(pkg *packages.Package) { if _, ok := pkgRoots[pkg.PkgPath]; !ok { - mdir := util.GetModDir(pkg.PkgPath) - pdir := util.GetPkgDir(pkg.PkgPath) + mdir := util.GetModDir(pkg.PkgPath, modFlag) + pdir := util.GetPkgDir(pkg.PkgPath, modFlag) if mdir == "" { mdir = pdir } diff --git a/extractor/util/util.go b/extractor/util/util.go index 21a7d4ef25c..a4f44afd8c0 100644 --- a/extractor/util/util.go +++ b/extractor/util/util.go @@ -25,9 +25,17 @@ func Getenv(key string, aliases ...string) string { return "" } -// GetModDir gets directory of the module containing the package with path `pkgpath`. -func GetModDir(pkgpath string) string { - mod, err := exec.Command("go", "list", "-e", "-f", "{{.Module}}", pkgpath).Output() +// GetModDir gets directory of the module containing the package with path `pkgpath`. It passes the +// `go list` command `modflag`, which should be of the form `-mod=`, as described by `go +// help modules`. +func GetModDir(pkgpath string, modflag string) string { + var cmd *exec.Cmd + if modflag != "" { + cmd = exec.Command("go", "list", "-e", "-f", "{{.Module}}", modflag, pkgpath) + } else { + cmd = exec.Command("go", "list", "-e", "-f", "{{.Module}}", pkgpath) + } + mod, err := cmd.Output() if err != nil { if err, ok := err.(*exec.ExitError); ok { log.Printf("Warning: go list command failed, output below:\n%s%s", mod, err.Stderr) @@ -43,7 +51,12 @@ func GetModDir(pkgpath string) string { return "" } - modDir, err := exec.Command("go", "list", "-e", "-f", "{{.Module.Dir}}", pkgpath).Output() + if modflag != "" { + cmd = exec.Command("go", "list", "-e", "-f", "{{.Module.Dir}}", modflag, pkgpath) + } else { + cmd = exec.Command("go", "list", "-e", "-f", "{{.Module.Dir}}", pkgpath) + } + modDir, err := cmd.Output() if err != nil { if err, ok := err.(*exec.ExitError); ok { log.Printf("Warning: go list command failed, output below:\n%s%s", modDir, err.Stderr) @@ -62,9 +75,17 @@ func GetModDir(pkgpath string) string { return abs } -// GetPkgDir gets directory containing the package with path `pkgpath`. -func GetPkgDir(pkgpath string) string { - pkgDir, err := exec.Command("go", "list", "-e", "-f", "{{.Dir}}", pkgpath).Output() +// GetPkgDir gets directory containing the package with path `pkgpath`. It passes the `go list` +// command `modflag`, which should be of the form `-mod=`, as described by `go help +// modules`. +func GetPkgDir(pkgpath string, modflag string) string { + var cmd *exec.Cmd + if modflag != "" { + cmd = exec.Command("go", "list", "-e", "-f", "{{.Dir}}", modflag, pkgpath) + } else { + cmd = exec.Command("go", "list", "-e", "-f", "{{.Dir}}", pkgpath) + } + pkgDir, err := cmd.Output() if err != nil { if err, ok := err.(*exec.ExitError); ok { log.Printf("Warning: go list command failed, output below:\n%s%s", pkgDir, err.Stderr)