-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
x/tools/go/analysis: The first time singlechecker -fix parameter is executed, it does not fix the problem in the _test.go file #54740
Comments
I did some debug. --- a/vendor/golang.org/x/tools/go/analysis/internal/checker/checker.go
+++ b/vendor/golang.org/x/tools/go/analysis/internal/checker/checker.go
@@ -384,7 +384,9 @@ func applyFixes(roots []*action) {
return nil
}
- visitAll(roots)
+ if err := visitAll(roots); err != nil {
+ fmt.Println(err)
+ }
fset := token.NewFileSet() // Shared by parse calls below
// Now we've got a set of valid edits for each file. Get the new file contents. run again: $ go run ../showlintererr/main.go -fix -test ./...
analyses applying overlapping text edits affecting pos range (47, 52) and (47, 52)
/Users/fupeng/go/src/showlinterexample/a.go:5:2: self-assignment of x to x
/Users/fupeng/go/src/showlinterexample/a.go:7:2: self-assignment of y to y
/Users/fupeng/go/src/showlinterexample/a_test.go:9:2: self-assignment of z to z
exit status 3 So the problem is that there are multiple copies of the same k := key{posn, end, act.a, diag.Message}
if seen[k] {
continue // duplicate
}
seen[k] = true
analysisflags.PrintPlain(act.pkg.Fset, diag) but if the json is output, it still shows 2 copies: $ go run ../showlintererr/main.go -json ./...
{
"showlinterexample": {
"assign": [
{
"posn": "/Users/fupeng/go/src/showlinterexample/a.go:5:2",
"message": "self-assignment of x to x"
},
{
"posn": "/Users/fupeng/go/src/showlinterexample/a.go:7:2",
"message": "self-assignment of y to y"
}
]
},
"showlinterexample [showlinterexample.test]": {
"assign": [
{
"posn": "/Users/fupeng/go/src/showlinterexample/a.go:5:2",
"message": "self-assignment of x to x"
},
{
"posn": "/Users/fupeng/go/src/showlinterexample/a.go:7:2",
"message": "self-assignment of y to y"
},
{
"posn": "/Users/fupeng/go/src/showlinterexample/a_test.go:9:2",
"message": "self-assignment of z to z"
}
]
}
} this json show why. // If Tests is set, the loader includes not just the packages
// matching a particular pattern but also any related test packages,
// including test-only variants of the package and the test executable.
//
// For example, when using the go command, loading "fmt" with Tests=true
// returns four packages, with IDs "fmt" (the standard package),
// "fmt [fmt.test]" (the package as compiled for the test),
// "fmt_test" (the test functions from source files in package fmt_test),
// and "fmt.test" (the test binary).
//
// In build systems with explicit names for tests,
// setting Tests may have no effect.
Tests bool I would like to know how I can avoid outputting the same 2 copies of |
A Diagnostic per I think it is fine to not have an error if there are overlapping identical edits. This would effectively fix this issue with multiple "go/packages.Packages" referring to the same Thoughts from @zpavlinovic and @adonovan ? (As a side note, we should be writing out |
I agree with this.
Could you explain the solution in more detail? It seems to me that the fix is being applied to |
We have 5 diagnostics and let's name them after the variable + package: My proposed fix is to do a deep equality check on |
Change https://go.dev/cl/426594 mentions this issue: |
Change https://go.dev/cl/426734 mentions this issue: |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
1. Create the simplest linter by following the example in the singlechecker documentation
2. give some error example
and
_test.go
:3. run this linter
What did you expect to see?
auto-fix should run once, and all error should be fix.
What did you see instead?
errors in the test file are displayed, but not automatically fixed.
The text was updated successfully, but these errors were encountered: