From 01e687557420ed7f31bd82787c3f0af0a9bf0336 Mon Sep 17 00:00:00 2001 From: Heschi Kreinick Date: Tue, 21 Apr 2020 14:19:53 -0400 Subject: [PATCH] cmd/goimports: set correct permissions on Windows As of Go 1.14, WriteFile on Windows will set read-only on existing files if you pass 0 for perms. Pass the pre-existing permissions. Updates golang/go#38225. Change-Id: I3174469efd4dc4c7eacc8522386a0712cfa39d11 Reviewed-on: https://go-review.googlesource.com/c/tools/+/229297 Reviewed-by: Ian Cottrell Run-TryBot: Heschi Kreinick TryBot-Result: Gobot Gobot --- cmd/goimports/goimports.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cmd/goimports/goimports.go b/cmd/goimports/goimports.go index a5680d8d6c..c195caaf8b 100644 --- a/cmd/goimports/goimports.go +++ b/cmd/goimports/goimports.go @@ -160,7 +160,12 @@ func processFile(filename string, in io.Reader, out io.Writer, argType argumentT // filename is "" return errors.New("can't use -w on stdin") } - err = ioutil.WriteFile(filename, res, 0) + // On Windows, we need to re-set the permissions from the file. See golang/go#38225. + var perms os.FileMode + if fi, err := os.Stat(filename); err == nil { + perms = fi.Mode() & os.ModePerm + } + err = ioutil.WriteFile(filename, res, perms) if err != nil { return err }