default:
// The argument is a package or module path.
- if pkgs := modload.TargetPackages(path); len(pkgs) != 0 {
- // The path is in the main module. Nothing to query.
- if vers != "upgrade" && vers != "patch" {
- base.Errorf("go get %s: can't request explicit version of path in main module", arg)
+ if modload.HasModRoot() {
+ if pkgs := modload.TargetPackages(path); len(pkgs) != 0 {
+ // The path is in the main module. Nothing to query.
+ if vers != "upgrade" && vers != "patch" {
+ base.Errorf("go get %s: can't request explicit version of path in main module", arg)
+ }
+ continue
}
- continue
}
first := path
// matching pattern, which may be relative to the working directory, under all
// build tag settings.
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})
}
! stdout .
! stderr .
-# 'go mod why' should report that nothing is a dependency.
-go mod why -m example.com/version
-stdout 'does not need'
-
+# 'go mod why' should fail, since there is no main module to depend on anything.
+! go mod why -m example.com/version
+stderr 'cannot find main module'
# 'go mod edit', 'go mod tidy', and 'go mod fmt' should fail:
# there is no go.mod file to edit.