Skip to content

Commit 9b20d49

Browse files
surikldez
andauthored
output: convert backslashes to forward slashes for GitHub Action annotations printer (#4149)
Co-authored-by: Fernandez Ludovic <[email protected]>
1 parent 3d58209 commit 9b20d49

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

pkg/printers/github.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package printers
33
import (
44
"fmt"
55
"io"
6+
"path/filepath"
67

78
"github.com/golangci/golangci-lint/pkg/result"
89
)
@@ -26,7 +27,12 @@ func formatIssueAsGithub(issue *result.Issue) string {
2627
severity = issue.Severity
2728
}
2829

29-
ret := fmt.Sprintf("::%s file=%s,line=%d", severity, issue.FilePath(), issue.Line())
30+
// Convert backslashes to forward slashes.
31+
// This is needed when running on windows.
32+
// Otherwise, GitHub won't be able to show the annotations pointing to the file path with backslashes.
33+
file := filepath.ToSlash(issue.FilePath())
34+
35+
ret := fmt.Sprintf("::%s file=%s,line=%d", severity, file, issue.Line())
3036
if issue.Pos.Column != 0 {
3137
ret += fmt.Sprintf(",col=%d", issue.Pos.Column)
3238
}

pkg/printers/github_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package printers
44
import (
55
"bytes"
66
"go/token"
7+
"runtime"
78
"testing"
89

910
"github.com/stretchr/testify/assert"
@@ -72,3 +73,24 @@ func TestFormatGithubIssue(t *testing.T) {
7273
sampleIssue.Pos.Column = 0
7374
require.Equal(t, "::error file=path/to/file.go,line=10::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue))
7475
}
76+
77+
func TestFormatGithubIssueWindows(t *testing.T) {
78+
if runtime.GOOS != "windows" {
79+
t.Skip("Skipping test on non Windows")
80+
}
81+
82+
sampleIssue := result.Issue{
83+
FromLinter: "sample-linter",
84+
Text: "some issue",
85+
Pos: token.Position{
86+
Filename: "path\\to\\file.go",
87+
Offset: 2,
88+
Line: 10,
89+
Column: 4,
90+
},
91+
}
92+
require.Equal(t, "::error file=path/to/file.go,line=10,col=4::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue))
93+
94+
sampleIssue.Pos.Column = 0
95+
require.Equal(t, "::error file=path/to/file.go,line=10::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue))
96+
}

0 commit comments

Comments
 (0)