From 4ec27c15406b789186e9619e79bfc6ad2ebeb26a Mon Sep 17 00:00:00 2001 From: Ivan Trubach Date: Tue, 20 Apr 2021 04:01:21 +0300 Subject: [PATCH] cmd/go: allow generate to skip missing files This change allows go generate to process packages where files may disappear during code generation. See also #36422. Updates #36068 --- src/cmd/go/internal/generate/generate.go | 4 +++ .../go/testdata/script/generate_removed.txt | 36 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/cmd/go/testdata/script/generate_removed.txt diff --git a/src/cmd/go/internal/generate/generate.go b/src/cmd/go/internal/generate/generate.go index 80ea32b4284..5cc52f8b7d4 100644 --- a/src/cmd/go/internal/generate/generate.go +++ b/src/cmd/go/internal/generate/generate.go @@ -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) } diff --git a/src/cmd/go/testdata/script/generate_removed.txt b/src/cmd/go/testdata/script/generate_removed.txt new file mode 100644 index 00000000000..a0bf93a2183 --- /dev/null +++ b/src/cmd/go/testdata/script/generate_removed.txt @@ -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) + } +}