Skip to content

Commit ee788db

Browse files
g4s8gopherbot
authored andcommitted
cmd/go/internal/generate: error if failed to find a package
Add check for package loader to print error and fail `go generate` command, if package can not be found. Fixes #60079 Change-Id: Ib9e730c2b69df6e5ac307c7bdfea0ee993ab6ed8 GitHub-Last-Rev: d933324 GitHub-Pull-Request: #60178 Reviewed-on: https://go-review.googlesource.com/c/go/+/494836 Auto-Submit: Bryan Mills <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Run-TryBot: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 267323e commit ee788db

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/cmd/go/internal/generate/generate.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ func runGenerate(ctx context.Context, cmd *base.Command, args []string) {
210210
continue
211211
}
212212

213+
if pkg.Error != nil && len(pkg.InternalAllGoFiles()) == 0 {
214+
// A directory only contains a Go package if it has at least
215+
// one .go source file, so the fact that there are no files
216+
// implies that the package couldn't be found.
217+
base.Errorf("%v", pkg.Error)
218+
}
219+
213220
for _, file := range pkg.InternalGoFiles() {
214221
if !generate(file) {
215222
break
@@ -222,6 +229,7 @@ func runGenerate(ctx context.Context, cmd *base.Command, args []string) {
222229
}
223230
}
224231
}
232+
base.ExitIfErrors()
225233
}
226234

227235
// generate runs the generation directives for a single file.

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ go install echo.go
66
env PATH=$GOBIN${:}$PATH
77

88
# Test go generate for directory with no go files
9-
go generate ./nogo
9+
! go generate ./nogo
1010
! stdout 'Fail'
11+
stderr 'no Go files'
12+
13+
# Test go generate for module which doesn't exist should fail
14+
! go generate foo.bar/nothing
15+
stderr 'no required module provides package foo.bar/nothing'
1116

1217
# Test go generate for package where all .go files are excluded by build
1318
# constraints

0 commit comments

Comments
 (0)