Skip to content

Commit

Permalink
cmd/go: allow generate to skip missing files
Browse files Browse the repository at this point in the history
This change allows go generate to process packages where files may
disappear during code generation. See also golang#36422.

Updates golang#36068
  • Loading branch information
tie committed Apr 21, 2021
1 parent 3711ea0 commit 4ec27c1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/cmd/go/internal/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ func runGenerate(ctx context.Context, cmd *base.Command, args []string) {
func generate(absFile string) bool {
src, err := os.ReadFile(absFile)
if err != nil {
if os.IsNotExist(err) {
// Disappeared during generation - ignore file.
return true
}
log.Fatalf("generate: %s", err)
}

Expand Down
36 changes: 36 additions & 0 deletions src/cmd/go/testdata/script/generate_removed.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Install an rm command because some systems don't have it.
env GOBIN=$WORK/tmp/bin
go install rm.go
[plan9] env path=$GOBIN${:}$path
[!plan9] env PATH=$GOBIN${:}$PATH

go generate ./...

-- go.mod --
module genclean

go 1.16

-- a.go --
package genclean

//go:generate rm b.go

-- b.go --
package genclean

-- rm.go --
// +build ignore

package main

import (
"log"
"os"
)

func main() {
if err := os.Remove(os.Args[1]); err != nil {
log.Fatal(err)
}
}

0 comments on commit 4ec27c1

Please sign in to comment.