Go Tool for Review Bot
Review Request #11240 — Created Oct. 22, 2020 and submitted
This tool's ability to format was removed and placed into another tool,
that tool is tentatively known as the GofmtTool. This was done because,
go fmt
is rather lightweight and does not rely on other files in order
to carry out its function. However,go test
andgo vet
require more
information in order to work consistently. In particular they need
access to the entire repository so that patched code can be fully
analyzed against the package code. Therefore GofmtTool inherits from
Tool
, whereas GoTool inherits fromRepositoryTool
and is generally
a much more computationally heavy tool.
Manual testing was done to confirm that
go test
is working correctly.
As of now it just creates general comments based on which tests have
failed.
Manual testing was done to confirm thatgo vet
is working and known
issues about failures when used against test files have been resolved.
Description | From | Last Updated |
---|---|---|
Go/Go Tools? |
david | |
gofmt still exists, but I believe the modern recommendation is to use go fmt. Can we do that here to … |
david | |
Perhaps "This file contains formatting errors and should be run through go fmt" ? Also, shouldn't this be checking the … |
david | |
Since we have the output already in a variable, it's kind of silly to write it out to a file … |
david | |
The imports should be in alphabetical order. So import json should be placed before import logging. |
ceciliawei | |
Using the string's "split" method isn't portable (for example, windows doesn't use "/" as a path splitter). That said, there's … |
david | |
It looks like we're running this for every single file, which means that if you have multiple changed files in … |
david | |
Let's pull the result of dest_file.lower() out into a variable so we don't have to call it twice in these … |
chipx86 | |
Blank line between statements and the start of new blocks. |
chipx86 | |
Same here. |
chipx86 | |
This will scan the entirety of packages every iteration of files. If packages is a set, this will be faster, … |
chipx86 | |
This can fail, so we'll want to check for exceptions, just as we do further down. |
chipx86 | |
It's generally better to use %-formatted strings to join in variables, as this is faster in Python. |
chipx86 | |
We prefer %-formatted strings, rather than .format(), as it's technically faster and more consistent with the rest of our codebase. … |
chipx86 | |
When spanning lines, we prefer the % on the line with the variables, as it gives more room for the … |
chipx86 | |
No blank line here. |
chipx86 | |
Can you incorporate the package name in here, or something to help debug this if this comes up in production? |
chipx86 | |
Same as above regarding %-formatted strings. |
chipx86 | |
Let's use single quotes here, since the inner string doesn't use them. We prefer single quotes in Python strings wherever … |
chipx86 | |
If we compile this regex before we do the outer loop, it'll speed this part up. |
chipx86 | |
While common in Python, we shouldn't ever override _. That's because _ is commonly used as an alias to ugettext … |
chipx86 | |
Rather than putting this into a variable and pulling the indexes out, let's just unpack with: filename, line_num = \ … |
chipx86 | |
You can put the format argument on the same line, since it fits. No need for parens there either, since … |
chipx86 | |
No blank line here. |
chipx86 | |
Same comment as above regarding having identifying information in the message. |
chipx86 | |
Things within here are getting pretty deeply nested. Maybe break out the test and vet implementations into their own methods? |
david | |
This should be wrapped in a try/except. |
david | |
Would it be possible to add a comment showing an example of what the JSON output from go vet looks … |
david | |
This is potentially running a lot of times, since it's deeply nested inside several loops. At the top level, can … |
david |
- Commit:
-
af6ed353931a4e3ddecaa739c08618f2e8d525324ad11e7957ebe6b29f6907d0eb8268814d2a4623
Checks run (2 succeeded)
- Description:
-
~ The tool is currently only capable of checking whether a file is
~ correctly formatted or not based on go fmt
. Work still needs to be~ done to implement go fix
,go test
, andgo vet
.~ The tool is currently capable of checking whether a file is
~ correctly formatted or not based on go fmt
. As well as doing some~ static analysis using go vet
, more time is needed to complete the+ go test
feature. - Testing Done:
-
~ Manual testing was done to confirm that
go fmt
is working correctly.~ Manual testing was done to confirm that
go fmt
is working correctly.+ Manual testing was done to confirm that go vet
is working in almost+ all cases. There are known issues right now with test files and correct + adjustments to the code will be made in the next week. - Commit:
-
4ad11e7957ebe6b29f6907d0eb8268814d2a46233baa171c9d1df9e5b9533aac18e075a4582930f9
Checks run (2 succeeded)
-
-
gofmt
still exists, but I believe the modern recommendation is to usego fmt
. Can we do that here to be more future-proof? -
Perhaps "This file contains formatting errors and should be run through
go fmt
" ?Also, shouldn't this be checking the output or return value from the format command? Seems like it just unconditionally adds the comment now (maybe that's still part of the WIP?)
-
Since we have the output already in a variable, it's kind of silly to write it out to a file and load it back in. We can just do:
try: json_data = json.loads(cleaned_output) except Exception as e: ...
- Summary:
-
[WIP] Go Tool for ReviewBotGo Tool for ReviewBot
- Description:
-
~ The tool is currently capable of checking whether a file is
~ correctly formatted or not based on go fmt
. As well as doing some~ static analysis using go vet
, more time is needed to complete the~ go test
feature.~ This tool's ability to format was removed and placed into another tool,
~ that tool is tentatively known as the GofmtTool. This was done because, ~ go fmt
is rather lightweight and does not rely on other files in order~ to carry out its function. However, go test
andgo vet
require more+ information in order to work consistently. In particular they need + access to the entire repository so that patched code can be fully + analyzed against the package code. Therefore GofmtTool inherits from + Tool
, whereas GoTool inherits fromRepositoryTool
and is generally+ a much more computationally heavy tool. - Testing Done:
-
~ Manual testing was done to confirm that
go fmt
is working correctly.~ Manual testing was done to confirm that go vet
is working in almost~ all cases. There are known issues right now with test files and correct ~ adjustments to the code will be made in the next week. ~ Manual testing was done to confirm that
go test
is working correctly.~ As of now it just creates general comments based on which tests have ~ failed. ~ Manual testing was done to confirm that go vet
is working and known+ issues about failures when used against test files have been resolved. - Commit:
-
3baa171c9d1df9e5b9533aac18e075a4582930f961427f66214d6fa076bdeb38f115e9662266f7dc
Checks run (2 succeeded)
-
-
Using the string's "split" method isn't portable (for example, windows doesn't use "/" as a path splitter). That said, there's both a portable and easier way to do this:
package = os.path.dirname(path)
-
It looks like we're running this for every single file, which means that if you have multiple changed files in a given package, we'll add the test failures multiple times.
Instead of overriding
handle_file
, can we instead overridehandle_files
, and use the file list to build a list of changed packages? We can then run the tests once per package.
- Commit:
-
61427f66214d6fa076bdeb38f115e9662266f7dc8ab7b4cf5335e42e78ad7e233b5972eeccaa9755
-
gotool_success.png: go_vet_success.pnggo_tool.png
Checks run (2 succeeded)
-
-
Let's pull the result of
dest_file.lower()
out into a variable so we don't have to call it twice in these checks. -
-
-
This will scan the entirety of
packages
every iteration offiles
. Ifpackages
is aset
, this will be faster, though then we lose the ordering. Perhaps that's okay, and we can sort the results when iterating it?If so, we can safely
.add()
into aset
without needing to check existence first. -
-
-
We prefer
%
-formatted strings, rather than.format()
, as it's technically faster and more consistent with the rest of our codebase.This can also be combiend with the previous line:
formatted_output = '[%s]' % ','.join(gotest_output)
-
When spanning lines, we prefer the
%
on the line with the variables, as it gives more room for the strings and helps more clearly indicate that we're formatting variables in. -
-
Can you incorporate the package name in here, or something to help debug this if this comes up in production?
-
-
Let's use single quotes here, since the inner string doesn't use them. We prefer single quotes in Python strings wherever possible.
-
-
While common in Python, we shouldn't ever override
_
. That's because_
is commonly used as an alias tougettext
orugettext_lazy
, and this can have unintentional side-effects.Also for
items
, we need to usesix.iteritems()
to get consistent behavior between Python 2 and 3. -
Rather than putting this into a variable and pulling the indexes out, let's just unpack with:
filename, line_num = \ os.path.basename(key['posn']).split(':', 2)
Could you also put a comment above this showing the general format of what we should expect here, so it's documented?
-
You can put the format argument on the same line, since it fits. No need for parens there either, since we're not building a tuple (only one arg, and also
(message)
is equivalent tomessage
since there's no tuple indicator, like a trailing comma or a second value). -
-
- Change Summary:
-
Addressed most of Christian's comments, though I would like some clarification on 2 of them.
- Commit:
-
8ab7b4cf5335e42e78ad7e233b5972eeccaa97556641780e134a70fc7ea0765d6c8661b69905485a
- People:
-
Checks run (2 succeeded)
flake8 passed.JSHint passed.
- Change Summary:
-
Addressed more comments by Christian, now only 1 comments requires a check.
- Summary:
-
Go Tool for ReviewBotGo Tool for Review Bot
- Commit:
-
6641780e134a70fc7ea0765d6c8661b69905485a7b4ed64bc45b060b596a5b9bdaee4fb9707924a7
Checks run (2 succeeded)
-
-
Things within here are getting pretty deeply nested. Maybe break out the test and vet implementations into their own methods?
-
-
Would it be possible to add a comment showing an example of what the JSON output from
go vet
looks like? That way people looking at the code can see how it maps to what it's parsing. -
This is potentially running a lot of times, since it's deeply nested inside several loops.
At the top level, can we create a new dict that maps the patched file path back to f? That way we can just index into it instead of looping every time.
- Change Summary:
-
Addressed David's comments and updated code to include sample JSON in the comments, as well as a dictionary to decrease execution time.
- Commit:
-
7b4ed64bc45b060b596a5b9bdaee4fb9707924a72c4183cb42e072c7dd0948f330ce267a877fe84b