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

Closed
orlangure opened this issue Apr 1, 2020 · 7 comments
Closed
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@orlangure
Copy link

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 commented Apr 1, 2020

@neild @jba

@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 the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Apr 1, 2020
@andybons andybons added this to the Unplanned milestone Apr 1, 2020
@neild
Copy link
Contributor

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

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

@neild
Copy link
Contributor

neild commented Apr 3, 2020

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

@harshavardhana
Copy link
Contributor

Would it be helpful to update the os.IsNotExist documentation to ensure that users end up using errors.Is() instead of os.IsNotExist when applicable?

@ianlancetaylor
Copy link
Contributor

@harshavardhana That has been done for the future 1.16 release for #41122 in https://golang.org/cl/268897.

There is nothing to do here, so closing this issue. Please comment if you disagree.

@harshavardhana
Copy link
Contributor

There is nothing to do here, so closing this issue. Please comment if you disagree.

This looks good @ianlancetaylor - thank you

gqgs added a commit to gqgs/GoZeroNet that referenced this issue Jun 27, 2021
@golang golang locked and limited conversation to collaborators Nov 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

6 participants