-
Notifications
You must be signed in to change notification settings - Fork 1.4k
chore: scripts: make go imports at least 2X faster #11695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "io" | ||
| "io/fs" | ||
| "os" | ||
| "path/filepath" | ||
| "regexp" | ||
| "strings" | ||
|
|
||
| "golang.org/x/tools/imports" | ||
| ) | ||
|
|
||
| var ( | ||
| // groupByPrefixes is the list of import prefixes that should _each_ be grouped separately. | ||
| // See: imports.LocalPrefix. | ||
| groupByPrefixes = []string{ | ||
| "github.com/filecoin-project", | ||
| "github.com/filecoin-project/lotus", | ||
| } | ||
| newline = []byte("\n") | ||
| importBlockRegex = regexp.MustCompile(`(?s)import\s*\((.*?)\)`) | ||
| consecutiveNewlinesRegex = regexp.MustCompile(`\n\s*\n`) | ||
| ) | ||
|
|
||
| func main() { | ||
| if err := filepath.Walk(".", func(path string, info fs.FileInfo, err error) error { | ||
| switch { | ||
| case err != nil: | ||
| return err | ||
| case // Skip the entire "./extern/..." directory and its contents. | ||
| strings.HasPrefix(path, "extern/"): | ||
| return filepath.SkipDir | ||
| case // Skip directories, generated cborgen go files and any other non-go files. | ||
| info.IsDir(), | ||
| strings.HasSuffix(info.Name(), "_cbor_gen.go"), | ||
| !strings.HasSuffix(info.Name(), ".go"): | ||
| return nil | ||
| } | ||
| return fixGoImports(path) | ||
| }); err != nil { | ||
| fmt.Printf("Error fixing go imports: %v\n", err) | ||
|
masih marked this conversation as resolved.
|
||
| os.Exit(1) | ||
| } | ||
| } | ||
|
|
||
| func fixGoImports(path string) error { | ||
| sourceFile, err := os.OpenFile(path, os.O_RDWR, 0666) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer func() { _ = sourceFile.Close() }() | ||
|
|
||
| source, err := io.ReadAll(sourceFile) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| formatted := collapseImportNewlines(source) | ||
| for _, prefix := range groupByPrefixes { | ||
| imports.LocalPrefix = prefix | ||
| formatted, err = imports.Process(path, formatted, nil) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| if !bytes.Equal(source, formatted) { | ||
| if err := replaceFileContent(sourceFile, formatted); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func replaceFileContent(target *os.File, replacement []byte) error { | ||
| if _, err := target.Seek(0, io.SeekStart); err != nil { | ||
| return err | ||
| } | ||
| written, err := target.Write(replacement) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return target.Truncate(int64(written)) | ||
|
Comment on lines
+80
to
+84
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I'd generally recommend that you truncate then write. It shouldn't make any difference at the OS level, but it'll reduce the chances of someone observing a garbage version. (well, actually, it might make a difference with a CoW filesystem...) But this is getting deep into nit forest.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting; thank you for pointing that out |
||
| } | ||
|
|
||
| func collapseImportNewlines(content []byte) []byte { | ||
| return importBlockRegex.ReplaceAllFunc(content, func(importBlock []byte) []byte { | ||
| // Replace consecutive newlines with a single newline within the import block | ||
| return consecutiveNewlinesRegex.ReplaceAll(importBlock, newline) | ||
| }) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.