Commit 696c4148 authored by Constantin Konstantinidis's avatar Constantin Konstantinidis Committed by Bryan C. Mills

cmd/go: error out of 'go mod download' if the main module is passed as argument

Test added.

Fixes #28338

Change-Id: Iab72ba5646360ae91671261161d8fda451f7a717
Reviewed-on: https://go-review.googlesource.com/c/go/+/189797
Run-TryBot: Bryan C. Mills <bcmills@google.com>
Reviewed-by: default avatarBryan C. Mills <bcmills@google.com>
parent b3885dbc
...@@ -81,6 +81,17 @@ func runDownload(cmd *base.Command, args []string) { ...@@ -81,6 +81,17 @@ func runDownload(cmd *base.Command, args []string) {
} }
if len(args) == 0 { if len(args) == 0 {
args = []string{"all"} args = []string{"all"}
} else if modload.HasModRoot() {
modload.InitMod() // to fill Target
targetAtLatest := modload.Target.Path + "@latest"
targetAtUpgrade := modload.Target.Path + "@upgrade"
targetAtPatch := modload.Target.Path + "@patch"
for _, arg := range args {
switch arg {
case modload.Target.Path, targetAtLatest, targetAtUpgrade, targetAtPatch:
os.Stderr.WriteString("go mod download: skipping argument "+ arg + " that resolves to the main module\n")
}
}
} }
var mods []*moduleJSON var mods []*moduleJSON
...@@ -91,8 +102,9 @@ func runDownload(cmd *base.Command, args []string) { ...@@ -91,8 +102,9 @@ func runDownload(cmd *base.Command, args []string) {
if info.Replace != nil { if info.Replace != nil {
info = info.Replace info = info.Replace
} }
if info.Version == "" && info.Error == nil { if (module.Version{Path: info.Path, Version: info.Version} == modload.Target) {
// main module // skipping main module.
// go mod download without dependencies is silent.
continue continue
} }
m := &moduleJSON{ m := &moduleJSON{
......
...@@ -93,6 +93,12 @@ stderr '^rsc.io/quote@v1.999.999: reading .*/v1.999.999.info: 404 Not Found$' ...@@ -93,6 +93,12 @@ stderr '^rsc.io/quote@v1.999.999: reading .*/v1.999.999.info: 404 Not Found$'
! go mod download -json bad/path ! go mod download -json bad/path
stdout '^\t"Error": "module bad/path: not a known dependency"' stdout '^\t"Error": "module bad/path: not a known dependency"'
# download main module returns an error
go mod download m
stderr '^go mod download: skipping argument m that resolves to the main module\n'
go mod download m@latest
stderr '^go mod download: skipping argument m@latest that resolves to the main module\n'
# allow go mod download without go.mod # allow go mod download without go.mod
env GO111MODULE=auto env GO111MODULE=auto
rm go.mod rm go.mod
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment