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

Commit 87fa240

Browse files
committed
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.
1 parent ef4e0fa commit 87fa240

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

main.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,19 @@ type listPkg struct {
474474
}
475475
}
476476

477+
type modEditModule struct {
478+
Path string
479+
Version string
480+
}
481+
482+
type modEdit struct {
483+
Module modEditModule
484+
Replace []struct {
485+
Old modEditModule
486+
New modEditModule
487+
}
488+
}
489+
477490
// arg is a wrapper around a command line-provided package
478491
type arg struct {
479492
patt string // the command line-provided pattern
@@ -610,6 +623,43 @@ func (a *arg) list(proxy string) error {
610623
return err
611624
}
612625

626+
// now we need to drop all the replacements for which the RHS value does
627+
// not include a version... because these are directory replacements
628+
{
629+
var out bytes.Buffer
630+
gmeCmd := goCommand("mod", "edit", "-json")
631+
gmeCmd.Dir = a.wd
632+
gmeCmd.Stdout = &out
633+
gmeCmd.Env = buildEnv("")
634+
if err := gmeCmd.run(); err != nil {
635+
return err
636+
}
637+
var mod modEdit
638+
if err := json.Unmarshal(out.Bytes(), &mod); err != nil {
639+
return fmt.Errorf("failed to process output of %v: %v\n%s", strings.Join(gmeCmd.Args, " "), err, out.Bytes())
640+
}
641+
var todrop []string
642+
for _, r := range mod.Replace {
643+
if r.New.Version != "" {
644+
continue
645+
}
646+
drop := r.Old.Path
647+
if r.Old.Version != "" {
648+
drop += "@" + r.Old.Version
649+
}
650+
todrop = append(todrop, "-dropreplace="+drop)
651+
}
652+
if len(todrop) > 0 {
653+
gmeCmd := goCommand("mod", "edit")
654+
gmeCmd.Args = append(gmeCmd.Args, todrop...)
655+
gmeCmd.Dir = a.wd
656+
gmeCmd.Env = buildEnv("")
657+
if err := gmeCmd.run(); err != nil {
658+
return err
659+
}
660+
}
661+
}
662+
613663
// now that we effectively have a copy of everything relevant in the
614664
// target module (including replace directives), list to ensure they
615665
// have been resolved
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
-- .mod --
2+
module github.com/gobin-testrepos/simple-main-directory-replace
3+
4+
require github.com/gobin-testrepos/food v1.0.0
5+
6+
replace github.com/gobin-testrepos/food => /path/to/nowhere
7+
-- .info --
8+
{"Version":"v1.0.0","Time":"2018-10-22T18:45:39Z"}
9+
10+
-- go.mod --
11+
module github.com/gobin-testrepos/simple-main-directory-replace
12+
13+
require github.com/gobin-testrepos/food v1.0.0
14+
15+
replace github.com/gobin-testrepos/food => /path/to/nowhere
16+
17+
-- main.go --
18+
package main
19+
20+
import "fmt"
21+
22+
import "github.com/gobin-testrepos/food"
23+
24+
func main() {
25+
fmt.Println("Simple module-based main v1.0.0")
26+
fmt.Printf("Today we will eat %v\n", food.MainCourse)
27+
}

testdata/replace_directory.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Verify that directory-target replace statements in the main package's module
2+
# are ignored.
3+
4+
env HOME=$WORK/home
5+
gobin -run github.com/gobin-testrepos/[email protected]
6+
stdout '^Simple module-based main v1.0.0$'
7+
stdout '^Today we will eat fish$'

0 commit comments

Comments
 (0)