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

disable comparison linter for errors from golang.org/x/sys/unix #10

Closed
kolyshkin opened this issue Jun 28, 2021 · 6 comments · Fixed by #47
Closed

disable comparison linter for errors from golang.org/x/sys/unix #10

kolyshkin opened this issue Jun 28, 2021 · 6 comments · Fixed by #47
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@kolyshkin
Copy link
Contributor

Functions from the golang.org/x/sys/unix package wraps its error, always returning bare errno, and so it's OK to use direct error comparison.

Example:

err := unix.Rmdir(path)
    if err == nil || err == unix.ENOENT {
    return nil
}

On the above code, the linter says:

comparing with == will fail on wrapped errors. Use errors.Is to check for a specific error (errorlint)

For a package that uses a lot of unix functions, it's a burden to annotate all such code as the above example with //nolint:errorlint // unix errors are bare, but this is exactly what I had to do in runc: opencontainers/runc@56e4780

Surely, the alternative is to switch to errors.Is(err, unix.ENOENT) and the like, but in the particular case it seems redudnant.

Would be nice to have comparison linter to add an exclusion for errors returned from golang.org/x/sys/unix.* functions.

@kolyshkin
Copy link
Contributor Author

https://pkg.go.dev/golang.org/x/sys/unix#section-documentation says:

These calls return err == nil to indicate success; otherwise err represents an operating system error describing the failure and holds a value of type syscall.Errno.

Summoning @tklauser to confirm (or deny) that there are no plans to change that, i.e. the errors returned from unix pkg will always be bare errnos.

@tklauser
Copy link

https://pkg.go.dev/golang.org/x/sys/unix#section-documentation says:

These calls return err == nil to indicate success; otherwise err represents an operating system error describing the failure and holds a value of type syscall.Errno.

Summoning @tklauser to confirm (or deny) that there are no plans to change that, i.e. the errors returned from unix pkg will always be bare errnos.

That's correct. As far as I know there are no plans to change that. FWIW, the tests in the golang.org/x/sys/unix package use direct error comparison quite extensively too.

@polyfloyd
Copy link
Owner

allowed.go in the errorlint source contains a set of errors and functions from pkg which are allowed to be compared. We can extend this list with the errors from golang.org/x/sys/unix.

One thing that does concern me about the current approach is that it uses only the package path basename which can become ambiguous with multiple packages. If the scope of the allowlist will be greater than just the standard library it is a good idea to match on absolute paths.

@mitar
Copy link

mitar commented Jul 25, 2023

Based on golang/go#40827, it seems like only io.EOF is really something which should not be wrapped while others might be?

@polyfloyd
Copy link
Owner

@mitar Yes, you are right. I had taken the current approach because there is not really anything preventing people from still wrapping io.EOF in their own code. Ignoring io.EOF altogether should be complemented by a lint that detects wrapping io.EOF

kolyshkin added a commit to kolyshkin/go-errorlint that referenced this issue Jul 27, 2023
This is my naive (and incomplete) approach to fix GH issue polyfloyd#10.

The problems with this implementation are:
- no test cases (it seems those would need the whole golang.org/x/sys/unix
  to be available under testdata/src);
- a check is too relaxed (ignoring all errors that start from "E" from
  any package named "unix").

Alas, I have no idea how to fix those.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
kolyshkin added a commit to kolyshkin/go-errorlint that referenced this issue Jul 28, 2023
This is my naive (and incomplete) approach to fix GH issue polyfloyd#10.

The problems with this implementation are:
- no test cases (it seems those would need the whole golang.org/x/sys/unix
  to be available under testdata/src);
- a check is too relaxed (ignoring all errors that start from "E" from
  any package named "unix").

Alas, I have no idea how to fix those.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
kolyshkin added a commit to kolyshkin/go-errorlint that referenced this issue Aug 2, 2023
All functions in golang.org/x/sys/unix always return raw errno values,
which can be compared directly.

There are about 100-130 errors, and about 350 functions in unix package.
Listing them separately is hardly an option, so let's ignore all unix.E*
errors that come from all functions in unix package.

This functionality is made possible thanks to commit cd16050.

Add a small test case (we can't really add the whole x/sys/unix under
testdata, so use a stub implementation).

Fixes: polyfloyd#10

Co-Authored-by: polyfloyd <floyd@polyfloyd.net>
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
polyfloyd added a commit that referenced this issue Aug 2, 2023
All functions in golang.org/x/sys/unix always return raw errno values,
which can be compared directly.

There are about 100-130 errors, and about 350 functions in unix package.
Listing them separately is hardly an option, so let's ignore all unix.E*
errors that come from all functions in unix package.

This functionality is made possible thanks to commit cd16050.

Add a small test case (we can't really add the whole x/sys/unix under
testdata, so use a stub implementation).

Fixes: #10

Co-Authored-by: polyfloyd <floyd@polyfloyd.net>
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
@kolyshkin
Copy link
Contributor Author

Fixed in v.1.4.4, which is included into golangci-lint v1.54.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
@tklauser @mitar @kolyshkin @polyfloyd and others