From a2704f1435c8c26e072f036c32820571498b9052 Mon Sep 17 00:00:00 2001 From: Damian Gryski Date: Fri, 5 Aug 2022 13:18:48 -0700 Subject: [PATCH] all: move from os.IsFoo to errors.Is(err, ErrFoo) --- builder/build.go | 3 ++- builder/cc.go | 5 +++-- builder/env.go | 6 ++++-- builder/library.go | 6 ++++-- goenv/goenv.go | 3 ++- loader/goroot.go | 5 +++-- src/testing/testing.go | 4 +++- testdata/filesystem.go | 4 +++- tools/gen-device-svd/gen-device-svd.go | 4 +++- 9 files changed, 27 insertions(+), 13 deletions(-) diff --git a/builder/build.go b/builder/build.go index d5d7a242..c6d0ff26 100644 --- a/builder/build.go +++ b/builder/build.go @@ -14,6 +14,7 @@ import ( "fmt" "go/types" "hash/crc32" + "io/fs" "math/bits" "os" "os/exec" @@ -147,7 +148,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil libcDependencies = append(libcDependencies, libcJob) case "wasi-libc": path := filepath.Join(root, "lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a") - if _, err := os.Stat(path); os.IsNotExist(err) { + if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) { return errors.New("could not find wasi-libc, perhaps you need to run `make wasi-libc`?") } libcDependencies = append(libcDependencies, dummyCompileJob(path)) diff --git a/builder/cc.go b/builder/cc.go index 7d7c10ad..080ef2bf 100644 --- a/builder/cc.go +++ b/builder/cc.go @@ -10,6 +10,7 @@ import ( "errors" "fmt" "io" + "io/fs" "os" "path/filepath" "sort" @@ -107,11 +108,11 @@ func compileAndCacheCFile(abspath, tmpdir string, cflags []string, thinlto bool, if err == nil { if _, err := os.Stat(outpath); err == nil { return outpath, nil - } else if !os.IsNotExist(err) { + } else if !errors.Is(err, fs.ErrNotExist) { return "", err } } - } else if !os.IsNotExist(err) { + } else if !errors.Is(err, fs.ErrNotExist) { // expected either nil or IsNotExist return "", err } diff --git a/builder/env.go b/builder/env.go index b28173c4..d6086131 100644 --- a/builder/env.go +++ b/builder/env.go @@ -1,6 +1,8 @@ package builder import ( + "errors" + "io/fs" "io/ioutil" "os" "os/exec" @@ -17,13 +19,13 @@ import ( func getClangHeaderPath(TINYGOROOT string) string { // Check whether we're running from the source directory. path := filepath.Join(TINYGOROOT, "llvm-project", "clang", "lib", "Headers") - if _, err := os.Stat(path); !os.IsNotExist(err) { + if _, err := os.Stat(path); !errors.Is(err, fs.ErrNotExist) { return path } // Check whether we're running from the installation directory. path = filepath.Join(TINYGOROOT, "lib", "clang", "include") - if _, err := os.Stat(path); !os.IsNotExist(err) { + if _, err := os.Stat(path); !errors.Is(err, fs.ErrNotExist) { return path } diff --git a/builder/library.go b/builder/library.go index d514568a..0de71808 100644 --- a/builder/library.go +++ b/builder/library.go @@ -1,6 +1,8 @@ package builder import ( + "errors" + "io/fs" "os" "path/filepath" "runtime" @@ -109,10 +111,10 @@ func (l *Library) load(config *compileopts.Config, tmpdir string) (job *compileJ err = os.Rename(temporaryHeaderPath, headerPath) if err != nil { switch { - case os.IsExist(err): + case errors.Is(err, fs.ErrExist): // Another invocation of TinyGo also seems to have already created the headers. - case runtime.GOOS == "windows" && os.IsPermission(err): + case runtime.GOOS == "windows" && errors.Is(err, fs.ErrPermission): // On Windows, a rename with a destination directory that already // exists does not result in an IsExist error, but rather in an // access denied error. To be sure, check for this case by checking diff --git a/goenv/goenv.go b/goenv/goenv.go index 6fcd913f..1e62eb35 100644 --- a/goenv/goenv.go +++ b/goenv/goenv.go @@ -6,6 +6,7 @@ import ( "bytes" "errors" "fmt" + "io/fs" "os" "os/exec" "os/user" @@ -122,7 +123,7 @@ func findWasmOpt() string { } _, err := os.Stat(path) - if err != nil && os.IsNotExist(err) { + if err != nil && errors.Is(err, fs.ErrNotExist) { continue } diff --git a/loader/goroot.go b/loader/goroot.go index 5039ed29..f1d28b0e 100644 --- a/loader/goroot.go +++ b/loader/goroot.go @@ -17,6 +17,7 @@ import ( "encoding/json" "errors" "io" + "io/fs" "io/ioutil" "os" "os/exec" @@ -122,13 +123,13 @@ func GetCachedGoroot(config *compileopts.Config) (string, error) { // Rename the new merged gorooot into place. err = os.Rename(tmpgoroot, cachedgoroot) if err != nil { - if os.IsExist(err) { + if errors.Is(err, fs.ErrExist) { // Another invocation of TinyGo also seems to have created a GOROOT. // Use that one instead. Our new GOROOT will be automatically // deleted by the defer above. return cachedgoroot, nil } - if runtime.GOOS == "windows" && os.IsPermission(err) { + if runtime.GOOS == "windows" && errors.Is(err, fs.ErrPermission) { // On Windows, a rename with a destination directory that already // exists does not result in an IsExist error, but rather in an // access denied error. To be sure, check for this case by checking diff --git a/src/testing/testing.go b/src/testing/testing.go index 018ededa..27b1df54 100644 --- a/src/testing/testing.go +++ b/src/testing/testing.go @@ -10,8 +10,10 @@ package testing import ( "bytes" + "errors" "flag" "fmt" + "io/fs" "os" "strings" "time" @@ -277,7 +279,7 @@ func (c *common) TempDir() string { nonExistent = true } else { _, err := os.Stat(c.tempDir) - nonExistent = os.IsNotExist(err) + nonExistent = errors.Is(err, fs.ErrNotExist) if err != nil && !nonExistent { c.Fatalf("TempDir: %v", err) } diff --git a/testdata/filesystem.go b/testdata/filesystem.go index 3825d618..de7fcbdf 100644 --- a/testdata/filesystem.go +++ b/testdata/filesystem.go @@ -1,13 +1,15 @@ package main import ( + "errors" "io" + "io/fs" "os" ) func main() { _, err := os.Open("non-exist") - if !os.IsNotExist(err) { + if !errors.Is(err, fs.ErrNotExist) { panic("should be non exist error") } diff --git a/tools/gen-device-svd/gen-device-svd.go b/tools/gen-device-svd/gen-device-svd.go index cb14c2c5..f10b7d23 100755 --- a/tools/gen-device-svd/gen-device-svd.go +++ b/tools/gen-device-svd/gen-device-svd.go @@ -3,8 +3,10 @@ package main import ( "bufio" "encoding/xml" + "errors" "flag" "fmt" + "io/fs" "os" "path/filepath" "regexp" @@ -1402,7 +1404,7 @@ __isr_vector: } func generate(indir, outdir, sourceURL, interruptSystem string) error { - if _, err := os.Stat(indir); os.IsNotExist(err) { + if _, err := os.Stat(indir); errors.Is(err, fs.ErrNotExist) { fmt.Fprintln(os.Stderr, "cannot find input directory:", indir) os.Exit(1) }