diff --git a/pkg/printers/github.go b/pkg/printers/github.go index 7f148097ab8c..c1da9df9c602 100644 --- a/pkg/printers/github.go +++ b/pkg/printers/github.go @@ -3,6 +3,7 @@ package printers import ( "fmt" "io" + "path/filepath" "github.com/golangci/golangci-lint/pkg/result" ) @@ -26,7 +27,12 @@ func formatIssueAsGithub(issue *result.Issue) string { severity = issue.Severity } - ret := fmt.Sprintf("::%s file=%s,line=%d", severity, issue.FilePath(), issue.Line()) + // Convert backslashes to forward slashes. + // This is needed when running on windows. + // Otherwise, GitHub won't be able to show the annotations pointing to the file path with backslashes. + file := filepath.ToSlash(issue.FilePath()) + + ret := fmt.Sprintf("::%s file=%s,line=%d", severity, file, issue.Line()) if issue.Pos.Column != 0 { ret += fmt.Sprintf(",col=%d", issue.Pos.Column) } diff --git a/pkg/printers/github_test.go b/pkg/printers/github_test.go index 4652b0c760f7..214a16e8f33f 100644 --- a/pkg/printers/github_test.go +++ b/pkg/printers/github_test.go @@ -4,6 +4,7 @@ package printers import ( "bytes" "go/token" + "runtime" "testing" "github.com/stretchr/testify/assert" @@ -72,3 +73,24 @@ func TestFormatGithubIssue(t *testing.T) { sampleIssue.Pos.Column = 0 require.Equal(t, "::error file=path/to/file.go,line=10::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue)) } + +func TestFormatGithubIssueWindows(t *testing.T) { + if runtime.GOOS != "windows" { + t.Skip("Skipping test on non Windows") + } + + sampleIssue := result.Issue{ + FromLinter: "sample-linter", + Text: "some issue", + Pos: token.Position{ + Filename: "path\\to\\file.go", + Offset: 2, + Line: 10, + Column: 4, + }, + } + require.Equal(t, "::error file=path/to/file.go,line=10,col=4::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue)) + + sampleIssue.Pos.Column = 0 + require.Equal(t, "::error file=path/to/file.go,line=10::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue)) +}