-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
New linter: Go responsewriter lint #3614
base: master
Are you sure you want to change the base?
Conversation
Hey, thank you for opening your first Pull Request ! |
56919e3
to
17eb3fc
Compare
I'm having some issue running the tests that I can't seem to resolve, or even find the cause of:
UPDATE: this was fixed by using the |
In order for a pull request adding a linter to be reviewed, the linter and the PR must follow some requirements. Pull Request Description
Linter
The Linter Tests Inside Golangci-lint
|
Accepted suggestion. Co-authored-by: Ludovic Fernandez <[email protected]>
Can someone explain to me why this code generates the func serveDepsJS(rw http.ResponseWriter, req *http.Request, dir string) {
...
b, err := closure.GenDeps(root)
if err != nil {
log.Print(err)
http.Error(rw, "Server error", 500)
return
}
rw.Header().Set("Content-Type", "text/javascript; charset=utf-8")
rw.Write([]byte("// auto-generated from perkeepd\n"))
rw.Write(b)
}
|
@alexandear I'm unsure which one is your question:
The PR itself has a link to the linter's own repository, where the readme has info on what it aims to do and why. I wrote it such that if I was in a situation where I wanted to find out what it's for, the readme would answer it. Let me know if you have additional questions. |
…sing it Good catch @leonklingele, thank you! Co-authored-by: leonklingele <[email protected]>
@javorszky, I apologize if my question comes across as rude - as a non-native speaker, I want to make sure I understand how this linter can be helpful in finding bugs. I'm checking the linter requirement What I did:
My understanding is that when you created the PR to add I am curious to know if you have analyzed any open-source projects with |
@alexandear that context makes a lot more sense, thank you. When I made the linter I followed this tutorial: https://disaev.me/p/writing-useful-go-analysis-linter/#run-on-the-real-and-large-repositories, and ran the linter on the Go source (1.19.2 at the time). That one did not flag up any issues. With false positives / negatives, I agree with you. In this case I don't really count it as a false positive:
I wrote in the description that there are times when multiple calls or out of order calls to those methods are desirable. The purpose of this linter is to get developers to be purposeful in calling them out of order or multiple times. As an example, there's the |
The tests are what I used to validate the linter so that it triggers when I want it to, and does not trigger when I do not want it to. It's copy pasted from the linter's own repository. Tests there pass. Testing is written as per the tutorial. It seems like the way golangci-lint wraps the analysis tests is somewhat different. Does it bail on the first found issue? I would like some help with figuring out the differences between my own testing, and golangci-lint's way of doing it. |
Good point in adding instructions / blurb to installation methods. I do not have access to a Windows computer, but will do my best to accommodate users of other OSs. |
Maybe the flagging of multiple successive Write calls should be made optional and default to off. That would allow zero false positives and still allow to enable that check. Multiple write calls are common in API responses that stream data and want to send out some initial data as soon as possible (e.g. a. CSV header in a CSV response). I would not like to flag all these in the future to make a linter happy. |
Issue I see sometimes is that the order in which calls made to responsewriter end up with unexpected bug for those who don't have the side effects of calling
Write
,WriteHeader
memorized in the http ResponseWriter interface.This linter should flag up potential places that might lead to bugs by finding calls that are in the wrong order, for example:
WriteHeader
called afterWrite
. By that time,Write
has already calledWriteHeader
with anhttp.StatusOK
, so additional call toWriteHeader
does nothingHeader
called after eitherWrite
orWriteHeader
: headers are already sent, manipulating headers after that will take no effectWrite
orWriteHeader
are probably not intended in the same function body, so those are also probably bugsLinter repository
https://github.com/javorszky/go-responsewriter-lint