diff --git a/cmd/gazelle/fix-update.go b/cmd/gazelle/fix-update.go index 67c1090b2..fe8fee57f 100644 --- a/cmd/gazelle/fix-update.go +++ b/cmd/gazelle/fix-update.go @@ -21,7 +21,6 @@ import ( "errors" "flag" "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -493,7 +492,7 @@ func runFixUpdate(wd string, cmd command, args []string) (err error) { } } if uc.patchPath != "" { - if err := ioutil.WriteFile(uc.patchPath, uc.patchBuffer.Bytes(), 0o666); err != nil { + if err := os.WriteFile(uc.patchPath, uc.patchBuffer.Bytes(), 0o666); err != nil { return err } } @@ -676,12 +675,12 @@ func findOutputPath(c *config.Config, f *rule.File) string { } outputDir := filepath.Join(baseDir, filepath.FromSlash(f.Pkg)) defaultOutputPath := filepath.Join(outputDir, c.DefaultBuildFileName()) - files, err := ioutil.ReadDir(outputDir) + ents, err := os.ReadDir(outputDir) if err != nil { // Ignore error. Directory probably doesn't exist. return defaultOutputPath } - outputPath := rule.MatchBuildFileName(outputDir, c.ValidBuildFileNames, files) + outputPath := rule.MatchBuildFile(outputDir, c.ValidBuildFileNames, ents) if outputPath == "" { return defaultOutputPath } diff --git a/language/go/config.go b/language/go/config.go index 96295c143..e23ed5437 100644 --- a/language/go/config.go +++ b/language/go/config.go @@ -20,7 +20,6 @@ import ( "flag" "fmt" "go/build" - "io/ioutil" "log" "os" "path" @@ -726,7 +725,7 @@ Update io_bazel_rules_go to a newer version in your WORKSPACE file.` // Bazel has already fetched io_bazel_rules_go. We can read its version // from //go:def.bzl. defBzlPath := filepath.Join(rulesGoPath, "go", "def.bzl") - defBzlContent, err := ioutil.ReadFile(defBzlPath) + defBzlContent, err := os.ReadFile(defBzlPath) if err != nil { return nil, err } @@ -799,7 +798,7 @@ func detectNamingConvention(c *config.Config, rootFile *rule.File) namingConvent var f *rule.File for _, name := range c.ValidBuildFileNames { fpath := filepath.Join(dir, name) - data, err := ioutil.ReadFile(fpath) + data, err := os.ReadFile(fpath) if err != nil { continue } @@ -821,15 +820,15 @@ func detectNamingConvention(c *config.Config, rootFile *rule.File) namingConvent } } - infos, err := ioutil.ReadDir(c.RepoRoot) + ents, err := os.ReadDir(c.RepoRoot) if err != nil { return importNamingConvention } - for _, info := range infos { - if !info.IsDir() { + for _, ent := range ents { + if !ent.IsDir() { continue } - dirName := info.Name() + dirName := ent.Name() dirNC := detectInDir(filepath.Join(c.RepoRoot, dirName), dirName) if dirNC == unknownNamingConvention { continue diff --git a/rule/rule.go b/rule/rule.go index de92c0119..4b22c917c 100644 --- a/rule/rule.go +++ b/rule/rule.go @@ -27,7 +27,7 @@ package rule import ( "fmt" - "io/ioutil" + "io/fs" "os" "path/filepath" "sort" @@ -95,7 +95,7 @@ func EmptyFile(path, pkg string) *File { // This function returns I/O and parse errors without modification. It's safe // to use os.IsNotExist and similar predicates. func LoadFile(path, pkg string) (*File, error) { - data, err := ioutil.ReadFile(path) + data, err := os.ReadFile(path) if err != nil { return nil, err } @@ -105,7 +105,7 @@ func LoadFile(path, pkg string) (*File, error) { // LoadWorkspaceFile is similar to LoadFile but parses the file as a WORKSPACE // file. func LoadWorkspaceFile(path, pkg string) (*File, error) { - data, err := ioutil.ReadFile(path) + data, err := os.ReadFile(path) if err != nil { return nil, err } @@ -118,7 +118,7 @@ func LoadWorkspaceFile(path, pkg string) (*File, error) { // The function's syntax tree will be returned within File and can be modified by // Sync and Save calls. func LoadMacroFile(path, pkg, defName string) (*File, error) { - data, err := ioutil.ReadFile(path) + data, err := os.ReadFile(path) if err != nil { return nil, err } @@ -253,10 +253,22 @@ func scanExprs(defName string, stmt []bzl.Expr) (rules []*Rule, loads []*Load, f return rules, loads, fn } -// MatchBuildFileName looks for a file in files that has a name from names. +// MatchBuildFile looks for a file in files that has a name from names. // If there is at least one matching file, a path will be returned by joining // dir and the first matching name. If there are no matching files, the // empty string is returned. +func MatchBuildFile(dir string, names []string, ents []fs.DirEntry) string { + for _, name := range names { + for _, ent := range ents { + if ent.Name() == name && !ent.IsDir() { + return filepath.Join(dir, name) + } + } + } + return "" +} + +// Deprecated: Prefer MatchBuildFile, it's more efficient to fetch a []fs.DirEntry func MatchBuildFileName(dir string, names []string, files []os.FileInfo) string { for _, name := range names { for _, fi := range files { @@ -412,7 +424,7 @@ func (f *File) SortMacro() { func (f *File) Save(path string) error { f.Sync() f.Content = bzl.Format(f.File) - return ioutil.WriteFile(path, f.Content, 0o666) + return os.WriteFile(path, f.Content, 0o666) } // HasDefaultVisibility returns whether the File contains a "package" rule with @@ -757,17 +769,19 @@ func NewRule(kind, name string) *Rule { // is either `*bzl.DotExpr` or `*bzl.Ident`. // // For `myKind` kind it returns: -// &bzl.Ident{ -// Name: "myKind" -// } +// +// &bzl.Ident{ +// Name: "myKind" +// } // // For `myKind.inner` kind it returns: -// &bzl.DotExpr{ -// Name: "inner", -// X: &bzl.Ident { -// Name: "myKind" -// } -// } +// +// &bzl.DotExpr{ +// Name: "inner", +// X: &bzl.Ident { +// Name: "myKind" +// } +// } func createDotExpr(kind string) bzl.Expr { var expr bzl.Expr parts := strings.Split(kind, ".") diff --git a/walk/walk.go b/walk/walk.go index 8413e8c57..7128d4684 100644 --- a/walk/walk.go +++ b/walk/walk.go @@ -19,7 +19,6 @@ package walk import ( "io/fs" - "io/ioutil" "log" "os" "path" @@ -125,13 +124,13 @@ func Walk(c *config.Config, cexts []config.Configurer, dirs []string, mode Mode, // TODO: OPT: ReadDir stats all the files, which is slow. We just care about // names and modes, so we should use something like // golang.org/x/tools/internal/fastwalk to speed this up. - files, err := ioutil.ReadDir(dir) + ents, err := os.ReadDir(dir) if err != nil { log.Print(err) return } - f, err := loadBuildFile(c, rel, dir, files) + f, err := loadBuildFile(c, rel, dir, ents) if err != nil { log.Print(err) if c.Strict { @@ -150,13 +149,13 @@ func Walk(c *config.Config, cexts []config.Configurer, dirs []string, mode Mode, } var subdirs, regularFiles []string - for _, fi := range files { - base := fi.Name() - fi := resolveFileInfo(wc, dir, rel, fi) + for _, ent := range ents { + base := ent.Name() + ent := resolveFileInfo(wc, dir, rel, ent) switch { - case fi == nil: + case ent == nil: continue - case fi.IsDir(): + case ent.IsDir(): subdirs = append(subdirs, base) default: regularFiles = append(regularFiles, base) @@ -251,18 +250,18 @@ func shouldVisit(rel string, mode Mode, updateParent bool, updateRels map[string } } -func loadBuildFile(c *config.Config, pkg, dir string, files []os.FileInfo) (*rule.File, error) { +func loadBuildFile(c *config.Config, pkg, dir string, ents []fs.DirEntry) (*rule.File, error) { var err error readDir := dir - readFiles := files + readEnts := ents if c.ReadBuildFilesDir != "" { readDir = filepath.Join(c.ReadBuildFilesDir, filepath.FromSlash(pkg)) - readFiles, err = ioutil.ReadDir(readDir) + readEnts, err = os.ReadDir(readDir) if err != nil { return nil, err } } - path := rule.MatchBuildFileName(readDir, c.ValidBuildFileNames, readFiles) + path := rule.MatchBuildFile(readDir, c.ValidBuildFileNames, readEnts) if path == "" { return nil, nil } @@ -315,16 +314,16 @@ func findGenFiles(wc *walkConfig, f *rule.File) []string { return genFiles } -func resolveFileInfo(wc *walkConfig, dir, rel string, fi fs.FileInfo) fs.FileInfo { - base := fi.Name() +func resolveFileInfo(wc *walkConfig, dir, rel string, ent fs.DirEntry) fs.DirEntry { + base := ent.Name() if base == "" || wc.isExcluded(rel, base) { return nil } - if fi.Mode()&os.ModeSymlink == 0 { + if ent.Type()&os.ModeSymlink == 0 { // Not a symlink, use the original FileInfo. - return fi + return ent } - if !wc.shouldFollow(rel, fi.Name()) { + if !wc.shouldFollow(rel, ent.Name()) { // A symlink, but not one we should follow. return nil } @@ -333,5 +332,5 @@ func resolveFileInfo(wc *walkConfig, dir, rel string, fi fs.FileInfo) fs.FileInf // A symlink, but not one we could resolve. return nil } - return fi + return fs.FileInfoToDirEntry(fi) }