Debian Codegraph @

cmd/goimports/goimports.go

raw symbols SCIP
1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package main
6
7 import (
8 "bufio"
9 "bytes"
10 "errors"
11 "flag"
12 "fmt"
13 "go/scanner"
14 "io"
15 "log"
16 "os"
17 "os/exec"
18 "path/filepath"
19 "runtime"
20 "runtime/pprof"
21 "strings"
22
23 "golang.org/x/telemetry/counter"
24 "golang.org/x/tools/internal/gocommand"
25 "golang.org/x/tools/internal/imports"
26 )
27
28 var (
29 // main operation modes
30 list = flag.Bool("l", false, "list files whose formatting differs from goimport's")
31 write = flag.Bool("w", false, "write result to (source) file instead of stdout")
32 doDiff = flag.Bool("d", false, "display diffs instead of rewriting files")
33 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.")
34
35 verbose bool // verbose logging
36
37 cpuProfile = flag.String("cpuprofile", "", "CPU profile output")
38 memProfile = flag.String("memprofile", "", "memory profile output")
39 memProfileRate = flag.Int("memrate", 0, "if > 0, sets runtime.MemProfileRate")
40
41 options = &imports.Options{
42 TabWidth: 8,
43 TabIndent: true,
44 Comments: true,
45 Fragment: true,
46 Env: &imports.ProcessEnv{
47 GocmdRunner: &gocommand.Runner{},
48 },
49 }
50 exitCode = 0
51 )
52
53 func init() {
54 flag.BoolVar(&options.AllErrors, "e", false, "report all errors (not just the first 10 on different lines)")
55 flag.StringVar(&options.LocalPrefix, "local", "", "put imports beginning with this string after 3rd-party packages; comma-separated list")
56 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.")
57 }
58
59 func report(err error) {
60 scanner.PrintError(os.Stderr, err)
61 exitCode = 2
62 }
63
64 func usage() {
65 fmt.Fprintf(os.Stderr, "usage: goimports [flags] [path ...]\n")
66 flag.PrintDefaults()
67 os.Exit(2)
68 }
69
70 func isGoFile(f os.FileInfo) bool {
71 // ignore non-Go files
72 name := f.Name()
73 return !f.IsDir() && !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
74 }
75
76 // argumentType is which mode goimports was invoked as.
77 type argumentType int
78
79 const (
80 // fromStdin means the user is piping their source into goimports.
81 fromStdin argumentType = iota
82
83 // singleArg is the common case from editors, when goimports is run on
84 // a single file.
85 singleArg
86
87 // multipleArg is when the user ran "goimports file1.go file2.go"
88 // or ran goimports on a directory tree.
89 multipleArg
90 )
91
92 func processFile(filename string, in io.Reader, out io.Writer, argType argumentType) error {
93 opt := options
94 if argType == fromStdin {
95 nopt := *options
96 nopt.Fragment = true
97 opt = &nopt
98 }
99
100 if in == nil {
101 f, err := os.Open(filename)
102 if err != nil {
103 return err
104 }
105 defer f.Close()
106 in = f
107 }
108
109 src, err := io.ReadAll(in)
110 if err != nil {
111 return err
112 }
113
114 target := filename
115 if *srcdir != "" {
116 // Determine whether the provided -srcdirc is a directory or file
117 // and then use it to override the target.
118 //
119 // See https://github.com/dominikh/go-mode.el/issues/146
120 if isFile(*srcdir) {
121 if argType == multipleArg {
122 return errors.New("-srcdir value can't be a file when passing multiple arguments or when walking directories")
123 }
124 target = *srcdir
125 } else if argType == singleArg && strings.HasSuffix(*srcdir, ".go") && !isDir(*srcdir) {
126 // For a file which doesn't exist on disk yet, but might shortly.
127 // e.g. user in editor opens $DIR/newfile.go and newfile.go doesn't yet exist on disk.
128 // The goimports on-save hook writes the buffer to a temp file
129 // first and runs goimports before the actual save to newfile.go.
130 // The editor's buffer is named "newfile.go" so that is passed to goimports as:
131 // goimports -srcdir=/gopath/src/pkg/newfile.go /tmp/gofmtXXXXXXXX.go
132 // and then the editor reloads the result from the tmp file and writes
133 // it to newfile.go.
134 target = *srcdir
135 } else {
136 // Pretend that file is from *srcdir in order to decide
137 // visible imports correctly.
138 target = filepath.Join(*srcdir, filepath.Base(filename))
139 }
140 }
141
142 res, err := imports.Process(target, src, opt)
143 if err != nil {
144 return err
145 }
146
147 if !bytes.Equal(src, res) {
148 // formatting has changed
149 if *list {
150 fmt.Fprintln(out, filename)
151 }
152 if *write {
153 if argType == fromStdin {
154 // filename is "<standard input>"
155 return errors.New("can't use -w on stdin")
156 }
157 // On Windows, we need to re-set the permissions from the file. See golang/go#38225.
158 var perms os.FileMode
159 if fi, err := os.Stat(filename); err == nil {
160 perms = fi.Mode() & os.ModePerm
161 }
162 err = os.WriteFile(filename, res, perms)
163 if err != nil {
164 return err
165 }
166 }
167 if *doDiff {
168 if argType == fromStdin {
169 filename = "stdin.go" // because <standard input>.orig looks silly
170 }
171 data, err := diff(src, res, filename)
172 if err != nil {
173 return fmt.Errorf("computing diff: %s", err)
174 }
175 fmt.Printf("diff -u %s %s\n", filepath.ToSlash(filename+".orig"), filepath.ToSlash(filename))
176 out.Write(data)
177 }
178 }
179
180 if !*list && !*write && !*doDiff {
181 _, err = out.Write(res)
182 }
183
184 return err
185 }
186
187 func visitFile(path string, f os.FileInfo, err error) error {
188 if err == nil && isGoFile(f) {
189 err = processFile(path, nil, os.Stdout, multipleArg)
190 }
191 if err != nil {
192 report(err)
193 }
194 return nil
195 }
196
197 func walkDir(path string) {
198 filepath.Walk(path, visitFile)
199 }
200
201 func main() {
202 // is anyone using this command?
203 counter.Open()
204 counter.Inc("tools/cmd:goimports")
205 runtime.GOMAXPROCS(runtime.NumCPU())
206
207 // call gofmtMain in a separate function
208 // so that it can use defer and have them
209 // run before the exit.
210 gofmtMain()
211 os.Exit(exitCode)
212 }
213
214 // parseFlags parses command line flags and returns the paths to process.
215 // It's a var so that custom implementations can replace it in other files.
216 var parseFlags = func() []string {
217 flag.BoolVar(&verbose, "v", false, "verbose logging")
218
219 flag.Parse()
220 return flag.Args()
221 }
222
223 func bufferedFileWriter(dest string) (w io.Writer, close func()) {
224 f, err := os.Create(dest)
225 if err != nil {
226 log.Fatal(err)
227 }
228 bw := bufio.NewWriter(f)
229 return bw, func() {
230 if err := bw.Flush(); err != nil {
231 log.Fatalf("error flushing %v: %v", dest, err)
232 }
233 if err := f.Close(); err != nil {
234 log.Fatal(err)
235 }
236 }
237 }
238
239 func gofmtMain() {
240 flag.Usage = usage
241 paths := parseFlags()
242
243 if *cpuProfile != "" {
244 bw, flush := bufferedFileWriter(*cpuProfile)
245 pprof.StartCPUProfile(bw)
246 defer flush()
247 defer pprof.StopCPUProfile()
248 }
249 // doTrace is a conditionally compiled wrapper around runtime/trace. It is
250 // used to allow goimports to compile under gccgo, which does not support
251 // runtime/trace. See https://golang.org/issue/15544.
252 defer doTrace()()
253 if *memProfileRate > 0 {
254 runtime.MemProfileRate = *memProfileRate
255 bw, flush := bufferedFileWriter(*memProfile)
256 defer func() {
257 runtime.GC() // materialize all statistics
258 if err := pprof.WriteHeapProfile(bw); err != nil {
259 log.Fatal(err)
260 }
261 flush()
262 }()
263 }
264
265 if verbose {
266 log.SetFlags(log.LstdFlags | log.Lmicroseconds)
267 options.Env.Logf = log.Printf
268 }
269 if options.TabWidth < 0 {
270 fmt.Fprintf(os.Stderr, "negative tabwidth %d\n", options.TabWidth)
271 exitCode = 2
272 return
273 }
274
275 if len(paths) == 0 {
276 if err := processFile("<standard input>", os.Stdin, os.Stdout, fromStdin); err != nil {
277 report(err)
278 }
279 return
280 }
281
282 argType := singleArg
283 if len(paths) > 1 {
284 argType = multipleArg
285 }
286
287 for _, path := range paths {
288 switch dir, err := os.Stat(path); {
289 case err != nil:
290 report(err)
291 case dir.IsDir():
292 walkDir(path)
293 default:
294 if err := processFile(path, nil, os.Stdout, argType); err != nil {
295 report(err)
296 }
297 }
298 }
299 }
300
301 func writeTempFile(dir, prefix string, data []byte) (string, error) {
302 file, err := os.CreateTemp(dir, prefix)
303 if err != nil {
304 return "", err
305 }
306 _, err = file.Write(data)
307 if err1 := file.Close(); err == nil {
308 err = err1
309 }
310 if err != nil {
311 os.Remove(file.Name())
312 return "", err
313 }
314 return file.Name(), nil
315 }
316
317 func diff(b1, b2 []byte, filename string) (data []byte, err error) {
318 f1, err := writeTempFile("", "gofmt", b1)
319 if err != nil {
320 return
321 }
322 defer os.Remove(f1)
323
324 f2, err := writeTempFile("", "gofmt", b2)
325 if err != nil {
326 return
327 }
328 defer os.Remove(f2)
329
330 cmd := "diff"
331 if runtime.GOOS == "plan9" {
332 cmd = "/bin/ape/diff"
333 }
334
335 data, err = exec.Command(cmd, "-u", f1, f2).CombinedOutput()
336 if len(data) > 0 {
337 // diff exits with a non-zero status when the files don't match.
338 // Ignore that failure as long as we get output.
339 return replaceTempFilename(data, filename)
340 }
341 return
342 }
343
344 // replaceTempFilename replaces temporary filenames in diff with actual one.
345 //
346 // --- /tmp/gofmt316145376 2017-02-03 19:13:00.280468375 -0500
347 // +++ /tmp/gofmt617882815 2017-02-03 19:13:00.280468375 -0500
348 // ...
349 // ->
350 // --- path/to/file.go.orig 2017-02-03 19:13:00.280468375 -0500
351 // +++ path/to/file.go 2017-02-03 19:13:00.280468375 -0500
352 // ...
353 func replaceTempFilename(diff []byte, filename string) ([]byte, error) {
354 bs := bytes.SplitN(diff, []byte{'\n'}, 3)
355 if len(bs) < 3 {
356 return nil, fmt.Errorf("got unexpected diff for %s", filename)
357 }
358 // Preserve timestamps.
359 var t0, t1 []byte
360 if i := bytes.LastIndexByte(bs[0], '\t'); i != -1 {
361 t0 = bs[0][i:]
362 }
363 if i := bytes.LastIndexByte(bs[1], '\t'); i != -1 {
364 t1 = bs[1][i:]
365 }
366 // Always print filepath with slash separator.
367 f := filepath.ToSlash(filename)
368 bs[0] = fmt.Appendf(nil, "--- %s%s", f+".orig", t0)
369 bs[1] = fmt.Appendf(nil, "+++ %s%s", f, t1)
370 return bytes.Join(bs, []byte{'\n'}), nil
371 }
372
373 // isFile reports whether name is a file.
374 func isFile(name string) bool {
375 fi, err := os.Stat(name)
376 return err == nil && fi.Mode().IsRegular()
377 }
378
379 // isDir reports whether name is a directory.
380 func isDir(name string) bool {
381 fi, err := os.Stat(name)
382 return err == nil && fi.IsDir()
383 }