src/cmd/gofmt/gofmt.go GO 578 lines View on github.com → Search inside
1// Copyright 2009 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.45package main67import (8	"bytes"9	"context"10	"flag"11	"fmt"12	"go/ast"13	"go/parser"14	"go/printer"15	"go/scanner"16	"go/token"17	"internal/diff"18	"io"19	"io/fs"20	"math/rand"21	"os"22	"path/filepath"23	"runtime"24	"runtime/pprof"25	"strconv"26	"strings"2728	"cmd/internal/telemetry/counter"2930	"golang.org/x/sync/semaphore"31)3233var (34	// main operation modes35	list        = flag.Bool("l", false, "list files whose formatting differs from gofmt's")36	write       = flag.Bool("w", false, "write result to (source) file instead of stdout")37	rewriteRule = flag.String("r", "", "rewrite rule (e.g., 'a[b:len(a)] -> a[b:]')")38	simplifyAST = flag.Bool("s", false, "simplify code")39	doDiff      = flag.Bool("d", false, "display diffs instead of rewriting files")40	allErrors   = flag.Bool("e", false, "report all errors (not just the first 10 on different lines)")4142	// debugging43	cpuprofile = flag.String("cpuprofile", "", "write cpu profile to this file")4445	// errors46	errFormattingDiffers = fmt.Errorf("formatting differs from gofmt's")47)4849// Keep these in sync with go/format/format.go.50const (51	tabWidth    = 852	printerMode = printer.UseSpaces | printer.TabIndent | printerNormalizeNumbers5354	// printerNormalizeNumbers means to canonicalize number literal prefixes55	// and exponents while printing. See https://golang.org/doc/go1.13#gofmt.56	//57	// This value is defined in go/printer specifically for go/format and cmd/gofmt.58	printerNormalizeNumbers = 1 << 3059)6061// fdSem guards the number of concurrently-open file descriptors.62//63// For now, this is arbitrarily set to 200, based on the observation that many64// platforms default to a kernel limit of 256. Ideally, perhaps we should derive65// it from rlimit on platforms that support that system call.66//67// File descriptors opened from outside of this package are not tracked,68// so this limit may be approximate.69var fdSem = make(chan bool, 200)7071var (72	rewrite    func(*token.FileSet, *ast.File) *ast.File73	parserMode parser.Mode74)7576func usage() {77	fmt.Fprintf(os.Stderr, "usage: gofmt [flags] [path ...]\n")78	flag.PrintDefaults()79}8081func initParserMode() {82	parserMode = parser.ParseComments | parser.SkipObjectResolution83	if *allErrors {84		parserMode |= parser.AllErrors85	}86	// It's only -r that makes use of go/ast's object resolution,87	// so avoid the unnecessary work if the flag isn't used.88	if *rewriteRule == "" {89		parserMode |= parser.SkipObjectResolution90	}91}9293func isGoFilename(name string) bool {94	return !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")95}9697// A sequencer performs concurrent tasks that may write output, but emits that98// output in a deterministic order.99type sequencer struct {100	maxWeight int64101	sem       *semaphore.Weighted   // weighted by input bytes (an approximate proxy for memory overhead)102	prev      <-chan *reporterState // 1-buffered103}104105// newSequencer returns a sequencer that allows concurrent tasks up to maxWeight106// and writes tasks' output to out and err.107func newSequencer(maxWeight int64, out, err io.Writer) *sequencer {108	sem := semaphore.NewWeighted(maxWeight)109	prev := make(chan *reporterState, 1)110	prev <- &reporterState{out: out, err: err}111	return &sequencer{112		maxWeight: maxWeight,113		sem:       sem,114		prev:      prev,115	}116}117118// exclusive is a weight that can be passed to a sequencer to cause119// a task to be executed without any other concurrent tasks.120const exclusive = -1121122// Add blocks until the sequencer has enough weight to spare, then adds f as a123// task to be executed concurrently.124//125// If the weight is either negative or larger than the sequencer's maximum126// weight, Add blocks until all other tasks have completed, then the task127// executes exclusively (blocking all other calls to Add until it completes).128//129// f may run concurrently in a goroutine, but its output to the passed-in130// reporter will be sequential relative to the other tasks in the sequencer.131//132// If f invokes a method on the reporter, execution of that method may block133// until the previous task has finished. (To maximize concurrency, f should134// avoid invoking the reporter until it has finished any parallelizable work.)135//136// If f returns a non-nil error, that error will be reported after f's output137// (if any) and will cause a nonzero final exit code.138func (s *sequencer) Add(weight int64, f func(*reporter) error) {139	if weight < 0 || weight > s.maxWeight {140		weight = s.maxWeight141	}142	if err := s.sem.Acquire(context.TODO(), weight); err != nil {143		// Change the task from "execute f" to "report err".144		weight = 0145		f = func(*reporter) error { return err }146	}147148	r := &reporter{prev: s.prev}149	next := make(chan *reporterState, 1)150	s.prev = next151152	// Start f in parallel: it can run until it invokes a method on r, at which153	// point it will block until the previous task releases the output state.154	go func() {155		if err := f(r); err != nil {156			r.Report(err)157		}158		next <- r.getState() // Release the next task.159		s.sem.Release(weight)160	}()161}162163// AddReport prints an error to s after the output of any previously-added164// tasks, causing the final exit code to be nonzero.165func (s *sequencer) AddReport(err error) {166	s.Add(0, func(*reporter) error { return err })167}168169// GetExitCode waits for all previously-added tasks to complete, then returns an170// exit code for the sequence suitable for passing to os.Exit.171func (s *sequencer) GetExitCode() int {172	c := make(chan int, 1)173	s.Add(0, func(r *reporter) error {174		c <- r.ExitCode()175		return nil176	})177	return <-c178}179180// A reporter reports output, warnings, and errors.181type reporter struct {182	prev  <-chan *reporterState183	state *reporterState184}185186// reporterState carries the state of a reporter instance.187//188// Only one reporter at a time may have access to a reporterState.189type reporterState struct {190	out, err io.Writer191	exitCode int192}193194// getState blocks until any prior reporters are finished with the reporter195// state, then returns the state for manipulation.196func (r *reporter) getState() *reporterState {197	if r.state == nil {198		r.state = <-r.prev199	}200	return r.state201}202203// Warnf emits a warning message to the reporter's error stream,204// without changing its exit code.205func (r *reporter) Warnf(format string, args ...any) {206	fmt.Fprintf(r.getState().err, format, args...)207}208209// Write emits a slice to the reporter's output stream.210//211// Any error is returned to the caller, and does not otherwise affect the212// reporter's exit code.213func (r *reporter) Write(p []byte) (int, error) {214	return r.getState().out.Write(p)215}216217// Report emits a non-nil error to the reporter's error stream,218// changing its exit code to a nonzero value.219func (r *reporter) Report(err error) {220	if err == nil {221		panic("Report with nil error")222	}223	st := r.getState()224	if err == errFormattingDiffers {225		st.exitCode = 1226	} else {227		scanner.PrintError(st.err, err)228		st.exitCode = 2229	}230}231232func (r *reporter) ExitCode() int {233	return r.getState().exitCode234}235236// If info == nil, we are formatting stdin instead of a file.237// If in == nil, the source is the contents of the file with the given filename.238func processFile(filename string, info fs.FileInfo, in io.Reader, r *reporter) error {239	src, err := readFile(filename, info, in)240	if err != nil {241		return err242	}243244	fileSet := token.NewFileSet()245	// If we are formatting stdin, we accept a program fragment in lieu of a246	// complete source file.247	fragmentOk := info == nil248	file, sourceAdj, indentAdj, err := parse(fileSet, filename, src, fragmentOk)249	if err != nil {250		return err251	}252253	if rewrite != nil {254		if sourceAdj == nil {255			file = rewrite(fileSet, file)256		} else {257			r.Warnf("warning: rewrite ignored for incomplete programs\n")258		}259	}260261	ast.SortImports(fileSet, file)262263	if *simplifyAST {264		simplify(file)265	}266267	res, err := format(fileSet, file, sourceAdj, indentAdj, src, printer.Config{Mode: printerMode, Tabwidth: tabWidth})268	if err != nil {269		return err270	}271272	if !bytes.Equal(src, res) {273		// formatting has changed274		if *list {275			fmt.Fprintln(r, filename)276		}277		if *write {278			if info == nil {279				panic("-w should not have been allowed with stdin")280			}281282			perm := info.Mode().Perm()283			if err := writeFile(filename, src, res, perm, info.Size()); err != nil {284				return err285			}286		}287		if *doDiff {288			newName := filepath.ToSlash(filename)289			oldName := newName + ".orig"290			r.Write(diff.Diff(oldName, src, newName, res))291			return errFormattingDiffers292		}293	}294295	if !*list && !*write && !*doDiff {296		_, err = r.Write(res)297	}298299	return err300}301302// readFile reads the contents of filename, described by info.303// If in is non-nil, readFile reads directly from it.304// Otherwise, readFile opens and reads the file itself,305// with the number of concurrently-open files limited by fdSem.306func readFile(filename string, info fs.FileInfo, in io.Reader) ([]byte, error) {307	if in == nil {308		fdSem <- true309		var err error310		f, err := os.Open(filename)311		if err != nil {312			return nil, err313		}314		in = f315		defer func() {316			f.Close()317			<-fdSem318		}()319	}320321	// Compute the file's size and read its contents with minimal allocations.322	//323	// If we have the FileInfo from filepath.WalkDir, use it to make324	// a buffer of the right size and avoid ReadAll's reallocations.325	//326	// If the size is unknown (or bogus, or overflows an int), fall back to327	// a size-independent ReadAll.328	size := -1329	if info != nil && info.Mode().IsRegular() && int64(int(info.Size())) == info.Size() {330		size = int(info.Size())331	}332	if size+1 <= 0 {333		// The file is not known to be regular, so we don't have a reliable size for it.334		var err error335		src, err := io.ReadAll(in)336		if err != nil {337			return nil, err338		}339		return src, nil340	}341342	// We try to read size+1 bytes so that we can detect modifications: if we343	// read more than size bytes, then the file was modified concurrently.344	// (If that happens, we could, say, append to src to finish the read, or345	// proceed with a truncated buffer — but the fact that it changed at all346	// indicates a possible race with someone editing the file, so we prefer to347	// stop to avoid corrupting it.)348	src := make([]byte, size+1)349	n, err := io.ReadFull(in, src)350	switch err {351	case nil, io.EOF, io.ErrUnexpectedEOF:352		// io.ReadFull returns io.EOF (for an empty file) or io.ErrUnexpectedEOF353		// (for a non-empty file) if the file was changed unexpectedly. Continue354		// with comparing file sizes in those cases.355	default:356		return nil, err357	}358	if n < size {359		return nil, fmt.Errorf("error: size of %s changed during reading (from %d to %d bytes)", filename, size, n)360	} else if n > size {361		return nil, fmt.Errorf("error: size of %s changed during reading (from %d to >=%d bytes)", filename, size, len(src))362	}363	return src[:n], nil364}365366func main() {367	// Arbitrarily limit in-flight work to 2MiB times the number of threads.368	//369	// The actual overhead for the parse tree and output will depend on the370	// specifics of the file, but this at least keeps the footprint of the process371	// roughly proportional to GOMAXPROCS.372	maxWeight := (2 << 20) * int64(runtime.GOMAXPROCS(0))373	s := newSequencer(maxWeight, os.Stdout, os.Stderr)374375	// call gofmtMain in a separate function376	// so that it can use defer and have them377	// run before the exit.378	gofmtMain(s)379	os.Exit(s.GetExitCode())380}381382func gofmtMain(s *sequencer) {383	counter.Open()384	flag.Usage = usage385	flag.Parse()386	counter.Inc("gofmt/invocations")387	counter.CountFlags("gofmt/flag:", *flag.CommandLine)388389	if *cpuprofile != "" {390		fdSem <- true391		f, err := os.Create(*cpuprofile)392		if err != nil {393			s.AddReport(fmt.Errorf("creating cpu profile: %s", err))394			return395		}396		defer func() {397			f.Close()398			<-fdSem399		}()400		pprof.StartCPUProfile(f)401		defer pprof.StopCPUProfile()402	}403404	initParserMode()405	initRewrite()406407	args := flag.Args()408	if len(args) == 0 {409		if *write {410			s.AddReport(fmt.Errorf("error: cannot use -w with standard input"))411			return412		}413		s.Add(0, func(r *reporter) error {414			return processFile("<standard input>", nil, os.Stdin, r)415		})416		return417	}418419	for _, arg := range args {420		// Walk each given argument as a directory tree.421		// If the argument is not a directory, it's always formatted as a Go file.422		// If the argument is a directory, we walk it, ignoring non-Go files.423		if err := filepath.WalkDir(arg, func(path string, d fs.DirEntry, err error) error {424			switch {425			case err != nil:426				return err427			case d.IsDir():428				return nil // simply recurse into directories429			case path == arg:430				// non-directories given as explicit arguments are always formatted431			case !isGoFilename(d.Name()):432				return nil // skip walked non-Go files433			}434			info, err := d.Info()435			if err != nil {436				return err437			}438			s.Add(fileWeight(path, info), func(r *reporter) error {439				return processFile(path, info, nil, r)440			})441			return nil442		}); err != nil {443			s.AddReport(err)444		}445	}446}447448func fileWeight(path string, info fs.FileInfo) int64 {449	if info == nil {450		return exclusive451	}452	if info.Mode().Type() == fs.ModeSymlink {453		var err error454		info, err = os.Stat(path)455		if err != nil {456			return exclusive457		}458	}459	if !info.Mode().IsRegular() {460		// For non-regular files, FileInfo.Size is system-dependent and thus not a461		// reliable indicator of weight.462		return exclusive463	}464	return info.Size()465}466467// writeFile updates a file with the new formatted data.468func writeFile(filename string, orig, formatted []byte, perm fs.FileMode, size int64) error {469	// Make a temporary backup file before rewriting the original file.470	bakname, err := backupFile(filename, orig, perm)471	if err != nil {472		return err473	}474475	fdSem <- true476	defer func() { <-fdSem }()477478	fout, err := os.OpenFile(filename, os.O_WRONLY, perm)479	if err != nil {480		// We couldn't even open the file, so it should481		// not have changed.482		os.Remove(bakname)483		return err484	}485	defer fout.Close() // for error paths486487	restoreFail := func(err error) {488		fmt.Fprintf(os.Stderr, "gofmt: %s: error restoring file to original: %v; backup in %s\n", filename, err, bakname)489	}490491	n, err := fout.Write(formatted)492	if err == nil && int64(n) < size {493		err = fout.Truncate(int64(n))494	}495496	if err != nil {497		// Rewriting the file failed.498499		if n == 0 {500			// Original file unchanged.501			os.Remove(bakname)502			return err503		}504505		// Try to restore the original contents.506507		no, erro := fout.WriteAt(orig, 0)508		if erro != nil {509			// That failed too.510			restoreFail(erro)511			return err512		}513514		if no < n {515			// Original file is shorter. Truncate.516			if erro = fout.Truncate(int64(no)); erro != nil {517				restoreFail(erro)518				return err519			}520		}521522		if erro := fout.Close(); erro != nil {523			restoreFail(erro)524			return err525		}526527		// Original contents restored.528		os.Remove(bakname)529		return err530	}531532	if err := fout.Close(); err != nil {533		restoreFail(err)534		return err535	}536537	// File updated.538	os.Remove(bakname)539	return nil540}541542// backupFile writes data to a new file named filename<number> with permissions perm,543// with <number> randomly chosen such that the file name is unique. backupFile returns544// the chosen file name.545func backupFile(filename string, data []byte, perm fs.FileMode) (string, error) {546	fdSem <- true547	defer func() { <-fdSem }()548549	nextRandom := func() string {550		return strconv.Itoa(rand.Int())551	}552553	dir, base := filepath.Split(filename)554	var (555		bakname string556		f       *os.File557	)558	for {559		bakname = filepath.Join(dir, base+"."+nextRandom())560		var err error561		f, err = os.OpenFile(bakname, os.O_RDWR|os.O_CREATE|os.O_EXCL, perm)562		if err == nil {563			break564		}565		if !os.IsExist(err) {566			return "", err567		}568	}569570	// write data to backup file571	_, err := f.Write(data)572	if err1 := f.Close(); err == nil {573		err = err1574	}575576	return bakname, err577}

Code quality findings 4

Ensure errors are handled or logged
warning correctness unhandled-error
if err != nil {
Placeholder context; replace with a meaningful context in production code
info correctness placeholder-context
if err := s.sem.Acquire(context.TODO(), weight); err != nil {
Manual scheduling hint; usually unnecessary and indicates deeper design issues
info correctness manual-scheduling
maxWeight := (2 << 20) * int64(runtime.GOMAXPROCS(0))
Can cause issues on Windows consider filepath.Join instead
info correctness path-join-windows
bakname = filepath.Join(dir, base+"."+nextRandom())

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.