-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathformat.go
590 lines (494 loc) · 15 KB
/
format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
package cli
import (
"context"
"errors"
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
"runtime"
"runtime/pprof"
"syscall"
"time"
"git.numtide.com/numtide/treefmt/format"
"git.numtide.com/numtide/treefmt/stats"
"git.numtide.com/numtide/treefmt/cache"
"git.numtide.com/numtide/treefmt/config"
"git.numtide.com/numtide/treefmt/walk"
"github.com/charmbracelet/log"
"golang.org/x/sync/errgroup"
)
const (
BatchSize = 1024
)
var ErrFailOnChange = errors.New("unexpected changes detected, --fail-on-change is enabled")
func (f *Format) Run() (err error) {
// set log level and other options
f.configureLogging()
// ci mode
if f.Ci {
f.NoCache = true
f.FailOnChange = true
// ensure INFO level
if f.Verbosity < 1 {
f.Verbosity = 1
}
// reconfigure logging
f.configureLogging()
log.Info("ci mode enabled")
startAfter := time.Now().
// truncate to second precision
Truncate(time.Second).
// add one second
Add(1 * time.Second).
// a little extra to ensure we don't start until the next second
Add(10 * time.Millisecond)
log.Debugf("waiting until %v before continuing", startAfter)
// Wait until we tick over into the next second before processing to ensure our EPOCH level modtime comparisons
// for change detection are accurate.
// This can fail in CI between checkout and running treefmt if everything happens too quickly.
// For humans, the second level precision should not be a problem as they are unlikely to run treefmt in sub-second succession.
<-time.After(time.Until(startAfter))
}
// cpu profiling
if f.CpuProfile != "" {
cpuProfile, err := os.Create(f.CpuProfile)
if err != nil {
return fmt.Errorf("failed to open file for writing cpu profile: %w", err)
} else if err = pprof.StartCPUProfile(cpuProfile); err != nil {
return fmt.Errorf("failed to start cpu profile: %w", err)
}
defer func() {
pprof.StopCPUProfile()
if err := cpuProfile.Close(); err != nil {
log.Errorf("failed to close cpu profile: %v", err)
}
}()
}
// create a prefixed logger
log.SetPrefix("format")
// ensure cache is closed on return
defer func() {
if err := cache.Close(); err != nil {
log.Errorf("failed to close cache: %v", err)
}
}()
// find the config file unless specified
if f.ConfigFile == "" {
pwd, err := os.Getwd()
if err != nil {
return err
}
f.ConfigFile, _, err = findUp(pwd, "treefmt.toml", ".treefmt.toml")
if err != nil {
return err
}
}
// default the tree root to the directory containing the config file
if f.TreeRoot == "" {
f.TreeRoot = filepath.Dir(f.ConfigFile)
}
// search the tree root using the --tree-root-file if specified
if f.TreeRootFile != "" {
pwd, err := os.Getwd()
if err != nil {
return err
}
_, f.TreeRoot, err = findUp(pwd, f.TreeRootFile)
if err != nil {
return err
}
}
log.Debugf("config-file=%s tree-root=%s", f.ConfigFile, f.TreeRoot)
// read config
cfg, err := config.ReadFile(f.ConfigFile, f.Formatters)
if err != nil {
return fmt.Errorf("failed to read config file %v: %w", f.ConfigFile, err)
}
// compile global exclude globs
if f.globalExcludes, err = format.CompileGlobs(cfg.Global.Excludes); err != nil {
return fmt.Errorf("failed to compile global excludes: %w", err)
}
// initialise formatters
f.formatters = make(map[string]*format.Formatter)
for name, formatterCfg := range cfg.Formatters {
formatter, err := format.NewFormatter(name, f.TreeRoot, formatterCfg)
if errors.Is(err, format.ErrCommandNotFound) && f.AllowMissingFormatter {
log.Debugf("formatter command not found: %v", name)
continue
} else if err != nil {
return fmt.Errorf("%w: failed to initialise formatter: %v", err, name)
}
// store formatter by name
f.formatters[name] = formatter
}
// open the cache if configured
if !f.NoCache {
if err = cache.Open(f.TreeRoot, f.ClearCache, f.formatters); err != nil {
// if we can't open the cache, we log a warning and fallback to no cache
log.Warnf("failed to open cache: %v", err)
f.NoCache = true
}
}
// create an app context and listen for shutdown
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
exit := make(chan os.Signal, 1)
signal.Notify(exit, os.Interrupt, syscall.SIGTERM)
<-exit
cancel()
}()
// initialise stats collection
stats.Init()
// create an overall error group for executing high level tasks concurrently
eg, ctx := errgroup.WithContext(ctx)
// create a channel for files needing to be processed
// we use a multiple of batch size here as a rudimentary concurrency optimization based on the host machine
f.filesCh = make(chan *walk.File, BatchSize*runtime.NumCPU())
// create a channel for files that have been formatted
f.formattedCh = make(chan *walk.File, cap(f.filesCh))
// create a channel for files that have been processed
f.processedCh = make(chan *walk.File, cap(f.filesCh))
// start concurrent processing tasks in reverse order
eg.Go(f.updateCache(ctx))
eg.Go(f.detectFormatted(ctx))
eg.Go(f.applyFormatters(ctx))
eg.Go(f.walkFilesystem(ctx))
// wait for everything to complete
return eg.Wait()
}
func (f *Format) walkFilesystem(ctx context.Context) func() error {
return func() error {
eg, ctx := errgroup.WithContext(ctx)
pathsCh := make(chan string, BatchSize)
// By default, we use the cli arg, but if the stdin flag has been set we force a filesystem walk
// since we will only be processing one file from a temp directory
walkerType := f.Walk
if f.Stdin {
walkerType = walk.Filesystem
// check we have only received one path arg which we use for the file extension / matching to formatters
if len(f.Paths) != 1 {
return fmt.Errorf("only one path should be specified when using the --stdin flag")
}
// read stdin into a temporary file with the same file extension
pattern := fmt.Sprintf("*%s", filepath.Ext(f.Paths[0]))
file, err := os.CreateTemp("", pattern)
if err != nil {
return fmt.Errorf("failed to create a temporary file for processing stdin: %w", err)
}
if _, err = io.Copy(file, os.Stdin); err != nil {
return fmt.Errorf("failed to copy stdin into a temporary file")
}
f.Paths[0] = file.Name()
}
walkPaths := func() error {
defer close(pathsCh)
var idx int
for idx < len(f.Paths) {
select {
case <-ctx.Done():
return ctx.Err()
default:
pathsCh <- f.Paths[idx]
idx += 1
}
}
return nil
}
if len(f.Paths) > 0 {
eg.Go(walkPaths)
} else {
// no explicit paths to process, so we only need to process root
pathsCh <- f.TreeRoot
close(pathsCh)
}
// create a filesystem walker
walker, err := walk.New(walkerType, f.TreeRoot, pathsCh)
if err != nil {
return fmt.Errorf("failed to create walker: %w", err)
}
// close the files channel when we're done walking the file system
defer close(f.filesCh)
// if no cache has been configured, or we are processing from stdin, we invoke the walker directly
if f.NoCache || f.Stdin {
return walker.Walk(ctx, func(file *walk.File, err error) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
stats.Add(stats.Traversed, 1)
stats.Add(stats.Emitted, 1)
f.filesCh <- file
return nil
}
})
}
// otherwise we pass the walker to the cache and have it generate files for processing based on whether or not
// they have been added/changed since the last invocation
if err = cache.ChangeSet(ctx, walker, f.filesCh); err != nil {
return fmt.Errorf("failed to generate change set: %w", err)
}
return nil
}
}
// applyFormatters
func (f *Format) applyFormatters(ctx context.Context) func() error {
// create our own errgroup for concurrent formatting tasks.
// we don't want a cancel clause, in order to let formatters run up to the end.
fg := errgroup.Group{}
// simple optimization to avoid too many concurrent formatting tasks
// we can queue them up faster than the formatters can process them, this paces things a bit
fg.SetLimit(runtime.NumCPU())
// track batches of formatting task based on their batch keys, which are determined by the unique sequence of
// formatters which should be applied to their respective files
batches := make(map[string][]*format.Task)
apply := func(key string, flush bool) {
// lookup the batch and exit early if it's empty
batch := batches[key]
if len(batch) == 0 {
return
}
// process the batch if it's full, or we've been asked to flush partial batches
if flush || len(batch) == BatchSize {
// copy the batch as we re-use it for the next batch
tasks := make([]*format.Task, len(batch))
copy(tasks, batch)
// asynchronously apply the sequence formatters to the batch
fg.Go(func() error {
// iterate the formatters, applying them in sequence to the batch of tasks
// we get the formatters list from the first task since they have all the same formatters list
for _, f := range tasks[0].Formatters {
if err := f.Apply(ctx, tasks); err != nil {
return err
}
}
// pass each file to the formatted channel
for _, task := range tasks {
f.formattedCh <- task.File
}
return nil
})
// reset the batch
batches[key] = batch[:0]
}
}
tryApply := func(task *format.Task) {
// append to batch
key := task.BatchKey
batches[key] = append(batches[key], task)
// try to apply
apply(key, false)
}
return func() error {
defer func() {
// close processed channel
close(f.formattedCh)
}()
// iterate the files channel
for file := range f.filesCh {
// first check if this file has been globally excluded
if format.PathMatches(file.RelPath, f.globalExcludes) {
log.Debugf("path matched global excludes: %s", file.RelPath)
// mark it as processed and continue to the next
f.formattedCh <- file
continue
}
// check if any formatters are interested in this file
var matches []*format.Formatter
for _, formatter := range f.formatters {
if formatter.Wants(file) {
matches = append(matches, formatter)
}
}
// see if any formatters matched
if len(matches) == 0 {
if f.OnUnmatched == log.FatalLevel {
return fmt.Errorf("no formatter for path: %s", file.RelPath)
}
log.Logf(f.OnUnmatched, "no formatter for path: %s", file.RelPath)
// mark it as processed and continue to the next
f.formattedCh <- file
} else {
// record the match
stats.Add(stats.Matched, 1)
// create a new format task, add it to a batch based on its batch key and try to apply if the batch is full
task := format.NewTask(file, matches)
tryApply(&task)
}
}
// flush any partial batches which remain
for key := range batches {
apply(key, true)
}
// wait for all outstanding formatting tasks to complete
if err := fg.Wait(); err != nil {
return fmt.Errorf("formatting failure: %w", err)
}
return nil
}
}
func (f *Format) detectFormatted(ctx context.Context) func() error {
return func() error {
defer func() {
// close formatted channel
close(f.processedCh)
}()
for {
select {
// detect ctx cancellation
case <-ctx.Done():
return ctx.Err()
// take the next file that has been processed
case file, ok := <-f.formattedCh:
if !ok {
// channel has been closed, no further files to process
return nil
}
// check if the file has changed
changed, newInfo, err := file.HasChanged()
if err != nil {
return err
}
if changed {
// record the change
stats.Add(stats.Formatted, 1)
logMethod := log.Debug
if f.FailOnChange {
// surface the changed file more obviously
logMethod = log.Error
}
// log the change
logMethod(
"file has changed",
"path", file.RelPath,
"prev_size", file.Info.Size(),
"prev_mod_time", file.Info.ModTime().Truncate(time.Second),
"current_size", newInfo.Size(),
"current_mod_time", newInfo.ModTime().Truncate(time.Second),
)
// update the file info
file.Info = newInfo
}
// mark as processed
f.processedCh <- file
}
}
}
}
func (f *Format) updateCache(ctx context.Context) func() error {
return func() error {
// used to batch updates for more efficient txs
batch := make([]*walk.File, 0, BatchSize)
// apply a batch
processBatch := func() error {
// pass the batch to the cache for updating
if err := cache.Update(batch); err != nil {
return err
}
batch = batch[:0]
return nil
}
// if we are processing from stdin that means we are outputting to stdout, no caching involved
// if f.NoCache is set that means either the user explicitly disabled the cache or we failed to open on
if f.Stdin || f.NoCache {
// do nothing
processBatch = func() error { return nil }
}
LOOP:
for {
select {
// detect ctx cancellation
case <-ctx.Done():
return ctx.Err()
// respond to formatted files
case file, ok := <-f.processedCh:
if !ok {
// channel has been closed, no further files to process
break LOOP
}
if f.Stdin {
// dump file into stdout
f, err := os.Open(file.Path)
if err != nil {
return fmt.Errorf("failed to open %s: %w", file.Path, err)
}
if _, err = io.Copy(os.Stdout, f); err != nil {
return fmt.Errorf("failed to copy %s to stdout: %w", file.Path, err)
}
if err = os.Remove(f.Name()); err != nil {
return fmt.Errorf("failed to remove temp file %s: %w", file.Path, err)
}
continue
}
// append to batch and process if we have enough
batch = append(batch, file)
if len(batch) == BatchSize {
if err := processBatch(); err != nil {
return err
}
}
}
}
// final flush
if err := processBatch(); err != nil {
return err
}
// if fail on change has been enabled, check that no files were actually formatted, throwing an error if so
if f.FailOnChange && stats.Value(stats.Formatted) != 0 {
return ErrFailOnChange
}
// print stats to stdout unless we are processing stdin and printing the results to stdout
if !f.Stdin {
stats.Print()
}
return nil
}
}
func findUp(searchDir string, fileNames ...string) (path string, dir string, err error) {
for _, dir := range eachDir(searchDir) {
for _, f := range fileNames {
path := filepath.Join(dir, f)
if fileExists(path) {
return path, dir, nil
}
}
}
return "", "", fmt.Errorf("could not find %s in %s", fileNames, searchDir)
}
func eachDir(path string) (paths []string) {
path, err := filepath.Abs(path)
if err != nil {
return
}
paths = []string{path}
if path == "/" {
return
}
for i := len(path) - 1; i >= 0; i-- {
if path[i] == os.PathSeparator {
path = path[:i]
if path == "" {
path = "/"
}
paths = append(paths, path)
}
}
return
}
func fileExists(path string) bool {
// Some broken filesystems like SSHFS return file information on stat() but
// then cannot open the file. So we use os.Open.
f, err := os.Open(path)
if err != nil {
return false
}
defer f.Close()
// Next, check that the file is a regular file.
fi, err := f.Stat()
if err != nil {
return false
}
return fi.Mode().IsRegular()
}