Commit b3241912 authored by Alexander Zolotov's avatar Alexander Zolotov Committed by Rob Pike

cmd/go: run gofmt from current GOROOT

The existing implementation executes `gofmt` binary from PATH
environment variable on invocation `go fmt` command.
Relying on PATH might lead to confusions for users with several Go installations.
It's more appropriate to run `gofmt` from GOBIN (if defined) or GOROOT.

Fixes #10755

Change-Id: I56d42a747319c766f2911508fab3994c3a366d12
Reviewed-on: https://go-review.googlesource.com/9900Reviewed-by: default avatarRob Pike <r@golang.org>
parent 8b83306c
......@@ -4,6 +4,12 @@
package main
import (
"os"
"path/filepath"
"runtime"
)
func init() {
addBuildFlagsNX(cmdFmt)
}
......@@ -29,10 +35,31 @@ See also: go fix, go vet.
}
func runFmt(cmd *Command, args []string) {
gofmt := gofmtPath()
for _, pkg := range packages(args) {
// Use pkg.gofiles instead of pkg.Dir so that
// the command only applies to this package,
// not to packages in subdirectories.
run(stringList("gofmt", "-l", "-w", relPaths(pkg.allgofiles)))
run(stringList(gofmt, "-l", "-w", relPaths(pkg.allgofiles)))
}
}
func gofmtPath() string {
gofmt := "gofmt"
if toolIsWindows {
gofmt += toolWindowsExtension
}
gofmtPath := filepath.Join(gobin, gofmt)
if _, err := os.Stat(gofmtPath); err == nil {
return gofmtPath
}
gofmtPath = filepath.Join(goroot, "bin", gofmt)
if _, err := os.Stat(gofmtPath); err == nil {
return gofmtPath
}
// fallback to looking for gofmt in $PATH
return "gofmt"
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment