Skip to content

Commit

Permalink
Remove some usage of deprecated ioutil (bazel-contrib#1704)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzbarsky authored Dec 29, 2023
1 parent 26aa0cf commit a1efe84
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 44 deletions.
7 changes: 3 additions & 4 deletions cmd/gazelle/fix-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
Expand Down
13 changes: 6 additions & 7 deletions language/go/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"flag"
"fmt"
"go/build"
"io/ioutil"
"log"
"os"
"path"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
44 changes: 29 additions & 15 deletions rule/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ package rule

import (
"fmt"
"io/ioutil"
"io/fs"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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, ".")
Expand Down
35 changes: 17 additions & 18 deletions walk/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package walk

import (
"io/fs"
"io/ioutil"
"log"
"os"
"path"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}

0 comments on commit a1efe84

Please sign in to comment.