errcheck
errcheck copied to clipboard
support giving a file name
I would love to use errcheck as a linter, but for that I need to be able to supply a filename rather than a module. Would you consider supporting that?
I'm not sure that would work. errcheck relies on go/types
and go/build
to analyze the code and generally that requires the entire entire package.
We could determine the package based on the file name, type check the whole package but only lint the provided file.
Ah ok, that seems reasonable. I don't think it would be too hard to add. Do you know how to find a package given a filename?
Do you know how to find a package given a filename?
Yes.
// importPath returns the import path of Go package that file belongs to.
func importPath(file string) (string, error) {
path, _ := filepath.Split(file)
path = path[:len(path)-1] // Remove trailing slash. TODO: Better.
for _, srcRoot := range build.Default.SrcDirs() {
if strings.HasPrefix(path, srcRoot) {
return path[len(srcRoot)+1:], nil
}
}
return "", fmt.Errorf("couldn't find an import path corresponding to %q", file)
}
Source: https://github.com/shurcooL/play/blob/456f0172d733e4e6730f290ed042849370c132a7/200/cmd/importpathof/main.go#L93-L103
Great, thanks. I figured this was something someone would have solved already :) I'll try to whip up a patch some time soon On Sat, Aug 13, 2016 at 12:39 Dmitri Shuralyov [email protected] wrote:
Do you know how to find a package given a filename?
Yes:
https://github.com/shurcooL/play/blob/456f0172d733e4e6730f290ed042849370c132a7/200/cmd/importpathof/main.go#L93-L103
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kisielk/errcheck/issues/106#issuecomment-239637908, or mute the thread https://github.com/notifications/unsubscribe-auth/AADk-rckhhcz6w-Sth-l_hRTYKaAiyy9ks5qfh1ggaJpZM4JjjNY .
I would love to use errcheck as a linter, but for that I need to be able to supply a filename rather than a module.
I have a question. Why can't a linter be used on an entire package at once? Why does it need to work on a single filename at a time?
For example, when I use golint
in the terminal, I just run it on the entire package. What am I missing? Is it for running those linters inside an editor?
@shurcooL because the linting framework I use does a file at a time
@kisielk it seems that golint already handles this well:
https://github.com/golang/lint/blob/master/golint/golint.go
@shurcooL because the linting framework I use does a file at a time
I see.
Hi 👋 Any updates on this?