Skip to content
This repository has been archived by the owner on Mar 23, 2021. It is now read-only.

Commit

Permalink
Strip out an directory-based replacements from main pkg's module (#78)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
myitcv committed Jul 29, 2019
1 parent ef4e0fa commit 350ab10
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
50 changes: 50 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 => /road/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 => /road/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)
}
7 changes: 7 additions & 0 deletions testdata/replace_directory.txt
Original file line number Diff line number Diff line change
@@ -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$'

0 comments on commit 350ab10

Please sign in to comment.