From f922cc64463939d5561abf5f6e30291b4c034892 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Thu, 24 Oct 2019 11:29:33 -0400 Subject: [PATCH] cmd/go: correct GoMod field in 'go list' for replacements that lack an explicit go.mod file Change-Id: I241a3bbaf9c4b779b74146232d2f144bb46a0dc7 Reviewed-on: https://go-review.googlesource.com/c/go/+/203178 Run-TryBot: Bryan C. Mills TryBot-Result: Gobot Gobot Reviewed-by: Jay Conrod --- src/cmd/go/internal/modload/build.go | 3 +- .../mod/not-rsc.io_quote_v0.1.0-nomod.txt | 59 +++++++++++++++++++ src/cmd/go/testdata/script/mod_replace.txt | 7 +++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/cmd/go/testdata/mod/not-rsc.io_quote_v0.1.0-nomod.txt diff --git a/src/cmd/go/internal/modload/build.go b/src/cmd/go/internal/modload/build.go index f31f60a448..acbebb6d66 100644 --- a/src/cmd/go/internal/modload/build.go +++ b/src/cmd/go/internal/modload/build.go @@ -179,11 +179,12 @@ func moduleInfo(m module.Version, fromBuildList bool) *modinfo.ModulePublic { } else { info.Replace.Dir = filepath.Join(ModRoot(), r.Path) } + info.Replace.GoMod = filepath.Join(info.Replace.Dir, "go.mod") } if cfg.BuildMod != "vendor" { completeFromModCache(info.Replace) info.Dir = info.Replace.Dir - info.GoMod = filepath.Join(info.Dir, "go.mod") + info.GoMod = info.Replace.GoMod } return info } diff --git a/src/cmd/go/testdata/mod/not-rsc.io_quote_v0.1.0-nomod.txt b/src/cmd/go/testdata/mod/not-rsc.io_quote_v0.1.0-nomod.txt new file mode 100644 index 0000000000..efff08826a --- /dev/null +++ b/src/cmd/go/testdata/mod/not-rsc.io_quote_v0.1.0-nomod.txt @@ -0,0 +1,59 @@ +Constructed by hand. +(derived from rsc.io/quote@e7a685a342, but without an explicit go.mod file.) + +-- .mod -- +module "not-rsc.io/quote" +-- .info -- +{"Version":"v0.1.0-nomod","Time":"2018-02-14T00:51:33Z"} +-- quote.go -- +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package quote collects pithy sayings. +package quote // import "rsc.io/quote" + +// Hello returns a greeting. +func Hello() string { + return "Hello, world." +} + +// Glass returns a useful phrase for world travelers. +func Glass() string { + // See http://www.oocities.org/nodotus/hbglass.html. + return "I can eat glass and it doesn't hurt me." +} + +// Go returns a Go proverb. +func Go() string { + return "Don't communicate by sharing memory, share memory by communicating." +} +-- quote_test.go -- +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package quote + +import "testing" + +func TestHello(t *testing.T) { + hello := "Hello, world." + if out := Hello(); out != hello { + t.Errorf("Hello() = %q, want %q", out, hello) + } +} + +func TestGlass(t *testing.T) { + glass := "I can eat glass and it doesn't hurt me." + if out := Glass(); out != glass { + t.Errorf("Glass() = %q, want %q", out, glass) + } +} + +func TestGo(t *testing.T) { + go1 := "Don't communicate by sharing memory. Share memory by communicating." + if out := Go(); out != go1 { + t.Errorf("Go() = %q, want %q", out, go1) + } +} diff --git a/src/cmd/go/testdata/script/mod_replace.txt b/src/cmd/go/testdata/script/mod_replace.txt index e4301b50d0..c21f172002 100644 --- a/src/cmd/go/testdata/script/mod_replace.txt +++ b/src/cmd/go/testdata/script/mod_replace.txt @@ -45,6 +45,13 @@ go mod edit -replace=rsc.io/quote/v3=./local/rsc.io/quote/v3 ! go get -d rsc.io/quote/v3/missing-package stderr 'module rsc.io/quote/v3@upgrade found \(v3.0.0, replaced by ./local/rsc.io/quote/v3\), but does not contain package' +# The reported Dir and GoMod for a replaced module should be accurate. +cp go.mod.orig go.mod +go mod edit -replace=rsc.io/quote/v3=not-rsc.io/quote@v0.1.0-nomod +go mod download +go list -m -f '{{.Path}} {{.Version}} {{.Dir}} {{.GoMod}}{{with .Replace}} => {{.Path}} {{.Version}} {{.Dir}} {{.GoMod}}{{end}}' rsc.io/quote/v3 +stdout '^rsc.io/quote/v3 v3.0.0 '$GOPATH'[/\\]pkg[/\\]mod[/\\]not-rsc.io[/\\]quote@v0.1.0-nomod '$GOPATH'[/\\]pkg[/\\]mod[/\\]cache[/\\]download[/\\]not-rsc.io[/\\]quote[/\\]@v[/\\]v0.1.0-nomod.mod => not-rsc.io/quote v0.1.0-nomod '$GOPATH'[/\\]pkg[/\\]mod[/\\]not-rsc.io[/\\]quote@v0.1.0-nomod '$GOPATH'[/\\]pkg[/\\]mod[/\\]cache[/\\]download[/\\]not-rsc.io[/\\]quote[/\\]@v[/\\]v0.1.0-nomod.mod$' + -- go.mod -- module quoter