golangci / golangci-lint

Fast linters runner for Go
https://golangci-lint.run
GNU General Public License v3.0
15.56k stars 1.38k forks source link

Can't extract issues from gofmt output on windows #307

Closed crsdrw closed 5 years ago

crsdrw commented 5 years ago

I'm trying to run golangci-lint with the gofmt linter enabled on windows:

golangci-lint run -E gofmt

And get the following error:

level=warning msg="[runner] Can't run linter gofmt: can't extract issues from gofmt diff output \"--- G:/dev/linttest/main.go.or
ig\\t2018-11-26 18:07:15.209770400 +0000\\r\\n+++ G:/dev/linttest/main.go\\t2018-11-26 18:07:15.210770400 +0000\\r\\n@@ -2,8 +2,
8 @@\\r\\n \\r\\n import (\\r\\n \\t// goimports error - wrong order\\r\\n-\\t\\\"time\\\"\\r\\n \\t\\\"fmt\\\"\\r\\n+\\t\\\"tim
e\\\"\\r\\n )\\r\\n \\r\\n func main() {\\r\\n\": can't parse patch: line 3, char 149: bad hunk header: @@ -2,8 +2,8 @@\r"

Verbose output:

level=warning msg="[runner] Can't run linter gofmt: can't extract issues from gofmt diff output \"--- G:/dev/linttest/main.go.or
ig\\t2018-11-26 18:07:15.209770400 +0000\\r\\n+++ G:/dev/linttest/main.go\\t2018-11-26 18:07:15.210770400 +0000\\r\\n@@ -2,8 +2,
8 @@\\r\\n \\r\\n import (\\r\\n \\t// goimports error - wrong order\\r\\n-\\t\\\"time\\\"\\r\\n \\t\\\"fmt\\\"\\r\\n+\\t\\\"tim
e\\\"\\r\\n )\\r\\n \\r\\n func main() {\\r\\n\": can't parse patch: line 3, char 149: bad hunk header: @@ -2,8 +2,8 @@\r"

G:\dev\linttest>golangci-lint run -v -E gofmt
level=info msg="[config_reader] Config search paths: [./ G:\\dev\\linttest G:\\dev G:\\]"
level=info msg="Gocritic enabled checks: [appendAssign assignOp caseOrder dupArg dupBranchBody dupCase flagDeref ifElseChain reg
expMust singleCaseSwitch sloppyLen switchTrue typeSwitchVar underef unlambda unslice defaultCaseOrder]"
level=info msg="[lintersdb] Active 9 linters: [deadcode errcheck gofmt govet ineffassign megacheck structcheck typecheck varchec
k]"
level=info msg="[loader] Go packages loading at mode load deps types and syntax took 1.7967282s"
level=info msg="[loader] SSA repr building timing: packages building 12.5015ms, total 212.5269ms"
level=info msg="[loader] SSA for megacheck repr building timing: packages building 5.5007ms, total 178.5227ms"
level=info msg="[runner] worker.7 took 0s"
level=info msg="[runner] worker.12 took 0s"
level=info msg="[runner] worker.5 took 0s"
level=info msg="[runner] worker.13 took 0s"
level=info msg="[runner] worker.8 took 0s"
level=info msg="[runner] worker.4 took 500.1µs with stages: typecheck: 0s"
level=info msg="[runner] worker.11 took 0s"
level=info msg="[runner] worker.15 took 1.0002ms with stages: varcheck: 500.1µs"
level=info msg="[runner] worker.14 took 500.1µs with stages: structcheck: 500.1µs"
level=info msg="[runner] worker.9 took 0s"
level=info msg="[runner] worker.16 took 1.0002ms with stages: errcheck: 1.0002ms"
level=info msg="[runner] worker.6 took 1.0002ms with stages: deadcode: 1.0002ms"
level=info msg="[runner] worker.10 took 1.0002ms with stages: ineffassign: 1.0002ms"
level=info msg="[runner] worker.1 took 4.0006ms with stages: govet: 4.0006ms"
level=info msg="[runner] worker.3 took 86.011ms with stages: megacheck: 86.011ms"
level=info msg="[runner] worker.2 took 95.0121ms with stages: gofmt: 95.0121ms"
level=info msg="[runner] Workers idle times: #1: 43.0054ms, #3: 8.5011ms, #4: 84.0106ms, #5: 94.0119ms, #6: 55.007ms, #7: 100.01
27ms, #8: 89.0113ms, #9: 68.0086ms, #10: 48.5061ms, #11: 81.5103ms, #12: 97.0123ms, #13: 91.5116ms, #14: 70.5089ms, #15: 75.5096
ms, #16: 61.5078ms"
level=warning msg="[runner] Can't run linter gofmt: can't extract issues from gofmt diff output \"--- G:/dev/linttest/main.go.or
ig\\t2018-11-26 18:09:06.346770400 +0000\\r\\n+++ G:/dev/linttest/main.go\\t2018-11-26 18:09:06.347770400 +0000\\r\\n@@ -2,8 +2,
8 @@\\r\\n \\r\\n import (\\r\\n \\t// goimports error - wrong order\\r\\n-\\t\\\"time\\\"\\r\\n \\t\\\"fmt\\\"\\r\\n+\\t\\\"tim
e\\\"\\r\\n )\\r\\n \\r\\n func main() {\\r\\n\": can't parse patch: line 3, char 149: bad hunk header: @@ -2,8 +2,8 @@\r"
level=info msg="[runner] processing took 0s with stages: autogenerated_exclude: 0s, diff: 0s, path_prettifier: 0s, cgo: 0s, noli
nt: 0s, uniq_by_line: 0s, skip_files: 0s, skip_dirs: 0s, max_same_issues: 0s, exclude: 0s, max_per_file_from_linter: 0s, max_fro
m_linter: 0s, source_code: 0s, path_shortener: 0s"
level=info msg="Memory: 34 samples, avg is 61.7MB, max is 236.2MB"
level=info msg="Execution took 3.4024321s"

Version

golangci-lint has version 1.12.3 built from 014a924 on 2018-11-24T08:54:08Z

Output from go version && go env:

go version go1.11.1 windows/amd64
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\CDrew\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=G:\dev\GoWork
set GOPROXY=
set GORACE=
set GOROOT=D:\Go
set GOTMPDIR=
set GOTOOLDIR=D:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=G:\dev\linttest\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=D:\NOTBAC~1\Temp\go-build767837518=/tmp/go-build -gno-record
-gcc-switches

The file I'm linting has a simple linting error:

package main

import (
    // goimports error - wrong order
    "time"
    "fmt"
)

func main() {
    fmt.Println("The time is", time.Now())
}
crsdrw commented 5 years ago

It seems to depend on what kind of shell I use. The above reproduction was using windows command prompt. If I use Git bash I don't get the error. With Powershell Console I get the same error.

crsdrw commented 5 years ago

I wonder if this issue is related to the issue of enabling linters in Visual Studio Code on windows: #222

crsdrw commented 5 years ago

More info. I have GnuWin32 diff in my path. If I remove it from my path I get this error instead:

level=warning msg="[runner] Can't run linter gofmt: error computing diff: exec: \"diff\": executable file not found in %PATH%"
jirfag commented 5 years ago

hi! thank you for the issue looks difficult to reproduce because I have only Mac OS and Linux

crsdrw commented 5 years ago

@jirfag I think the question is, can golangci-lint be made to work without a "diff" executable. Because that will typically not be available on windows.

jirfag commented 5 years ago

does gofmt without golangci-lint work for you? it uses diff

crsdrw commented 5 years ago

@jirfag Interesting... without any diff executable in my path all the functionality of gofmt works, except gofmt -d. You are right, gofmt -d does use diff. If I have a diff tool such as GnuWin32 DiffUtils in my path then I get the output I expect from gofmt -d .

jirfag commented 5 years ago

ok, got it. I've looked into the error text, looks like diff-parsing library doesn't support \r, I will fix it.

crsdrw commented 5 years ago

I have created a pull request on the diff-parsing library: https://github.com/sourcegraph/go-diff/pull/29

peachestao commented 3 years ago

l have similar question on windows 10,golangci-lint version is V1.32.2 run command:

golangci-lint run

got the error:

level=warning msg="[runner] Can't run linter goanalysis_metalinter: goimports: can't extract issues from gofmt diff output \"\": got no diffs from patch parser: []"
level=error msg="Running error: goimports: can't extract issues from gofmt diff output \"\": got no diffs from patch parser: []"

Has this problem been fixed?thank you!

mrwormhole commented 3 years ago

This issue still exists, I confirm on windows and ubuntu WSL. Installing GnuUtil32Installer.exe then adding bin folder into PATH will resolve this issue on any windows machine

yury-kozlov commented 2 years ago

The issue reproduced on windows after upgrading git to version 2.34.1. The fix was to add C:\Program Files\git\usr\bin (where diff.exe is located) to PATH variable.

chenyanchen commented 1 year ago

Still exists.

$ go version
go version go1.19.3 darwin/amd64
$ golangci-lint version
golangci-lint has version 1.50.1 built from 8926a95 on 2022-10-22T10:48:48Z

Error log:

ERRO Running error: 1 error occurred:
        * can't run linter goanalysis_metalinter: gofmt: can't extract issues from gofmt diff output "...some codes..."
SnowWarri0r commented 1 year ago

I got the error

level=warning msg="[runner] Can't run linter goanalysis_metalinter: gofmt: error computing diff: diff resolves to executable relative to current directory (.\\diff)"
level=error msg="Running error: 1 error occurred:\n\t* can't run linter goanalysis_metalinter: gofmt: error computing diff: diff resolves to executable relative to current directory (.\\diff)\n\n"

I have diff.exe in my PATH env,but it didn't work.

mirusky commented 1 year ago

For anyone with this problem install the executables and add it to system PATH variable, after that logout and login again.

sohailmd1-lhat commented 10 months ago

I am seeing this error in windows.

level=error msg="Running error: 1 error occurred:\n\t* can't run linter goanalysis_metalinter: goimports: can't extract issues from gofmt diff output \"\": got no diffs from patch parser: \n\n"

First I got the error that "diff" was not present in the PATH value. I installed it and added it to the PATH. Then it gave the above error where diff parser did not return any output to goimports.