Use the -mod argument from the build when calling go list

This commit is contained in:
Sauyon Lee
2020-06-11 03:51:32 -07:00
parent 296d2d5fd3
commit 7863bb656e
2 changed files with 39 additions and 11 deletions

View File

@@ -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
}

View File

@@ -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=<mod mode>`, 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=<mod mode>`, 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)