Skip to content

Commit

Permalink
tools: replace references to obsolete package ioutils
Browse files Browse the repository at this point in the history
ioutil defines 7 functions. 6 of these are replaced by
functions in io or os with the same signature.
ReadDir is deprecated, but the suggested replacement has a different
signature.

These changes were generated by a program, with some manual adjutments.
The program replaces ReadDir with a call to a function named ioutilReadDir
that has the same signature. The code for this function
is added to files if necessary. The program replaces all the others
with their new versions. The program removes the 'io/ioutil' import
and adds, as necessary, 'os', 'io', and 'io/fs', the latter being
needed for the signature of ioutilReadDir.

The automatic process fails in a few ways:
1. ReadFile occurs only in a comment but the program adds an unneeded import.
2. ioutilReadDir is added to more than one file in the same package
Both of these could be viewed as bugs and fixed by looking harder.

After manual adjustment, two tests failed:
1. gopls/internal/lsp/regtesg/mis:TestGenerateProgress. The reason
	 was a use of ioutil in a txtar constant. The calls were changed,
	 but the code is not smart enough to change the import inside the
	 string constant. (Or it's not smart enough not to change the
	 contents of a string.)
2. gopls/internal/lsp/analysis/deprecated, which wants to see a use
	 of ioutil

These tests were adjused by hand, and all tests (-short) pass.

Change-Id: If9efe40bbb0edda36173d9a88afaf71245db8e79
Reviewed-on: https://go-review.googlesource.com/c/tools/+/527675
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Peter Weinberger <pjw@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
  • Loading branch information
pjweinbgo committed Sep 12, 2023
1 parent 0b3914d commit 559c430
Show file tree
Hide file tree
Showing 117 changed files with 389 additions and 346 deletions.
3 changes: 1 addition & 2 deletions cmd/auth/netrcauth/netrcauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main

import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
Expand All @@ -41,7 +40,7 @@ func main() {

path := os.Args[1]

data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return
Expand Down
3 changes: 1 addition & 2 deletions cmd/bundle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ import (
"go/printer"
"go/token"
"go/types"
"io/ioutil"
"log"
"os"
"strconv"
Expand Down Expand Up @@ -149,7 +148,7 @@ func main() {
log.Fatal(err)
}
if *outputFile != "" {
err := ioutil.WriteFile(*outputFile, code, 0666)
err := os.WriteFile(*outputFile, code, 0666)
if err != nil {
log.Fatal(err)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/bundle/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package main

import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"runtime"
Expand All @@ -18,7 +17,7 @@ import (
func TestBundle(t *testing.T) { packagestest.TestAll(t, testBundle) }
func testBundle(t *testing.T, x packagestest.Exporter) {
load := func(name string) string {
data, err := ioutil.ReadFile(name)
data, err := os.ReadFile(name)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -53,7 +52,7 @@ func testBundle(t *testing.T, x packagestest.Exporter) {
if got, want := string(out), load("testdata/out.golden"); got != want {
t.Errorf("-- got --\n%s\n-- want --\n%s\n-- diff --", got, want)

if err := ioutil.WriteFile("testdata/out.got", out, 0644); err != nil {
if err := os.WriteFile("testdata/out.got", out, 0644); err != nil {
t.Fatal(err)
}
t.Log(diff("testdata/out.golden", "testdata/out.got"))
Expand Down
11 changes: 5 additions & 6 deletions cmd/compilebench/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -388,7 +387,7 @@ func (c compile) run(name string, count int) error {
opath := pkg.Dir + "/_compilebench_.o"
if *flagObj {
// TODO(josharian): object files are big; just read enough to find what we seek.
data, err := ioutil.ReadFile(opath)
data, err := os.ReadFile(opath)
if err != nil {
log.Print(err)
}
Expand Down Expand Up @@ -498,7 +497,7 @@ func runBuildCmd(name string, count int, dir, tool string, args []string) error
haveAllocs, haveRSS := false, false
var allocs, allocbytes, rssbytes int64
if *flagAlloc || *flagMemprofile != "" {
out, err := ioutil.ReadFile(dir + "/_compilebench_.memprof")
out, err := os.ReadFile(dir + "/_compilebench_.memprof")
if err != nil {
log.Print("cannot find memory profile after compilation")
}
Expand Down Expand Up @@ -531,23 +530,23 @@ func runBuildCmd(name string, count int, dir, tool string, args []string) error
if *flagCount != 1 {
outpath = fmt.Sprintf("%s_%d", outpath, count)
}
if err := ioutil.WriteFile(outpath, out, 0666); err != nil {
if err := os.WriteFile(outpath, out, 0666); err != nil {
log.Print(err)
}
}
os.Remove(dir + "/_compilebench_.memprof")
}

if *flagCpuprofile != "" {
out, err := ioutil.ReadFile(dir + "/_compilebench_.cpuprof")
out, err := os.ReadFile(dir + "/_compilebench_.cpuprof")
if err != nil {
log.Print(err)
}
outpath := *flagCpuprofile
if *flagCount != 1 {
outpath = fmt.Sprintf("%s_%d", outpath, count)
}
if err := ioutil.WriteFile(outpath, out, 0666); err != nil {
if err := os.WriteFile(outpath, out, 0666); err != nil {
log.Print(err)
}
os.Remove(dir + "/_compilebench_.cpuprof")
Expand Down
3 changes: 1 addition & 2 deletions cmd/eg/eg.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"go/parser"
"go/token"
"go/types"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -77,7 +76,7 @@ func doMain() error {
if err != nil {
return err
}
template, err := ioutil.ReadFile(tAbs)
template, err := os.ReadFile(tAbs)
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/file2fuzz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand All @@ -51,7 +50,7 @@ func dirWriter(dir string) func([]byte) error {
if err := os.MkdirAll(dir, 0777); err != nil {
return err
}
if err := ioutil.WriteFile(name, b, 0666); err != nil {
if err := os.WriteFile(name, b, 0666); err != nil {
os.Remove(name)
return err
}
Expand Down Expand Up @@ -98,14 +97,14 @@ func convert(inputArgs []string, outputArg string) error {
output = dirWriter(outputArg)
} else {
output = func(b []byte) error {
return ioutil.WriteFile(outputArg, b, 0666)
return os.WriteFile(outputArg, b, 0666)
}
}
}
}

for _, f := range input {
b, err := ioutil.ReadAll(f)
b, err := io.ReadAll(f)
if err != nil {
return fmt.Errorf("unable to read input: %s", err)
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/file2fuzz/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package main

import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -118,9 +117,9 @@ func TestFile2Fuzz(t *testing.T) {

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tmp, err := ioutil.TempDir(os.TempDir(), "file2fuzz")
tmp, err := os.MkdirTemp(os.TempDir(), "file2fuzz")
if err != nil {
t.Fatalf("ioutil.TempDir failed: %s", err)
t.Fatalf("os.MkdirTemp failed: %s", err)
}
defer os.RemoveAll(tmp)
for _, f := range tc.inputFiles {
Expand All @@ -129,7 +128,7 @@ func TestFile2Fuzz(t *testing.T) {
t.Fatalf("failed to create test directory: %s", err)
}
} else {
if err := ioutil.WriteFile(filepath.Join(tmp, f.name), []byte(f.content), 0666); err != nil {
if err := os.WriteFile(filepath.Join(tmp, f.name), []byte(f.content), 0666); err != nil {
t.Fatalf("failed to create test input file: %s", err)
}
}
Expand All @@ -146,7 +145,7 @@ func TestFile2Fuzz(t *testing.T) {
}

for _, f := range tc.expectedFiles {
c, err := ioutil.ReadFile(filepath.Join(tmp, f.name))
c, err := os.ReadFile(filepath.Join(tmp, f.name))
if err != nil {
t.Fatalf("failed to read expected output file %q: %s", f.name, err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/fiximports/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ import (
"go/parser"
"go/token"
"io"
"io/ioutil"
"log"
"os"
"path"
Expand All @@ -100,7 +99,7 @@ var (
// seams for testing
var (
stderr io.Writer = os.Stderr
writeFile = ioutil.WriteFile
writeFile = os.WriteFile
)

const usage = `fiximports: rewrite import paths to use canonical package names.
Expand Down
7 changes: 3 additions & 4 deletions cmd/getgo/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -51,7 +50,7 @@ func downloadGoVersion(version, ops, arch, dest string) error {
}
defer resp.Body.Close()

tmpf, err := ioutil.TempFile("", "go")
tmpf, err := os.CreateTemp("", "go")
if err != nil {
return err
}
Expand All @@ -75,7 +74,7 @@ func downloadGoVersion(version, ops, arch, dest string) error {
return fmt.Errorf("Downloading Go sha256 from %s.sha256 failed with HTTP status %s", uri, sresp.Status)
}

shasum, err := ioutil.ReadAll(sresp.Body)
shasum, err := io.ReadAll(sresp.Body)
if err != nil {
return err
}
Expand Down Expand Up @@ -174,7 +173,7 @@ func getLatestGoVersion() (string, error) {
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
b, _ := ioutil.ReadAll(io.LimitReader(resp.Body, 1024))
b, _ := io.ReadAll(io.LimitReader(resp.Body, 1024))
return "", fmt.Errorf("Could not get current Go release: HTTP %d: %q", resp.StatusCode, b)
}
var releases []struct {
Expand Down
3 changes: 1 addition & 2 deletions cmd/getgo/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package main

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -19,7 +18,7 @@ func TestDownloadGoVersion(t *testing.T) {
t.Skipf("Skipping download in short mode")
}

tmpd, err := ioutil.TempDir("", "go")
tmpd, err := os.MkdirTemp("", "go")
if err != nil {
t.Fatal(err)
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/getgo/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"testing"
Expand All @@ -37,7 +36,7 @@ func TestMain(m *testing.M) {
}

func createTmpHome(t *testing.T) string {
tmpd, err := ioutil.TempDir("", "testgetgo")
tmpd, err := os.MkdirTemp("", "testgetgo")
if err != nil {
t.Fatalf("creating test tempdir failed: %v", err)
}
Expand Down Expand Up @@ -86,7 +85,7 @@ func TestCommandVerbose(t *testing.T) {
if err != nil {
t.Fatal(err)
}
b, err := ioutil.ReadFile(shellConfig)
b, err := os.ReadFile(shellConfig)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -122,7 +121,7 @@ func TestCommandPathExists(t *testing.T) {
if err != nil {
t.Fatal(err)
}
b, err := ioutil.ReadFile(shellConfig)
b, err := os.ReadFile(shellConfig)
if err != nil {
t.Fatal(err)
}
Expand All @@ -146,7 +145,7 @@ export PATH=$PATH:%s/go/bin
t.Fatal(err)
}

b, err = ioutil.ReadFile(shellConfig)
b, err = os.ReadFile(shellConfig)
if err != nil {
t.Fatal(err)
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/getgo/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
package main

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)

func TestAppendPath(t *testing.T) {
tmpd, err := ioutil.TempDir("", "go")
tmpd, err := os.MkdirTemp("", "go")
if err != nil {
t.Fatal(err)
}
Expand All @@ -35,7 +34,7 @@ func TestAppendPath(t *testing.T) {
if err != nil {
t.Fatal(err)
}
b, err := ioutil.ReadFile(shellConfig)
b, err := os.ReadFile(shellConfig)
if err != nil {
t.Fatal(err)
}
Expand All @@ -49,7 +48,7 @@ func TestAppendPath(t *testing.T) {
if err := appendToPATH(filepath.Join(GOPATH, "bin")); err != nil {
t.Fatal(err)
}
b, err = ioutil.ReadFile(shellConfig)
b, err = os.ReadFile(shellConfig)
if err != nil {
t.Fatal(err)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/go-contrib-init/contrib.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"fmt"
"go/build"
exec "golang.org/x/sys/execabs"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -66,7 +65,7 @@ func detectrepo() string {
var googleSourceRx = regexp.MustCompile(`(?m)^(go|go-review)?\.googlesource.com\b`)

func checkCLA() {
slurp, err := ioutil.ReadFile(cookiesFile())
slurp, err := os.ReadFile(cookiesFile())
if err != nil && !os.IsNotExist(err) {
log.Fatal(err)
}
Expand Down Expand Up @@ -135,7 +134,7 @@ func checkGoroot() {
"your GOROOT or set it to the path of your development version\n"+
"of Go.", v)
}
slurp, err := ioutil.ReadFile(filepath.Join(v, "VERSION"))
slurp, err := os.ReadFile(filepath.Join(v, "VERSION"))
if err == nil {
slurp = bytes.TrimSpace(slurp)
log.Fatalf("Your GOROOT environment variable is set to %q\n"+
Expand Down
Loading

0 comments on commit 559c430

Please sign in to comment.