diff --git a/internal/cache/cache.go b/internal/cache/cache.go index 51c75a77d2da7..5c3fda705815a 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -370,7 +370,7 @@ func (c *Cache) putIndexEntry(id ActionID, out OutputID, size int64, allowVerify // Truncate the file only *after* writing it. // (This should be a no-op, but truncate just in case of previous corruption.) // - // This differs from ioutil.WriteFile, which truncates to 0 *before* writing + // This differs from os.WriteFile, which truncates to 0 *before* writing // via os.O_TRUNC. Truncating only after writing ensures that a second write // of the same content to the same file is idempotent, and does not — even // temporarily! — undo the effect of the first write. diff --git a/internal/cache/cache_test.go b/internal/cache/cache_test.go index 514adc44aa104..0efcee545b2dc 100644 --- a/internal/cache/cache_test.go +++ b/internal/cache/cache_test.go @@ -8,7 +8,6 @@ import ( "bytes" "encoding/binary" "fmt" - "io/ioutil" "os" "path/filepath" "testing" @@ -22,7 +21,7 @@ func init() { func TestBasic(t *testing.T) { t.Parallel() - dir, err := ioutil.TempDir("", "cachetest-") + dir, err := os.MkdirTemp("", "cachetest-") if err != nil { t.Fatal(err) } @@ -69,7 +68,7 @@ func TestBasic(t *testing.T) { func TestGrowth(t *testing.T) { t.Parallel() - dir, err := ioutil.TempDir("", "cachetest-") + dir, err := os.MkdirTemp("", "cachetest-") if err != nil { t.Fatal(err) } @@ -122,7 +121,7 @@ func TestVerifyPanic(t *testing.T) { t.Fatal("initEnv did not set verify") } - dir, err := ioutil.TempDir("", "cachetest-") + dir, err := os.MkdirTemp("", "cachetest-") if err != nil { t.Fatal(err) } @@ -157,7 +156,7 @@ func dummyID(x int) [HashSize]byte { func TestCacheTrim(t *testing.T) { t.Parallel() - dir, err := ioutil.TempDir("", "cachetest-") + dir, err := os.MkdirTemp("", "cachetest-") if err != nil { t.Fatal(err) } @@ -213,7 +212,7 @@ func TestCacheTrim(t *testing.T) { t.Fatal(err) } c.OutputFile(entry.OutputID) - data, err := ioutil.ReadFile(filepath.Join(dir, "trim.txt")) + data, err := os.ReadFile(filepath.Join(dir, "trim.txt")) if err != nil { t.Fatal(err) } @@ -226,7 +225,7 @@ func TestCacheTrim(t *testing.T) { t.Fatal(err) } c.OutputFile(entry.OutputID) - data2, err := ioutil.ReadFile(filepath.Join(dir, "trim.txt")) + data2, err := os.ReadFile(filepath.Join(dir, "trim.txt")) if err != nil { t.Fatal(err) } diff --git a/internal/cache/default.go b/internal/cache/default.go index e8866cb30ccf2..6695006298aaa 100644 --- a/internal/cache/default.go +++ b/internal/cache/default.go @@ -6,7 +6,6 @@ package cache import ( "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -39,7 +38,7 @@ func initDefaultCache() { } if _, err := os.Stat(filepath.Join(dir, "README")); err != nil { // Best effort. - if wErr := ioutil.WriteFile(filepath.Join(dir, "README"), []byte(cacheREADME), 0666); wErr != nil { + if wErr := os.WriteFile(filepath.Join(dir, "README"), []byte(cacheREADME), 0666); wErr != nil { log.Fatalf("Failed to write README file to cache dir %s: %s", dir, err) } } diff --git a/internal/cache/hash_test.go b/internal/cache/hash_test.go index d96fb31e989fa..810e5aa0e64ba 100644 --- a/internal/cache/hash_test.go +++ b/internal/cache/hash_test.go @@ -6,7 +6,6 @@ package cache import ( "fmt" - "io/ioutil" "os" "testing" ) @@ -28,7 +27,7 @@ func TestHash(t *testing.T) { } func TestHashFile(t *testing.T) { - f, err := ioutil.TempFile("", "cmd-go-test-") + f, err := os.CreateTemp("", "cmd-go-test-") if err != nil { t.Fatal(err) } diff --git a/internal/renameio/renameio.go b/internal/renameio/renameio.go index fa9d93bf7ad78..2f88f4f7cc5a0 100644 --- a/internal/renameio/renameio.go +++ b/internal/renameio/renameio.go @@ -24,7 +24,7 @@ func Pattern(filename string) string { return filepath.Join(filepath.Dir(filename), filepath.Base(filename)+patternSuffix) } -// WriteFile is like ioutil.WriteFile, but first writes data to an arbitrary +// WriteFile is like os.WriteFile, but first writes data to an arbitrary // file in the same directory as filename, then renames it atomically to the // final name. // @@ -79,7 +79,7 @@ func tempFile(dir, prefix string, perm os.FileMode) (f *os.File, err error) { return } -// ReadFile is like ioutil.ReadFile, but on Windows retries spurious errors that +// ReadFile is like os.ReadFile, but on Windows retries spurious errors that // may occur if the file is concurrently replaced. // // Errors are classified heuristically and retries are bounded, so even this diff --git a/internal/renameio/renameio_test.go b/internal/renameio/renameio_test.go index ae188e58e11fa..0f8dd3489760f 100644 --- a/internal/renameio/renameio_test.go +++ b/internal/renameio/renameio_test.go @@ -8,7 +8,6 @@ package renameio import ( "encoding/binary" - "io/ioutil" "math/rand" "os" "path/filepath" @@ -23,7 +22,7 @@ import ( ) func TestConcurrentReadsAndWrites(t *testing.T) { - dir, err := ioutil.TempDir("", "renameio") + dir, err := os.MkdirTemp("", "renameio") if err != nil { t.Fatal(err) } diff --git a/internal/renameio/umask_test.go b/internal/renameio/umask_test.go index d75d67c9a939a..921a2bdb61747 100644 --- a/internal/renameio/umask_test.go +++ b/internal/renameio/umask_test.go @@ -7,7 +7,6 @@ package renameio import ( - "io/ioutil" "os" "path/filepath" "syscall" @@ -15,7 +14,7 @@ import ( ) func TestWriteFileModeAppliesUmask(t *testing.T) { - dir, err := ioutil.TempDir("", "renameio") + dir, err := os.MkdirTemp("", "renameio") if err != nil { t.Fatalf("Failed to create temporary directory: %v", err) } diff --git a/internal/robustio/robustio.go b/internal/robustio/robustio.go index 76e47ad1ffad7..ce3dbbde6db93 100644 --- a/internal/robustio/robustio.go +++ b/internal/robustio/robustio.go @@ -22,7 +22,7 @@ func Rename(oldpath, newpath string) error { return rename(oldpath, newpath) } -// ReadFile is like ioutil.ReadFile, but on Windows retries errors that may +// ReadFile is like os.ReadFile, but on Windows retries errors that may // occur if the file is concurrently replaced. // // (See golang.org/issue/31247 and golang.org/issue/32188.) diff --git a/internal/robustio/robustio_flaky.go b/internal/robustio/robustio_flaky.go index e0bf5b9b3b977..5963027ee1174 100644 --- a/internal/robustio/robustio_flaky.go +++ b/internal/robustio/robustio_flaky.go @@ -7,7 +7,6 @@ package robustio import ( - "io/ioutil" "math/rand" "os" "syscall" @@ -70,11 +69,11 @@ func rename(oldpath, newpath string) (err error) { }) } -// readFile is like ioutil.ReadFile, but retries ephemeral errors. +// readFile is like os.ReadFile, but retries ephemeral errors. func readFile(filename string) ([]byte, error) { var b []byte err := retry(func() (err error, mayRetry bool) { - b, err = ioutil.ReadFile(filename) + b, err = os.ReadFile(filename) // Unlike in rename, we do not retry errFileNotFound here: it can occur // as a spurious error, but the file may also genuinely not exist, so the diff --git a/internal/robustio/robustio_other.go b/internal/robustio/robustio_other.go index a2428856f2e94..b7d01b340b820 100644 --- a/internal/robustio/robustio_other.go +++ b/internal/robustio/robustio_other.go @@ -7,7 +7,6 @@ package robustio import ( - "io/ioutil" "os" ) @@ -16,7 +15,7 @@ func rename(oldpath, newpath string) error { } func readFile(filename string) ([]byte, error) { - return ioutil.ReadFile(filename) + return os.ReadFile(filename) } func removeAll(path string) error { diff --git a/pkg/commands/run.go b/pkg/commands/run.go index f2ea5415a7fe5..11ae7d7e776cf 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -3,7 +3,7 @@ package commands import ( "context" "fmt" - "io/ioutil" + "io" "log" "os" "runtime" @@ -390,7 +390,7 @@ func (e *Executor) runAndPrint(ctx context.Context, args []string) error { if !logutils.HaveDebugTag("linters_output") { // Don't allow linters and loader to print anything - log.SetOutput(ioutil.Discard) + log.SetOutput(io.Discard) savedStdout, savedStderr := e.setOutputToDevNull() defer func() { os.Stdout, os.Stderr = savedStdout, savedStderr diff --git a/pkg/fsutils/filecache.go b/pkg/fsutils/filecache.go index 2b17a039861cb..04c66823df2cc 100644 --- a/pkg/fsutils/filecache.go +++ b/pkg/fsutils/filecache.go @@ -2,7 +2,7 @@ package fsutils import ( "fmt" - "io/ioutil" + "os" "sync" "github.com/pkg/errors" @@ -24,7 +24,7 @@ func (fc *FileCache) GetFileBytes(filePath string) ([]byte, error) { return cachedBytes.([]byte), nil } - fileBytes, err := ioutil.ReadFile(filePath) + fileBytes, err := os.ReadFile(filePath) if err != nil { return nil, errors.Wrapf(err, "can't read file %s", filePath) } diff --git a/pkg/golinters/gofumpt.go b/pkg/golinters/gofumpt.go index 75c08814419e7..455572d6ebc2d 100644 --- a/pkg/golinters/gofumpt.go +++ b/pkg/golinters/gofumpt.go @@ -3,7 +3,7 @@ package golinters import ( "bytes" "fmt" - "io/ioutil" + "os" "sync" "github.com/pkg/errors" @@ -50,7 +50,7 @@ func NewGofumpt() *goanalysis.Linter { var issues []goanalysis.Issue for _, f := range fileNames { - input, err := ioutil.ReadFile(f) + input, err := os.ReadFile(f) if err != nil { return nil, fmt.Errorf("unable to open file %s: %w", f, err) } diff --git a/pkg/golinters/gosec.go b/pkg/golinters/gosec.go index 85a2a6e0b878e..9610b3e836b18 100644 --- a/pkg/golinters/gosec.go +++ b/pkg/golinters/gosec.go @@ -3,7 +3,7 @@ package golinters import ( "fmt" "go/token" - "io/ioutil" + "io" "log" "strconv" "strings" @@ -42,7 +42,7 @@ func NewGosec(settings *config.GoSecSettings) *goanalysis.Linter { ruleDefinitions := rules.Generate(filters...) - logger := log.New(ioutil.Discard, "", 0) + logger := log.New(io.Discard, "", 0) analyzer := &analysis.Analyzer{ Name: gosecName, diff --git a/pkg/golinters/revive.go b/pkg/golinters/revive.go index 590332e66c40d..061c9b475d889 100644 --- a/pkg/golinters/revive.go +++ b/pkg/golinters/revive.go @@ -5,7 +5,7 @@ import ( "encoding/json" "fmt" "go/token" - "io/ioutil" + "os" "reflect" "github.com/BurntSushi/toml" @@ -65,7 +65,7 @@ func NewRevive(cfg *config.ReviveSettings) *goanalysis.Linter { return nil, err } - revive := lint.New(ioutil.ReadFile) + revive := lint.New(os.ReadFile) lintingRules, err := reviveConfig.GetLintingRules(conf) if err != nil { diff --git a/pkg/result/processors/diff.go b/pkg/result/processors/diff.go index 92ab2bd5ca4ce..65e01785bce65 100644 --- a/pkg/result/processors/diff.go +++ b/pkg/result/processors/diff.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "os" "strings" @@ -44,7 +43,7 @@ func (p Diff) Process(issues []result.Issue) ([]result.Issue, error) { var patchReader io.Reader if p.patchFilePath != "" { - patch, err := ioutil.ReadFile(p.patchFilePath) + patch, err := os.ReadFile(p.patchFilePath) if err != nil { return nil, fmt.Errorf("can't read from patch file %s: %s", p.patchFilePath, err) } diff --git a/scripts/expand_website_templates/main.go b/scripts/expand_website_templates/main.go index a6dfe435f7bda..9d045cf93bad3 100644 --- a/scripts/expand_website_templates/main.go +++ b/scripts/expand_website_templates/main.go @@ -7,7 +7,7 @@ import ( "encoding/json" "flag" "fmt" - "io/ioutil" + "io" "log" "net/http" "os" @@ -96,7 +96,7 @@ func rewriteDocs(replacements map[string]string) error { } func processDoc(path string, replacements map[string]string, madeReplacements map[string]bool) error { - contentBytes, err := ioutil.ReadFile(path) + contentBytes, err := os.ReadFile(path) if err != nil { return fmt.Errorf("failed to read %s: %w", path, err) } @@ -147,7 +147,7 @@ func getLatestVersion() (string, error) { return "", fmt.Errorf("failed to get http response for the latest tag: %s", err) } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return "", fmt.Errorf("failed to read a body for the latest tag: %s", err) } @@ -160,7 +160,7 @@ func getLatestVersion() (string, error) { } func buildTemplateContext() (map[string]string, error) { - golangciYamlExample, err := ioutil.ReadFile(".golangci.example.yml") + golangciYamlExample, err := os.ReadFile(".golangci.example.yml") if err != nil { return nil, fmt.Errorf("can't read .golangci.example.yml: %s", err) } @@ -191,7 +191,7 @@ func buildTemplateContext() (map[string]string, error) { helpLines := bytes.Split(help, []byte("\n")) shortHelp := bytes.Join(helpLines[2:], []byte("\n")) - changeLog, err := ioutil.ReadFile("CHANGELOG.md") + changeLog, err := os.ReadFile("CHANGELOG.md") if err != nil { return nil, err } diff --git a/test/errchk.go b/test/errchk.go index b84d7b9ce5ead..09710a5f8ec3f 100644 --- a/test/errchk.go +++ b/test/errchk.go @@ -4,8 +4,8 @@ import ( "bytes" "errors" "fmt" - "io/ioutil" "log" + "os" "regexp" "strconv" "strings" @@ -183,7 +183,7 @@ var ( func wantedErrors(file, short, defaultLinter string) (errs []wantedError) { cache := make(map[string]*regexp.Regexp) - src, err := ioutil.ReadFile(file) + src, err := os.ReadFile(file) if err != nil { log.Fatal(err) } diff --git a/test/fix_test.go b/test/fix_test.go index 97cde38834b0c..644a7d0bd4266 100644 --- a/test/fix_test.go +++ b/test/fix_test.go @@ -1,7 +1,6 @@ package test import ( - "io/ioutil" "os" "os/exec" "path/filepath" @@ -54,10 +53,10 @@ func TestFix(t *testing.T) { require.NoError(t, err) testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...) - output, err := ioutil.ReadFile(input) + output, err := os.ReadFile(input) require.NoError(t, err) - expectedOutput, err := ioutil.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input))) + expectedOutput, err := os.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input))) require.NoError(t, err) require.Equal(t, string(expectedOutput), string(output)) diff --git a/test/linters_test.go b/test/linters_test.go index 93fa1eac06587..355370480e917 100644 --- a/test/linters_test.go +++ b/test/linters_test.go @@ -2,7 +2,6 @@ package test import ( "bufio" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -99,7 +98,7 @@ func TestGciLocal(t *testing.T) { } func saveConfig(t *testing.T, cfg map[string]interface{}) (cfgPath string, finishFunc func()) { - f, err := ioutil.TempFile("", "golangci_lint_test") + f, err := os.CreateTemp("", "golangci_lint_test") require.NoError(t, err) cfgPath = f.Name() + ".yml" diff --git a/test/testshared/testshared.go b/test/testshared/testshared.go index c1a89d697e547..0d1f91c01dd6b 100644 --- a/test/testshared/testshared.go +++ b/test/testshared/testshared.go @@ -1,7 +1,6 @@ package testshared import ( - "io/ioutil" "os" "os/exec" "strings" @@ -134,7 +133,7 @@ func (r *LintRunner) RunWithYamlConfig(cfg string, args ...string) *RunResult { } func (r *LintRunner) RunCommandWithYamlConfig(cfg, command string, args ...string) *RunResult { - f, err := ioutil.TempFile("", "golangci_lint_test") + f, err := os.CreateTemp("", "golangci_lint_test") assert.NoError(r.t, err) f.Close() @@ -149,7 +148,7 @@ func (r *LintRunner) RunCommandWithYamlConfig(cfg, command string, args ...strin cfg = strings.TrimSpace(cfg) cfg = strings.Replace(cfg, "\t", " ", -1) - err = ioutil.WriteFile(cfgPath, []byte(cfg), os.ModePerm) + err = os.WriteFile(cfgPath, []byte(cfg), os.ModePerm) assert.NoError(r.t, err) pargs := append([]string{"-c", cfgPath}, args...)