Skip to content

Commit a9c0cc6

Browse files
author
Bryan C. Mills
committed
cmd/go: make 'go mod why' require a main module
Updates #32027 Change-Id: Ifc9427f35188c3fd356917d8510f3e01866ebca8 Reviewed-on: https://go-review.googlesource.com/c/go/+/205065 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 9514914 commit a9c0cc6

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

src/cmd/go/internal/modget/get.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,14 @@ func runGet(cmd *base.Command, args []string) {
373373

374374
default:
375375
// The argument is a package or module path.
376-
if pkgs := modload.TargetPackages(path); len(pkgs) != 0 {
377-
// The path is in the main module. Nothing to query.
378-
if vers != "upgrade" && vers != "patch" {
379-
base.Errorf("go get %s: can't request explicit version of path in main module", arg)
376+
if modload.HasModRoot() {
377+
if pkgs := modload.TargetPackages(path); len(pkgs) != 0 {
378+
// The path is in the main module. Nothing to query.
379+
if vers != "upgrade" && vers != "patch" {
380+
base.Errorf("go get %s: can't request explicit version of path in main module", arg)
381+
}
382+
continue
380383
}
381-
continue
382384
}
383385

384386
first := path

src/cmd/go/internal/modload/load.go

+4
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,10 @@ func loadAll(testAll bool) []string {
406406
// matching pattern, which may be relative to the working directory, under all
407407
// build tag settings.
408408
func TargetPackages(pattern string) []string {
409+
// TargetPackages is relative to the main module, so ensure that the main
410+
// module is a thing that can contain packages.
411+
ModRoot()
412+
409413
return matchPackages(pattern, imports.AnyTags(), false, []module.Version{Target})
410414
}
411415

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,9 @@ go mod graph
6767
! stdout .
6868
! stderr .
6969

70-
# 'go mod why' should report that nothing is a dependency.
71-
go mod why -m example.com/version
72-
stdout 'does not need'
73-
70+
# 'go mod why' should fail, since there is no main module to depend on anything.
71+
! go mod why -m example.com/version
72+
stderr 'cannot find main module'
7473

7574
# 'go mod edit', 'go mod tidy', and 'go mod fmt' should fail:
7675
# there is no go.mod file to edit.

0 commit comments

Comments
 (0)