From c2e4dc5abfc74e003ec69b82946e1a14e3f89ce1 Mon Sep 17 00:00:00 2001 From: Hau Nguyen Date: Tue, 17 Jan 2023 23:50:28 +0700 Subject: [PATCH] feat: cache module name even when read dir to improve perf --- internal/imports/formatter.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/internal/imports/formatter.go b/internal/imports/formatter.go index 2c0293b..a8248e4 100644 --- a/internal/imports/formatter.go +++ b/internal/imports/formatter.go @@ -142,6 +142,13 @@ func (ft *Formatter) formatDir(path string) error { } if dirEntry.IsDir() { + // Get module name ASAP to cache it + moduleName, err := ft.moduleName(path) + if err != nil { + return err + } + ft.log("formatFile: moduleName: [%s]\n", moduleName) + return nil } @@ -401,6 +408,13 @@ func (ft *Formatter) moduleName(path string) (string, error) { dirPath := filepath.Clean(path) var goModPath string for { + ft.muModuleNames.RLock() + if pkgName, ok := ft.moduleNames[dirPath]; ok { + ft.muModuleNames.RUnlock() + return pkgName, nil + } + ft.muModuleNames.RUnlock() + goModPath = filepath.Join(dirPath, "go.mod") fileInfo, err := os.Stat(goModPath) if err == nil && !fileInfo.IsDir() {