Skip to content

Commit 696c414

Browse files
iwdgoBryan C. Mills
authored and
Bryan C. Mills
committed
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 <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent b3885db commit 696c414

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/cmd/go/internal/modcmd/download.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ func runDownload(cmd *base.Command, args []string) {
8181
}
8282
if len(args) == 0 {
8383
args = []string{"all"}
84+
} else if modload.HasModRoot() {
85+
modload.InitMod() // to fill Target
86+
targetAtLatest := modload.Target.Path + "@latest"
87+
targetAtUpgrade := modload.Target.Path + "@upgrade"
88+
targetAtPatch := modload.Target.Path + "@patch"
89+
for _, arg := range args {
90+
switch arg {
91+
case modload.Target.Path, targetAtLatest, targetAtUpgrade, targetAtPatch:
92+
os.Stderr.WriteString("go mod download: skipping argument "+ arg + " that resolves to the main module\n")
93+
}
94+
}
8495
}
8596

8697
var mods []*moduleJSON
@@ -91,8 +102,9 @@ func runDownload(cmd *base.Command, args []string) {
91102
if info.Replace != nil {
92103
info = info.Replace
93104
}
94-
if info.Version == "" && info.Error == nil {
95-
// main module
105+
if (module.Version{Path: info.Path, Version: info.Version} == modload.Target) {
106+
// skipping main module.
107+
// go mod download without dependencies is silent.
96108
continue
97109
}
98110
m := &moduleJSON{

src/cmd/go/testdata/script/mod_download.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ stderr '^rsc.io/[email protected]: reading .*/v1.999.999.info: 404 Not Found$'
9393
! go mod download -json bad/path
9494
stdout '^\t"Error": "module bad/path: not a known dependency"'
9595

96+
# download main module returns an error
97+
go mod download m
98+
stderr '^go mod download: skipping argument m that resolves to the main module\n'
99+
go mod download m@latest
100+
stderr '^go mod download: skipping argument m@latest that resolves to the main module\n'
101+
96102
# allow go mod download without go.mod
97103
env GO111MODULE=auto
98104
rm go.mod

0 commit comments

Comments
 (0)