Skip to content

Commit 559c430

Browse files
committed
tools: replace references to obsolete package ioutils
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 <[email protected]> Run-TryBot: Peter Weinberger <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent 0b3914d commit 559c430

File tree

117 files changed

+389
-346
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+389
-346
lines changed

cmd/auth/netrcauth/netrcauth.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package main
1616

1717
import (
1818
"fmt"
19-
"io/ioutil"
2019
"log"
2120
"net/http"
2221
"net/url"
@@ -41,7 +40,7 @@ func main() {
4140

4241
path := os.Args[1]
4342

44-
data, err := ioutil.ReadFile(path)
43+
data, err := os.ReadFile(path)
4544
if err != nil {
4645
if os.IsNotExist(err) {
4746
return

cmd/bundle/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ import (
7979
"go/printer"
8080
"go/token"
8181
"go/types"
82-
"io/ioutil"
8382
"log"
8483
"os"
8584
"strconv"
@@ -149,7 +148,7 @@ func main() {
149148
log.Fatal(err)
150149
}
151150
if *outputFile != "" {
152-
err := ioutil.WriteFile(*outputFile, code, 0666)
151+
err := os.WriteFile(*outputFile, code, 0666)
153152
if err != nil {
154153
log.Fatal(err)
155154
}

cmd/bundle/main_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package main
66

77
import (
88
"bytes"
9-
"io/ioutil"
109
"os"
1110
"os/exec"
1211
"runtime"
@@ -18,7 +17,7 @@ import (
1817
func TestBundle(t *testing.T) { packagestest.TestAll(t, testBundle) }
1918
func testBundle(t *testing.T, x packagestest.Exporter) {
2019
load := func(name string) string {
21-
data, err := ioutil.ReadFile(name)
20+
data, err := os.ReadFile(name)
2221
if err != nil {
2322
t.Fatal(err)
2423
}
@@ -53,7 +52,7 @@ func testBundle(t *testing.T, x packagestest.Exporter) {
5352
if got, want := string(out), load("testdata/out.golden"); got != want {
5453
t.Errorf("-- got --\n%s\n-- want --\n%s\n-- diff --", got, want)
5554

56-
if err := ioutil.WriteFile("testdata/out.got", out, 0644); err != nil {
55+
if err := os.WriteFile("testdata/out.got", out, 0644); err != nil {
5756
t.Fatal(err)
5857
}
5958
t.Log(diff("testdata/out.golden", "testdata/out.got"))

cmd/compilebench/main.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ import (
8181
"encoding/json"
8282
"flag"
8383
"fmt"
84-
"io/ioutil"
8584
"log"
8685
"os"
8786
"path/filepath"
@@ -388,7 +387,7 @@ func (c compile) run(name string, count int) error {
388387
opath := pkg.Dir + "/_compilebench_.o"
389388
if *flagObj {
390389
// TODO(josharian): object files are big; just read enough to find what we seek.
391-
data, err := ioutil.ReadFile(opath)
390+
data, err := os.ReadFile(opath)
392391
if err != nil {
393392
log.Print(err)
394393
}
@@ -498,7 +497,7 @@ func runBuildCmd(name string, count int, dir, tool string, args []string) error
498497
haveAllocs, haveRSS := false, false
499498
var allocs, allocbytes, rssbytes int64
500499
if *flagAlloc || *flagMemprofile != "" {
501-
out, err := ioutil.ReadFile(dir + "/_compilebench_.memprof")
500+
out, err := os.ReadFile(dir + "/_compilebench_.memprof")
502501
if err != nil {
503502
log.Print("cannot find memory profile after compilation")
504503
}
@@ -531,23 +530,23 @@ func runBuildCmd(name string, count int, dir, tool string, args []string) error
531530
if *flagCount != 1 {
532531
outpath = fmt.Sprintf("%s_%d", outpath, count)
533532
}
534-
if err := ioutil.WriteFile(outpath, out, 0666); err != nil {
533+
if err := os.WriteFile(outpath, out, 0666); err != nil {
535534
log.Print(err)
536535
}
537536
}
538537
os.Remove(dir + "/_compilebench_.memprof")
539538
}
540539

541540
if *flagCpuprofile != "" {
542-
out, err := ioutil.ReadFile(dir + "/_compilebench_.cpuprof")
541+
out, err := os.ReadFile(dir + "/_compilebench_.cpuprof")
543542
if err != nil {
544543
log.Print(err)
545544
}
546545
outpath := *flagCpuprofile
547546
if *flagCount != 1 {
548547
outpath = fmt.Sprintf("%s_%d", outpath, count)
549548
}
550-
if err := ioutil.WriteFile(outpath, out, 0666); err != nil {
549+
if err := os.WriteFile(outpath, out, 0666); err != nil {
551550
log.Print(err)
552551
}
553552
os.Remove(dir + "/_compilebench_.cpuprof")

cmd/eg/eg.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"go/parser"
1616
"go/token"
1717
"go/types"
18-
"io/ioutil"
1918
"os"
2019
"path/filepath"
2120
"strings"
@@ -77,7 +76,7 @@ func doMain() error {
7776
if err != nil {
7877
return err
7978
}
80-
template, err := ioutil.ReadFile(tAbs)
79+
template, err := os.ReadFile(tAbs)
8180
if err != nil {
8281
return err
8382
}

cmd/file2fuzz/main.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"flag"
2626
"fmt"
2727
"io"
28-
"io/ioutil"
2928
"log"
3029
"os"
3130
"path/filepath"
@@ -51,7 +50,7 @@ func dirWriter(dir string) func([]byte) error {
5150
if err := os.MkdirAll(dir, 0777); err != nil {
5251
return err
5352
}
54-
if err := ioutil.WriteFile(name, b, 0666); err != nil {
53+
if err := os.WriteFile(name, b, 0666); err != nil {
5554
os.Remove(name)
5655
return err
5756
}
@@ -98,14 +97,14 @@ func convert(inputArgs []string, outputArg string) error {
9897
output = dirWriter(outputArg)
9998
} else {
10099
output = func(b []byte) error {
101-
return ioutil.WriteFile(outputArg, b, 0666)
100+
return os.WriteFile(outputArg, b, 0666)
102101
}
103102
}
104103
}
105104
}
106105

107106
for _, f := range input {
108-
b, err := ioutil.ReadAll(f)
107+
b, err := io.ReadAll(f)
109108
if err != nil {
110109
return fmt.Errorf("unable to read input: %s", err)
111110
}

cmd/file2fuzz/main_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package main
66

77
import (
8-
"io/ioutil"
98
"os"
109
"os/exec"
1110
"path/filepath"
@@ -118,9 +117,9 @@ func TestFile2Fuzz(t *testing.T) {
118117

119118
for _, tc := range tests {
120119
t.Run(tc.name, func(t *testing.T) {
121-
tmp, err := ioutil.TempDir(os.TempDir(), "file2fuzz")
120+
tmp, err := os.MkdirTemp(os.TempDir(), "file2fuzz")
122121
if err != nil {
123-
t.Fatalf("ioutil.TempDir failed: %s", err)
122+
t.Fatalf("os.MkdirTemp failed: %s", err)
124123
}
125124
defer os.RemoveAll(tmp)
126125
for _, f := range tc.inputFiles {
@@ -129,7 +128,7 @@ func TestFile2Fuzz(t *testing.T) {
129128
t.Fatalf("failed to create test directory: %s", err)
130129
}
131130
} else {
132-
if err := ioutil.WriteFile(filepath.Join(tmp, f.name), []byte(f.content), 0666); err != nil {
131+
if err := os.WriteFile(filepath.Join(tmp, f.name), []byte(f.content), 0666); err != nil {
133132
t.Fatalf("failed to create test input file: %s", err)
134133
}
135134
}
@@ -146,7 +145,7 @@ func TestFile2Fuzz(t *testing.T) {
146145
}
147146

148147
for _, f := range tc.expectedFiles {
149-
c, err := ioutil.ReadFile(filepath.Join(tmp, f.name))
148+
c, err := os.ReadFile(filepath.Join(tmp, f.name))
150149
if err != nil {
151150
t.Fatalf("failed to read expected output file %q: %s", f.name, err)
152151
}

cmd/fiximports/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ import (
7676
"go/parser"
7777
"go/token"
7878
"io"
79-
"io/ioutil"
8079
"log"
8180
"os"
8281
"path"
@@ -100,7 +99,7 @@ var (
10099
// seams for testing
101100
var (
102101
stderr io.Writer = os.Stderr
103-
writeFile = ioutil.WriteFile
102+
writeFile = os.WriteFile
104103
)
105104

106105
const usage = `fiximports: rewrite import paths to use canonical package names.

cmd/getgo/download.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"encoding/json"
1616
"fmt"
1717
"io"
18-
"io/ioutil"
1918
"net/http"
2019
"os"
2120
"path/filepath"
@@ -51,7 +50,7 @@ func downloadGoVersion(version, ops, arch, dest string) error {
5150
}
5251
defer resp.Body.Close()
5352

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

78-
shasum, err := ioutil.ReadAll(sresp.Body)
77+
shasum, err := io.ReadAll(sresp.Body)
7978
if err != nil {
8079
return err
8180
}
@@ -174,7 +173,7 @@ func getLatestGoVersion() (string, error) {
174173
}
175174
defer resp.Body.Close()
176175
if resp.StatusCode != http.StatusOK {
177-
b, _ := ioutil.ReadAll(io.LimitReader(resp.Body, 1024))
176+
b, _ := io.ReadAll(io.LimitReader(resp.Body, 1024))
178177
return "", fmt.Errorf("Could not get current Go release: HTTP %d: %q", resp.StatusCode, b)
179178
}
180179
var releases []struct {

cmd/getgo/download_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
package main
99

1010
import (
11-
"io/ioutil"
1211
"os"
1312
"path/filepath"
1413
"testing"
@@ -19,7 +18,7 @@ func TestDownloadGoVersion(t *testing.T) {
1918
t.Skipf("Skipping download in short mode")
2019
}
2120

22-
tmpd, err := ioutil.TempDir("", "go")
21+
tmpd, err := os.MkdirTemp("", "go")
2322
if err != nil {
2423
t.Fatal(err)
2524
}

cmd/getgo/main_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ package main
1010
import (
1111
"bytes"
1212
"fmt"
13-
"io/ioutil"
1413
"os"
1514
"os/exec"
1615
"testing"
@@ -37,7 +36,7 @@ func TestMain(m *testing.M) {
3736
}
3837

3938
func createTmpHome(t *testing.T) string {
40-
tmpd, err := ioutil.TempDir("", "testgetgo")
39+
tmpd, err := os.MkdirTemp("", "testgetgo")
4140
if err != nil {
4241
t.Fatalf("creating test tempdir failed: %v", err)
4342
}
@@ -86,7 +85,7 @@ func TestCommandVerbose(t *testing.T) {
8685
if err != nil {
8786
t.Fatal(err)
8887
}
89-
b, err := ioutil.ReadFile(shellConfig)
88+
b, err := os.ReadFile(shellConfig)
9089
if err != nil {
9190
t.Fatal(err)
9291
}
@@ -122,7 +121,7 @@ func TestCommandPathExists(t *testing.T) {
122121
if err != nil {
123122
t.Fatal(err)
124123
}
125-
b, err := ioutil.ReadFile(shellConfig)
124+
b, err := os.ReadFile(shellConfig)
126125
if err != nil {
127126
t.Fatal(err)
128127
}
@@ -146,7 +145,7 @@ export PATH=$PATH:%s/go/bin
146145
t.Fatal(err)
147146
}
148147

149-
b, err = ioutil.ReadFile(shellConfig)
148+
b, err = os.ReadFile(shellConfig)
150149
if err != nil {
151150
t.Fatal(err)
152151
}

cmd/getgo/path_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
package main
99

1010
import (
11-
"io/ioutil"
1211
"os"
1312
"path/filepath"
1413
"strings"
1514
"testing"
1615
)
1716

1817
func TestAppendPath(t *testing.T) {
19-
tmpd, err := ioutil.TempDir("", "go")
18+
tmpd, err := os.MkdirTemp("", "go")
2019
if err != nil {
2120
t.Fatal(err)
2221
}
@@ -35,7 +34,7 @@ func TestAppendPath(t *testing.T) {
3534
if err != nil {
3635
t.Fatal(err)
3736
}
38-
b, err := ioutil.ReadFile(shellConfig)
37+
b, err := os.ReadFile(shellConfig)
3938
if err != nil {
4039
t.Fatal(err)
4140
}
@@ -49,7 +48,7 @@ func TestAppendPath(t *testing.T) {
4948
if err := appendToPATH(filepath.Join(GOPATH, "bin")); err != nil {
5049
t.Fatal(err)
5150
}
52-
b, err = ioutil.ReadFile(shellConfig)
51+
b, err = os.ReadFile(shellConfig)
5352
if err != nil {
5453
t.Fatal(err)
5554
}

cmd/go-contrib-init/contrib.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"fmt"
1515
"go/build"
1616
exec "golang.org/x/sys/execabs"
17-
"io/ioutil"
1817
"log"
1918
"os"
2019
"path/filepath"
@@ -66,7 +65,7 @@ func detectrepo() string {
6665
var googleSourceRx = regexp.MustCompile(`(?m)^(go|go-review)?\.googlesource.com\b`)
6766

6867
func checkCLA() {
69-
slurp, err := ioutil.ReadFile(cookiesFile())
68+
slurp, err := os.ReadFile(cookiesFile())
7069
if err != nil && !os.IsNotExist(err) {
7170
log.Fatal(err)
7271
}
@@ -135,7 +134,7 @@ func checkGoroot() {
135134
"your GOROOT or set it to the path of your development version\n"+
136135
"of Go.", v)
137136
}
138-
slurp, err := ioutil.ReadFile(filepath.Join(v, "VERSION"))
137+
slurp, err := os.ReadFile(filepath.Join(v, "VERSION"))
139138
if err == nil {
140139
slurp = bytes.TrimSpace(slurp)
141140
log.Fatalf("Your GOROOT environment variable is set to %q\n"+

0 commit comments

Comments
 (0)