all: move from os.IsFoo to errors.Is(err, ErrFoo)

Этот коммит содержится в:
Damian Gryski 2022-08-05 13:18:48 -07:00 коммит произвёл Ron Evans
родитель edbbca5614
коммит a2704f1435
9 изменённых файлов: 27 добавлений и 13 удалений

Просмотреть файл

@ -14,6 +14,7 @@ import (
"fmt" "fmt"
"go/types" "go/types"
"hash/crc32" "hash/crc32"
"io/fs"
"math/bits" "math/bits"
"os" "os"
"os/exec" "os/exec"
@ -147,7 +148,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
libcDependencies = append(libcDependencies, libcJob) libcDependencies = append(libcDependencies, libcJob)
case "wasi-libc": case "wasi-libc":
path := filepath.Join(root, "lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a") 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`?") return errors.New("could not find wasi-libc, perhaps you need to run `make wasi-libc`?")
} }
libcDependencies = append(libcDependencies, dummyCompileJob(path)) libcDependencies = append(libcDependencies, dummyCompileJob(path))

Просмотреть файл

@ -10,6 +10,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
@ -107,11 +108,11 @@ func compileAndCacheCFile(abspath, tmpdir string, cflags []string, thinlto bool,
if err == nil { if err == nil {
if _, err := os.Stat(outpath); err == nil { if _, err := os.Stat(outpath); err == nil {
return outpath, nil return outpath, nil
} else if !os.IsNotExist(err) { } else if !errors.Is(err, fs.ErrNotExist) {
return "", err return "", err
} }
} }
} else if !os.IsNotExist(err) { } else if !errors.Is(err, fs.ErrNotExist) {
// expected either nil or IsNotExist // expected either nil or IsNotExist
return "", err return "", err
} }

Просмотреть файл

@ -1,6 +1,8 @@
package builder package builder
import ( import (
"errors"
"io/fs"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
@ -17,13 +19,13 @@ import (
func getClangHeaderPath(TINYGOROOT string) string { func getClangHeaderPath(TINYGOROOT string) string {
// Check whether we're running from the source directory. // Check whether we're running from the source directory.
path := filepath.Join(TINYGOROOT, "llvm-project", "clang", "lib", "Headers") 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 return path
} }
// Check whether we're running from the installation directory. // Check whether we're running from the installation directory.
path = filepath.Join(TINYGOROOT, "lib", "clang", "include") 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 return path
} }

Просмотреть файл

@ -1,6 +1,8 @@
package builder package builder
import ( import (
"errors"
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -109,10 +111,10 @@ func (l *Library) load(config *compileopts.Config, tmpdir string) (job *compileJ
err = os.Rename(temporaryHeaderPath, headerPath) err = os.Rename(temporaryHeaderPath, headerPath)
if err != nil { if err != nil {
switch { switch {
case os.IsExist(err): case errors.Is(err, fs.ErrExist):
// Another invocation of TinyGo also seems to have already created the headers. // 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 // On Windows, a rename with a destination directory that already
// exists does not result in an IsExist error, but rather in an // exists does not result in an IsExist error, but rather in an
// access denied error. To be sure, check for this case by checking // access denied error. To be sure, check for this case by checking

Просмотреть файл

@ -6,6 +6,7 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"io/fs"
"os" "os"
"os/exec" "os/exec"
"os/user" "os/user"
@ -122,7 +123,7 @@ func findWasmOpt() string {
} }
_, err := os.Stat(path) _, err := os.Stat(path)
if err != nil && os.IsNotExist(err) { if err != nil && errors.Is(err, fs.ErrNotExist) {
continue continue
} }

Просмотреть файл

@ -17,6 +17,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"io" "io"
"io/fs"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
@ -122,13 +123,13 @@ func GetCachedGoroot(config *compileopts.Config) (string, error) {
// Rename the new merged gorooot into place. // Rename the new merged gorooot into place.
err = os.Rename(tmpgoroot, cachedgoroot) err = os.Rename(tmpgoroot, cachedgoroot)
if err != nil { if err != nil {
if os.IsExist(err) { if errors.Is(err, fs.ErrExist) {
// Another invocation of TinyGo also seems to have created a GOROOT. // Another invocation of TinyGo also seems to have created a GOROOT.
// Use that one instead. Our new GOROOT will be automatically // Use that one instead. Our new GOROOT will be automatically
// deleted by the defer above. // deleted by the defer above.
return cachedgoroot, nil 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 // On Windows, a rename with a destination directory that already
// exists does not result in an IsExist error, but rather in an // exists does not result in an IsExist error, but rather in an
// access denied error. To be sure, check for this case by checking // access denied error. To be sure, check for this case by checking

Просмотреть файл

@ -10,8 +10,10 @@ package testing
import ( import (
"bytes" "bytes"
"errors"
"flag" "flag"
"fmt" "fmt"
"io/fs"
"os" "os"
"strings" "strings"
"time" "time"
@ -277,7 +279,7 @@ func (c *common) TempDir() string {
nonExistent = true nonExistent = true
} else { } else {
_, err := os.Stat(c.tempDir) _, err := os.Stat(c.tempDir)
nonExistent = os.IsNotExist(err) nonExistent = errors.Is(err, fs.ErrNotExist)
if err != nil && !nonExistent { if err != nil && !nonExistent {
c.Fatalf("TempDir: %v", err) c.Fatalf("TempDir: %v", err)
} }

4
testdata/filesystem.go предоставленный
Просмотреть файл

@ -1,13 +1,15 @@
package main package main
import ( import (
"errors"
"io" "io"
"io/fs"
"os" "os"
) )
func main() { func main() {
_, err := os.Open("non-exist") _, err := os.Open("non-exist")
if !os.IsNotExist(err) { if !errors.Is(err, fs.ErrNotExist) {
panic("should be non exist error") panic("should be non exist error")
} }

Просмотреть файл

@ -3,8 +3,10 @@ package main
import ( import (
"bufio" "bufio"
"encoding/xml" "encoding/xml"
"errors"
"flag" "flag"
"fmt" "fmt"
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -1402,7 +1404,7 @@ __isr_vector:
} }
func generate(indir, outdir, sourceURL, interruptSystem string) error { 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) fmt.Fprintln(os.Stderr, "cannot find input directory:", indir)
os.Exit(1) os.Exit(1)
} }