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: Bryan C. Mills <bcmills@google.com>
diff --git a/src/cmd/go/internal/modcmd/download.go b/src/cmd/go/internal/modcmd/download.go
index 1687cc5..6ea18ea 100644
--- a/src/cmd/go/internal/modcmd/download.go
+++ b/src/cmd/go/internal/modcmd/download.go
@@ -81,6 +81,17 @@
 	}
 	if len(args) == 0 {
 		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
@@ -91,8 +102,9 @@
 		if info.Replace != nil {
 			info = info.Replace
 		}
-		if info.Version == "" && info.Error == nil {
-			// main module
+		if (module.Version{Path: info.Path, Version: info.Version} == modload.Target) {
+			// skipping main module.
+			// go mod download without dependencies is silent.
 			continue
 		}
 		m := &moduleJSON{
diff --git a/src/cmd/go/testdata/script/mod_download.txt b/src/cmd/go/testdata/script/mod_download.txt
index 0777913..e341222 100644
--- a/src/cmd/go/testdata/script/mod_download.txt
+++ b/src/cmd/go/testdata/script/mod_download.txt
@@ -93,6 +93,12 @@
 ! go mod download -json bad/path
 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
 env GO111MODULE=auto
 rm go.mod