Skip to content

Commit ea6cb02

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
cmd/go: propagate match errors in 'go run'
Fixes #51604. Change-Id: I3bc86652c62d2b329d9c2db5ea443d56cf17f8d1 Reviewed-on: https://go-review.googlesource.com/c/go/+/418094 Reviewed-by: Nooras Saba‎ <saba@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Run-TryBot: Bryan Mills <bcmills@google.com> Auto-Submit: Bryan Mills <bcmills@google.com>
1 parent bd1bff4 commit ea6cb02

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

src/cmd/go/internal/load/pkg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2915,7 +2915,7 @@ func mainPackagesOnly(pkgs []*Package, matches []*search.Match) []*Package {
29152915

29162916
var mains []*Package
29172917
for _, pkg := range pkgs {
2918-
if pkg.Name == "main" {
2918+
if pkg.Name == "main" || (pkg.Name == "" && pkg.Error != nil) {
29192919
treatAsMain[pkg.ImportPath] = true
29202920
mains = append(mains, pkg)
29212921
continue

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ func resolveLocalPackage(ctx context.Context, dir string, rs *Requirements) (str
607607
if pkg == "" {
608608
if inWorkspaceMode() {
609609
if mr := findModuleRoot(absDir); mr != "" {
610-
return "", fmt.Errorf("directory %s is contained in a module that is not one of the workspace modules listed in go.work. You can add the module to the workspace using go work use %s", base.ShortPath(absDir), base.ShortPath(mr))
610+
return "", fmt.Errorf("directory %s is contained in a module that is not one of the workspace modules listed in go.work. You can add the module to the workspace using:\n\tgo work use %s", base.ShortPath(absDir), base.ShortPath(mr))
611611
}
612612
return "", fmt.Errorf("directory %s outside modules listed in go.work or their selected dependencies", base.ShortPath(absDir))
613613
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@
77
stderr 'pattern ./...: directory prefix . does not contain modules listed in go.work or their selected dependencies'
88

99
! go list ./a/c
10-
stderr 'directory a[\\/]c is contained in a module that is not one of the workspace modules listed in go.work. You can add the module to the workspace using go work use a'
10+
stderr 'directory a[\\/]c is contained in a module that is not one of the workspace modules listed in go.work. You can add the module to the workspace using:\n\tgo work use a'
11+
12+
! go install ./a/c
13+
stderr 'directory a[\\/]c is contained in a module that is not one of the workspace modules listed in go.work. You can add the module to the workspace using:\n\tgo work use a'
14+
15+
cd a/c
16+
! go run .
17+
stderr 'directory . is contained in a module that is not one of the workspace modules listed in go.work. You can add the module to the workspace using:\n\tgo work use \.\.'
1118

1219
-- go.work --
1320
go 1.18
@@ -20,7 +27,7 @@ go 1.18
2027
-- a/a.go --
2128
package a
2229
-- a/c/c.go --
23-
package c
30+
package main
2431
-- b/go.mod --
2532
module example.com/b
2633

0 commit comments

Comments
 (0)