cmd/go/internal/modfile: fix producing unquoted module path duplicates
The function `strconv.Unquote` will return an error if we pass a
unquoted string, so SetRequire produces duplicates.
Fixes golang/go#25972
Change-Id: I824519a7a2d602babefd0c2c2f8b2a0f72e99f32
Reviewed-on: https://go-review.googlesource.com/120656
Run-TryBot: Baokun Lee <nototon@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
diff --git a/vendor/cmd/go/internal/modfile/rule.go b/vendor/cmd/go/internal/modfile/rule.go
index 3170146..24bae6b 100644
--- a/vendor/cmd/go/internal/modfile/rule.go
+++ b/vendor/cmd/go/internal/modfile/rule.go
@@ -431,7 +431,7 @@
if len(stmt.Token) > 0 && stmt.Token[0] == "require" {
var newLines []*Line
for _, line := range stmt.Line {
- if p, err := strconv.Unquote(line.Token[0]); err == nil && need[p] != "" {
+ if p, err := parseString(&line.Token[0]); err == nil && need[p] != "" {
line.Token[1] = need[p]
delete(need, p)
newLines = append(newLines, line)
@@ -445,7 +445,7 @@
case *Line:
if len(stmt.Token) > 0 && stmt.Token[0] == "require" {
- if p, err := strconv.Unquote(stmt.Token[1]); err == nil && need[p] != "" {
+ if p, err := parseString(&stmt.Token[1]); err == nil && need[p] != "" {
stmt.Token[2] = need[p]
delete(need, p)
} else {
diff --git a/vendor/cmd/go/vgo_test.go b/vendor/cmd/go/vgo_test.go
index df360bf..5fd8469 100644
--- a/vendor/cmd/go/vgo_test.go
+++ b/vendor/cmd/go/vgo_test.go
@@ -693,3 +693,39 @@
tg.cd(tg.path("x"))
tg.run("-vgo", "build")
}
+
+func TestModSyncPrintJson(t *testing.T) {
+ testenv.MustHaveExternalNetwork(t)
+ tg := testgo(t)
+ defer tg.cleanup()
+ tg.makeTempdir()
+
+ tg.setenv("GOPATH", tg.path("."))
+ tg.must(os.MkdirAll(tg.path("x"), 0777))
+ tg.must(ioutil.WriteFile(tg.path("x/main.go"), []byte(`
+ package x
+ import "github.com/gorilla/mux"
+ func main() {
+ _ := mux.NewRouter()
+ }`), 0666))
+ tg.must(ioutil.WriteFile(tg.path("x/go.mod"), []byte("module x"), 0666))
+ tg.cd(tg.path("x"))
+ tg.run("-vgo", "mod", "-sync", "-json")
+ count := tg.grepCountBoth(`"Path": "github.com/gorilla/mux",`)
+ if count != 1 {
+ t.Fatal("produces duplicate imports")
+ }
+ // test quoted module path
+ tg.must(ioutil.WriteFile(tg.path("x/go.mod"), []byte(`
+ module x
+ require (
+ "github.com/gorilla/context" v1.1.1
+ "github.com/gorilla/mux" v1.6.2
+ )`), 0666))
+ tg.run("-vgo", "mod", "-sync", "-json")
+ count = tg.grepCountBoth(`"Path": "github.com/gorilla/mux",`)
+ if count != 1 {
+ t.Fatal("produces duplicate imports")
+ }
+
+}