Skip to content

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

@orlangure

Description

@orlangure

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions