]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/generate: error if failed to find a package
authorKirill Che <g4s8.public@gmail.com>
Fri, 8 Sep 2023 07:45:12 +0000 (07:45 +0000)
committerGopher Robot <gobot@golang.org>
Sat, 9 Sep 2023 01:48:44 +0000 (01:48 +0000)
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: d93332425a980d0298c74f33f1d154e0afbf5373
GitHub-Pull-Request: golang/go#60178
Reviewed-on: https://go-review.googlesource.com/c/go/+/494836
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

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

index f1f4a6cf30de0853933e009e18504d4fa1d701ff..486de1862d09f94b495d408a64d8ade27175dd86 100644 (file)
@@ -210,6 +210,13 @@ func runGenerate(ctx context.Context, cmd *base.Command, args []string) {
                        continue
                }
 
+               if pkg.Error != nil && len(pkg.InternalAllGoFiles()) == 0 {
+                       // A directory only contains a Go package if it has at least
+                       // one .go source file, so the fact that there are no files
+                       // implies that the package couldn't be found.
+                       base.Errorf("%v", pkg.Error)
+               }
+
                for _, file := range pkg.InternalGoFiles() {
                        if !generate(file) {
                                break
@@ -222,6 +229,7 @@ func runGenerate(ctx context.Context, cmd *base.Command, args []string) {
                        }
                }
        }
+       base.ExitIfErrors()
 }
 
 // generate runs the generation directives for a single file.
index e18e62ccf34fa8877beac479f1998b0916438637..3bede321a90429b1cab04f2668f505fa9d3b06e5 100644 (file)
@@ -6,8 +6,13 @@ go install echo.go
 env PATH=$GOBIN${:}$PATH
 
 # Test go generate for directory with no go files
-go generate ./nogo
+go generate ./nogo
 ! stdout 'Fail'
+stderr 'no Go files'
+
+# Test go  generate for module which doesn't exist should fail
+! go generate foo.bar/nothing
+stderr 'no required module provides package foo.bar/nothing'
 
 # Test go generate for package where all .go files are excluded by build
 # constraints