From 498908831b05575080a44a1214d4fb8e96b8bb2f Mon Sep 17 00:00:00 2001 From: Iliya Lyan <68940374+12ya@users.noreply.github.com> Date: Fri, 23 May 2025 07:41:12 +0900 Subject: [PATCH 1/2] replace deprecated io/ioutil create separate function for retrieving file info from the path, export the func --- go/buildutil/util.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/go/buildutil/util.go b/go/buildutil/util.go index bee6390de4c..b7f5022be8c 100644 --- a/go/buildutil/util.go +++ b/go/buildutil/util.go @@ -11,7 +11,7 @@ import ( "go/parser" "go/token" "io" - "io/ioutil" + "io/fs" "os" "path" "path/filepath" @@ -180,7 +180,23 @@ func ReadDir(ctxt *build.Context, path string) ([]os.FileInfo, error) { if ctxt.ReadDir != nil { return ctxt.ReadDir(path) } - return ioutil.ReadDir(path) + return GetFileInfos(path) +} + +func GetFileInfos(path string) ([]os.FileInfo, error) { + entries, err := os.ReadDir(path) + if err != nil { + return nil, err + } + infos := make([]fs.FileInfo, 0, len(entries)) + for _, entry := range entries { + info, err := entry.Info() + if err != nil { + continue + } + infos = append(infos, info) + } + return infos, nil } // SplitPathList behaves like filepath.SplitList, From eda2f3c5ee0facef6f2a07a35f7c5f132e26553f Mon Sep 17 00:00:00 2001 From: Iliya Lyan <68940374+12ya@users.noreply.github.com> Date: Fri, 23 May 2025 07:41:55 +0900 Subject: [PATCH 2/2] godoc/vfs/os.go, internal/imports/fix.go: replace deprecated ioutil.ReadDir with internal buildutil.GetFileInfos --- godoc/vfs/os.go | 5 +++-- internal/imports/fix.go | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/godoc/vfs/os.go b/godoc/vfs/os.go index fe21a58662e..8076cd112bc 100644 --- a/godoc/vfs/os.go +++ b/godoc/vfs/os.go @@ -7,12 +7,13 @@ package vfs import ( "fmt" "go/build" - "io/ioutil" "os" pathpkg "path" "path/filepath" "runtime" "slices" + + "golang.org/x/tools/go/buildutil" ) // We expose a new variable because otherwise we need to copy the findGOROOT logic again @@ -100,5 +101,5 @@ func (root osFS) Stat(path string) (os.FileInfo, error) { } func (root osFS) ReadDir(path string) ([]os.FileInfo, error) { - return ioutil.ReadDir(root.resolve(path)) // is sorted + return buildutil.GetFileInfos(root.resolve(path)) // is sorted } diff --git a/internal/imports/fix.go b/internal/imports/fix.go index d2e275934e4..b592dbfb54b 100644 --- a/internal/imports/fix.go +++ b/internal/imports/fix.go @@ -15,7 +15,6 @@ import ( "go/token" "go/types" "io/fs" - "io/ioutil" "os" "path" "path/filepath" @@ -30,6 +29,7 @@ import ( "maps" "golang.org/x/tools/go/ast/astutil" + "golang.org/x/tools/go/buildutil" "golang.org/x/tools/internal/event" "golang.org/x/tools/internal/gocommand" "golang.org/x/tools/internal/gopathwalk" @@ -1081,7 +1081,7 @@ func (e *ProcessEnv) buildContext() (*build.Context, error) { // HACK: setting any of the Context I/O hooks prevents Import from invoking // 'go list', regardless of GO111MODULE. This is undocumented, but it's // unlikely to change before GOPATH support is removed. - ctx.ReadDir = ioutil.ReadDir + ctx.ReadDir = buildutil.GetFileInfos return &ctx, nil }