Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errors: inconsistency between os.IsNotExist and errors.Is with wrapped error #38198

Open
orlangure opened this issue Apr 1, 2020 · 4 comments
Open

Comments

@orlangure
Copy link

@orlangure orlangure commented Apr 1, 2020

What version of Go are you using (go version)?

$ go version
go version go1.14.1 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/yury/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"

What did you do?

Playground

package main

import (
	"errors"
	"fmt"
	"io/ioutil"
	"os"
)

func main() {
	_, err := ioutil.ReadFile("foobar")
	fmt.Println("original error:", err)

	fmt.Println("os.IsNotExist", os.IsNotExist(err))
	fmt.Println("errors.Is", errors.Is(err, os.ErrNotExist))
	
	fmt.Println("wrapping with Errorf")
	
	err = fmt.Errorf("can't read file: %w", err)

	fmt.Println("os.IsNotExist", os.IsNotExist(err))
	fmt.Println("errors.Is", errors.Is(err, os.ErrNotExist))
}

// Output:
// original error: open foobar: No such file or directory
// os.IsNotExist true
// errors.Is true
// wrapping with Errorf
// os.IsNotExist false
// errors.Is true

What did you expect to see?

Consistent reporting in both errors.Is and os.IsNotExist with both wrapped and not wrapped no such file or directory error.

What did you see instead?

The original error returned by ioutil.ReadFile is recognized as ErrNotExist by both errors.Is and os.IsNotExist.
Error wrapped with fmt.Errorf("text: %w", err) is no longer recognized by os.IsNotExist, but still recognized by errors.Is.

@andybons
Copy link
Member

@andybons andybons commented Apr 1, 2020

@andybons andybons changed the title Inconsistency between os.IsNotExist and errors.Is with wrapped error errors: inconsistency between os.IsNotExist and errors.Is with wrapped error Apr 1, 2020
@andybons andybons added this to the Unplanned milestone Apr 1, 2020
@neild
Copy link
Contributor

@neild neild commented Apr 1, 2020

For better or worse, we chose not to change the definition of os.IsNotExist and other preexisting predicate functions to unwrap errors. This is intentional behavior.

@orlangure
Copy link
Author

@orlangure orlangure commented Apr 2, 2020

Thank you. Does it mean the we should always use errors.Is, and consider os.IsNotExist deprecated?

@neild
Copy link
Contributor

@neild neild commented Apr 3, 2020

You should use errors.Is if you want the check to unwrap errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.