2013-12-17 19:21:03 -07:00
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
2016-07-17 18:47:35 -06:00
"bufio"
2013-12-17 19:21:03 -07:00
"bytes"
2016-07-21 13:29:03 -06:00
"errors"
2013-12-17 19:21:03 -07:00
"flag"
"fmt"
"go/scanner"
"io"
"io/ioutil"
cmd/goimports, imports: make goimports great again
I felt the burn of my laptop on my legs, spinning away while processing
goimports, and felt that it was time to make goimports great again.
Over the past few years goimports fell into a slow state of disrepair
with too many feature additions and no attention to the performance
death by a thousand cuts. This was particularly terrible on OS X with
its lackluster filesystem buffering.
This CL makes goimports stronger, together with various optimizations
and more visibility into what goimports is doing.
* adds more internal documentation
* avoids scanning $GOPATH for answers when running goimports on a file
under $GOROOT (for Go core hackers)
* don't read all $GOROOT & $GOPATH directories' Go code looking for
their package names until much later. Require the package name of
missing imports to be present in the last two directory path
components. Then only try importing them in order from best to
worst (shortest to longest, as before), so we can stop early.
* when adding imports, add names to imports when the imported package name
doesn't match the baes of its import path. For example:
import foo "example.net/foo/v1"
* don't read all *.go files in a package directory once the first file
in a directory has revealed itself to be a package we're not looking
for. For example, if we're looking for the right "client" for "client.Foo",
we used to consider a directory "bar/client" as a candidate and read
all 50 of its *.go files instead of stopping after its first *.go
file had a "package main" line.
* add some fast paths to remove allocations
* add some fast paths to remove disk I/O when looking up the base
package name of a standard library import (of existing imports in a
file, which are very common)
* adds a special case for import "C", to avoid some disk I/O.
* add a -verbose flag to goimports for debugging
On my Mac laptop with a huge $GOPATH, with a test file like:
package foo
import (
"fmt"
"net/http"
)
/*
*/
import "C"
var _ = cloudbilling.New
var _ = http.NewRequest
var _ = client.New
... this took like 10 seconds before, and now 1.3 seconds. (Still
slow; disk-based caching can come later)
Updates golang/go#16367 (goimports is slow)
Updates golang/go#16384 (refactor TestRename is broken on Windows)
Change-Id: I97e85d3016afc9f2ad5501f97babad30c7989183
Reviewed-on: https://go-review.googlesource.com/24941
Reviewed-by: Andrew Gerrand <adg@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-07-14 18:08:27 -06:00
"log"
2013-12-17 19:21:03 -07:00
"os"
"os/exec"
"path/filepath"
"runtime"
2016-07-17 18:47:35 -06:00
"runtime/pprof"
2013-12-17 19:21:03 -07:00
"strings"
2020-07-06 13:18:50 -06:00
"golang.org/x/tools/internal/gocommand"
2019-05-06 14:34:48 -06:00
"golang.org/x/tools/internal/imports"
2013-12-17 19:21:03 -07:00
)
var (
// main operation modes
2019-04-29 13:51:48 -06:00
list = flag . Bool ( "l" , false , "list files whose formatting differs from goimport's" )
write = flag . Bool ( "w" , false , "write result to (source) file instead of stdout" )
doDiff = flag . Bool ( "d" , false , "display diffs instead of rewriting files" )
srcdir = flag . String ( "srcdir" , "" , "choose imports as if source code is from `dir`. When operating on a single file, dir may instead be the complete file name." )
2016-08-09 12:04:40 -06:00
verbose bool // verbose logging
2013-12-17 19:21:03 -07:00
2016-07-17 18:47:35 -06:00
cpuProfile = flag . String ( "cpuprofile" , "" , "CPU profile output" )
memProfile = flag . String ( "memprofile" , "" , "memory profile output" )
memProfileRate = flag . Int ( "memrate" , 0 , "if > 0, sets runtime.MemProfileRate" )
2014-02-12 09:35:02 -07:00
options = & imports . Options {
TabWidth : 8 ,
TabIndent : true ,
Comments : true ,
2014-04-28 15:15:33 -06:00
Fragment : true ,
2020-07-06 13:18:50 -06:00
Env : & imports . ProcessEnv {
GocmdRunner : & gocommand . Runner { } ,
} ,
2014-02-12 09:35:02 -07:00
}
2013-12-17 19:21:03 -07:00
exitCode = 0
)
func init ( ) {
flag . BoolVar ( & options . AllErrors , "e" , false , "report all errors (not just the first 10 on different lines)" )
2020-07-06 13:18:50 -06:00
flag . StringVar ( & options . LocalPrefix , "local" , "" , "put imports beginning with this string after 3rd-party packages; comma-separated list" )
2019-04-29 13:51:48 -06:00
flag . BoolVar ( & options . FormatOnly , "format-only" , false , "if true, don't fix imports and only format. In this mode, goimports is effectively gofmt, with the addition that imports are grouped into sections." )
2013-12-17 19:21:03 -07:00
}
func report ( err error ) {
scanner . PrintError ( os . Stderr , err )
exitCode = 2
}
func usage ( ) {
fmt . Fprintf ( os . Stderr , "usage: goimports [flags] [path ...]\n" )
flag . PrintDefaults ( )
os . Exit ( 2 )
}
func isGoFile ( f os . FileInfo ) bool {
// ignore non-Go files
name := f . Name ( )
return ! f . IsDir ( ) && ! strings . HasPrefix ( name , "." ) && strings . HasSuffix ( name , ".go" )
}
2016-07-21 13:29:03 -06:00
// argumentType is which mode goimports was invoked as.
type argumentType int
const (
// fromStdin means the user is piping their source into goimports.
fromStdin argumentType = iota
// singleArg is the common case from editors, when goimports is run on
// a single file.
singleArg
// multipleArg is when the user ran "goimports file1.go file2.go"
// or ran goimports on a directory tree.
multipleArg
)
func processFile ( filename string , in io . Reader , out io . Writer , argType argumentType ) error {
2013-12-17 19:21:03 -07:00
opt := options
2016-07-21 13:29:03 -06:00
if argType == fromStdin {
2013-12-17 19:21:03 -07:00
nopt := * options
nopt . Fragment = true
opt = & nopt
}
if in == nil {
f , err := os . Open ( filename )
if err != nil {
return err
}
defer f . Close ( )
in = f
}
src , err := ioutil . ReadAll ( in )
if err != nil {
return err
}
2015-12-10 23:32:07 -07:00
target := filename
if * srcdir != "" {
2016-07-21 13:29:03 -06:00
// Determine whether the provided -srcdirc is a directory or file
// and then use it to override the target.
//
// See https://github.com/dominikh/go-mode.el/issues/146
if isFile ( * srcdir ) {
if argType == multipleArg {
return errors . New ( "-srcdir value can't be a file when passing multiple arguments or when walking directories" )
}
target = * srcdir
} else if argType == singleArg && strings . HasSuffix ( * srcdir , ".go" ) && ! isDir ( * srcdir ) {
// For a file which doesn't exist on disk yet, but might shortly.
// e.g. user in editor opens $DIR/newfile.go and newfile.go doesn't yet exist on disk.
// The goimports on-save hook writes the buffer to a temp file
// first and runs goimports before the actual save to newfile.go.
// The editor's buffer is named "newfile.go" so that is passed to goimports as:
// goimports -srcdir=/gopath/src/pkg/newfile.go /tmp/gofmtXXXXXXXX.go
// and then the editor reloads the result from the tmp file and writes
// it to newfile.go.
target = * srcdir
} else {
// Pretend that file is from *srcdir in order to decide
// visible imports correctly.
target = filepath . Join ( * srcdir , filepath . Base ( filename ) )
}
2015-12-10 23:32:07 -07:00
}
res , err := imports . Process ( target , src , opt )
2013-12-17 19:21:03 -07:00
if err != nil {
return err
}
if ! bytes . Equal ( src , res ) {
// formatting has changed
if * list {
fmt . Fprintln ( out , filename )
}
if * write {
2017-07-07 10:16:50 -06:00
if argType == fromStdin {
// filename is "<standard input>"
return errors . New ( "can't use -w on stdin" )
}
2020-04-21 12:19:53 -06:00
// 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 )
2013-12-17 19:21:03 -07:00
if err != nil {
return err
}
}
if * doDiff {
2017-07-07 10:16:50 -06:00
if argType == fromStdin {
filename = "stdin.go" // because <standard input>.orig looks silly
}
2017-02-09 02:43:25 -07:00
data , err := diff ( src , res , filename )
2013-12-17 19:21:03 -07:00
if err != nil {
return fmt . Errorf ( "computing diff: %s" , err )
}
2017-02-09 02:43:25 -07:00
fmt . Printf ( "diff -u %s %s\n" , filepath . ToSlash ( filename + ".orig" ) , filepath . ToSlash ( filename ) )
2013-12-17 19:21:03 -07:00
out . Write ( data )
}
}
if ! * list && ! * write && ! * doDiff {
_ , err = out . Write ( res )
}
return err
}
func visitFile ( path string , f os . FileInfo , err error ) error {
if err == nil && isGoFile ( f ) {
2016-07-21 13:29:03 -06:00
err = processFile ( path , nil , os . Stdout , multipleArg )
2013-12-17 19:21:03 -07:00
}
if err != nil {
report ( err )
}
return nil
}
func walkDir ( path string ) {
filepath . Walk ( path , visitFile )
}
func main ( ) {
runtime . GOMAXPROCS ( runtime . NumCPU ( ) )
// call gofmtMain in a separate function
// so that it can use defer and have them
// run before the exit.
gofmtMain ( )
os . Exit ( exitCode )
}
2015-03-27 12:09:40 -06:00
// parseFlags parses command line flags and returns the paths to process.
// It's a var so that custom implementations can replace it in other files.
var parseFlags = func ( ) [ ] string {
2016-08-09 12:04:40 -06:00
flag . BoolVar ( & verbose , "v" , false , "verbose logging" )
2015-03-27 12:09:40 -06:00
flag . Parse ( )
return flag . Args ( )
}
2016-07-17 18:47:35 -06:00
func bufferedFileWriter ( dest string ) ( w io . Writer , close func ( ) ) {
f , err := os . Create ( dest )
if err != nil {
log . Fatal ( err )
}
bw := bufio . NewWriter ( f )
return bw , func ( ) {
if err := bw . Flush ( ) ; err != nil {
log . Fatalf ( "error flushing %v: %v" , dest , err )
}
if err := f . Close ( ) ; err != nil {
log . Fatal ( err )
}
}
}
2013-12-17 19:21:03 -07:00
func gofmtMain ( ) {
flag . Usage = usage
2015-03-27 12:09:40 -06:00
paths := parseFlags ( )
2013-12-17 19:21:03 -07:00
2016-07-17 18:47:35 -06:00
if * cpuProfile != "" {
bw , flush := bufferedFileWriter ( * cpuProfile )
pprof . StartCPUProfile ( bw )
defer flush ( )
defer pprof . StopCPUProfile ( )
}
2016-08-12 19:48:14 -06:00
// doTrace is a conditionally compiled wrapper around runtime/trace. It is
// used to allow goimports to compile under gccgo, which does not support
// runtime/trace. See https://golang.org/issue/15544.
defer doTrace ( ) ( )
2016-07-17 18:47:35 -06:00
if * memProfileRate > 0 {
runtime . MemProfileRate = * memProfileRate
bw , flush := bufferedFileWriter ( * memProfile )
defer func ( ) {
runtime . GC ( ) // materialize all statistics
if err := pprof . WriteHeapProfile ( bw ) ; err != nil {
log . Fatal ( err )
}
flush ( )
} ( )
}
2016-08-09 12:04:40 -06:00
if verbose {
cmd/goimports, imports: make goimports great again
I felt the burn of my laptop on my legs, spinning away while processing
goimports, and felt that it was time to make goimports great again.
Over the past few years goimports fell into a slow state of disrepair
with too many feature additions and no attention to the performance
death by a thousand cuts. This was particularly terrible on OS X with
its lackluster filesystem buffering.
This CL makes goimports stronger, together with various optimizations
and more visibility into what goimports is doing.
* adds more internal documentation
* avoids scanning $GOPATH for answers when running goimports on a file
under $GOROOT (for Go core hackers)
* don't read all $GOROOT & $GOPATH directories' Go code looking for
their package names until much later. Require the package name of
missing imports to be present in the last two directory path
components. Then only try importing them in order from best to
worst (shortest to longest, as before), so we can stop early.
* when adding imports, add names to imports when the imported package name
doesn't match the baes of its import path. For example:
import foo "example.net/foo/v1"
* don't read all *.go files in a package directory once the first file
in a directory has revealed itself to be a package we're not looking
for. For example, if we're looking for the right "client" for "client.Foo",
we used to consider a directory "bar/client" as a candidate and read
all 50 of its *.go files instead of stopping after its first *.go
file had a "package main" line.
* add some fast paths to remove allocations
* add some fast paths to remove disk I/O when looking up the base
package name of a standard library import (of existing imports in a
file, which are very common)
* adds a special case for import "C", to avoid some disk I/O.
* add a -verbose flag to goimports for debugging
On my Mac laptop with a huge $GOPATH, with a test file like:
package foo
import (
"fmt"
"net/http"
)
/*
*/
import "C"
var _ = cloudbilling.New
var _ = http.NewRequest
var _ = client.New
... this took like 10 seconds before, and now 1.3 seconds. (Still
slow; disk-based caching can come later)
Updates golang/go#16367 (goimports is slow)
Updates golang/go#16384 (refactor TestRename is broken on Windows)
Change-Id: I97e85d3016afc9f2ad5501f97babad30c7989183
Reviewed-on: https://go-review.googlesource.com/24941
Reviewed-by: Andrew Gerrand <adg@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-07-14 18:08:27 -06:00
log . SetFlags ( log . LstdFlags | log . Lmicroseconds )
2020-02-28 15:03:27 -07:00
options . Env . Logf = log . Printf
cmd/goimports, imports: make goimports great again
I felt the burn of my laptop on my legs, spinning away while processing
goimports, and felt that it was time to make goimports great again.
Over the past few years goimports fell into a slow state of disrepair
with too many feature additions and no attention to the performance
death by a thousand cuts. This was particularly terrible on OS X with
its lackluster filesystem buffering.
This CL makes goimports stronger, together with various optimizations
and more visibility into what goimports is doing.
* adds more internal documentation
* avoids scanning $GOPATH for answers when running goimports on a file
under $GOROOT (for Go core hackers)
* don't read all $GOROOT & $GOPATH directories' Go code looking for
their package names until much later. Require the package name of
missing imports to be present in the last two directory path
components. Then only try importing them in order from best to
worst (shortest to longest, as before), so we can stop early.
* when adding imports, add names to imports when the imported package name
doesn't match the baes of its import path. For example:
import foo "example.net/foo/v1"
* don't read all *.go files in a package directory once the first file
in a directory has revealed itself to be a package we're not looking
for. For example, if we're looking for the right "client" for "client.Foo",
we used to consider a directory "bar/client" as a candidate and read
all 50 of its *.go files instead of stopping after its first *.go
file had a "package main" line.
* add some fast paths to remove allocations
* add some fast paths to remove disk I/O when looking up the base
package name of a standard library import (of existing imports in a
file, which are very common)
* adds a special case for import "C", to avoid some disk I/O.
* add a -verbose flag to goimports for debugging
On my Mac laptop with a huge $GOPATH, with a test file like:
package foo
import (
"fmt"
"net/http"
)
/*
*/
import "C"
var _ = cloudbilling.New
var _ = http.NewRequest
var _ = client.New
... this took like 10 seconds before, and now 1.3 seconds. (Still
slow; disk-based caching can come later)
Updates golang/go#16367 (goimports is slow)
Updates golang/go#16384 (refactor TestRename is broken on Windows)
Change-Id: I97e85d3016afc9f2ad5501f97babad30c7989183
Reviewed-on: https://go-review.googlesource.com/24941
Reviewed-by: Andrew Gerrand <adg@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-07-14 18:08:27 -06:00
}
2013-12-17 19:21:03 -07:00
if options . TabWidth < 0 {
fmt . Fprintf ( os . Stderr , "negative tabwidth %d\n" , options . TabWidth )
exitCode = 2
return
}
2015-03-27 12:09:40 -06:00
if len ( paths ) == 0 {
2016-07-21 13:29:03 -06:00
if err := processFile ( "<standard input>" , os . Stdin , os . Stdout , fromStdin ) ; err != nil {
2013-12-17 19:21:03 -07:00
report ( err )
}
return
}
2016-07-21 13:29:03 -06:00
argType := singleArg
if len ( paths ) > 1 {
argType = multipleArg
}
2015-03-27 12:09:40 -06:00
for _ , path := range paths {
2013-12-17 19:21:03 -07:00
switch dir , err := os . Stat ( path ) ; {
case err != nil :
report ( err )
case dir . IsDir ( ) :
walkDir ( path )
default :
2016-07-21 13:29:03 -06:00
if err := processFile ( path , nil , os . Stdout , argType ) ; err != nil {
2013-12-17 19:21:03 -07:00
report ( err )
}
}
}
}
2017-03-30 20:52:04 -06:00
func writeTempFile ( dir , prefix string , data [ ] byte ) ( string , error ) {
file , err := ioutil . TempFile ( dir , prefix )
if err != nil {
return "" , err
}
_ , err = file . Write ( data )
if err1 := file . Close ( ) ; err == nil {
err = err1
}
if err != nil {
os . Remove ( file . Name ( ) )
return "" , err
}
return file . Name ( ) , nil
}
2017-02-09 02:43:25 -07:00
func diff ( b1 , b2 [ ] byte , filename string ) ( data [ ] byte , err error ) {
2017-03-30 20:52:04 -06:00
f1 , err := writeTempFile ( "" , "gofmt" , b1 )
2013-12-17 19:21:03 -07:00
if err != nil {
return
}
2017-03-30 20:52:04 -06:00
defer os . Remove ( f1 )
2013-12-17 19:21:03 -07:00
2017-03-30 20:52:04 -06:00
f2 , err := writeTempFile ( "" , "gofmt" , b2 )
2013-12-17 19:21:03 -07:00
if err != nil {
return
}
2017-03-30 20:52:04 -06:00
defer os . Remove ( f2 )
2013-12-17 19:21:03 -07:00
2017-02-09 02:43:25 -07:00
cmd := "diff"
if runtime . GOOS == "plan9" {
cmd = "/bin/ape/diff"
}
2017-03-30 20:52:04 -06:00
data , err = exec . Command ( cmd , "-u" , f1 , f2 ) . CombinedOutput ( )
2013-12-17 19:21:03 -07:00
if len ( data ) > 0 {
// diff exits with a non-zero status when the files don't match.
// Ignore that failure as long as we get output.
2017-03-30 20:52:04 -06:00
return replaceTempFilename ( data , filename )
2013-12-17 19:21:03 -07:00
}
return
}
2016-07-21 13:29:03 -06:00
2017-02-09 02:43:25 -07:00
// replaceTempFilename replaces temporary filenames in diff with actual one.
//
// --- /tmp/gofmt316145376 2017-02-03 19:13:00.280468375 -0500
// +++ /tmp/gofmt617882815 2017-02-03 19:13:00.280468375 -0500
// ...
// ->
// --- path/to/file.go.orig 2017-02-03 19:13:00.280468375 -0500
// +++ path/to/file.go 2017-02-03 19:13:00.280468375 -0500
// ...
func replaceTempFilename ( diff [ ] byte , filename string ) ( [ ] byte , error ) {
bs := bytes . SplitN ( diff , [ ] byte { '\n' } , 3 )
if len ( bs ) < 3 {
return nil , fmt . Errorf ( "got unexpected diff for %s" , filename )
}
// Preserve timestamps.
var t0 , t1 [ ] byte
if i := bytes . LastIndexByte ( bs [ 0 ] , '\t' ) ; i != - 1 {
t0 = bs [ 0 ] [ i : ]
}
if i := bytes . LastIndexByte ( bs [ 1 ] , '\t' ) ; i != - 1 {
t1 = bs [ 1 ] [ i : ]
}
// Always print filepath with slash separator.
f := filepath . ToSlash ( filename )
bs [ 0 ] = [ ] byte ( fmt . Sprintf ( "--- %s%s" , f + ".orig" , t0 ) )
bs [ 1 ] = [ ] byte ( fmt . Sprintf ( "+++ %s%s" , f , t1 ) )
return bytes . Join ( bs , [ ] byte { '\n' } ) , nil
}
2016-07-21 13:29:03 -06:00
// isFile reports whether name is a file.
func isFile ( name string ) bool {
fi , err := os . Stat ( name )
return err == nil && fi . Mode ( ) . IsRegular ( )
}
// isDir reports whether name is a directory.
func isDir ( name string ) bool {
fi , err := os . Stat ( name )
return err == nil && fi . IsDir ( )
}