errcheck icon indicating copy to clipboard operation
errcheck copied to clipboard

support giving a file name

Open ryanking opened this issue 7 years ago • 10 comments

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?

ryanking avatar Aug 12 '16 23:08 ryanking

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.

kisielk avatar Aug 13 '16 06:08 kisielk

We could determine the package based on the file name, type check the whole package but only lint the provided file.

dominikh avatar Aug 13 '16 07:08 dominikh

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?

kisielk avatar Aug 13 '16 18:08 kisielk

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

dmitshur avatar Aug 13 '16 19:08 dmitshur

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 .

kisielk avatar Aug 13 '16 19:08 kisielk

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?

dmitshur avatar Aug 13 '16 20:08 dmitshur

@shurcooL because the linting framework I use does a file at a time

ryanking avatar Aug 15 '16 04:08 ryanking

@kisielk it seems that golint already handles this well:

https://github.com/golang/lint/blob/master/golint/golint.go

ryanking avatar Aug 15 '16 04:08 ryanking

@shurcooL because the linting framework I use does a file at a time

I see.

dmitshur avatar Aug 15 '16 04:08 dmitshur

Hi 👋 Any updates on this?

sandro avatar May 17 '18 00:05 sandro