From 87fa24075eed4f11982c564b456bdf074a8f3eee Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Thu, 11 Jul 2019 10:28:17 +0100 Subject: [PATCH] Strip out an directory-based replacements from main pkg's module Per much previous discussion, we do apply replace directives in the main package's module. But for directory replacements this is not guaranteed to make sense. Indeed it almost certainly doesn't make sense most of the time. Therefore, strip out these replace directives, leaving behind the versioned, non-directory replaces. --- main.go | 50 +++++++++++++++++++ ...s_simple-main-directory-replace_v1.0.0.txt | 27 ++++++++++ testdata/replace_directory.txt | 7 +++ 3 files changed, 84 insertions(+) create mode 100644 testdata/mod/github.com_gobin-testrepos_simple-main-directory-replace_v1.0.0.txt create mode 100644 testdata/replace_directory.txt diff --git a/main.go b/main.go index 488680b..29e2763 100644 --- a/main.go +++ b/main.go @@ -474,6 +474,19 @@ type listPkg struct { } } +type modEditModule struct { + Path string + Version string +} + +type modEdit struct { + Module modEditModule + Replace []struct { + Old modEditModule + New modEditModule + } +} + // arg is a wrapper around a command line-provided package type arg struct { patt string // the command line-provided pattern @@ -610,6 +623,43 @@ func (a *arg) list(proxy string) error { return err } + // now we need to drop all the replacements for which the RHS value does + // not include a version... because these are directory replacements + { + var out bytes.Buffer + gmeCmd := goCommand("mod", "edit", "-json") + gmeCmd.Dir = a.wd + gmeCmd.Stdout = &out + gmeCmd.Env = buildEnv("") + if err := gmeCmd.run(); err != nil { + return err + } + var mod modEdit + if err := json.Unmarshal(out.Bytes(), &mod); err != nil { + return fmt.Errorf("failed to process output of %v: %v\n%s", strings.Join(gmeCmd.Args, " "), err, out.Bytes()) + } + var todrop []string + for _, r := range mod.Replace { + if r.New.Version != "" { + continue + } + drop := r.Old.Path + if r.Old.Version != "" { + drop += "@" + r.Old.Version + } + todrop = append(todrop, "-dropreplace="+drop) + } + if len(todrop) > 0 { + gmeCmd := goCommand("mod", "edit") + gmeCmd.Args = append(gmeCmd.Args, todrop...) + gmeCmd.Dir = a.wd + gmeCmd.Env = buildEnv("") + if err := gmeCmd.run(); err != nil { + return err + } + } + } + // now that we effectively have a copy of everything relevant in the // target module (including replace directives), list to ensure they // have been resolved diff --git a/testdata/mod/github.com_gobin-testrepos_simple-main-directory-replace_v1.0.0.txt b/testdata/mod/github.com_gobin-testrepos_simple-main-directory-replace_v1.0.0.txt new file mode 100644 index 0000000..3a44f84 --- /dev/null +++ b/testdata/mod/github.com_gobin-testrepos_simple-main-directory-replace_v1.0.0.txt @@ -0,0 +1,27 @@ +-- .mod -- +module github.com/gobin-testrepos/simple-main-directory-replace + +require github.com/gobin-testrepos/food v1.0.0 + +replace github.com/gobin-testrepos/food => /path/to/nowhere +-- .info -- +{"Version":"v1.0.0","Time":"2018-10-22T18:45:39Z"} + +-- go.mod -- +module github.com/gobin-testrepos/simple-main-directory-replace + +require github.com/gobin-testrepos/food v1.0.0 + +replace github.com/gobin-testrepos/food => /path/to/nowhere + +-- main.go -- +package main + +import "fmt" + +import "github.com/gobin-testrepos/food" + +func main() { + fmt.Println("Simple module-based main v1.0.0") + fmt.Printf("Today we will eat %v\n", food.MainCourse) +} diff --git a/testdata/replace_directory.txt b/testdata/replace_directory.txt new file mode 100644 index 0000000..250892b --- /dev/null +++ b/testdata/replace_directory.txt @@ -0,0 +1,7 @@ +# Verify that directory-target replace statements in the main package's module +# are ignored. + +env HOME=$WORK/home +gobin -run github.com/gobin-testrepos/simple-main-directory-replace@v1.0.0 +stdout '^Simple module-based main v1.0.0$' +stdout '^Today we will eat fish$'