Commit a9c0cc6f authored by Bryan C. Mills's avatar Bryan C. Mills

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 <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarJay Conrod <jayconrod@google.com>
parent 95149147
...@@ -373,6 +373,7 @@ func runGet(cmd *base.Command, args []string) { ...@@ -373,6 +373,7 @@ func runGet(cmd *base.Command, args []string) {
default: default:
// The argument is a package or module path. // The argument is a package or module path.
if modload.HasModRoot() {
if pkgs := modload.TargetPackages(path); len(pkgs) != 0 { if pkgs := modload.TargetPackages(path); len(pkgs) != 0 {
// The path is in the main module. Nothing to query. // The path is in the main module. Nothing to query.
if vers != "upgrade" && vers != "patch" { if vers != "upgrade" && vers != "patch" {
...@@ -380,6 +381,7 @@ func runGet(cmd *base.Command, args []string) { ...@@ -380,6 +381,7 @@ func runGet(cmd *base.Command, args []string) {
} }
continue continue
} }
}
first := path first := path
if i := strings.IndexByte(first, '/'); i >= 0 { if i := strings.IndexByte(first, '/'); i >= 0 {
......
...@@ -406,6 +406,10 @@ func loadAll(testAll bool) []string { ...@@ -406,6 +406,10 @@ func loadAll(testAll bool) []string {
// matching pattern, which may be relative to the working directory, under all // matching pattern, which may be relative to the working directory, under all
// build tag settings. // build tag settings.
func TargetPackages(pattern string) []string { func TargetPackages(pattern string) []string {
// TargetPackages is relative to the main module, so ensure that the main
// module is a thing that can contain packages.
ModRoot()
return matchPackages(pattern, imports.AnyTags(), false, []module.Version{Target}) return matchPackages(pattern, imports.AnyTags(), false, []module.Version{Target})
} }
......
...@@ -67,10 +67,9 @@ go mod graph ...@@ -67,10 +67,9 @@ go mod graph
! stdout . ! stdout .
! stderr . ! stderr .
# 'go mod why' should report that nothing is a dependency. # 'go mod why' should fail, since there is no main module to depend on anything.
go mod why -m example.com/version ! go mod why -m example.com/version
stdout 'does not need' stderr 'cannot find main module'
# 'go mod edit', 'go mod tidy', and 'go mod fmt' should fail: # 'go mod edit', 'go mod tidy', and 'go mod fmt' should fail:
# there is no go.mod file to edit. # there is no go.mod file to edit.
......
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